private static boolean isAcumulationAssign(Assignment assignment, InfixExpression.Operator op, Predicate<Expression> acceptExpression) { if(!( assignment.getRightHandSide() instanceof InfixExpression && assignment.getLeftHandSide() instanceof SimpleName && assignment.getOperator() == Assignment.Operator.ASSIGN)) return false; InfixExpression exp = (InfixExpression) assignment.getRightHandSide(); if(exp.getOperator() != op) return false; String assignVar = assignment.getLeftHandSide().toString(); if( exp.getLeftOperand() instanceof SimpleName && exp.getLeftOperand().toString().equals(assignVar) && acceptExpression.test(exp.getRightOperand())) return true; if( exp.getRightOperand() instanceof SimpleName && exp.getRightOperand().toString().equals(assignVar) && acceptExpression.test(exp.getLeftOperand())) return true; return false; }
private Block createWithMethodBody(AST ast, BuilderField builderField) { String originalFieldName = builderField.getOriginalFieldName(); String builderFieldName = builderField.getBuilderFieldName(); Block newBlock = ast.newBlock(); ReturnStatement builderReturnStatement = ast.newReturnStatement(); builderReturnStatement.setExpression(ast.newThisExpression()); Assignment newAssignment = ast.newAssignment(); FieldAccess fieldAccess = ast.newFieldAccess(); fieldAccess.setExpression(ast.newThisExpression()); fieldAccess.setName(ast.newSimpleName(originalFieldName)); newAssignment.setLeftHandSide(fieldAccess); newAssignment.setRightHandSide(ast.newSimpleName(builderFieldName)); newBlock.statements().add(ast.newExpressionStatement(newAssignment)); newBlock.statements().add(builderReturnStatement); return newBlock; }
private Block createWithMethodBody(AST ast, String originalFieldName, String builderFieldName) { Block newBlock = ast.newBlock(); ReturnStatement builderReturnStatement = ast.newReturnStatement(); builderReturnStatement.setExpression(ast.newThisExpression()); Assignment newAssignment = ast.newAssignment(); FieldAccess fieldAccess = ast.newFieldAccess(); fieldAccess.setExpression(ast.newThisExpression()); fieldAccess.setName(ast.newSimpleName(originalFieldName)); newAssignment.setLeftHandSide(fieldAccess); newAssignment.setRightHandSide(ast.newSimpleName(builderFieldName)); newBlock.statements().add(ast.newExpressionStatement(newAssignment)); newBlock.statements().add(builderReturnStatement); return newBlock; }
/** * Returns the binding of the variable written in an Assignment. * @param assignment The assignment * @return The binding or <code>null</code> if no bindings are available. */ public static IVariableBinding getAssignedVariable(Assignment assignment) { Expression leftHand = assignment.getLeftHandSide(); switch (leftHand.getNodeType()) { case ASTNode.SIMPLE_NAME: return (IVariableBinding) ((SimpleName) leftHand).resolveBinding(); case ASTNode.QUALIFIED_NAME: return (IVariableBinding) ((QualifiedName) leftHand).getName().resolveBinding(); case ASTNode.FIELD_ACCESS: return ((FieldAccess) leftHand).resolveFieldBinding(); case ASTNode.SUPER_FIELD_ACCESS: return ((SuperFieldAccess) leftHand).resolveFieldBinding(); default: return null; } }
static boolean isLeftHandSideOfAssignment(ASTNode node) { Assignment assignment = (Assignment) ASTNodes.getParent(node, ASTNode.ASSIGNMENT); if (assignment != null) { Expression leftHandSide = assignment.getLeftHandSide(); if (leftHandSide == node) { return true; } if (ASTNodes.isParent(node, leftHandSide)) { switch (leftHandSide.getNodeType()) { case ASTNode.SIMPLE_NAME: return true; case ASTNode.FIELD_ACCESS: return node == ((FieldAccess) leftHandSide).getName(); case ASTNode.QUALIFIED_NAME: return node == ((QualifiedName) leftHandSide).getName(); case ASTNode.SUPER_FIELD_ACCESS: return node == ((SuperFieldAccess) leftHandSide).getName(); default: return false; } } } return false; }
@Override public void endVisit(Assignment node) { if (skipNode(node)) { return; } FlowInfo lhs = getFlowInfo(node.getLeftHandSide()); FlowInfo rhs = getFlowInfo(node.getRightHandSide()); if (lhs instanceof LocalFlowInfo) { LocalFlowInfo llhs = (LocalFlowInfo) lhs; llhs.setWriteAccess(fFlowContext); if (node.getOperator() != Assignment.Operator.ASSIGN) { GenericSequentialFlowInfo tmp = createSequential(); tmp.merge(new LocalFlowInfo(llhs, FlowInfo.READ, fFlowContext), fFlowContext); tmp.merge(rhs, fFlowContext); rhs = tmp; } } GenericSequentialFlowInfo info = createSequential(node); // first process right and side and then left hand side. info.merge(rhs, fFlowContext); info.merge(lhs, fFlowContext); }
/** * for an assignment x = y returns the variable binding of x * * @param varBinding * @param assignment * @return */ private static IVariableBinding getLeftHandSideVarBinding(Assignment assignment) { Expression leftHnsd = assignment.getLeftHandSide(); if (leftHnsd instanceof SimpleName) { IBinding bind = ((SimpleName) leftHnsd).resolveBinding(); if (bind instanceof IVariableBinding) { return ((IVariableBinding) bind).getVariableDeclaration(); } } if (leftHnsd instanceof FieldAccess) { FieldAccess fa = (FieldAccess) leftHnsd; return fa.resolveFieldBinding(); } // Leave it null - cannot determine actual domains for arrays // workaround for bugs related to objects created in complex expression // if (leftHnsd instanceof ArrayAccess) { // ArrayAccess aa = (ArrayAccess) leftHnsd; // return getArrayAccessVarBinding(aa); // } return null; }
/** * returns the name associated with an assignment instruction * * @param instr * @return */ public static String getSimpleAssignName(AssignmentInstruction instr) { if (instr instanceof LoadLiteralInstruction) return ((LoadLiteralInstruction) instr).getLiteral().toString(); ASTNode parentInstr = instr.getNode().getParent(); if (parentInstr instanceof VariableDeclarationFragment) { return ((VariableDeclarationFragment) parentInstr).getName().getFullyQualifiedName(); } if (parentInstr instanceof Assignment) { Expression leftHnsd = ((Assignment) parentInstr).getLeftHandSide(); if (leftHnsd instanceof SimpleName) { return ((SimpleName) leftHnsd).getFullyQualifiedName(); } else if (leftHnsd instanceof FieldAccess) { return ((FieldAccess) leftHnsd).getName().getFullyQualifiedName(); } } throw new IllegalStateException("cannot find local variable associated with " + instr); }
public static FieldWrite createFrom(ASTNode node){ FieldWrite retNode = null; boolean compatibleType = false; if(node instanceof Assignment){ compatibleType = true; } else if (node instanceof VariableDeclarationFragment){ VariableDeclarationFragment vdf = (VariableDeclarationFragment)node; if (vdf.getInitializer()!=null){ compatibleType = true; } } if (compatibleType){ Adapter factory = Adapter.getInstance(); AstNode astNode = factory.get(node); if ( astNode instanceof FieldWrite ) { retNode = (FieldWrite)astNode; } else { retNode = FieldWrite.create(); retNode.complexExpression = node.toString(); factory.map(node, retNode); } } else { throw new IllegalArgumentException(); } return retNode; }
/** * Returns the binding of the variable written in an Assignment. * * @param assignment The assignment * @return The binding or <code>null</code> if no bindings are available. */ public static IVariableBinding getAssignedVariable(Assignment assignment) { Expression leftHand = assignment.getLeftHandSide(); switch (leftHand.getNodeType()) { case ASTNode.SIMPLE_NAME: return (IVariableBinding) ((SimpleName) leftHand).resolveBinding(); case ASTNode.QUALIFIED_NAME: return (IVariableBinding) ((QualifiedName) leftHand).getName().resolveBinding(); case ASTNode.FIELD_ACCESS: return ((FieldAccess) leftHand).resolveFieldBinding(); case ASTNode.SUPER_FIELD_ACCESS: return ((SuperFieldAccess) leftHand).resolveFieldBinding(); default: return null; } }
private Statement newFieldAssignment( AST ast, SimpleName fieldNameNode, Expression initializer, boolean useThisAccess) { Assignment assignment = ast.newAssignment(); if (useThisAccess) { FieldAccess access = ast.newFieldAccess(); access.setExpression(ast.newThisExpression()); access.setName(fieldNameNode); assignment.setLeftHandSide(access); } else { assignment.setLeftHandSide(fieldNameNode); } assignment.setOperator(Assignment.Operator.ASSIGN); assignment.setRightHandSide(initializer); return ast.newExpressionStatement(assignment); }
private static boolean isLeftHandSideOfAssignment(ASTNode node) { Assignment assignment = (Assignment) ASTNodes.getParent(node, ASTNode.ASSIGNMENT); if (assignment != null) { Expression leftHandSide = assignment.getLeftHandSide(); if (leftHandSide == node) { return true; } if (ASTNodes.isParent(node, leftHandSide)) { switch (leftHandSide.getNodeType()) { case ASTNode.SIMPLE_NAME: return true; case ASTNode.FIELD_ACCESS: return node == ((FieldAccess) leftHandSide).getName(); case ASTNode.QUALIFIED_NAME: return node == ((QualifiedName) leftHandSide).getName(); case ASTNode.SUPER_FIELD_ACCESS: return node == ((SuperFieldAccess) leftHandSide).getName(); default: return false; } } } return false; }
public static IBinding resolveBinding(Expression expression) { if (expression instanceof Name) return ((Name) expression).resolveBinding(); if (expression instanceof ParenthesizedExpression) return resolveBinding(((ParenthesizedExpression) expression).getExpression()); else if (expression instanceof Assignment) return resolveBinding(((Assignment) expression).getLeftHandSide()); // TODO ??? else if (expression instanceof MethodInvocation) return ((MethodInvocation) expression).resolveMethodBinding(); else if (expression instanceof SuperMethodInvocation) return ((SuperMethodInvocation) expression).resolveMethodBinding(); else if (expression instanceof FieldAccess) return ((FieldAccess) expression).resolveFieldBinding(); else if (expression instanceof SuperFieldAccess) return ((SuperFieldAccess) expression).resolveFieldBinding(); else if (expression instanceof ConditionalExpression) return resolveBinding(((ConditionalExpression) expression).getThenExpression()); return null; }
/** * Helper to generate an iterator based <code>for</code> loop to iterate over an {@link Iterable}. * * @param ast the {@link AST} instance to rewrite the loop to * @return the complete {@link ASTRewrite} object */ private ASTRewrite generateIteratorBasedForRewrite(AST ast) { ASTRewrite rewrite = ASTRewrite.create(ast); ForStatement loopStatement = ast.newForStatement(); ITypeBinding loopOverType = extractElementType(ast); SimpleName loopVariableName = resolveLinkedVariableNameWithProposals(rewrite, "iterator", null, true); // $NON-NLS-1$ loopStatement.initializers().add(getIteratorBasedForInitializer(rewrite, loopVariableName)); MethodInvocation loopExpression = ast.newMethodInvocation(); loopExpression.setName(ast.newSimpleName("hasNext")); // $NON-NLS-1$ SimpleName expressionName = ast.newSimpleName(loopVariableName.getIdentifier()); addLinkedPosition( rewrite.track(expressionName), LinkedPositionGroup.NO_STOP, expressionName.getIdentifier()); loopExpression.setExpression(expressionName); loopStatement.setExpression(loopExpression); Block forLoopBody = ast.newBlock(); Assignment assignResolvedVariable = getIteratorBasedForBodyAssignment(rewrite, loopOverType, loopVariableName); forLoopBody.statements().add(ast.newExpressionStatement(assignResolvedVariable)); forLoopBody.statements().add(createBlankLineStatementWithCursorPosition(rewrite)); loopStatement.setBody(forLoopBody); rewrite.replace(fCurrentNode, loopStatement, null); return rewrite; }
/** * Generates the Assignment in an iterator based for, used in the first statement of an iterator * based <code>for</code> loop body, to retrieve the next element of the {@link Iterable} * instance. * * @param rewrite the current instance of {@link ASTRewrite} * @param loopOverType the {@link ITypeBinding} of the loop variable * @param loopVariableName the name of the loop variable * @return an {@link Assignment}, which retrieves the next element of the {@link Iterable} using * the active {@link Iterator} */ private Assignment getIteratorBasedForBodyAssignment( ASTRewrite rewrite, ITypeBinding loopOverType, SimpleName loopVariableName) { AST ast = rewrite.getAST(); Assignment assignResolvedVariable = ast.newAssignment(); // left hand side SimpleName resolvedVariableName = resolveLinkedVariableNameWithProposals( rewrite, loopOverType.getName(), loopVariableName.getIdentifier(), false); VariableDeclarationFragment resolvedVariableDeclarationFragment = ast.newVariableDeclarationFragment(); resolvedVariableDeclarationFragment.setName(resolvedVariableName); VariableDeclarationExpression resolvedVariableDeclaration = ast.newVariableDeclarationExpression(resolvedVariableDeclarationFragment); resolvedVariableDeclaration.setType( getImportRewrite() .addImport( loopOverType, ast, new ContextSensitiveImportRewriteContext(fCurrentNode, getImportRewrite()))); assignResolvedVariable.setLeftHandSide(resolvedVariableDeclaration); // right hand side MethodInvocation invokeIteratorNextExpression = ast.newMethodInvocation(); invokeIteratorNextExpression.setName(ast.newSimpleName("next")); // $NON-NLS-1$ SimpleName currentElementName = ast.newSimpleName(loopVariableName.getIdentifier()); addLinkedPosition( rewrite.track(currentElementName), LinkedPositionGroup.NO_STOP, currentElementName.getIdentifier()); invokeIteratorNextExpression.setExpression(currentElementName); assignResolvedVariable.setRightHandSide(invokeIteratorNextExpression); assignResolvedVariable.setOperator(Assignment.Operator.ASSIGN); return assignResolvedVariable; }
/** * Creates an {@link Assignment} as first expression appearing in a <code>for</code> loop's body. * This Assignment declares a local variable and initializes it using the array's current element * identified by the loop index. * * @param rewrite the current {@link ASTRewrite} instance * @param loopVariableName the name of the index variable in String representation * @return a completed {@link Assignment} containing the mentioned declaration and initialization */ private Assignment getForBodyAssignment(ASTRewrite rewrite, SimpleName loopVariableName) { AST ast = rewrite.getAST(); ITypeBinding loopOverType = extractElementType(ast); Assignment assignResolvedVariable = ast.newAssignment(); // left hand side SimpleName resolvedVariableName = resolveLinkedVariableNameWithProposals( rewrite, loopOverType.getName(), loopVariableName.getIdentifier(), false); VariableDeclarationFragment resolvedVariableDeclarationFragment = ast.newVariableDeclarationFragment(); resolvedVariableDeclarationFragment.setName(resolvedVariableName); VariableDeclarationExpression resolvedVariableDeclaration = ast.newVariableDeclarationExpression(resolvedVariableDeclarationFragment); resolvedVariableDeclaration.setType( getImportRewrite() .addImport( loopOverType, ast, new ContextSensitiveImportRewriteContext(fCurrentNode, getImportRewrite()))); assignResolvedVariable.setLeftHandSide(resolvedVariableDeclaration); // right hand side ArrayAccess access = ast.newArrayAccess(); access.setArray((Expression) rewrite.createCopyTarget(fCurrentExpression)); SimpleName indexName = ast.newSimpleName(loopVariableName.getIdentifier()); addLinkedPosition( rewrite.track(indexName), LinkedPositionGroup.NO_STOP, indexName.getIdentifier()); access.setIndex(indexName); assignResolvedVariable.setRightHandSide(access); assignResolvedVariable.setOperator(Assignment.Operator.ASSIGN); return assignResolvedVariable; }
/** * Creates an {@link Assignment} as first expression appearing in an index based <code>for</code> * loop's body. This Assignment declares a local variable and initializes it using the {@link * List}'s current element identified by the loop index. * * @param rewrite the current {@link ASTRewrite} instance * @param loopVariableName the name of the index variable in String representation * @return a completed {@link Assignment} containing the mentioned declaration and initialization */ private Expression getIndexBasedForBodyAssignment( ASTRewrite rewrite, SimpleName loopVariableName) { AST ast = rewrite.getAST(); ITypeBinding loopOverType = extractElementType(ast); Assignment assignResolvedVariable = ast.newAssignment(); // left hand side SimpleName resolvedVariableName = resolveLinkedVariableNameWithProposals( rewrite, loopOverType.getName(), loopVariableName.getIdentifier(), false); VariableDeclarationFragment resolvedVariableDeclarationFragment = ast.newVariableDeclarationFragment(); resolvedVariableDeclarationFragment.setName(resolvedVariableName); VariableDeclarationExpression resolvedVariableDeclaration = ast.newVariableDeclarationExpression(resolvedVariableDeclarationFragment); resolvedVariableDeclaration.setType( getImportRewrite() .addImport( loopOverType, ast, new ContextSensitiveImportRewriteContext(fCurrentNode, getImportRewrite()))); assignResolvedVariable.setLeftHandSide(resolvedVariableDeclaration); // right hand side MethodInvocation invokeGetExpression = ast.newMethodInvocation(); invokeGetExpression.setName(ast.newSimpleName("get")); // $NON-NLS-1$ SimpleName indexVariableName = ast.newSimpleName(loopVariableName.getIdentifier()); addLinkedPosition( rewrite.track(indexVariableName), LinkedPositionGroup.NO_STOP, indexVariableName.getIdentifier()); invokeGetExpression.arguments().add(indexVariableName); invokeGetExpression.setExpression((Expression) rewrite.createCopyTarget(fCurrentExpression)); assignResolvedVariable.setRightHandSide(invokeGetExpression); assignResolvedVariable.setOperator(Assignment.Operator.ASSIGN); return assignResolvedVariable; }
/** {@inheritDoc} */ @Override public void endVisit(Assignment assignment) { if ((assignment.getRightHandSide() instanceof MethodInvocation) || (assignment.getRightHandSide() instanceof ClassInstanceCreation)) { // treated in respective endVisit methods return; } VariableReference varRef = retrieveVariableReference(assignment.getLeftHandSide(), null); varRef.setOriginalCode(assignment.getLeftHandSide().toString()); VariableReference newAssignment = retrieveVariableReference(assignment.getRightHandSide(), null); newAssignment.setOriginalCode(assignment.getRightHandSide().toString()); if (varRef instanceof ArrayIndex) { AssignmentStatement assignmentStatement = new AssignmentStatement( testCase.getReference(), varRef, newAssignment); testCase.addStatement(assignmentStatement); return; } testCase.variableAssignment(varRef, newAssignment); }
private VariableReference retrieveResultReference( SuperMethodInvocation superMethodInvocation) { // TODO Duplicate code from retrieveResultReference(MethodInvocation) // too bad they don't have a common matching interface VariableReference result = calleeResultMap.get(superMethodInvocation.toString()); if (result != null) { return result; } ASTNode parent = superMethodInvocation.getParent(); if (parent instanceof VariableDeclarationFragment) { return retrieveVariableReference(parent, null); } if (parent instanceof Assignment) { Assignment assignment = (Assignment) parent; return retrieveVariableReference(assignment.getLeftHandSide(), null); } IMethodBinding methodBinding = superMethodInvocation.resolveMethodBinding(); result = retrieveVariableReference(methodBinding.getReturnType(), null); calleeResultMap.put(superMethodInvocation.toString(), result); return result; }
@Override public void createArrayInitStmt(final CaptureLog log, final int logRecNo) { final int oid = log.objectIds.get(logRecNo); final Object[] params = log.params.get(logRecNo); final String arrTypeName = log.oidClassNames.get(log.oidRecMapping.get(oid)); final Class<?> arrType = getClassForName(arrTypeName); // --- create array instance creation e.g. int[] var = new int[10]; final ArrayType arrAstType = (ArrayType) createAstArrayType(arrTypeName, ast); final ArrayCreation arrCreationExpr = ast.newArrayCreation(); arrCreationExpr.setType(arrAstType); arrCreationExpr.dimensions().add(ast.newNumberLiteral(String.valueOf(params.length))); final String arrVarName = this.createNewVarName(oid, arrTypeName); final VariableDeclarationFragment vd = ast.newVariableDeclarationFragment(); final SimpleName arrVarNameExpr = ast.newSimpleName(arrVarName); vd.setName(arrVarNameExpr); vd.setInitializer(arrCreationExpr); final VariableDeclarationStatement varDeclStmt = ast.newVariableDeclarationStatement(vd); varDeclStmt.setType(this.createAstType(arrTypeName, ast)); methodBlock.statements().add(varDeclStmt); // create array access statements var[0] = var1; Integer paramOID; Assignment assign; ArrayAccess arrAccessExpr; for(int i = 0; i < params.length; i++) { assign = ast.newAssignment(); arrAccessExpr = ast.newArrayAccess(); arrAccessExpr.setIndex(ast.newNumberLiteral(String.valueOf(i))); arrAccessExpr.setArray(arrVarNameExpr); assign.setLeftHandSide(arrAccessExpr); paramOID = (Integer) params[i]; if(paramOID == null) { assign.setRightHandSide(ast.newNullLiteral()); } else { assign.setRightHandSide(ast.newSimpleName(this.oidToVarMapping.get(paramOID))); } methodBlock.statements().add(assign); } }
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; }
@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? }
private static boolean isLeftHandSideOfAssignment(ASTNode node) { Assignment assignment= (Assignment)ASTNodes.getParent(node, ASTNode.ASSIGNMENT); if (assignment != null) { Expression leftHandSide= assignment.getLeftHandSide(); if (leftHandSide == node) { return true; } if (ASTNodes.isParent(node, leftHandSide)) { switch (leftHandSide.getNodeType()) { case ASTNode.SIMPLE_NAME: return true; case ASTNode.FIELD_ACCESS: return node == ((FieldAccess)leftHandSide).getName(); case ASTNode.QUALIFIED_NAME: return node == ((QualifiedName)leftHandSide).getName(); case ASTNode.SUPER_FIELD_ACCESS: return node == ((SuperFieldAccess)leftHandSide).getName(); default: return false; } } } return false; }
@Override public void endVisit(Assignment node) { if (skipNode(node)) return; FlowInfo lhs= getFlowInfo(node.getLeftHandSide()); FlowInfo rhs= getFlowInfo(node.getRightHandSide()); if (lhs instanceof LocalFlowInfo) { LocalFlowInfo llhs= (LocalFlowInfo)lhs; llhs.setWriteAccess(fFlowContext); if (node.getOperator() != Assignment.Operator.ASSIGN) { GenericSequentialFlowInfo tmp= createSequential(); tmp.merge(new LocalFlowInfo(llhs, FlowInfo.READ, fFlowContext), fFlowContext); tmp.merge(rhs, fFlowContext); rhs= tmp; } } GenericSequentialFlowInfo info= createSequential(node); // first process right and side and then left hand side. info.merge(rhs, fFlowContext); info.merge(lhs, fFlowContext); }
private static boolean isLeftValue(ASTNode node) { ASTNode parent= node.getParent(); if (parent instanceof Assignment) { Assignment assignment= (Assignment) parent; if (assignment.getLeftHandSide() == node) return true; } if (parent instanceof PostfixExpression) return true; if (parent instanceof PrefixExpression) { PrefixExpression.Operator op= ((PrefixExpression) parent).getOperator(); if (op.equals(PrefixExpression.Operator.DECREMENT)) return true; if (op.equals(PrefixExpression.Operator.INCREMENT)) return true; return false; } return false; }
private RefactoringStatus checkExpression() throws JavaModelException { Expression selectedExpression= getSelectedExpression().getAssociatedExpression(); if (selectedExpression != null) { final ASTNode parent= selectedExpression.getParent(); if (selectedExpression instanceof NullLiteral) { return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_null_literals); } else if (selectedExpression instanceof ArrayInitializer) { return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_array_initializer); } else if (selectedExpression instanceof Assignment) { if (parent instanceof Expression && !(parent instanceof ParenthesizedExpression)) return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_assignment); else return null; } else if (selectedExpression instanceof SimpleName) { if ((((SimpleName) selectedExpression)).isDeclaration()) return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_names_in_declarations); if (parent instanceof QualifiedName && selectedExpression.getLocationInParent() == QualifiedName.NAME_PROPERTY || parent instanceof FieldAccess && selectedExpression.getLocationInParent() == FieldAccess.NAME_PROPERTY) return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_select_expression); } else if (selectedExpression instanceof VariableDeclarationExpression && parent instanceof TryStatement) { return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_resource_in_try_with_resources); } } return null; }
public static IBinding resolveBinding(Expression expression){ if (expression instanceof Name) return ((Name)expression).resolveBinding(); if (expression instanceof ParenthesizedExpression) return resolveBinding(((ParenthesizedExpression)expression).getExpression()); else if (expression instanceof Assignment) return resolveBinding(((Assignment)expression).getLeftHandSide());//TODO ??? else if (expression instanceof MethodInvocation) return ((MethodInvocation)expression).resolveMethodBinding(); else if (expression instanceof SuperMethodInvocation) return ((SuperMethodInvocation)expression).resolveMethodBinding(); else if (expression instanceof FieldAccess) return ((FieldAccess)expression).resolveFieldBinding(); else if (expression instanceof SuperFieldAccess) return ((SuperFieldAccess)expression).resolveFieldBinding(); else if (expression instanceof ConditionalExpression) return resolveBinding(((ConditionalExpression)expression).getThenExpression()); return null; }
private Statement prepareAssignment(Expression rightHand) { // result = PRIME*result + (...) InfixExpression mul= fAst.newInfixExpression(); mul.setLeftOperand(fAst.newSimpleName(VARIABLE_NAME_PRIME)); mul.setRightOperand(fAst.newSimpleName(VARIABLE_NAME_RESULT)); mul.setOperator(Operator.TIMES); Assignment ass= fAst.newAssignment(); ass.setLeftHandSide(fAst.newSimpleName(VARIABLE_NAME_RESULT)); InfixExpression plus= fAst.newInfixExpression(); plus.setLeftOperand(mul); plus.setOperator(Operator.PLUS); plus.setRightOperand(rightHand); ass.setRightHandSide(plus); return fAst.newExpressionStatement(ass); }
private Statement makeSelfAssignment(ASTRewrite rewrite, ReturnValueResolutionVisitor rvrFinder) { AST rootNode = rewrite.getAST(); Assignment newAssignment = rootNode.newAssignment(); Expression leftExpression = rvrFinder.badMethodInvocation.getExpression(); while (leftExpression instanceof MethodInvocation) { leftExpression = ((MethodInvocation) leftExpression).getExpression(); } newAssignment.setLeftHandSide((Expression) rewrite.createCopyTarget(leftExpression)); newAssignment.setRightHandSide((Expression) rewrite.createCopyTarget( rvrFinder.badMethodInvocation)); return rootNode.newExpressionStatement(newAssignment); }
/** * Generates the Assignment in an iterator based for, used in the first statement of an iterator * based <code>for</code> loop body, to retrieve the next element of the {@link Iterable} * instance. * * @param rewrite the current instance of {@link ASTRewrite} * @param loopOverType the {@link ITypeBinding} of the loop variable * @param loopVariableName the name of the loop variable * @return an {@link Assignment}, which retrieves the next element of the {@link Iterable} using * the active {@link Iterator} */ private Assignment getIteratorBasedForBodyAssignment(ASTRewrite rewrite, ITypeBinding loopOverType, SimpleName loopVariableName) { AST ast= rewrite.getAST(); Assignment assignResolvedVariable= ast.newAssignment(); // left hand side SimpleName resolvedVariableName= resolveLinkedVariableNameWithProposals(rewrite, loopOverType.getName(), loopVariableName.getIdentifier(), false); VariableDeclarationFragment resolvedVariableDeclarationFragment= ast.newVariableDeclarationFragment(); resolvedVariableDeclarationFragment.setName(resolvedVariableName); VariableDeclarationExpression resolvedVariableDeclaration= ast.newVariableDeclarationExpression(resolvedVariableDeclarationFragment); resolvedVariableDeclaration.setType(getImportRewrite().addImport(loopOverType, ast, new ContextSensitiveImportRewriteContext(fCurrentNode, getImportRewrite()))); assignResolvedVariable.setLeftHandSide(resolvedVariableDeclaration); // right hand side MethodInvocation invokeIteratorNextExpression= ast.newMethodInvocation(); invokeIteratorNextExpression.setName(ast.newSimpleName("next")); //$NON-NLS-1$ SimpleName currentElementName= ast.newSimpleName(loopVariableName.getIdentifier()); addLinkedPosition(rewrite.track(currentElementName), LinkedPositionGroup.NO_STOP, currentElementName.getIdentifier()); invokeIteratorNextExpression.setExpression(currentElementName); assignResolvedVariable.setRightHandSide(invokeIteratorNextExpression); assignResolvedVariable.setOperator(Assignment.Operator.ASSIGN); return assignResolvedVariable; }
/** * Returns all statements within a method. * * @return Set of all statements */ private ArraySet<AbstractJavaStatement> assignments() { final List<AbstractJavaStatement> list = new ArrayList<AbstractJavaStatement>(); methodDeclaration.accept(new ASTVisitor() { @Override public boolean visit(Assignment node) { Expression lhs = node.getLeftHandSide(); ITypeBinding typeBinding = lhs.resolveTypeBinding(); Expression rhs = node.getRightHandSide(); if (rhs instanceof ClassInstanceCreation && node.getParent() instanceof ExpressionStatement) { ExpressionStatement parent = (ExpressionStatement) node.getParent(); JavaAssignment assignment = new JavaAssignment(JavaMethod.this, parent, lhs.toString(), typeBinding, (ClassInstanceCreation) rhs); list.add(assignment); } // TODO SE: (Static) method invocations, e.g. call of another // factory are not supported. return false; } }); return new ArraySet<AbstractJavaStatement>(list.toArray(new JavaAssignment[list.size()]), JavaAssignment.class); }
public boolean visit(final Assignment node) { final IVariableBinding field = this.getLocalField(node.getLeftHandSide()); if (field != null) { MethodAnalyzer.this.setFields.add(field); } this.assignedVariableIndex = this.getVariableIndex(node.getLeftHandSide()); if (this.assignedVariableIndex != -1) { MethodAnalyzer.this.variables.get(this.assignedVariableIndex).setModified(true); } this.assignedParameterIndex = this.getParameterIndex(node.getLeftHandSide()); if (this.assignedParameterIndex != -1) { MethodAnalyzer.this.parameters.get(this.assignedParameterIndex).setModified(true); } node.getLeftHandSide().accept((ASTVisitor)this); ++this.inAssignmentRightSide; node.getRightHandSide().accept((ASTVisitor)this); --this.inAssignmentRightSide; return false; }
private MethodDeclaration getWritingConstructor(SimpleName name) { Assignment assignement= (Assignment)ASTNodes.getParent(name, Assignment.class); if (assignement == null) return null; ASTNode expression= assignement.getParent(); if (!(expression instanceof ExpressionStatement)) return null; ASTNode block= expression.getParent(); if (!(block instanceof Block)) return null; ASTNode methodDeclaration= block.getParent(); if (!(methodDeclaration instanceof MethodDeclaration)) return null; return (MethodDeclaration)methodDeclaration; }
private void populateBodyWithFieldSetCalls(AST ast, TypeDeclaration builderType, Block body, List<ClassFieldSetterBuilderField> builderFields) { for (BuilderField field : builderFields) { Assignment assignment = ast.newAssignment(); FieldAccess fieldAccess = createThisFieldAccess(ast, field); assignment.setLeftHandSide(fieldAccess); FieldAccess builderFieldAccess = createBuilderFieldAccess(ast, builderType, field); assignment.setRightHandSide(builderFieldAccess); body.statements().add(ast.newExpressionStatement(assignment)); } }