Java Code Examples for org.eclipse.jdt.core.dom.Assignment#getRightHandSide()
The following examples show how to use
org.eclipse.jdt.core.dom.Assignment#getRightHandSide() .
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: TryPurifyByException.java From SimFix with GNU General Public License v2.0 | 7 votes |
private Set<Integer> findAllMethodCall(){ Set<Integer> methodStmt = new HashSet<>(); if(_backupBody != null){ Block body = _backupBody; for(int i = 0; i < body.statements().size(); i++){ ASTNode stmt = (ASTNode) body.statements().get(i); if(stmt instanceof ExpressionStatement){ stmt = ((ExpressionStatement) stmt).getExpression(); if(stmt instanceof MethodInvocation){ methodStmt.add(i); } else if(stmt instanceof Assignment){ Assignment assign = (Assignment) stmt; if(assign.getRightHandSide() instanceof MethodInvocation){ methodStmt.add(i); } } } } } return methodStmt; }
Example 2
Source File: JavaASTFlattener.java From xtext-xtend with Eclipse Public License 2.0 | 6 votes |
public StringBuffer handleRightHandSide(final Assignment a, final Type type) { StringBuffer _xifexpression = null; if ((this._aSTFlattenerUtils.needPrimitiveCast(type) && (!(a.getRightHandSide() instanceof ArrayCreation)))) { StringBuffer _xblockexpression = null; { this.appendToBuffer("("); a.getRightHandSide().accept(this); StringConcatenation _builder = new StringConcatenation(); _builder.append(") as "); _builder.append(type); _xblockexpression = this.appendToBuffer(_builder.toString()); } _xifexpression = _xblockexpression; } else { a.getRightHandSide().accept(this); } return _xifexpression; }
Example 3
Source File: ExtractClassRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private Expression handleSimpleNameAssignment(ASTNode replaceNode, ParameterObjectFactory pof, String parameterName, AST ast, IJavaProject javaProject, boolean useSuper) { if (replaceNode instanceof Assignment) { Assignment assignment= (Assignment) replaceNode; Expression rightHandSide= assignment.getRightHandSide(); if (rightHandSide.getNodeType() == ASTNode.SIMPLE_NAME) { SimpleName sn= (SimpleName) rightHandSide; IVariableBinding binding= ASTNodes.getVariableBinding(sn); if (binding != null && binding.isField()) { if (fDescriptor.getType().getFullyQualifiedName().equals(binding.getDeclaringClass().getQualifiedName())) { FieldInfo fieldInfo= getFieldInfo(binding.getName()); if (fieldInfo != null && binding == fieldInfo.pi.getOldBinding()) { return pof.createFieldReadAccess(fieldInfo.pi, parameterName, ast, javaProject, useSuper, null); } } } } } return null; }
Example 4
Source File: InferTypeArgumentsConstraintCreator.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
@Override public void endVisit(Assignment node) { Expression lhs= node.getLeftHandSide(); Expression rhs= node.getRightHandSide(); ConstraintVariable2 left= getConstraintVariable(lhs); ConstraintVariable2 right= getConstraintVariable(rhs); if (node.resolveBoxing()) { ImmutableTypeVariable2 boxed= fTCModel.makeImmutableTypeVariable(node.resolveTypeBinding(), node); setConstraintVariable(node, boxed); } else { setConstraintVariable(node, left); // type of assignement is type of 'left' } if (left == null || right == null) return; Assignment.Operator op= node.getOperator(); if (op == Assignment.Operator.PLUS_ASSIGN && (lhs.resolveTypeBinding() == node.getAST().resolveWellKnownType("java.lang.String"))) { //$NON-NLS-1$ //Special handling for automatic String conversion: do nothing; the RHS can be anything. } else { fTCModel.createElementEqualsConstraints(left, right); fTCModel.createSubtypeConstraint(right, left); // left= right; --> [right] <= [left] } //TODO: other implicit conversions: numeric promotion, autoboxing? }
Example 5
Source File: MethodObject.java From JDeodorant with MIT License | 6 votes |
public FieldInstructionObject isSetter() { if(getMethodBody() != null) { List<AbstractStatement> abstractStatements = getMethodBody().getCompositeStatement().getStatements(); if(abstractStatements.size() == 1 && abstractStatements.get(0) instanceof StatementObject) { StatementObject statementObject = (StatementObject)abstractStatements.get(0); Statement statement = statementObject.getStatement(); if(statement instanceof ExpressionStatement) { ExpressionStatement expressionStatement = (ExpressionStatement)statement; if(expressionStatement.getExpression() instanceof Assignment && statementObject.getFieldInstructions().size() == 1 && statementObject.getMethodInvocations().size() == 0 && statementObject.getLocalVariableDeclarations().size() == 0 && statementObject.getLocalVariableInstructions().size() == 1 && this.constructorObject.parameterList.size() == 1) { Assignment assignment = (Assignment)expressionStatement.getExpression(); if((assignment.getLeftHandSide() instanceof SimpleName || assignment.getLeftHandSide() instanceof FieldAccess) && assignment.getRightHandSide() instanceof SimpleName) return statementObject.getFieldInstructions().get(0); } } } } return null; }
Example 6
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 7
Source File: MethodDeclarationUtility.java From JDeodorant with MIT License | 5 votes |
public static SimpleName isSetter(MethodDeclaration methodDeclaration) { Block methodBody = methodDeclaration.getBody(); List<SingleVariableDeclaration> parameters = methodDeclaration.parameters(); if(methodBody != null) { List<Statement> statements = methodBody.statements(); if(statements.size() == 1 && parameters.size() == 1) { Statement statement = statements.get(0); if(statement instanceof ExpressionStatement) { ExpressionStatement expressionStatement = (ExpressionStatement)statement; Expression expressionStatementExpression = expressionStatement.getExpression(); if(expressionStatementExpression instanceof Assignment) { Assignment assignment = (Assignment)expressionStatementExpression; Expression rightHandSide = assignment.getRightHandSide(); if(rightHandSide instanceof SimpleName) { SimpleName rightHandSideSimpleName = (SimpleName)rightHandSide; if(rightHandSideSimpleName.resolveBinding().isEqualTo(parameters.get(0).resolveBinding())) { Expression leftHandSide = assignment.getLeftHandSide(); if(leftHandSide instanceof SimpleName) { return (SimpleName)leftHandSide; } else if(leftHandSide instanceof FieldAccess) { FieldAccess fieldAccess = (FieldAccess)leftHandSide; return fieldAccess.getName(); } } } } } } } return null; }
Example 8
Source File: AccessAnalyzer.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
@Override public boolean visit(Assignment node) { Expression leftHandSide = node.getLeftHandSide(); if (!considerBinding(resolveBinding(leftHandSide), leftHandSide)) { return true; } checkParent(node); Expression rightHandSide = node.getRightHandSide(); if (!fIsFieldFinal) { // Write access. AST ast = node.getAST(); MethodInvocation invocation = ast.newMethodInvocation(); invocation.setName(ast.newSimpleName(fSetter)); fReferencingSetter = true; Expression receiver = getReceiver(leftHandSide); if (receiver != null) { invocation.setExpression((Expression) fRewriter.createCopyTarget(receiver)); } List<Expression> arguments = invocation.arguments(); if (node.getOperator() == Assignment.Operator.ASSIGN) { arguments.add((Expression) fRewriter.createCopyTarget(rightHandSide)); } else { // This is the compound assignment case: field+= 10; InfixExpression exp = ast.newInfixExpression(); exp.setOperator(ASTNodes.convertToInfixOperator(node.getOperator())); MethodInvocation getter = ast.newMethodInvocation(); getter.setName(ast.newSimpleName(fGetter)); fReferencingGetter = true; if (receiver != null) { getter.setExpression((Expression) fRewriter.createCopyTarget(receiver)); } exp.setLeftOperand(getter); Expression rhs = (Expression) fRewriter.createCopyTarget(rightHandSide); if (NecessaryParenthesesChecker.needsParenthesesForRightOperand(rightHandSide, exp, leftHandSide.resolveTypeBinding())) { ParenthesizedExpression p = ast.newParenthesizedExpression(); p.setExpression(rhs); rhs = p; } exp.setRightOperand(rhs); arguments.add(exp); } fRewriter.replace(node, invocation, createGroupDescription(WRITE_ACCESS)); } rightHandSide.accept(this); return false; }
Example 9
Source File: AccessAnalyzer.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
@Override public boolean visit(Assignment node) { Expression leftHandSide= node.getLeftHandSide(); if (!considerBinding(resolveBinding(leftHandSide), leftHandSide)) return true; checkParent(node); Expression rightHandSide= node.getRightHandSide(); if (!fIsFieldFinal) { // Write access. AST ast= node.getAST(); MethodInvocation invocation= ast.newMethodInvocation(); invocation.setName(ast.newSimpleName(fSetter)); fReferencingSetter= true; Expression receiver= getReceiver(leftHandSide); if (receiver != null) invocation.setExpression((Expression)fRewriter.createCopyTarget(receiver)); List<Expression> arguments= invocation.arguments(); if (node.getOperator() == Assignment.Operator.ASSIGN) { arguments.add((Expression)fRewriter.createCopyTarget(rightHandSide)); } else { // This is the compound assignment case: field+= 10; InfixExpression exp= ast.newInfixExpression(); exp.setOperator(ASTNodes.convertToInfixOperator(node.getOperator())); MethodInvocation getter= ast.newMethodInvocation(); getter.setName(ast.newSimpleName(fGetter)); fReferencingGetter= true; if (receiver != null) getter.setExpression((Expression)fRewriter.createCopyTarget(receiver)); exp.setLeftOperand(getter); Expression rhs= (Expression)fRewriter.createCopyTarget(rightHandSide); if (NecessaryParenthesesChecker.needsParenthesesForRightOperand(rightHandSide, exp, leftHandSide.resolveTypeBinding())) { ParenthesizedExpression p= ast.newParenthesizedExpression(); p.setExpression(rhs); rhs= p; } exp.setRightOperand(rhs); arguments.add(exp); } fRewriter.replace(node, invocation, createGroupDescription(WRITE_ACCESS)); } rightHandSide.accept(this); return false; }
Example 10
Source File: GetterSetterUtil.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Converts an assignment, postfix expression or prefix expression into an assignable equivalent expression using the getter. * * @param node the assignment/prefix/postfix node * @param astRewrite the astRewrite to use * @param getterExpression the expression to insert for read accesses or <code>null</code> if such an expression does not exist * @param variableType the type of the variable that the result will be assigned to * @param is50OrHigher <code>true</code> if a 5.0 or higher environment can be used * @return an expression that can be assigned to the type variableType with node being replaced by a equivalent expression using the getter */ public static Expression getAssignedValue(ASTNode node, ASTRewrite astRewrite, Expression getterExpression, ITypeBinding variableType, boolean is50OrHigher) { InfixExpression.Operator op= null; AST ast= astRewrite.getAST(); if (isNotInBlock(node)) return null; if (node.getNodeType() == ASTNode.ASSIGNMENT) { Assignment assignment= ((Assignment) node); Expression rightHandSide= assignment.getRightHandSide(); Expression copiedRightOp= (Expression) astRewrite.createCopyTarget(rightHandSide); if (assignment.getOperator() == Operator.ASSIGN) { ITypeBinding rightHandSideType= rightHandSide.resolveTypeBinding(); copiedRightOp= createNarrowCastIfNessecary(copiedRightOp, rightHandSideType, ast, variableType, is50OrHigher); return copiedRightOp; } if (getterExpression != null) { InfixExpression infix= ast.newInfixExpression(); infix.setLeftOperand(getterExpression); infix.setOperator(ASTNodes.convertToInfixOperator(assignment.getOperator())); ITypeBinding infixType= infix.resolveTypeBinding(); if (NecessaryParenthesesChecker.needsParenthesesForRightOperand(rightHandSide, infix, variableType)) { ParenthesizedExpression p= ast.newParenthesizedExpression(); p.setExpression(copiedRightOp); copiedRightOp= p; } infix.setRightOperand(copiedRightOp); return createNarrowCastIfNessecary(infix, infixType, ast, variableType, is50OrHigher); } } else if (node.getNodeType() == ASTNode.POSTFIX_EXPRESSION) { PostfixExpression po= (PostfixExpression) node; if (po.getOperator() == PostfixExpression.Operator.INCREMENT) op= InfixExpression.Operator.PLUS; if (po.getOperator() == PostfixExpression.Operator.DECREMENT) op= InfixExpression.Operator.MINUS; } else if (node.getNodeType() == ASTNode.PREFIX_EXPRESSION) { PrefixExpression pe= (PrefixExpression) node; if (pe.getOperator() == PrefixExpression.Operator.INCREMENT) op= InfixExpression.Operator.PLUS; if (pe.getOperator() == PrefixExpression.Operator.DECREMENT) op= InfixExpression.Operator.MINUS; } if (op != null && getterExpression != null) { return createInfixInvocationFromPostPrefixExpression(op, getterExpression, ast, variableType, is50OrHigher); } return null; }
Example 11
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 12
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 13
Source File: ASTNodeMatcher.java From JDeodorant with MIT License | 4 votes |
public boolean match(ExpressionStatement node, Object other) { if (AbstractControlStructureUtilities.hasOneConditionalExpression(node) != null && other instanceof IfStatement) { TernaryControlStructure nodeTernaryControlStructure = new TernaryControlStructure(node); return ifMatch(nodeTernaryControlStructure, other); } else if(node.getExpression() instanceof Assignment && other instanceof VariableDeclarationStatement) { VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement)other; List fragments = variableDeclarationStatement.fragments(); if(fragments.size() == 1) { VariableDeclarationFragment fragment = (VariableDeclarationFragment)fragments.get(0); Assignment assignment = (Assignment)node.getExpression(); Expression leftHandSide = assignment.getLeftHandSide(); if(leftHandSide instanceof SimpleName) { SimpleName simpleName = (SimpleName)leftHandSide; boolean variableMatch = safeSubtreeMatch(simpleName, fragment.getName()); boolean variableTypeMatch = false; IBinding simpleNameBinding = simpleName.resolveBinding(); IBinding fragmentNameBinding = fragment.getName().resolveBinding(); if(simpleNameBinding.getKind() == IBinding.VARIABLE && fragmentNameBinding.getKind() == IBinding.VARIABLE) { IVariableBinding simpleNameVariableBinding = (IVariableBinding)simpleNameBinding; IVariableBinding fragmentNameVariableBinding = (IVariableBinding)fragmentNameBinding; variableTypeMatch = simpleNameVariableBinding.getType().isEqualTo(fragmentNameVariableBinding.getType()) && simpleNameVariableBinding.getType().getQualifiedName().equals(fragmentNameVariableBinding.getType().getQualifiedName()); } boolean initializerMatch = false; boolean initializerTypeMatch = false; Expression initializer = fragment.getInitializer(); Expression rightHandSide = assignment.getRightHandSide(); if(initializer != null && initializer.getNodeType() == rightHandSide.getNodeType()) { initializerMatch = safeSubtreeMatch(rightHandSide, initializer); initializerTypeMatch = initializer.resolveTypeBinding().isEqualTo(rightHandSide.resolveTypeBinding()) && initializer.resolveTypeBinding().getQualifiedName().equals(rightHandSide.resolveTypeBinding().getQualifiedName()); } if(variableMatch && variableTypeMatch && initializerMatch && initializerTypeMatch) { VariableDeclaration variableDeclaration = AbstractLoopUtilities.getVariableDeclaration(simpleName); if(variableDeclaration != null && hasEmptyInitializer(variableDeclaration)) { safeSubtreeMatch(variableDeclaration.getName(), fragment.getName()); List<ASTNode> astNodes = new ArrayList<ASTNode>(); astNodes.add(variableDeclaration); ASTInformationGenerator.setCurrentITypeRoot(typeRoot1); reportAdditionalFragments(astNodes, this.additionallyMatchedFragments1); return true; } } } } } return super.match(node, other); }
Example 14
Source File: ASTNodeMatcher.java From JDeodorant with MIT License | 4 votes |
public boolean match(VariableDeclarationStatement node, Object other) { List fragments = node.fragments(); if(fragments.size() == 1 && other instanceof ExpressionStatement) { VariableDeclarationFragment fragment = (VariableDeclarationFragment)fragments.get(0); ExpressionStatement expressionStatement = (ExpressionStatement)other; Expression expression = expressionStatement.getExpression(); if(expression instanceof Assignment) { Assignment assignment = (Assignment)expression; Expression leftHandSide = assignment.getLeftHandSide(); if(leftHandSide instanceof SimpleName) { SimpleName simpleName = (SimpleName)leftHandSide; boolean variableMatch = safeSubtreeMatch(fragment.getName(), simpleName); boolean variableTypeMatch = false; IBinding simpleNameBinding = simpleName.resolveBinding(); IBinding fragmentNameBinding = fragment.getName().resolveBinding(); if(simpleNameBinding.getKind() == IBinding.VARIABLE && fragmentNameBinding.getKind() == IBinding.VARIABLE) { IVariableBinding simpleNameVariableBinding = (IVariableBinding)simpleNameBinding; IVariableBinding fragmentNameVariableBinding = (IVariableBinding)fragmentNameBinding; variableTypeMatch = simpleNameVariableBinding.getType().isEqualTo(fragmentNameVariableBinding.getType()) && simpleNameVariableBinding.getType().getQualifiedName().equals(fragmentNameVariableBinding.getType().getQualifiedName());; } boolean initializerMatch = false; boolean initializerTypeMatch = false; Expression initializer = fragment.getInitializer(); Expression rightHandSide = assignment.getRightHandSide(); if(initializer != null && initializer.getNodeType() == rightHandSide.getNodeType()) { initializerMatch = safeSubtreeMatch(initializer, rightHandSide); initializerTypeMatch = initializer.resolveTypeBinding().isEqualTo(rightHandSide.resolveTypeBinding()) && initializer.resolveTypeBinding().getQualifiedName().equals(rightHandSide.resolveTypeBinding().getQualifiedName()); } if(variableMatch && variableTypeMatch && initializerMatch && initializerTypeMatch) { VariableDeclaration variableDeclaration = AbstractLoopUtilities.getVariableDeclaration(simpleName); if(variableDeclaration != null && hasEmptyInitializer(variableDeclaration)) { safeSubtreeMatch(fragment.getName(), variableDeclaration.getName()); List<ASTNode> astNodes = new ArrayList<ASTNode>(); astNodes.add(variableDeclaration); ASTInformationGenerator.setCurrentITypeRoot(typeRoot2); reportAdditionalFragments(astNodes, this.additionallyMatchedFragments2); return true; } } } } } return super.match(node, other); }
Example 15
Source File: AbstractLoopUtilities.java From JDeodorant with MIT License | 4 votes |
private static Integer assignmentUpdateValue(Assignment assignment) { Integer updateValue = null; Expression leftHandSide = assignment.getLeftHandSide(); Assignment.Operator operator = assignment.getOperator(); Expression rightHandSide = assignment.getRightHandSide(); if (operator == Assignment.Operator.PLUS_ASSIGN) { updateValue = AbstractLoopUtilities.getIntegerValue(rightHandSide); } else if (operator == Assignment.Operator.MINUS_ASSIGN) { Integer rightHandSideIntegerValue = AbstractLoopUtilities.getIntegerValue(rightHandSide); if (rightHandSideIntegerValue != null) { updateValue = (-1) * rightHandSideIntegerValue; } } else if (leftHandSide instanceof SimpleName && operator == Assignment.Operator.ASSIGN && rightHandSide instanceof InfixExpression) { SimpleName leftHandSideSimpleName = (SimpleName) leftHandSide; IBinding leftHandSideBinding = leftHandSideSimpleName.resolveBinding(); InfixExpression infixExpression = (InfixExpression) rightHandSide; InfixExpression.Operator infixOperator = infixExpression.getOperator(); Expression rightOperand = infixExpression.getRightOperand(); Expression leftOperand = infixExpression.getLeftOperand(); if (infixOperator.toString().equals("+") || infixOperator.toString().equals("-")) { if (leftOperand instanceof SimpleName) { SimpleName leftOperandSimpleName = (SimpleName) leftOperand; IBinding leftOperandBinding = leftOperandSimpleName.resolveBinding(); if (leftOperandBinding.isEqualTo(leftHandSideBinding)) { Integer rightOperandIntegerValue = AbstractLoopUtilities.getIntegerValue(rightOperand); if (infixOperator.toString().equals("+") && rightOperandIntegerValue != null) { updateValue = rightOperandIntegerValue; } else if (infixOperator.toString().equals("-") && rightOperandIntegerValue != null) { updateValue = (-1) * rightOperandIntegerValue; } } } else if (rightOperand instanceof SimpleName) { SimpleName rightOperandSimpleName = (SimpleName) rightOperand; IBinding rightOperandBinding = rightOperandSimpleName.resolveBinding(); if (rightOperandBinding.isEqualTo(leftHandSideBinding)) { Integer leftOperandIntegerValue = AbstractLoopUtilities.getIntegerValue(leftOperand); if (infixOperator.toString().equals("+") && leftOperandIntegerValue != null) { updateValue = leftOperandIntegerValue; } } } } } return updateValue; }
Example 16
Source File: AbstractLoopUtilities.java From JDeodorant with MIT License | 4 votes |
public static List<ASTNode> getVariableDeclarationsAndAssignmentsContainingAccessUsingVariable(Statement body, ControlVariable variable) { StatementExtractor statementExtractor = new StatementExtractor(); ExpressionExtractor expressionExtractor = new ExpressionExtractor(); ArrayList<ASTNode> returnList = new ArrayList<ASTNode>(); List<Statement> variableDeclarationStatements = statementExtractor.getVariableDeclarationStatements(body); List<Expression> assignments = expressionExtractor.getAssignments(body); for (Statement currentStatement : variableDeclarationStatements) { List<VariableDeclarationFragment> fragments = ((VariableDeclarationStatement)currentStatement).fragments(); for (VariableDeclarationFragment fragment : fragments) { Expression initializer = fragment.getInitializer(); if (isAccessUsingVariable(initializer, variable)) { returnList.add(fragment); } } } for (Expression currentExpression : assignments) { Assignment currentAssignment = (Assignment)currentExpression; Expression rightHandSide = currentAssignment.getRightHandSide(); if (isAccessUsingVariable(rightHandSide, variable)) { returnList.add(currentAssignment); } } List<Expression> methodInvocations = expressionExtractor.getMethodInvocations(body); for (Expression expression : methodInvocations) { if (expression instanceof MethodInvocation) { MethodInvocation methodInvocation = (MethodInvocation)expression; Expression methodInvocationExpression = methodInvocation.getExpression(); if(methodInvocationExpression != null) { if (isAccessUsingVariable(methodInvocationExpression, variable)) { returnList.add(methodInvocation); } } } } return returnList; }