Java Code Examples for org.eclipse.jdt.core.dom.InfixExpression#setOperator()

The following examples show how to use org.eclipse.jdt.core.dom.InfixExpression#setOperator() . 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: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static Expression getInversedAndOrExpression(ASTRewrite rewrite, InfixExpression infixExpression, Operator newOperator, SimpleNameRenameProvider provider) {
	InfixExpression newExpression= rewrite.getAST().newInfixExpression();
	newExpression.setOperator(newOperator);

	int newOperatorPrecedence= OperatorPrecedence.getOperatorPrecedence(newOperator);
	//
	Expression leftOperand= getInversedExpression(rewrite, infixExpression.getLeftOperand(), provider);
	newExpression.setLeftOperand(parenthesizeIfRequired(leftOperand, newOperatorPrecedence));

	Expression rightOperand= getInversedExpression(rewrite, infixExpression.getRightOperand(), provider);
	newExpression.setRightOperand(parenthesizeIfRequired(rightOperand, newOperatorPrecedence));

	List<Expression> extraOperands= infixExpression.extendedOperands();
	List<Expression> newExtraOperands= newExpression.extendedOperands();
	for (int i= 0; i < extraOperands.size(); i++) {
		Expression extraOperand= getInversedExpression(rewrite, extraOperands.get(i), provider);
		newExtraOperands.add(parenthesizeIfRequired(extraOperand, newOperatorPrecedence));
	}
	return newExpression;
}
 
Example 2
Source File: PolymorphismRefactoring.java    From JDeodorant with MIT License 6 votes vote down vote up
protected Expression constructExpression(AST ast, DefaultMutableTreeNode node) {
	Object object = node.getUserObject();
	if(object instanceof InfixExpression.Operator) {
		InfixExpression.Operator operator = (InfixExpression.Operator)object;
		InfixExpression infixExpression = ast.newInfixExpression();
		infixExpression.setOperator(operator);
		DefaultMutableTreeNode leftChild = (DefaultMutableTreeNode)node.getChildAt(0);
		DefaultMutableTreeNode rightChild = (DefaultMutableTreeNode)node.getChildAt(1);
		infixExpression.setLeftOperand(constructExpression(ast, leftChild));
		infixExpression.setRightOperand(constructExpression(ast, rightChild));
		return infixExpression;
	}
	else if(object instanceof Expression) {
		Expression expression = (Expression)object;
		return expression;
	}
	return null;
}
 
Example 3
Source File: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static Expression combineOperands(ASTRewrite rewrite, Expression existing, Expression originalNode, boolean removeParentheses, Operator operator) {
	if (existing == null && removeParentheses) {
		while (originalNode instanceof ParenthesizedExpression) {
			originalNode= ((ParenthesizedExpression)originalNode).getExpression();
		}
	}
	Expression newRight= (Expression)rewrite.createMoveTarget(originalNode);
	if (originalNode instanceof InfixExpression) {
		((InfixExpression)newRight).setOperator(((InfixExpression)originalNode).getOperator());
	}

	if (existing == null) {
		return newRight;
	}
	AST ast= rewrite.getAST();
	InfixExpression infix= ast.newInfixExpression();
	infix.setOperator(operator);
	infix.setLeftOperand(existing);
	infix.setRightOperand(newRight);
	return infix;
}
 
Example 4
Source File: InvertBooleanUtility.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static Expression getInversedAndOrExpression(ASTRewrite rewrite, InfixExpression infixExpression, Operator newOperator, SimpleNameRenameProvider provider) {
	InfixExpression newExpression = rewrite.getAST().newInfixExpression();
	newExpression.setOperator(newOperator);

	int newOperatorPrecedence = OperatorPrecedence.getOperatorPrecedence(newOperator);

	Expression leftOperand = getInversedExpression(rewrite, infixExpression.getLeftOperand(), provider);
	newExpression.setLeftOperand(parenthesizeIfRequired(leftOperand, newOperatorPrecedence));

	Expression rightOperand = getInversedExpression(rewrite, infixExpression.getRightOperand(), provider);
	newExpression.setRightOperand(parenthesizeIfRequired(rightOperand, newOperatorPrecedence));

	List<Expression> extraOperands = infixExpression.extendedOperands();
	List<Expression> newExtraOperands = newExpression.extendedOperands();
	for (int i = 0; i < extraOperands.size(); i++) {
		Expression extraOperand = getInversedExpression(rewrite, extraOperands.get(i), provider);
		newExtraOperands.add(parenthesizeIfRequired(extraOperand, newOperatorPrecedence));
	}
	return newExpression;
}
 
