Java Code Examples for org.eclipse.jdt.core.dom.ASTNode#VARIABLE_DECLARATION_FRAGMENT
The following examples show how to use
org.eclipse.jdt.core.dom.ASTNode#VARIABLE_DECLARATION_FRAGMENT .
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: CallInliner.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
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 2
Source File: CallInliner.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static boolean isMultiDeclarationFragment(ASTNode node) { int nodeType= node.getNodeType(); if (nodeType == 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 3
Source File: UnusedCodeFix.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
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 4
Source File: JavaTextSelection.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
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 5
Source File: ASTResolving.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
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 6
Source File: ASTResolving.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
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 7
Source File: ModifierChangeCorrectionProposal.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
@Override protected ASTRewrite getRewrite() throws CoreException { CompilationUnit astRoot = ASTResolving.findParentCompilationUnit(fNode); ASTNode boundNode = astRoot.findDeclaringNode(fBinding); ASTNode declNode = null; if (boundNode != null) { declNode = boundNode; // is same CU } else { //setSelectionDescription(selectionDescription); CompilationUnit newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null); declNode = newRoot.findDeclaringNode(fBinding.getKey()); } if (declNode != null) { AST ast = declNode.getAST(); ASTRewrite rewrite = ASTRewrite.create(ast); if (declNode.getNodeType() == ASTNode.VARIABLE_DECLARATION_FRAGMENT) { VariableDeclarationFragment fragment = (VariableDeclarationFragment) declNode; ASTNode parent = declNode.getParent(); if (parent instanceof FieldDeclaration) { FieldDeclaration fieldDecl = (FieldDeclaration) parent; if (fieldDecl.fragments().size() > 1 && (fieldDecl.getParent() instanceof AbstractTypeDeclaration)) { // split VariableDeclarationRewrite.rewriteModifiers(fieldDecl, new VariableDeclarationFragment[] { fragment }, fIncludedModifiers, fExcludedModifiers, rewrite, null); return rewrite; } } else if (parent instanceof VariableDeclarationStatement) { VariableDeclarationStatement varDecl = (VariableDeclarationStatement) parent; if (varDecl.fragments().size() > 1 && (varDecl.getParent() instanceof Block)) { // split VariableDeclarationRewrite.rewriteModifiers(varDecl, new VariableDeclarationFragment[] { fragment }, fIncludedModifiers, fExcludedModifiers, rewrite, null); return rewrite; } } else if (parent instanceof VariableDeclarationExpression) { // can't separate } declNode = parent; } else if (declNode.getNodeType() == ASTNode.METHOD_DECLARATION) { MethodDeclaration methodDecl = (MethodDeclaration) declNode; if (!methodDecl.isConstructor()) { IMethodBinding methodBinding = methodDecl.resolveBinding(); if (methodDecl.getBody() == null && methodBinding != null && Modifier.isAbstract(methodBinding.getModifiers()) && Modifier.isStatic(fIncludedModifiers)) { // add body ICompilationUnit unit = getCompilationUnit(); String delimiter = unit.findRecommendedLineSeparator(); String bodyStatement = ""; //$NON-NLS-1$ Block body = ast.newBlock(); rewrite.set(methodDecl, MethodDeclaration.BODY_PROPERTY, body, null); Type returnType = methodDecl.getReturnType2(); if (returnType != null) { Expression expression = ASTNodeFactory.newDefaultExpression(ast, returnType, methodDecl.getExtraDimensions()); if (expression != null) { ReturnStatement returnStatement = ast.newReturnStatement(); returnStatement.setExpression(expression); bodyStatement = ASTNodes.asFormattedString(returnStatement, 0, delimiter, unit.getJavaProject().getOptions(true)); } } String placeHolder = CodeGeneration.getMethodBodyContent(unit, methodBinding.getDeclaringClass().getName(), methodBinding.getName(), false, bodyStatement, delimiter); if (placeHolder != null) { ReturnStatement todoNode = (ReturnStatement) rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT); body.statements().add(todoNode); } } } } ModifierRewrite listRewrite = ModifierRewrite.create(rewrite, declNode); PositionInformation trackedDeclNode = listRewrite.setModifiers(fIncludedModifiers, fExcludedModifiers, null); LinkedProposalPositionGroupCore positionGroup = new LinkedProposalPositionGroupCore("group"); //$NON-NLS-1$ positionGroup.addPosition(trackedDeclNode); getLinkedProposalModel().addPositionGroup(positionGroup); if (boundNode != null) { // only set end position if in same CU setEndPosition(rewrite.track(fNode)); } return rewrite; } return null; }
Example 8
Source File: UnusedCodeFix.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * 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 9
Source File: ASTResolving.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
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 10
Source File: RemoveDeclarationCorrectionProposal.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * 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 } } }
Example 11
Source File: ModifierChangeCorrectionProposal.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
@Override protected ASTRewrite getRewrite() throws CoreException { CompilationUnit astRoot= ASTResolving.findParentCompilationUnit(fNode); ASTNode boundNode= astRoot.findDeclaringNode(fBinding); ASTNode declNode= null; if (boundNode != null) { declNode= boundNode; // is same CU } else { //setSelectionDescription(selectionDescription); CompilationUnit newRoot= ASTResolving.createQuickFixAST(getCompilationUnit(), null); declNode= newRoot.findDeclaringNode(fBinding.getKey()); } if (declNode != null) { AST ast= declNode.getAST(); ASTRewrite rewrite= ASTRewrite.create(ast); if (declNode.getNodeType() == ASTNode.VARIABLE_DECLARATION_FRAGMENT) { VariableDeclarationFragment fragment= (VariableDeclarationFragment)declNode; ASTNode parent= declNode.getParent(); if (parent instanceof FieldDeclaration) { FieldDeclaration fieldDecl= (FieldDeclaration) parent; if (fieldDecl.fragments().size() > 1 && (fieldDecl.getParent() instanceof AbstractTypeDeclaration)) { // split VariableDeclarationRewrite.rewriteModifiers(fieldDecl, new VariableDeclarationFragment[] {fragment}, fIncludedModifiers, fExcludedModifiers, rewrite, null); return rewrite; } } else if (parent instanceof VariableDeclarationStatement) { VariableDeclarationStatement varDecl= (VariableDeclarationStatement) parent; if (varDecl.fragments().size() > 1 && (varDecl.getParent() instanceof Block)) { // split VariableDeclarationRewrite.rewriteModifiers(varDecl, new VariableDeclarationFragment[] {fragment}, fIncludedModifiers, fExcludedModifiers, rewrite, null); return rewrite; } } else if (parent instanceof VariableDeclarationExpression) { // can't separate } declNode= parent; } else if (declNode.getNodeType() == ASTNode.METHOD_DECLARATION) { MethodDeclaration methodDecl= (MethodDeclaration) declNode; if (!methodDecl.isConstructor()) { IMethodBinding methodBinding= methodDecl.resolveBinding(); if (methodDecl.getBody() == null && methodBinding != null && Modifier.isAbstract(methodBinding.getModifiers()) && Modifier.isStatic(fIncludedModifiers)) { // add body ICompilationUnit unit= getCompilationUnit(); String delimiter= unit.findRecommendedLineSeparator(); String bodyStatement= ""; //$NON-NLS-1$ Block body= ast.newBlock(); rewrite.set(methodDecl, MethodDeclaration.BODY_PROPERTY, body, null); Type returnType= methodDecl.getReturnType2(); if (returnType != null) { Expression expression= ASTNodeFactory.newDefaultExpression(ast, returnType, methodDecl.getExtraDimensions()); if (expression != null) { ReturnStatement returnStatement= ast.newReturnStatement(); returnStatement.setExpression(expression); bodyStatement= ASTNodes.asFormattedString(returnStatement, 0, delimiter, unit.getJavaProject().getOptions(true)); } } String placeHolder= CodeGeneration.getMethodBodyContent(unit, methodBinding.getDeclaringClass().getName(), methodBinding.getName(), false, bodyStatement, delimiter); if (placeHolder != null) { ReturnStatement todoNode= (ReturnStatement) rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT); body.statements().add(todoNode); } } } } ModifierRewrite listRewrite= ModifierRewrite.create(rewrite, declNode); PositionInformation trackedDeclNode= listRewrite.setModifiers(fIncludedModifiers, fExcludedModifiers, null); LinkedProposalPositionGroup positionGroup= new LinkedProposalPositionGroup("group"); //$NON-NLS-1$ positionGroup.addPosition(trackedDeclNode); getLinkedProposalModel().addPositionGroup(positionGroup); if (boundNode != null) { // only set end position if in same CU setEndPosition(rewrite.track(fNode)); } return rewrite; } return null; }