Java Code Examples for org.eclipse.jdt.core.dom.AST#newInfixExpression()

The following examples show how to use org.eclipse.jdt.core.dom.AST#newInfixExpression() . 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: 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 2
Source File: InvertBooleanUtility.java    From eclipse.jdt.ls with Eclipse Public License 2.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 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: CreateAndOddnessCheckResolution.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Creates the new <CODE>InfixExpression</CODE> <CODE>(x &amp; 1) == 1</CODE>.
 */
@Override
protected InfixExpression createCorrectOddnessCheck(ASTRewrite rewrite, Expression numberExpression) {
    Assert.isNotNull(rewrite);
    Assert.isNotNull(numberExpression);

    final AST ast = rewrite.getAST();
    InfixExpression andOddnessCheck = ast.newInfixExpression();
    ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression();
    InfixExpression andExpression = ast.newInfixExpression();

    andExpression.setLeftOperand((Expression) rewrite.createMoveTarget(numberExpression));
    andExpression.setOperator(AND);
    andExpression.setRightOperand(ast.newNumberLiteral("1"));
    parenthesizedExpression.setExpression(andExpression);
    andOddnessCheck.setLeftOperand(parenthesizedExpression);
    andOddnessCheck.setOperator(EQUALS);
    andOddnessCheck.setRightOperand(ast.newNumberLiteral("1"));

    return andOddnessCheck;

}
 
Example 5
Source File: CreateRemainderOddnessCheckResolution.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Creates the new <CODE>InfixExpression</CODE> <CODE>x % 2 != 0</CODE>
 */
@Override
protected InfixExpression createCorrectOddnessCheck(ASTRewrite rewrite, Expression numberExpression) {
    Assert.isNotNull(rewrite);
    Assert.isNotNull(numberExpression);

    final AST ast = rewrite.getAST();
    InfixExpression correctOddnessCheck = ast.newInfixExpression();
    InfixExpression remainderExp = ast.newInfixExpression();

    correctOddnessCheck.setLeftOperand(remainderExp);
    correctOddnessCheck.setOperator(NOT_EQUALS);
    correctOddnessCheck.setRightOperand(ast.newNumberLiteral("0"));

    remainderExp.setLeftOperand((Expression) rewrite.createMoveTarget(numberExpression));
    remainderExp.setOperator(REMAINDER);
    remainderExp.setRightOperand(ast.newNumberLiteral("2"));

    return correctOddnessCheck;
}
 
Example 6
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 7
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 8
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 9
Source File: GetterSetterUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static Expression createInfixInvocationFromPostPrefixExpression(InfixExpression.Operator operator, Expression getterExpression, AST ast, ITypeBinding variableType, boolean is50OrHigher) {
	InfixExpression infix= ast.newInfixExpression();
	infix.setLeftOperand(getterExpression);
	infix.setOperator(operator);
	NumberLiteral number= ast.newNumberLiteral();
	number.setToken("1"); //$NON-NLS-1$
	infix.setRightOperand(number);
	ITypeBinding infixType= infix.resolveTypeBinding();
	return createNarrowCastIfNessecary(infix, infixType, ast, variableType, is50OrHigher);
}
 
Example 10
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 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.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static ThrowStatement getThrowForUnsupportedCase(Expression switchExpr, AST ast, ASTRewrite astRewrite) {
	ThrowStatement newThrowStatement = ast.newThrowStatement();
	ClassInstanceCreation newCic = ast.newClassInstanceCreation();
	newCic.setType(ast.newSimpleType(ast.newSimpleName("UnsupportedOperationException"))); //$NON-NLS-1$
	InfixExpression newInfixExpr = ast.newInfixExpression();
	StringLiteral newStringLiteral = ast.newStringLiteral();
	newStringLiteral.setLiteralValue("Unimplemented case: "); //$NON-NLS-1$
	newInfixExpr.setLeftOperand(newStringLiteral);
	newInfixExpr.setOperator(InfixExpression.Operator.PLUS);
	newInfixExpr.setRightOperand((Expression) astRewrite.createCopyTarget(switchExpr));
	newCic.arguments().add(newInfixExpr);
	newThrowStatement.setExpression(newCic);
	return newThrowStatement;
}
 
Example 13
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 14
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 15
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 16
Source File: GetterSetterUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Converts an assignment, postfix expression or prefix expression into an assignable equivalent expression using the getter.
 *
 * @param node the assignment/prefix/postfix node
 * @param astRewrite the astRewrite to use
 * @param getterExpression the expression to insert for read accesses or <code>null</code> if such an expression does not exist
 * @param variableType the type of the variable that the result will be assigned to
 * @param is50OrHigher <code>true</code> if a 5.0 or higher environment can be used
 * @return an expression that can be assigned to the type variableType with node being replaced by a equivalent expression using the getter
 */