Example 5
Source File: GenerateHashCodeEqualsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Expression createShiftAssignment(Expression shift1, Expression shift2) {
	// (int)(element ^ (element >>> 32));
	// see implementation in Arrays.hashCode(), Double.hashCode() and
	// Long.hashCode()
	CastExpression ce= fAst.newCastExpression();
	ce.setType(fAst.newPrimitiveType(PrimitiveType.INT));

	InfixExpression unsignedShiftRight= fAst.newInfixExpression();
	unsignedShiftRight.setLeftOperand(shift1);
	unsignedShiftRight.setRightOperand(fAst.newNumberLiteral("32")); //$NON-NLS-1$
	unsignedShiftRight.setOperator(Operator.RIGHT_SHIFT_UNSIGNED);

	InfixExpression xor= fAst.newInfixExpression();
	xor.setLeftOperand(shift2);
	xor.setRightOperand(parenthesize(unsignedShiftRight));
	xor.setOperator(InfixExpression.Operator.XOR);

	ce.setExpression(parenthesize(xor));
	return ce;
}
 
Example 6
Source File: BaseTranslator.java    From junion with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Expression getLongAddress(MethodInvocation mi) {
	Expression indexExpr = getLongAddressBase(mi);
	
	NumberLiteral lit = (NumberLiteral)mi.arguments().get(2);
	if(offset != Integer.parseInt(lit.getToken()))
		throw new IllegalArgumentException("offset != " + lit);
	
	Expression args;
	if(offset == 0) args = indexExpr;
	else {
		InfixExpression add = ast.newInfixExpression();
		add.setOperator(InfixExpression.Operator.PLUS);
		add.setLeftOperand(indexExpr);
		add.setRightOperand(returnInt(offset));
		args = add;
	}
	return args;
}
 
Example 7
Source File: AccessAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private MethodInvocation createInvocation(AST ast, Expression operand, String operator) {
	Expression receiver = getReceiver(operand);
	MethodInvocation invocation = ast.newMethodInvocation();
	invocation.setName(ast.newSimpleName(fSetter));
	if (receiver != null) {
		invocation.setExpression((Expression) fRewriter.createCopyTarget(receiver));
	}
	InfixExpression argument = ast.newInfixExpression();
	invocation.arguments().add(argument);
	if ("++".equals(operator)) { //$NON-NLS-1$
		argument.setOperator(InfixExpression.Operator.PLUS);
	} else if ("--".equals(operator)) { //$NON-NLS-1$
		argument.setOperator(InfixExpression.Operator.MINUS);
	} else {
		Assert.isTrue(false, "Should not happen"); //$NON-NLS-1$
	}
	MethodInvocation getter = ast.newMethodInvocation();
	getter.setName(ast.newSimpleName(fGetter));
	if (receiver != null) {
		getter.setExpression((Expression) fRewriter.createCopyTarget(receiver));
	}
	argument.setLeftOperand(getter);
	argument.setRightOperand(ast.newNumberLiteral("1")); //$NON-NLS-1$

	fReferencingGetter = true;
	fReferencingSetter = true;

	return invocation;
}
 
Example 8
Source File: InvertBooleanUtility.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static Expression getInversedInfixExpression(ASTRewrite rewrite, InfixExpression expression, InfixExpression.Operator newOperator, SimpleNameRenameProvider provider) {
	InfixExpression newExpression = rewrite.getAST().newInfixExpression();
	newExpression.setOperator(newOperator);
	newExpression.setLeftOperand(getRenamedNameCopy(provider, rewrite, expression.getLeftOperand()));
	newExpression.setRightOperand(getRenamedNameCopy(provider, rewrite, expression.getRightOperand()));
	return newExpression;
}
 
