org.eclipse.jdt.core.dom.Assignment Java Examples

The following examples show how to use org.eclipse.jdt.core.dom.Assignment. 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 vote down vote up
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: ExtractClassRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
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 #3
Source File: MethodObject.java    From JDeodorant with MIT License 6 votes vote down vote up
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 #4
Source File: AbstractLoopUtilities.java    From JDeodorant with MIT License 6 votes vote down vote up
public static boolean isUpdatingVariable(Expression updater, SimpleName variable)
{
	if (updater instanceof PrefixExpression)
	{
		PrefixExpression prefixExpression = (PrefixExpression) updater;
		return isSameVariable(prefixExpression.getOperand(), variable);
	}
	else if (updater instanceof PostfixExpression)
	{
		PostfixExpression postfixExpression = (PostfixExpression) updater;
		return isSameVariable(postfixExpression.getOperand(), variable);			
	}
	else if (updater instanceof Assignment)
	{
		Assignment assignment = (Assignment) updater;
		return isSameVariable(assignment.getLeftHandSide(), variable);
	}
	else if (updater instanceof MethodInvocation)
	{
		MethodInvocation methodInvocation = (MethodInvocation) updater;
		return isSameVariable(methodInvocation.getExpression(), variable);
	}
	return false;
}
 
Example #5
Source File: GenerateHashCodeEqualsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
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);
}
 
Example #6
Source File: StagedBuilderWithMethodAdderFragment.java    From SparkBuilderGenerator with MIT License 6 votes vote down vote up
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;
}
 
Example #7
Source File: RegularBuilderWithMethodAdderFragment.java    From SparkBuilderGenerator with MIT License 6 votes vote down vote up
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;
}
 
Example #8
Source File: PreconditionExaminer.java    From JDeodorant with MIT License 6 votes vote down vote up
private boolean isUpdated(Expression expr) {
	if(expr.getParent() instanceof Assignment) {
		Assignment assignment = (Assignment)expr.getParent();
		if(assignment.getLeftHandSide().equals(expr)) {
			return true;
		}
	}
	else if(expr.getParent() instanceof PostfixExpression) {
		return true;
	}
	else if(expr.getParent() instanceof PrefixExpression) {
		PrefixExpression prefix = (PrefixExpression)expr.getParent();
		if(prefix.getOperator().equals(PrefixExpression.Operator.INCREMENT) ||
				prefix.getOperator().equals(PrefixExpression.Operator.DECREMENT)) {
			return true;
		}
	}
	return false;
}
 
Example #9
Source File: SnippetFinder.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
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;
}
 
Example #10
Source File: FlowAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@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);
}
 
Example #11
Source File: ExtractTempRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private IExpressionFragment getSelectedExpression() throws JavaModelException {
	if (fSelectedExpression != null) {
		return fSelectedExpression;
	}
	IASTFragment selectedFragment = ASTFragmentFactory.createFragmentForSourceRange(new SourceRange(fSelectionStart, fSelectionLength), fCompilationUnitNode, fCu);

	if (selectedFragment instanceof IExpressionFragment && !Checks.isInsideJavadoc(selectedFragment.getAssociatedNode())) {
		fSelectedExpression = (IExpressionFragment) selectedFragment;
	} else if (selectedFragment != null) {
		if (selectedFragment.getAssociatedNode() instanceof ExpressionStatement) {
			ExpressionStatement exprStatement = (ExpressionStatement) selectedFragment.getAssociatedNode();
			Expression expression = exprStatement.getExpression();
			fSelectedExpression = (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(expression);
		} else if (selectedFragment.getAssociatedNode() instanceof Assignment) {
			Assignment assignment = (Assignment) selectedFragment.getAssociatedNode();
			fSelectedExpression = (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(assignment);
		}
	}

	if (fSelectedExpression != null && Checks.isEnumCase(fSelectedExpression.getAssociatedExpression().getParent())) {
		fSelectedExpression = null;
	}

	return fSelectedExpression;
}
 
Example #12
Source File: GenerateForLoopAssistProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #13
Source File: ExpressionVariable.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
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;
}
 
Example #14
Source File: ExtractFieldRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
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;
}
 