public static Expression getAssignedValue(ASTNode node, ASTRewrite astRewrite, Expression getterExpression, ITypeBinding variableType, boolean is50OrHigher) {
	InfixExpression.Operator op= null;
	AST ast= astRewrite.getAST();
	if (isNotInBlock(node))
		return null;
	if (node.getNodeType() == ASTNode.ASSIGNMENT) {
		Assignment assignment= ((Assignment) node);
		Expression rightHandSide= assignment.getRightHandSide();
		Expression copiedRightOp= (Expression) astRewrite.createCopyTarget(rightHandSide);
		if (assignment.getOperator() == Operator.ASSIGN) {
			ITypeBinding rightHandSideType= rightHandSide.resolveTypeBinding();
			copiedRightOp= createNarrowCastIfNessecary(copiedRightOp, rightHandSideType, ast, variableType, is50OrHigher);
			return copiedRightOp;
		}
		if (getterExpression != null) {
			InfixExpression infix= ast.newInfixExpression();
			infix.setLeftOperand(getterExpression);
			infix.setOperator(ASTNodes.convertToInfixOperator(assignment.getOperator()));
			ITypeBinding infixType= infix.resolveTypeBinding();
			if (NecessaryParenthesesChecker.needsParenthesesForRightOperand(rightHandSide, infix, variableType)) {
				ParenthesizedExpression p= ast.newParenthesizedExpression();
				p.setExpression(copiedRightOp);
				copiedRightOp= p;
			}
			infix.setRightOperand(copiedRightOp);
			return createNarrowCastIfNessecary(infix, infixType, ast, variableType, is50OrHigher);
		}
	} else if (node.getNodeType() == ASTNode.POSTFIX_EXPRESSION) {
		PostfixExpression po= (PostfixExpression) node;
		if (po.getOperator() == PostfixExpression.Operator.INCREMENT)
			op= InfixExpression.Operator.PLUS;
		if (po.getOperator() == PostfixExpression.Operator.DECREMENT)
			op= InfixExpression.Operator.MINUS;
	} else if (node.getNodeType() == ASTNode.PREFIX_EXPRESSION) {
		PrefixExpression pe= (PrefixExpression) node;
		if (pe.getOperator() == PrefixExpression.Operator.INCREMENT)
			op= InfixExpression.Operator.PLUS;
		if (pe.getOperator() == PrefixExpression.Operator.DECREMENT)
			op= InfixExpression.Operator.MINUS;
	}
	if (op != null && getterExpression != null) {
		return createInfixInvocationFromPostPrefixExpression(op, getterExpression, ast, variableType, is50OrHigher);
	}
	return null;
}
 
Example 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 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 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 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 19
Source File: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean getExchangeOperandsProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
	// check that user invokes quick assist on infix expression
	if (!(node instanceof InfixExpression)) {
		return false;
	}
	InfixExpression infixExpression= (InfixExpression)node;
	Operator operator= infixExpression.getOperator();
	if (operator != InfixExpression.Operator.CONDITIONAL_AND && operator != InfixExpression.Operator.AND
			&& operator != InfixExpression.Operator.CONDITIONAL_OR && operator != InfixExpression.Operator.OR
			&& operator != InfixExpression.Operator.EQUALS && operator != InfixExpression.Operator.NOT_EQUALS
			&& operator != InfixExpression.Operator.LESS && operator != InfixExpression.Operator.LESS_EQUALS
			&& operator != InfixExpression.Operator.GREATER && operator != InfixExpression.Operator.GREATER_EQUALS
			&& operator != InfixExpression.Operator.PLUS && operator != InfixExpression.Operator.TIMES
			&& operator != InfixExpression.Operator.XOR) {
		return false;
	}

	int offset= isOperatorSelected(infixExpression, context.getSelectionOffset(), context.getSelectionLength());
	if (offset == -1) {
		return false;
	}

	//  we could produce quick assist
	if (resultingCollections == null) {
		return true;
	}
	AST ast= infixExpression.getAST();
	ASTRewrite rewrite= ASTRewrite.create(ast);
	// prepare left and right expressions
	Expression leftExpression= null;
	Expression rightExpression= null;
	InfixExpression currentExpression= infixExpression;
	leftExpression= combineOperands(rewrite, leftExpression, infixExpression.getLeftOperand(), false, operator);
	if (infixExpression.getRightOperand().getStartPosition() <= context.getSelectionOffset()) {
		leftExpression= combineOperands(rewrite, leftExpression, infixExpression.getRightOperand(), false, operator);
	} else {
		rightExpression= combineOperands(rewrite, rightExpression, infixExpression.getRightOperand(), false, operator);
	}
	for (Iterator<Expression> iter= currentExpression.extendedOperands().iterator(); iter.hasNext();) {
		Expression extendedOperand= iter.next();
		if (extendedOperand.getStartPosition() <= context.getSelectionOffset()) {
			leftExpression= combineOperands(rewrite, leftExpression, extendedOperand, false, operator);
		} else {
			rightExpression= combineOperands(rewrite, rightExpression, extendedOperand, false, operator);
		}
	}

	if (NecessaryParenthesesChecker.needsParentheses(leftExpression, infixExpression, InfixExpression.RIGHT_OPERAND_PROPERTY)) {
		leftExpression= getParenthesizedExpression(ast, leftExpression);
	}
	if (NecessaryParenthesesChecker.needsParentheses(rightExpression, infixExpression, InfixExpression.LEFT_OPERAND_PROPERTY)) {
		rightExpression= getParenthesizedExpression(ast, rightExpression);
	}

	if (operator == InfixExpression.Operator.LESS) {
		operator= InfixExpression.Operator.GREATER;
	} else if (operator == InfixExpression.Operator.LESS_EQUALS) {
		operator= InfixExpression.Operator.GREATER_EQUALS;
	} else if (operator == InfixExpression.Operator.GREATER) {
		operator= InfixExpression.Operator.LESS;
	} else if (operator == InfixExpression.Operator.GREATER_EQUALS) {
		operator= InfixExpression.Operator.LESS_EQUALS;
	}

	// create new infix expression
	InfixExpression newInfix= ast.newInfixExpression();
	newInfix.setOperator(operator);
	newInfix.setLeftOperand(rightExpression);
	newInfix.setRightOperand(leftExpression);
	rewrite.replace(infixExpression, newInfix, null);
	// add correction proposal
	String label= CorrectionMessages.AdvancedQuickAssistProcessor_exchangeOperands_description;
	Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
	ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.EXCHANGE_OPERANDS, image);
	resultingCollections.add(proposal);
	return true;
}
 
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;
}