Example 9
Source File: GenerateForLoopAssistProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates an {@link InfixExpression} which is linked to the group of the variableToIncrement.
 * 
 * @param rewrite the current {@link ASTRewrite} instance
 * @param variableToIncrement the name of the variable to generate the {@link InfixExpression}
 *            for
 * @param rightHandSide the right hand side expression which shall be included in the
 *            {@link InfixExpression}
 * @param operator the {@link org.eclipse.jdt.core.dom.InfixExpression.Operator} to use in the
 *            {@link InfixExpression} to create
 * @return a filled, new {@link InfixExpression} instance
 */
private InfixExpression getLinkedInfixExpression(ASTRewrite rewrite, String variableToIncrement, Expression rightHandSide, InfixExpression.Operator operator) {
	AST ast= rewrite.getAST();
	InfixExpression loopExpression= ast.newInfixExpression();
	SimpleName name= ast.newSimpleName(variableToIncrement);
	addLinkedPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP, name.getIdentifier());
	loopExpression.setLeftOperand(name);

	loopExpression.setOperator(operator);

	loopExpression.setRightOperand(rightHandSide);
	return loopExpression;
}
 
Example 10
Source File: GenerateHashCodeEqualsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private Statement createReturningIfStatement(Expression left, Expression right, Operator operator, boolean whatToReturn) {
	InfixExpression newCondition= fAst.newInfixExpression();
	newCondition.setOperator(operator);
	newCondition.setLeftOperand(left);
	newCondition.setRightOperand(right);
	return createReturningIfStatement(whatToReturn, newCondition);
}
 
Example 11
Source File: LocalCorrectionsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static ThrowStatement getThrowForUnexpectedDefault(Expression switchExpression, AST ast, ASTRewrite astRewrite) {
	ThrowStatement newThrowStatement = ast.newThrowStatement();
	ClassInstanceCreation newCic = ast.newClassInstanceCreation();
	newCic.setType(ast.newSimpleType(ast.newSimpleName("IllegalArgumentException"))); //$NON-NLS-1$
	InfixExpression newInfixExpr = ast.newInfixExpression();
	StringLiteral newStringLiteral = ast.newStringLiteral();
	newStringLiteral.setLiteralValue("Unexpected value: "); //$NON-NLS-1$
	newInfixExpr.setLeftOperand(newStringLiteral);
	newInfixExpr.setOperator(InfixExpression.Operator.PLUS);
	newInfixExpr.setRightOperand((Expression) astRewrite.createCopyTarget(switchExpression));
	newCic.arguments().add(newInfixExpr);
	newThrowStatement.setExpression(newCic);
	return newThrowStatement;
}
 
Example 12
Source File: LocalCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static void addUnnecessaryInstanceofProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());

	ASTNode curr= selectedNode;
	while (curr instanceof ParenthesizedExpression) {
		curr= ((ParenthesizedExpression) curr).getExpression();
	}

	if (curr instanceof InstanceofExpression) {
		AST ast= curr.getAST();

		ASTRewrite rewrite= ASTRewrite.create(ast);

		InstanceofExpression inst= (InstanceofExpression) curr;

		InfixExpression expression= ast.newInfixExpression();
		expression.setLeftOperand((Expression) rewrite.createCopyTarget(inst.getLeftOperand()));
		expression.setOperator(InfixExpression.Operator.NOT_EQUALS);
		expression.setRightOperand(ast.newNullLiteral());

		rewrite.replace(inst, expression, null);

		String label= CorrectionMessages.LocalCorrectionsSubProcessor_unnecessaryinstanceof_description;
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
		ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.UNNECESSARY_INSTANCEOF, image);
		proposals.add(proposal);
	}

}
 