Example #15
Source File: ExtractTempRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private IExpressionFragment getSelectedExpression() throws JavaModelException {
	if (fSelectedExpression != null)
		return fSelectedExpression;
	IASTFragment selectedFragment= ASTFragmentFactory.createFragmentForSourceRange(new SourceRange(fSelectionStart, fSelectionLength), fCompilationUnitNode, fCu);

	if (selectedFragment instanceof IExpressionFragment && !Checks.isInsideJavadoc(selectedFragment.getAssociatedNode())) {
		fSelectedExpression= (IExpressionFragment) selectedFragment;
	} else if (selectedFragment != null) {
		if (selectedFragment.getAssociatedNode() instanceof ExpressionStatement) {
			ExpressionStatement exprStatement= (ExpressionStatement) selectedFragment.getAssociatedNode();
			Expression expression= exprStatement.getExpression();
			fSelectedExpression= (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(expression);
		} else if (selectedFragment.getAssociatedNode() instanceof Assignment) {
			Assignment assignment= (Assignment) selectedFragment.getAssociatedNode();
			fSelectedExpression= (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(assignment);
		}
	}

	if (fSelectedExpression != null && Checks.isEnumCase(fSelectedExpression.getAssociatedExpression().getParent())) {
		fSelectedExpression= null;
	}

	return fSelectedExpression;
}
 
Example #16
Source File: ExtractTempRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
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;
}
 
Example #17
Source File: AbstractLoopUtilities.java    From JDeodorant with MIT License 6 votes vote down vote up
public static Integer getUpdateValue(Expression updater)
{
	if (updater instanceof PrefixExpression || updater instanceof PostfixExpression)
	{
		return AbstractLoopUtilities.getIncrementValue(updater);
	}
	else if (updater instanceof Assignment)
	{
		Assignment assignment = (Assignment) updater;
		return assignmentUpdateValue(assignment);
	}
	else if (updater instanceof MethodInvocation)
	{
		MethodInvocation methodInvocation = (MethodInvocation) updater;
		AbstractLoopBindingInformation bindingInformation = AbstractLoopBindingInformation.getInstance();
		return bindingInformation.getUpdateMethodValue(methodInvocation.resolveMethodBinding().getMethodDeclaration().getKey());
	}
	return null;
}
 
Example #18
Source File: SnippetFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
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;
}
 
Example #19
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean visit(final Assignment node) {
  final Expression leftSide = node.getLeftHandSide();
  Type type = null;
  if ((leftSide instanceof ArrayAccess)) {
    Expression _array = ((ArrayAccess)leftSide).getArray();
    if ((_array instanceof SimpleName)) {
      Expression _array_1 = ((ArrayAccess)leftSide).getArray();
      type = this._aSTFlattenerUtils.findDeclaredType(((SimpleName) _array_1));
    }
    this.handleAssignment(node, ((ArrayAccess)leftSide), type);
  } else {
    if ((leftSide instanceof SimpleName)) {
      type = this._aSTFlattenerUtils.findDeclaredType(((SimpleName)leftSide));
    }
    this.handleAssignment(node, leftSide, type);
  }
  return false;
}
 
Example #20
Source File: InferTypeArgumentsConstraintCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@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 #21
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
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 #22
Source File: ASTNodeDifference.java    From JDeodorant with MIT License 5 votes vote down vote up
public boolean isLeftHandSideOfAssignment() {
	Expression exp1 = expression1.getExpression();
	Expression exp2 = expression2.getExpression();
	ASTNode node1 = exp1.getParent();
	ASTNode node2 = exp2.getParent();
	if(node1 instanceof Assignment && node2 instanceof Assignment) {
		Assignment assignment1 = (Assignment)node1;
		Assignment assignment2 = (Assignment)node2;
		if(assignment1.getLeftHandSide().equals(exp1) && assignment2.getLeftHandSide().equals(exp2)) {
			return true;
		}
	}
	return false;
}
 
