Java Code Examples for org.eclipse.jdt.core.dom.ASTNode#SINGLE_VARIABLE_DECLARATION

The following examples show how to use org.eclipse.jdt.core.dom.ASTNode#SINGLE_VARIABLE_DECLARATION . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ModifierRewrite.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private ListRewrite evaluateListRewrite(ASTRewrite rewrite, ASTNode declNode) {
	switch (declNode.getNodeType()) {
		case ASTNode.METHOD_DECLARATION:
			return rewrite.getListRewrite(declNode, MethodDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.FIELD_DECLARATION:
			return rewrite.getListRewrite(declNode, FieldDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
			return rewrite.getListRewrite(declNode, VariableDeclarationExpression.MODIFIERS2_PROPERTY);
		case ASTNode.VARIABLE_DECLARATION_STATEMENT:
			return rewrite.getListRewrite(declNode, VariableDeclarationStatement.MODIFIERS2_PROPERTY);
		case ASTNode.SINGLE_VARIABLE_DECLARATION:
			return rewrite.getListRewrite(declNode, SingleVariableDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.TYPE_DECLARATION:
			return rewrite.getListRewrite(declNode, TypeDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.ENUM_DECLARATION:
			return rewrite.getListRewrite(declNode, EnumDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.ANNOTATION_TYPE_DECLARATION:
			return rewrite.getListRewrite(declNode, AnnotationTypeDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.ENUM_CONSTANT_DECLARATION:
			return rewrite.getListRewrite(declNode, EnumConstantDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
			return rewrite.getListRewrite(declNode, AnnotationTypeMemberDeclaration.MODIFIERS2_PROPERTY);
		default:
			throw new IllegalArgumentException("node has no modifiers: " + declNode.getClass().getName()); //$NON-NLS-1$
	}
}
 
Example 2
Source File: ChangeTypeRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static Type getType(ASTNode node) {
	switch(node.getNodeType()){
		case ASTNode.SINGLE_VARIABLE_DECLARATION:
			return ((SingleVariableDeclaration) node).getType();
		case ASTNode.FIELD_DECLARATION:
			return ((FieldDeclaration) node).getType();
		case ASTNode.VARIABLE_DECLARATION_STATEMENT:
			return ((VariableDeclarationStatement) node).getType();
		case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
			return ((VariableDeclarationExpression) node).getType();
		case ASTNode.METHOD_DECLARATION:
			return ((MethodDeclaration)node).getReturnType2();
		case ASTNode.PARAMETERIZED_TYPE:
			return ((ParameterizedType)node).getType();
		default:
			Assert.isTrue(false);
			return null;
	}
}
 
Example 3
Source File: ChangeTypeRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
  * The selection corresponds to a ParameterizedType (return type of method)
 * @param pt the type
 * @return the message
  */
private String parameterizedTypeSelected(ParameterizedType pt) {
	ASTNode parent= pt.getParent();
	if (parent.getNodeType() == ASTNode.METHOD_DECLARATION){
		fMethodBinding= ((MethodDeclaration)parent).resolveBinding();
		fParamIndex= -1;
		fEffectiveSelectionStart= pt.getStartPosition();
		fEffectiveSelectionLength= pt.getLength();
		setOriginalType(pt.resolveBinding());
	} else if (parent.getNodeType() == ASTNode.SINGLE_VARIABLE_DECLARATION){
		return singleVariableDeclarationSelected((SingleVariableDeclaration)parent);
	} else if (parent.getNodeType() == ASTNode.VARIABLE_DECLARATION_STATEMENT){
		return variableDeclarationStatementSelected((VariableDeclarationStatement)parent);
	} else if (parent.getNodeType() == ASTNode.FIELD_DECLARATION){
		return fieldDeclarationSelected((FieldDeclaration)parent);
	} else {
		return nodeTypeNotSupported();
	}
	return null;
}
 
Example 4
Source File: ModifierRewrite.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private ListRewrite evaluateListRewrite(ASTRewrite rewrite, ASTNode declNode) {
	switch (declNode.getNodeType()) {
		case ASTNode.METHOD_DECLARATION:
			return rewrite.getListRewrite(declNode, MethodDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.FIELD_DECLARATION:
			return rewrite.getListRewrite(declNode, FieldDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
			return rewrite.getListRewrite(declNode, VariableDeclarationExpression.MODIFIERS2_PROPERTY);
		case ASTNode.VARIABLE_DECLARATION_STATEMENT:
			return rewrite.getListRewrite(declNode, VariableDeclarationStatement.MODIFIERS2_PROPERTY);
		case ASTNode.SINGLE_VARIABLE_DECLARATION:
			return rewrite.getListRewrite(declNode, SingleVariableDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.TYPE_DECLARATION:
			return rewrite.getListRewrite(declNode, TypeDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.ENUM_DECLARATION:
			return rewrite.getListRewrite(declNode, EnumDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.ANNOTATION_TYPE_DECLARATION:
			return rewrite.getListRewrite(declNode, AnnotationTypeDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.ENUM_CONSTANT_DECLARATION:
			return rewrite.getListRewrite(declNode, EnumConstantDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
			return rewrite.getListRewrite(declNode, AnnotationTypeMemberDeclaration.MODIFIERS2_PROPERTY);
		default:
			throw new IllegalArgumentException("node has no modifiers: " + declNode.getClass().getName()); //$NON-NLS-1$
	}
}
 
Example 5
Source File: CallInliner.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean isSingleDeclaration(ASTNode node) {
	int type= node.getNodeType();
	if (type == ASTNode.SINGLE_VARIABLE_DECLARATION)
		return true;
	if (type == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
		node= node.getParent();
		if (node.getNodeType() == ASTNode.VARIABLE_DECLARATION_STATEMENT) {
			VariableDeclarationStatement vs= (VariableDeclarationStatement)node;
			return vs.fragments().size() == 1;
		}
	}
	return false;
}
 
Example 6
Source File: UnusedCodeFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean hasSideEffect(SimpleName reference) {
	ASTNode parent= reference.getParent();
	while (parent instanceof QualifiedName) {
		parent= parent.getParent();
	}
	if (parent instanceof FieldAccess) {
		parent= parent.getParent();
	}

	ASTNode node= null;
	int nameParentType= parent.getNodeType();
	if (nameParentType == ASTNode.ASSIGNMENT) {
		Assignment assignment= (Assignment) parent;
		node= assignment.getRightHandSide();
	} else if (nameParentType == ASTNode.SINGLE_VARIABLE_DECLARATION) {
		SingleVariableDeclaration decl= (SingleVariableDeclaration)parent;
		node= decl.getInitializer();
		if (node == null)
			return false;
	} else if (nameParentType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
		node= parent;
	} else {
		return false;
	}

	ArrayList<Expression> sideEffects= new ArrayList<Expression>();
	node.accept(new SideEffectFinder(sideEffects));
	return sideEffects.size() > 0;
}
 
Example 7
Source File: JavaTextSelection.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public boolean resolveInVariableInitializer() {
	if (fInVariableInitializerRequested)
		return fInVariableInitializer;
	fInVariableInitializerRequested= true;
	resolveSelectedNodes();
	ASTNode node= getStartNode();
	ASTNode last= null;
	while (node != null) {
		int nodeType= node.getNodeType();
		if (node instanceof AbstractTypeDeclaration) {
			fInVariableInitializer= false;
			break;
		} else if (nodeType == ASTNode.ANONYMOUS_CLASS_DECLARATION) {
			fInVariableInitializer= false;
			break;
		} else if (nodeType == ASTNode.VARIABLE_DECLARATION_FRAGMENT &&
				   ((VariableDeclarationFragment)node).getInitializer() == last) {
			fInVariableInitializer= true;
			break;
		} else if (nodeType == ASTNode.SINGLE_VARIABLE_DECLARATION &&
			       ((SingleVariableDeclaration)node).getInitializer() == last) {
			fInVariableInitializer= true;
			break;
		} else if (nodeType == ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION &&
			       ((AnnotationTypeMemberDeclaration)node).getDefault() == last) {
			fInVariableInitializer= true;
			break;
		}
		last= node;
		node= node.getParent();
	}
	return fInVariableInitializer;
}
 
Example 8
Source File: ASTResolving.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isWriteAccess(Name selectedNode) {
	ASTNode curr= selectedNode;
	ASTNode parent= curr.getParent();
	while (parent != null) {
		switch (parent.getNodeType()) {
			case ASTNode.QUALIFIED_NAME:
				if (((QualifiedName) parent).getQualifier() == curr) {
					return false;
				}
				break;
			case ASTNode.FIELD_ACCESS:
				if (((FieldAccess) parent).getExpression() == curr) {
					return false;
				}
				break;
			case ASTNode.SUPER_FIELD_ACCESS:
				break;
			case ASTNode.ASSIGNMENT:
				return ((Assignment) parent).getLeftHandSide() == curr;
			case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
			case ASTNode.SINGLE_VARIABLE_DECLARATION:
				return ((VariableDeclaration) parent).getName() == curr;
			case ASTNode.POSTFIX_EXPRESSION:
				return true;
			case ASTNode.PREFIX_EXPRESSION:
				PrefixExpression.Operator op= ((PrefixExpression) parent).getOperator();
				return op == PrefixExpression.Operator.DECREMENT || op == PrefixExpression.Operator.INCREMENT;
			default:
				return false;
		}

		curr= parent;
		parent= curr.getParent();
	}
	return false;
}
 
Example 9
Source File: NewCUUsingWizardProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private ITypeBinding getPossibleSuperTypeBinding(ASTNode node) {
	if (fTypeKind == K_ANNOTATION) {
		return null;
	}

	AST ast= node.getAST();
	node= ASTNodes.getNormalizedNode(node);
	ASTNode parent= node.getParent();
	switch (parent.getNodeType()) {
		case ASTNode.METHOD_DECLARATION:
			if (node.getLocationInParent() == MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY) {
				return ast.resolveWellKnownType("java.lang.Exception"); //$NON-NLS-1$
			}
			break;
		case ASTNode.THROW_STATEMENT :
			return ast.resolveWellKnownType("java.lang.Exception"); //$NON-NLS-1$
		case ASTNode.SINGLE_VARIABLE_DECLARATION:
			if (parent.getLocationInParent() == CatchClause.EXCEPTION_PROPERTY) {
				return ast.resolveWellKnownType("java.lang.Exception"); //$NON-NLS-1$
			}
			break;
		case ASTNode.VARIABLE_DECLARATION_STATEMENT:
		case ASTNode.FIELD_DECLARATION:
			return null; // no guessing for LHS types, cannot be a supertype of a known type
		case ASTNode.PARAMETERIZED_TYPE:
			return null; // Inheritance doesn't help: A<X> z= new A<String>(); ->
	}
	ITypeBinding binding= ASTResolving.guessBindingForTypeReference(node);
	if (binding != null && !binding.isRecovered()) {
		return binding;
	}
	return null;
}
 
Example 10
Source File: ASTResolving.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public static Type guessTypeForReference(AST ast, ASTNode node) {
	ASTNode parent= node.getParent();
	while (parent != null) {
		switch (parent.getNodeType()) {
			case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
				if (((VariableDeclarationFragment) parent).getInitializer() == node) {
					return ASTNodeFactory.newType(ast, (VariableDeclaration) parent);
				}
				return null;
			case ASTNode.SINGLE_VARIABLE_DECLARATION:
				if (((VariableDeclarationFragment) parent).getInitializer() == node) {
					return ASTNodeFactory.newType(ast, (VariableDeclaration) parent);
				}
				return null;
			case ASTNode.ARRAY_ACCESS:
				if (!((ArrayAccess) parent).getIndex().equals(node)) {
					Type type = guessTypeForReference(ast, parent);
					if (type != null) {
						return ASTNodeFactory.newArrayType(type);
					}
				}
				return null;
			case ASTNode.FIELD_ACCESS:
				if (node.equals(((FieldAccess) parent).getName())) {
					node = parent;
					parent = parent.getParent();
				} else {
					return null;
				}
				break;
			case ASTNode.SUPER_FIELD_ACCESS:
			case ASTNode.PARENTHESIZED_EXPRESSION:
				node= parent;
				parent= parent.getParent();
				break;
			case ASTNode.QUALIFIED_NAME:
				if (node.equals(((QualifiedName) parent).getName())) {
					node = parent;
					parent = parent.getParent();
				} else {
					return null;
				}
				break;
			default:
				return null;
		}
	}
	return null;
}
 
Example 11
Source File: AssignToVariableAssistProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private ASTRewrite doAddField(ASTRewrite rewrite, ASTNode nodeToAssign, ITypeBinding typeBinding, int index) {
	boolean isParamToField= nodeToAssign.getNodeType() == ASTNode.SINGLE_VARIABLE_DECLARATION;

	ASTNode newTypeDecl= ASTResolving.findParentType(nodeToAssign);
	if (newTypeDecl == null) {
		return null;
	}

	Expression expression= isParamToField ? ((SingleVariableDeclaration) nodeToAssign).getName() : ((ExpressionStatement) nodeToAssign).getExpression();

	AST ast= newTypeDecl.getAST();

	createImportRewrite((CompilationUnit) nodeToAssign.getRoot());

	BodyDeclaration bodyDecl= ASTResolving.findParentBodyDeclaration(nodeToAssign);
	Block body;
	if (bodyDecl instanceof MethodDeclaration) {
		body= ((MethodDeclaration) bodyDecl).getBody();
	} else if (bodyDecl instanceof Initializer) {
		body= ((Initializer) bodyDecl).getBody();
	} else {
		return null;
	}

	IJavaProject project= getCompilationUnit().getJavaProject();
	boolean isAnonymous= newTypeDecl.getNodeType() == ASTNode.ANONYMOUS_CLASS_DECLARATION;
	boolean isStatic= Modifier.isStatic(bodyDecl.getModifiers()) && !isAnonymous;
	int modifiers= Modifier.PRIVATE;
	if (isStatic) {
		modifiers |= Modifier.STATIC;
	}

	VariableDeclarationFragment newDeclFrag= addFieldDeclaration(rewrite, newTypeDecl, modifiers, expression, nodeToAssign, typeBinding, index);
	String varName= newDeclFrag.getName().getIdentifier();

	Assignment assignment= ast.newAssignment();
	assignment.setRightHandSide((Expression) rewrite.createCopyTarget(expression));

	boolean needsThis= StubUtility.useThisForFieldAccess(project);
	if (isParamToField) {
		needsThis |= varName.equals(((SimpleName) expression).getIdentifier());
	}

	SimpleName accessName= ast.newSimpleName(varName);
	if (needsThis) {
		FieldAccess fieldAccess= ast.newFieldAccess();
		fieldAccess.setName(accessName);
		if (isStatic) {
			String typeName= ((AbstractTypeDeclaration) newTypeDecl).getName().getIdentifier();
			fieldAccess.setExpression(ast.newSimpleName(typeName));
		} else {
			fieldAccess.setExpression(ast.newThisExpression());
		}
		assignment.setLeftHandSide(fieldAccess);
	} else {
		assignment.setLeftHandSide(accessName);
	}

	ASTNode selectionNode;
	if (isParamToField) {
		// assign parameter to field
		ExpressionStatement statement= ast.newExpressionStatement(assignment);
		int insertIdx= findAssignmentInsertIndex(body.statements(), nodeToAssign) + index;
		rewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY).insertAt(statement, insertIdx, null);
		selectionNode= statement;
	} else {
		if (needsSemicolon(expression)) {
			rewrite.replace(expression, ast.newExpressionStatement(assignment), null);
		} else {
			rewrite.replace(expression, assignment, null);
		}
		selectionNode= nodeToAssign;
	}

	addLinkedPosition(rewrite.track(newDeclFrag.getName()), false, KEY_NAME + index);
	if (!isParamToField) {
		FieldDeclaration fieldDeclaration= (FieldDeclaration) newDeclFrag.getParent();
		addLinkedPosition(rewrite.track(fieldDeclaration.getType()), false, KEY_TYPE);
	}
	addLinkedPosition(rewrite.track(accessName), true, KEY_NAME + index);
	IVariableBinding variableBinding= newDeclFrag.resolveBinding();
	if (variableBinding != null) {
		SimpleName[] linkedNodes= LinkedNodeFinder.findByBinding(nodeToAssign.getRoot(), variableBinding);
		for (int i= 0; i < linkedNodes.length; i++) {
			addLinkedPosition(rewrite.track(linkedNodes[i]), false, KEY_NAME + index);
		}
	}
	setEndPosition(rewrite.track(selectionNode));

	return rewrite;
}
 
Example 12
Source File: ChangeTypeRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Determines what kind of ASTNode has been selected.
 * @param node the node
 * @return  A non-null String containing an error message
 * is returned if the ChangeTypeRefactoring refactoring cannot be applied to the selected ASTNode.
 * A return value of null indicates a valid selection.
 */
private String determineSelection(ASTNode node) {
	if (node == null) {
		return RefactoringCoreMessages.ChangeTypeRefactoring_invalidSelection;
	} else {

		if (DEBUG) System.out.println("node nodeType= " + node.getClass().getName()); //$NON-NLS-1$
		if (DEBUG) System.out.println("parent nodeType= " + node.getParent().getClass().getName()); //$NON-NLS-1$
		if (DEBUG) System.out.println("GrandParent nodeType= " + node.getParent().getParent().getClass().getName()); //$NON-NLS-1$

		ASTNode parent= node.getParent();
		ASTNode grandParent= parent.getParent();
		if (grandParent == null)
			return nodeTypeNotSupported();

		// adjustment needed if part of a parameterized type is selected
		if (grandParent.getNodeType() == ASTNode.PARAMETERIZED_TYPE){
			node= grandParent;
		}

		// adjustment needed if part of a qualified name is selected
		ASTNode current= null;
		if (node.getNodeType() == ASTNode.QUALIFIED_NAME){
			current= node;
			while (current.getNodeType() == ASTNode.QUALIFIED_NAME){
				current= current.getParent();
			}
			if (current.getNodeType() != ASTNode.SIMPLE_TYPE){
				return nodeTypeNotSupported();
			}
			node= current.getParent();
		} else if (parent.getNodeType() == ASTNode.QUALIFIED_NAME){
			current= parent;
			while (current.getNodeType() == ASTNode.QUALIFIED_NAME){
				current= current.getParent();
			}
			if (current.getNodeType() != ASTNode.SIMPLE_TYPE){
				return nodeTypeNotSupported();
			}
			node= current.getParent();
		}

		fObject= node.getAST().resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
		switch (node.getNodeType()) {
			case ASTNode.SIMPLE_NAME :
				return simpleNameSelected((SimpleName)node);
			case ASTNode.VARIABLE_DECLARATION_STATEMENT :
				return variableDeclarationStatementSelected((VariableDeclarationStatement) node);
			case ASTNode.FIELD_DECLARATION :
				return fieldDeclarationSelected((FieldDeclaration) node);
			case ASTNode.SINGLE_VARIABLE_DECLARATION :
				return singleVariableDeclarationSelected((SingleVariableDeclaration) node);
			case ASTNode.PARAMETERIZED_TYPE:
				return parameterizedTypeSelected((ParameterizedType) node);
			default :
				return nodeTypeNotSupported();
		}
	}
}
 
Example 13
Source File: UnusedCodeFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Remove the field or variable declaration including the initializer.
 * @param rewrite the AST rewriter to use
 * @param reference a reference to the variable to remove
 * @param group the text edit group to use
 */
private void removeVariableReferences(ASTRewrite rewrite, SimpleName reference, TextEditGroup group) {
	ASTNode parent= reference.getParent();
	while (parent instanceof QualifiedName) {
		parent= parent.getParent();
	}
	if (parent instanceof FieldAccess) {
		parent= parent.getParent();
	}

	int nameParentType= parent.getNodeType();
	if (nameParentType == ASTNode.ASSIGNMENT) {
		Assignment assignment= (Assignment) parent;
		Expression rightHand= assignment.getRightHandSide();

		ASTNode assignParent= assignment.getParent();
		if (assignParent.getNodeType() == ASTNode.EXPRESSION_STATEMENT && rightHand.getNodeType() != ASTNode.ASSIGNMENT) {
			removeVariableWithInitializer(rewrite, rightHand, assignParent, group);
		}	else {
			rewrite.replace(assignment, rewrite.createCopyTarget(rightHand), group);
		}
	} else if (nameParentType == ASTNode.SINGLE_VARIABLE_DECLARATION) {
		rewrite.remove(parent, group);
	} else if (nameParentType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
		VariableDeclarationFragment frag= (VariableDeclarationFragment) parent;
		ASTNode varDecl= frag.getParent();
		List<VariableDeclarationFragment> fragments;
		if (varDecl instanceof VariableDeclarationExpression) {
			fragments= ((VariableDeclarationExpression) varDecl).fragments();
		} else if (varDecl instanceof FieldDeclaration) {
			fragments= ((FieldDeclaration) varDecl).fragments();
		} else {
			fragments= ((VariableDeclarationStatement) varDecl).fragments();
		}
		Expression initializer = frag.getInitializer();
		ArrayList<Expression> sideEffects= new ArrayList<Expression>();
		if (initializer != null) {
			initializer.accept(new SideEffectFinder(sideEffects));
		}
		boolean sideEffectInitializer= sideEffects.size() > 0;
		if (fragments.size() == fUnusedNames.length) {
			if (fForceRemove) {
				rewrite.remove(varDecl, group);
				return;
			}
			if (parent.getParent() instanceof FieldDeclaration) {
				rewrite.remove(varDecl, group);
				return;
			}
			if (sideEffectInitializer) {
				Statement[] wrapped= new Statement[sideEffects.size()];
				for (int i= 0; i < wrapped.length; i++) {
					Expression sideEffect= sideEffects.get(i);
					Expression movedInit= (Expression) rewrite.createMoveTarget(sideEffect);
					wrapped[i]= rewrite.getAST().newExpressionStatement(movedInit);
				}
				StatementRewrite statementRewrite= new StatementRewrite(rewrite, new ASTNode[] { varDecl });
				statementRewrite.replace(wrapped, group);
			} else {
				rewrite.remove(varDecl, group);
			}
		} else {
			if (fForceRemove) {
				rewrite.remove(frag, group);
				return;
			}
			//multiple declarations in one line
			ASTNode declaration = parent.getParent();
			if (declaration instanceof FieldDeclaration) {
				rewrite.remove(frag, group);
				return;
			}
			if (declaration instanceof VariableDeclarationStatement) {
				splitUpDeclarations(rewrite, group, frag, (VariableDeclarationStatement) declaration, sideEffects);
				rewrite.remove(frag, group);
				return;
			}
			if (declaration instanceof VariableDeclarationExpression) {
				//keep constructors and method invocations
				if (!sideEffectInitializer){
					rewrite.remove(frag, group);
				}
			}
		}
	} else if (nameParentType == ASTNode.POSTFIX_EXPRESSION || nameParentType == ASTNode.PREFIX_EXPRESSION) {
		Expression expression= (Expression)parent;
		ASTNode expressionParent= expression.getParent();
		if (expressionParent.getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
			removeStatement(rewrite, expressionParent, group);
		} else {
			rewrite.remove(expression, group);
		}
	}
}
 
Example 14
Source File: ASTResolving.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static Type guessTypeForReference(AST ast, ASTNode node) {
	ASTNode parent= node.getParent();
	while (parent != null) {
		switch (parent.getNodeType()) {
			case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
				if (((VariableDeclarationFragment) parent).getInitializer() == node) {
					return ASTNodeFactory.newType(ast, (VariableDeclaration) parent);
				}
				return null;
			case ASTNode.SINGLE_VARIABLE_DECLARATION:
				if (((VariableDeclarationFragment) parent).getInitializer() == node) {
					return ASTNodeFactory.newType(ast, (VariableDeclaration) parent);
				}
				return null;
			case ASTNode.ARRAY_ACCESS:
				if (!((ArrayAccess) parent).getIndex().equals(node)) {
					Type type= guessTypeForReference(ast, parent);
					if (type != null) {
						return ASTNodeFactory.newArrayType(type);
					}
				}
				return null;
			case ASTNode.FIELD_ACCESS:
				if (node.equals(((FieldAccess) parent).getName())) {
					node= parent;
					parent= parent.getParent();
				} else {
					return null;
				}
				break;
			case ASTNode.SUPER_FIELD_ACCESS:
			case ASTNode.PARENTHESIZED_EXPRESSION:
				node= parent;
				parent= parent.getParent();
				break;
			case ASTNode.QUALIFIED_NAME:
				if (node.equals(((QualifiedName) parent).getName())) {
					node= parent;
					parent= parent.getParent();
				} else {
					return null;
				}
				break;
			default:
				return null;
		}
	}
	return null;
}
 
Example 15
Source File: SuppressWarningsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Adds a SuppressWarnings proposal if possible and returns whether parent nodes should be processed or not (and with what relevance).
 * 
 * @param cu the compilation unit
 * @param node the node on which to add a SuppressWarning token
 * @param warningToken the warning token to add
 * @param relevance the proposal's relevance
 * @param proposals collector to which the proposal should be added
 * @return <code>0</code> if no further proposals should be added to parent nodes, or the relevance of the next proposal
 * 
 * @since 3.6
 */
private static int addSuppressWarningsProposalIfPossible(ICompilationUnit cu, ASTNode node, String warningToken, int relevance, Collection<ICommandAccess> proposals) {

	ChildListPropertyDescriptor property;
	String name;
	boolean isLocalVariable= false;
	switch (node.getNodeType()) {
		case ASTNode.SINGLE_VARIABLE_DECLARATION:
			property= SingleVariableDeclaration.MODIFIERS2_PROPERTY;
			name= ((SingleVariableDeclaration) node).getName().getIdentifier();
			isLocalVariable= true;
			break;
		case ASTNode.VARIABLE_DECLARATION_STATEMENT:
			property= VariableDeclarationStatement.MODIFIERS2_PROPERTY;
			name= getFirstFragmentName(((VariableDeclarationStatement) node).fragments());
			isLocalVariable= true;
			break;
		case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
			property= VariableDeclarationExpression.MODIFIERS2_PROPERTY;
			name= getFirstFragmentName(((VariableDeclarationExpression) node).fragments());
			isLocalVariable= true;
			break;
		case ASTNode.TYPE_DECLARATION:
			property= TypeDeclaration.MODIFIERS2_PROPERTY;
			name= ((TypeDeclaration) node).getName().getIdentifier();
			break;
		case ASTNode.ANNOTATION_TYPE_DECLARATION:
			property= AnnotationTypeDeclaration.MODIFIERS2_PROPERTY;
			name= ((AnnotationTypeDeclaration) node).getName().getIdentifier();
			break;
		case ASTNode.ENUM_DECLARATION:
			property= EnumDeclaration.MODIFIERS2_PROPERTY;
			name= ((EnumDeclaration) node).getName().getIdentifier();
			break;
		case ASTNode.FIELD_DECLARATION:
			property= FieldDeclaration.MODIFIERS2_PROPERTY;
			name= getFirstFragmentName(((FieldDeclaration) node).fragments());
			break;
		// case ASTNode.INITIALIZER: not used, because Initializer cannot have annotations
		case ASTNode.METHOD_DECLARATION:
			property= MethodDeclaration.MODIFIERS2_PROPERTY;
			name= ((MethodDeclaration) node).getName().getIdentifier() + "()"; //$NON-NLS-1$
			break;
		case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
			property= AnnotationTypeMemberDeclaration.MODIFIERS2_PROPERTY;
			name= ((AnnotationTypeMemberDeclaration) node).getName().getIdentifier() + "()"; //$NON-NLS-1$
			break;
		case ASTNode.ENUM_CONSTANT_DECLARATION:
			property= EnumConstantDeclaration.MODIFIERS2_PROPERTY;
			name= ((EnumConstantDeclaration) node).getName().getIdentifier();
			break;
		default:
			return relevance;
	}

	String label= Messages.format(CorrectionMessages.SuppressWarningsSubProcessor_suppress_warnings_label, new String[] { warningToken, BasicElementLabels.getJavaElementName(name) });
	ASTRewriteCorrectionProposal proposal= new SuppressWarningsProposal(warningToken, label, cu, node, property, relevance);

	proposals.add(proposal);
	return isLocalVariable ? relevance - 1 : 0;
}
 
Example 16
Source File: RemoveDeclarationCorrectionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Remove the field or variable declaration including the initializer.
 * @param rewrite the ast rewrite
 * @param reference the reference
 */
private void removeVariableReferences(ASTRewrite rewrite, SimpleName reference) {
	ASTNode parent= reference.getParent();
	while (parent instanceof QualifiedName) {
		parent= parent.getParent();
	}
	if (parent instanceof FieldAccess) {
		parent= parent.getParent();
	}

	int nameParentType= parent.getNodeType();
	if (nameParentType == ASTNode.ASSIGNMENT) {
		Assignment assignment= (Assignment) parent;
		Expression rightHand= assignment.getRightHandSide();

		ASTNode assignParent= assignment.getParent();
		if (assignParent.getNodeType() == ASTNode.EXPRESSION_STATEMENT && rightHand.getNodeType() != ASTNode.ASSIGNMENT) {
			removeVariableWithInitializer(rewrite, rightHand, assignParent);
		}	else {
			rewrite.replace(assignment, rewrite.createCopyTarget(rightHand), null);
		}
	} else if (nameParentType == ASTNode.SINGLE_VARIABLE_DECLARATION) {
		rewrite.remove(parent, null);
	} else if (nameParentType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
		VariableDeclarationFragment frag= (VariableDeclarationFragment) parent;
		ASTNode varDecl= frag.getParent();
		List<VariableDeclarationFragment> fragments;
		if (varDecl instanceof VariableDeclarationExpression) {
			fragments= ((VariableDeclarationExpression) varDecl).fragments();
		} else if (varDecl instanceof FieldDeclaration) {
			fragments= ((FieldDeclaration) varDecl).fragments();
		} else {
			fragments= ((VariableDeclarationStatement) varDecl).fragments();
		}
		if (fragments.size() == 1) {
			rewrite.remove(varDecl, null);
		} else {
			rewrite.remove(frag, null); // don't try to preserve
		}
	}
}