Example 13
Source File: AccessAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private MethodInvocation createInvocation(AST ast, Expression operand, String operator) {
	Expression receiver= getReceiver(operand);
	MethodInvocation invocation= ast.newMethodInvocation();
	invocation.setName(ast.newSimpleName(fSetter));
	if (receiver != null)
		invocation.setExpression((Expression)fRewriter.createCopyTarget(receiver));
	InfixExpression argument= ast.newInfixExpression();
	invocation.arguments().add(argument);
	if ("++".equals(operator)) { //$NON-NLS-1$
		argument.setOperator(InfixExpression.Operator.PLUS);
	} else if ("--".equals(operator)) { //$NON-NLS-1$
		argument.setOperator(InfixExpression.Operator.MINUS);
	} else {
		Assert.isTrue(false, "Should not happen"); //$NON-NLS-1$
	}
	MethodInvocation getter= ast.newMethodInvocation();
	getter.setName(ast.newSimpleName(fGetter));
	if (receiver != null)
		getter.setExpression((Expression)fRewriter.createCopyTarget(receiver));
	argument.setLeftOperand(getter);
	argument.setRightOperand(ast.newNumberLiteral("1")); //$NON-NLS-1$

	fReferencingGetter= true;
	fReferencingSetter= true;

	return invocation;
}
 
Example 14
Source File: ASTNodeFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static Expression newInfixExpression(AST ast, Operator operator, ArrayList<Expression> operands) {
	if (operands.size() == 1)
		return operands.get(0);

	InfixExpression result= ast.newInfixExpression();
	result.setOperator(operator);
	result.setLeftOperand(operands.get(0));
	result.setRightOperand(operands.get(1));
	result.extendedOperands().addAll(operands.subList(2, operands.size()));
	return result;
}
 
Example 15
Source File: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static Expression getInversedInfixExpression(ASTRewrite rewrite, InfixExpression expression, InfixExpression.Operator newOperator, SimpleNameRenameProvider provider) {
	InfixExpression newExpression= rewrite.getAST().newInfixExpression();
	newExpression.setOperator(newOperator);
	newExpression.setLeftOperand(getRenamedNameCopy(provider, rewrite, expression.getLeftOperand()));
	newExpression.setRightOperand(getRenamedNameCopy(provider, rewrite, expression.getRightOperand()));
	return newExpression;
}
 
Example 16
Source File: AccessAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@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 17
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public boolean visit(final PostfixExpression node) {
  final AST dummyAST = AST.newAST(node.getAST().apiLevel());
  final PostfixExpression.Operator pfOperator = node.getOperator();
  Expression _operand = node.getOperand();
  if ((_operand instanceof ArrayAccess)) {
    Expression _operand_1 = node.getOperand();
    final ArrayAccess pfOperand = ((ArrayAccess) _operand_1);
    if ((Objects.equal(pfOperator, PostfixExpression.Operator.INCREMENT) || 
      Objects.equal(pfOperator, PostfixExpression.Operator.DECREMENT))) {
      final String arrayName = this.computeArrayName(pfOperand);
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("_postIndx_");
      _builder.append(arrayName);
      final String idxName = _builder.toString();
      StringConcatenation _builder_1 = new StringConcatenation();
      _builder_1.append("_postVal_");
      _builder_1.append(arrayName);
      final String tempVarName = _builder_1.toString();
      StringConcatenation _builder_2 = new StringConcatenation();
      _builder_2.append("{ var ");
      _builder_2.append(idxName);
      _builder_2.append("=");
      this.appendToBuffer(_builder_2.toString());
      pfOperand.getIndex().accept(this);
      StringConcatenation _builder_3 = new StringConcatenation();
      _builder_3.append(" ");
      _builder_3.append("var  ");
      this.appendToBuffer(_builder_3.toString());
      final VariableDeclarationFragment varDeclaration = dummyAST.newVariableDeclarationFragment();
      varDeclaration.setName(dummyAST.newSimpleName(tempVarName));
      ASTNode _copySubtree = ASTNode.copySubtree(dummyAST, pfOperand);
      final ArrayAccess arrayAccess = ((ArrayAccess) _copySubtree);
      arrayAccess.setIndex(dummyAST.newSimpleName(idxName));
      varDeclaration.setInitializer(arrayAccess);
      varDeclaration.accept(this);
      final InfixExpression infixOp = dummyAST.newInfixExpression();
      infixOp.setLeftOperand(dummyAST.newSimpleName(tempVarName));
      PostfixExpression.Operator _operator = node.getOperator();
      boolean _equals = Objects.equal(_operator, PostfixExpression.Operator.DECREMENT);
      if (_equals) {
        infixOp.setOperator(InfixExpression.Operator.MINUS);
      } else {
        infixOp.setOperator(InfixExpression.Operator.PLUS);
      }
      infixOp.setRightOperand(dummyAST.newNumberLiteral("1"));
      final Assignment assigment = dummyAST.newAssignment();
      ASTNode _copySubtree_1 = ASTNode.copySubtree(dummyAST, pfOperand);
      final ArrayAccess writeArray = ((ArrayAccess) _copySubtree_1);
      writeArray.setIndex(dummyAST.newSimpleName(idxName));
      assigment.setLeftHandSide(writeArray);
      ASTNode _copySubtree_2 = ASTNode.copySubtree(dummyAST, infixOp);
      assigment.setRightHandSide(((Expression) _copySubtree_2));
      assigment.accept(this);
      StringConcatenation _builder_4 = new StringConcatenation();
      String _xifexpression = null;
      boolean _needsReturnValue = this._aSTFlattenerUtils.needsReturnValue(node);
      if (_needsReturnValue) {
        _xifexpression = tempVarName;
      }
      _builder_4.append(_xifexpression);
      _builder_4.append(" }");
      this.appendToBuffer(_builder_4.toString());
      return false;
    }
  }
  node.getOperand().accept(this);
  this.appendToBuffer(pfOperator.toString());
  return false;
}
 