Example #23
Source File: OperatorPrecedence.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the precedence of the expression. Expression
 * with higher precedence are executed before expressions
 * with lower precedence.
 * i.e. in:
 * <br><code> int a= ++3--;</code></br>
 *
 * the  precedence order is
 * <ul>
 * <li>3</li>
 * <li>++</li>
 * <li>--</li>
 * <li>=</li>
 * </ul>
 * 1. 3 -(++)-> 4<br>
 * 2. 4 -(--)-> 3<br>
 * 3. 3 -(=)-> a<br>
 *
 * @param expression the expression to determine the precedence for
 * @return the precedence the higher to stronger the binding to its operand(s)
 */
public static int getExpressionPrecedence(Expression expression) {
	if (expression instanceof InfixExpression) {
		return getOperatorPrecedence(((InfixExpression)expression).getOperator());
	} else if (expression instanceof Assignment) {
		return ASSIGNMENT;
	} else if (expression instanceof ConditionalExpression) {
		return CONDITIONAL;
	} else if (expression instanceof InstanceofExpression) {
		return RELATIONAL;
	} else if (expression instanceof CastExpression) {
		return TYPEGENERATION;
	} else if (expression instanceof ClassInstanceCreation) {
		return POSTFIX;
	} else if (expression instanceof PrefixExpression) {
		return PREFIX;
	} else if (expression instanceof FieldAccess) {
		return POSTFIX;
	} else if (expression instanceof MethodInvocation) {
		return POSTFIX;
	} else if (expression instanceof ArrayAccess) {
		return POSTFIX;
	} else if (expression instanceof PostfixExpression) {
		return POSTFIX;
	}
	return Integer.MAX_VALUE;
}
 
Example #24
Source File: CodeStyleFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void handleVariable(SimpleName node, IVariableBinding varbinding) {
	if (!varbinding.isField())
		return;

	if (varbinding.isEnumConstant())
		return;

	ITypeBinding declaringClass= varbinding.getDeclaringClass();
	if (Modifier.isStatic(varbinding.getModifiers())) {
		if (fFindUnqualifiedStaticAccesses) {
			Initializer initializer= (Initializer) ASTNodes.getParent(node, Initializer.class);
			//Do not qualify assignments to static final fields in static initializers (would result in compile error)
			StructuralPropertyDescriptor parentDescription= node.getLocationInParent();
			if (initializer != null && Modifier.isStatic(initializer.getModifiers())
					&& Modifier.isFinal(varbinding.getModifiers()) && parentDescription == Assignment.LEFT_HAND_SIDE_PROPERTY)
				return;

			//Do not qualify static fields if defined inside an anonymous class
			if (declaringClass.isAnonymous())
				return;

			fResult.add(new AddStaticQualifierOperation(declaringClass, node));
		}
	} else if (fFindUnqualifiedAccesses){
		String qualifier= getThisExpressionQualifier(declaringClass, fImportRewrite, node);
		if (qualifier == null)
			return;

		if (qualifier.length() == 0)
			qualifier= null;

		fResult.add(new AddThisQualifierOperation(qualifier, node));
	}
}
 
Example #25
Source File: SuperTypeConstraintsCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public final void endVisit(final Assignment node) {
	final ConstraintVariable2 ancestor= (ConstraintVariable2) node.getLeftHandSide().getProperty(PROPERTY_CONSTRAINT_VARIABLE);
	final ConstraintVariable2 descendant= (ConstraintVariable2) node.getRightHandSide().getProperty(PROPERTY_CONSTRAINT_VARIABLE);
	node.setProperty(PROPERTY_CONSTRAINT_VARIABLE, ancestor);
	if (ancestor != null && descendant != null)
		fModel.createSubtypeConstraint(descendant, ancestor);
}
 