Example 18
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public boolean visit(final PrefixExpression node) {
  final Expression operand = node.getOperand();
  PrefixExpression.Operator _operator = node.getOperator();
  boolean _matched = false;
  if (Objects.equal(_operator, PrefixExpression.Operator.DECREMENT)) {
    _matched=true;
  }
  if (!_matched) {
    if (Objects.equal(_operator, PrefixExpression.Operator.INCREMENT)) {
      _matched=true;
    }
  }
  if (_matched) {
    if ((operand instanceof ArrayAccess)) {
      final String arrayName = this.computeArrayName(((ArrayAccess)operand));
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("_tPreInx_");
      _builder.append(arrayName);
      final String idxName = _builder.toString();
      String op = "-";
      PrefixExpression.Operator _operator_1 = node.getOperator();
      boolean _equals = Objects.equal(_operator_1, PrefixExpression.Operator.INCREMENT);
      if (_equals) {
        op = "+";
      }
      StringConcatenation _builder_1 = new StringConcatenation();
      _builder_1.append("{val ");
      _builder_1.append(idxName);
      _builder_1.append("=");
      this.appendToBuffer(_builder_1.toString());
      ((ArrayAccess)operand).getIndex().accept(this);
      StringConcatenation _builder_2 = new StringConcatenation();
      _builder_2.append(" ");
      _builder_2.append("val ");
      _builder_2.append(idxName, " ");
      _builder_2.append("_res=");
      _builder_2.append(arrayName, " ");
      _builder_2.append(".get(");
      _builder_2.append(idxName, " ");
      _builder_2.append(")");
      _builder_2.append(op, " ");
      _builder_2.append("1");
      this.appendToBuffer(_builder_2.toString());
      StringConcatenation _builder_3 = new StringConcatenation();
      _builder_3.append(" ");
      _builder_3.append(arrayName, " ");
      _builder_3.append(".set(");
      _builder_3.append(idxName, " ");
      _builder_3.append(", ");
      _builder_3.append(idxName, " ");
      _builder_3.append("_res)  ");
      _builder_3.append(idxName, " ");
      _builder_3.append("_res}");
      this.appendToBuffer(_builder_3.toString());
      return false;
    } else {
      final AST dummyAST = AST.newAST(node.getAST().apiLevel());
      final Assignment assigment = dummyAST.newAssignment();
      final InfixExpression infixOp = dummyAST.newInfixExpression();
      ASTNode _copySubtree = ASTNode.copySubtree(dummyAST, operand);
      infixOp.setLeftOperand(((Expression) _copySubtree));
      PrefixExpression.Operator _operator_2 = node.getOperator();
      boolean _equals_1 = Objects.equal(_operator_2, PrefixExpression.Operator.DECREMENT);
      if (_equals_1) {
        infixOp.setOperator(InfixExpression.Operator.MINUS);
      } else {
        infixOp.setOperator(InfixExpression.Operator.PLUS);
      }
      infixOp.setRightOperand(dummyAST.newNumberLiteral("1"));
      ASTNode _copySubtree_1 = ASTNode.copySubtree(dummyAST, operand);
      final Expression leftSide = ((Expression) _copySubtree_1);
      assigment.setLeftHandSide(leftSide);
      assigment.setRightHandSide(infixOp);
      this.appendToBuffer("{");
      Type type = null;
      if ((operand instanceof SimpleName)) {
        type = this._aSTFlattenerUtils.findDeclaredType(((SimpleName)operand));
      }
      this.handleAssignment(assigment, leftSide, type);
      this.appendToBuffer("}");
      return false;
    }
  }
  if (!_matched) {
    if (Objects.equal(_operator, PrefixExpression.Operator.COMPLEMENT)) {
      _matched=true;
      node.getOperand().accept(this);
      this.appendToBuffer(".bitwiseNot");
    }
  }
  if (!_matched) {
    {
      this.appendToBuffer(node.getOperator().toString());
      node.getOperand().accept(this);
    }
  }
  return false;
}
 
Example 19
Source File: AccessAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@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 20
Source File: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean getPickOutStringProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
	// we work with String's
	if (!(node instanceof StringLiteral)) {
		return false;
	}
	// user should select part of String
	int selectionPos= context.getSelectionOffset();
	int selectionLen= context.getSelectionLength();
	if (selectionLen == 0) {
		return false;
	}
	int valueStart= node.getStartPosition() + 1;
	int valueEnd= node.getStartPosition() + node.getLength() - 1;

	// selection must be inside node and the quotes and not contain the full value
	if (selectionPos < valueStart || selectionPos + selectionLen > valueEnd || valueEnd - valueStart == selectionLen) {
		return false;
	}

	// prepare string parts positions
	StringLiteral stringLiteral= (StringLiteral) node;
	String stringValue= stringLiteral.getEscapedValue();

	int firstPos= selectionPos - node.getStartPosition();
	int secondPos= firstPos + selectionLen;


	// prepare new string literals

	AST ast= node.getAST();
	StringLiteral leftLiteral= ast.newStringLiteral();
	StringLiteral centerLiteral= ast.newStringLiteral();
	StringLiteral rightLiteral= ast.newStringLiteral();
	try {
		leftLiteral.setEscapedValue('"' + stringValue.substring(1, firstPos) + '"');
		centerLiteral.setEscapedValue('"' + stringValue.substring(firstPos, secondPos) + '"');
		rightLiteral.setEscapedValue('"' + stringValue.substring(secondPos, stringValue.length() - 1) + '"');
	} catch (IllegalArgumentException e) {
		return false;
	}
	if (resultingCollections == null) {
		return true;
	}

	ASTRewrite rewrite= ASTRewrite.create(ast);

	// prepare new expression instead of StringLiteral
	InfixExpression expression= ast.newInfixExpression();
	expression.setOperator(InfixExpression.Operator.PLUS);
	if (firstPos != 1) {
		expression.setLeftOperand(leftLiteral);
	}


	if (firstPos == 1) {
		expression.setLeftOperand(centerLiteral);
	} else {
		expression.setRightOperand(centerLiteral);
	}

	if (secondPos < stringValue.length() - 1) {
		if (firstPos == 1) {
			expression.setRightOperand(rightLiteral);
		} else {
			expression.extendedOperands().add(rightLiteral);
		}
	}
	// use new expression instead of old StirngLiteral
	rewrite.replace(stringLiteral, expression, null);
	// add correction proposal
	String label= CorrectionMessages.AdvancedQuickAssistProcessor_pickSelectedString;
	Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
	LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.PICK_SELECTED_STRING, image);
	proposal.addLinkedPosition(rewrite.track(centerLiteral), true, "CENTER_STRING"); //$NON-NLS-1$
	resultingCollections.add(proposal);
	return true;
}