Example #26
Source File: MoveInstanceMethodProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public final boolean visit(final Assignment node) {
	Assert.isNotNull(node);
	final IVariableBinding binding= getFieldBinding(node.getLeftHandSide());
	if (binding != null)
		fWritten.add(binding.getKey());
	return true;
}
 
Example #27
Source File: OccurrencesFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(Assignment node) {
	SimpleName name= getSimpleName(node.getLeftHandSide());
	if (name != null)
		addWrite(name, name.resolveBinding());
	return true;
}
 
Example #28
Source File: MoveInnerToTopRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void createConstructor(final AbstractTypeDeclaration declaration, final ASTRewrite rewrite) throws CoreException {
	Assert.isNotNull(declaration);
	Assert.isNotNull(rewrite);
	final AST ast= declaration.getAST();
	final MethodDeclaration constructor= ast.newMethodDeclaration();
	constructor.setConstructor(true);
	constructor.setName(ast.newSimpleName(declaration.getName().getIdentifier()));
	final String comment= CodeGeneration.getMethodComment(fType.getCompilationUnit(), fType.getElementName(), fType.getElementName(), getNewConstructorParameterNames(), new String[0], null, null, StubUtility.getLineDelimiterUsed(fType.getJavaProject()));
	if (comment != null && comment.length() > 0) {
		final Javadoc doc= (Javadoc) rewrite.createStringPlaceholder(comment, ASTNode.JAVADOC);
		constructor.setJavadoc(doc);
	}
	if (fCreateInstanceField) {
		final SingleVariableDeclaration variable= ast.newSingleVariableDeclaration();
		final String name= getNameForEnclosingInstanceConstructorParameter();
		variable.setName(ast.newSimpleName(name));
		variable.setType(createEnclosingType(ast));
		constructor.parameters().add(variable);
		final Block body= ast.newBlock();
		final Assignment assignment= ast.newAssignment();
		if (fCodeGenerationSettings.useKeywordThis || fEnclosingInstanceFieldName.equals(fNameForEnclosingInstanceConstructorParameter)) {
			final FieldAccess access= ast.newFieldAccess();
			access.setExpression(ast.newThisExpression());
			access.setName(ast.newSimpleName(fEnclosingInstanceFieldName));
			assignment.setLeftHandSide(access);
		} else
			assignment.setLeftHandSide(ast.newSimpleName(fEnclosingInstanceFieldName));
		assignment.setRightHandSide(ast.newSimpleName(name));
		final Statement statement= ast.newExpressionStatement(assignment);
		body.statements().add(statement);
		constructor.setBody(body);
	} else
		constructor.setBody(ast.newBlock());
	rewrite.getListRewrite(declaration, declaration.getBodyDeclarationsProperty()).insertFirst(constructor, null);
}
 
Example #29
Source File: AstVisitor.java    From jdt2famix with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(Assignment node) {
	Access writeAccess = importer.createAccessFromExpression((Expression) node.getLeftHandSide());
	writeAccess.setIsWrite(true);
	importer.createAccessFromExpression((Expression) node.getRightHandSide());
	return true;
}
 
Example #30
Source File: StyledStringVisitor.java    From JDeodorant with MIT License 5 votes vote down vote up
public boolean visit(Assignment expr) {
	/*
	 * Assignment: Expression AssignmentOperator Expression
	 */
	activateDiffStyle(expr);
	handleExpression((Expression) expr.getLeftHandSide());
	appendSpace();
	styledString.append(expr.getOperator().toString(), determineDiffStyle(expr, new StyledStringStyler(ordinaryStyle)));
	appendSpace();
	handleExpression((Expression) expr.getRightHandSide());
	deactivateDiffStyle(expr);
	return false;
}