org.eclipse.jdt.core.dom.InfixExpression.Operator Java Examples

The following examples show how to use org.eclipse.jdt.core.dom.InfixExpression.Operator. 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: GenerateHashCodeEqualsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 7 votes vote down vote up
private Statement createArrayComparison(String name) {
	MethodInvocation invoc= fAst.newMethodInvocation();
	invoc.setName(fAst.newSimpleName(METHODNAME_EQUALS));
	invoc.setExpression(getQualifiedName(JAVA_UTIL_ARRAYS));
	invoc.arguments().add(getThisAccessForEquals(name));
	invoc.arguments().add(getOtherAccess(name));

	PrefixExpression pe= fAst.newPrefixExpression();
	pe.setOperator(PrefixExpression.Operator.NOT);
	pe.setOperand(invoc);

	IfStatement ifSt= fAst.newIfStatement();
	ifSt.setExpression(pe);
	ifSt.setThenStatement(getThenStatement(getReturnFalse()));

	return ifSt;
}
 
Example #2
Source File: NecessaryParenthesesChecker.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the type of infix expression based on its operands and operator.
 * 
 * @param operator the operator of infix expression
 * @param leftOperandType the type of left operand of infix expression
 * @param rightOperandType the type of right operand of infix expression
 * @return the type of infix expression if the type of both the operands is same or if the type
 *         of either operand of a + operator is String, <code>null</code> otherwise.
 * 
 * @since 3.9
 */
private static ITypeBinding getInfixExpressionType(InfixExpression.Operator operator, ITypeBinding leftOperandType, ITypeBinding rightOperandType) {
	if (leftOperandType == rightOperandType) {
		return leftOperandType;
	}
	if (operator == InfixExpression.Operator.PLUS) {
		if (isStringType(leftOperandType)) {
			return leftOperandType;
		} else if (isStringType(rightOperandType)) {
			return rightOperandType;
		}
	}
	// If the left and right operand types are different, we assume that parentheses are needed.
	// This is to avoid complications of numeric promotions and for readability of complicated code.
	return null;
}
 
Example #3
Source File: GenerateHashCodeEqualsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Statement createOuterComparison() {
	MethodInvocation outer1= fAst.newMethodInvocation();
	outer1.setName(fAst.newSimpleName(METHODNAME_OUTER_TYPE));

	MethodInvocation outer2= fAst.newMethodInvocation();
	outer2.setName(fAst.newSimpleName(METHODNAME_OUTER_TYPE));
	outer2.setExpression(fAst.newSimpleName(VARIABLE_NAME_EQUALS_CASTED));

	MethodInvocation outerEql= fAst.newMethodInvocation();
	outerEql.setName(fAst.newSimpleName(METHODNAME_EQUALS));
	outerEql.setExpression(outer1);
	outerEql.arguments().add(outer2);

	PrefixExpression not= fAst.newPrefixExpression();
	not.setOperand(outerEql);
	not.setOperator(PrefixExpression.Operator.NOT);

	IfStatement notEqNull= fAst.newIfStatement();
	notEqNull.setExpression(not);
	notEqNull.setThenStatement(getThenStatement(getReturnFalse()));
	return notEqNull;
}
 
Example #4
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 #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: GenerateHashCodeEqualsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Statement createAddQualifiedHashCode(IVariableBinding binding) {

		MethodInvocation invoc= fAst.newMethodInvocation();
		invoc.setExpression(getThisAccessForHashCode(binding.getName()));
		invoc.setName(fAst.newSimpleName(METHODNAME_HASH_CODE));

		InfixExpression expr= fAst.newInfixExpression();
		expr.setOperator(Operator.EQUALS);
		expr.setLeftOperand(getThisAccessForHashCode(binding.getName()));
		expr.setRightOperand(fAst.newNullLiteral());

		ConditionalExpression cexpr= fAst.newConditionalExpression();
		cexpr.setThenExpression(fAst.newNumberLiteral("0")); //$NON-NLS-1$
		cexpr.setElseExpression(invoc);
		cexpr.setExpression(parenthesize(expr));

		return prepareAssignment(parenthesize(cexpr));
	}
 
Example #7
Source File: StringBuilderChainGenerator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void addMemberCheckNull(Object member, boolean addSeparator) {
	IfStatement ifStatement= fAst.newIfStatement();
	ifStatement.setExpression(createInfixExpression(createMemberAccessExpression(member, true, true), Operator.NOT_EQUALS, fAst.newNullLiteral()));
	Block thenBlock= fAst.newBlock();
	flushTemporaryExpression();
	String[] arrayString= getContext().getTemplateParser().getBody();
	for (int i= 0; i < arrayString.length; i++) {
		addElement(processElement(arrayString[i], member), thenBlock);
	}
	if (addSeparator)
		addElement(getContext().getTemplateParser().getSeparator(), thenBlock);
	flushTemporaryExpression();

	if (thenBlock.statements().size() == 1 && !getContext().isForceBlocks()) {
		ifStatement.setThenStatement((Statement)ASTNode.copySubtree(fAst, (ASTNode)thenBlock.statements().get(0)));
	} else {
		ifStatement.setThenStatement(thenBlock);
	}
	toStringMethod.getBody().statements().add(ifStatement);
}
 
Example #8
Source File: GenerateHashCodeEqualsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Statement createMultiArrayComparison(String name) {
	MethodInvocation invoc= fAst.newMethodInvocation();
	invoc.setName(fAst.newSimpleName(METHODNAME_DEEP_EQUALS));
	invoc.setExpression(getQualifiedName(JAVA_UTIL_ARRAYS));
	invoc.arguments().add(getThisAccessForEquals(name));
	invoc.arguments().add(getOtherAccess(name));

	PrefixExpression pe= fAst.newPrefixExpression();
	pe.setOperator(PrefixExpression.Operator.NOT);
	pe.setOperand(invoc);

	IfStatement ifSt= fAst.newIfStatement();
	ifSt.setExpression(pe);
	ifSt.setThenStatement(getThenStatement(getReturnFalse()));

	return ifSt;
}
 
Example #9
Source File: StringBuilderGenerator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void addMemberCheckNull(Object member, boolean addSeparator) {
	IfStatement ifStatement= fAst.newIfStatement();
	ifStatement.setExpression(createInfixExpression(createMemberAccessExpression(member, true, true), Operator.NOT_EQUALS, fAst.newNullLiteral()));
	Block thenBlock= fAst.newBlock();
	flushBuffer(null);
	String[] arrayString= getContext().getTemplateParser().getBody();
	for (int i= 0; i < arrayString.length; i++) {
		addElement(processElement(arrayString[i], member), thenBlock);
	}
	if (addSeparator)
		addElement(getContext().getTemplateParser().getSeparator(), thenBlock);
	flushBuffer(thenBlock);

	if (thenBlock.statements().size() == 1 && !getContext().isForceBlocks()) {
		ifStatement.setThenStatement((Statement)ASTNode.copySubtree(fAst, (ASTNode)thenBlock.statements().get(0)));
	} else {
		ifStatement.setThenStatement(thenBlock);
	}
	toStringMethod.getBody().statements().add(ifStatement);
}
 
Example #10
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 #11
Source File: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static void breakInfixOperationAtOperation(ASTRewrite rewrite, Expression expression, Operator operator, int operatorOffset, boolean removeParentheses, Expression[] res) {
	if (expression.getStartPosition() + expression.getLength() <= operatorOffset) {
		// add to the left
		res[0]= combineOperands(rewrite, res[0], expression, removeParentheses, operator);
		return;
	}
	if (operatorOffset <= expression.getStartPosition()) {
		// add to the right
		res[1]= combineOperands(rewrite, res[1], expression, removeParentheses, operator);
		return;
	}
	if (!(expression instanceof InfixExpression)) {
		throw new IllegalArgumentException("Cannot break up non-infix expression"); //$NON-NLS-1$
	}
	InfixExpression infixExpression= (InfixExpression) expression;
	if (infixExpression.getOperator() != operator) {
		throw new IllegalArgumentException("Incompatible operator"); //$NON-NLS-1$
	}
	breakInfixOperationAtOperation(rewrite, infixExpression.getLeftOperand(), operator, operatorOffset, removeParentheses, res);
	breakInfixOperationAtOperation(rewrite, infixExpression.getRightOperand(), operator, operatorOffset, removeParentheses, res);

	List<Expression> extended= infixExpression.extendedOperands();
	for (int i= 0; i < extended.size(); i++) {
		breakInfixOperationAtOperation(rewrite, extended.get(i), operator, operatorOffset, removeParentheses, res);
	}
}
 
Example #12
Source File: NecessaryParenthesesChecker.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean isAssociative(InfixExpression.Operator operator, ITypeBinding infixExprType, boolean isAllOperandsHaveSameType) {
	if (operator == InfixExpression.Operator.PLUS)
		return isStringType(infixExprType) || isIntegerType(infixExprType) && isAllOperandsHaveSameType;

	if (operator == InfixExpression.Operator.TIMES)
		return isIntegerType(infixExprType) && isAllOperandsHaveSameType;

	if (operator == InfixExpression.Operator.CONDITIONAL_AND
			|| operator == InfixExpression.Operator.CONDITIONAL_OR
			|| operator == InfixExpression.Operator.AND
			|| operator == InfixExpression.Operator.OR
			|| operator == InfixExpression.Operator.XOR)
		return true;

	return false;
}
 
Example #13
Source File: OperatorPrecedence.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the precedence of an infix operator. Operators
 * with higher precedence are executed before expressions
 * with lower precedence.
 * <br>
 * i.e. in: <br>
 * <code>3 + 4 - 5 * 6;</code><br>
 * the  precedence order is
 * <ul>
 * <li>*</li>
 * <li>+</li>
 * <li>-</li>
 * </ul>
 * 1. 5,6 -(*)-> 30<br>
 * 2. 3,4 -(+)-> 7<br>
 * 3. 7,30 -(-)-> -23<br>
 *
 * @param operator the expression to determine the precedence for
 * @return the precedence the higher to stronger the binding to its operands
 */
public static int getOperatorPrecedence(Operator operator) {
	if (operator == Operator.CONDITIONAL_OR) {
		return CONDITIONAL_OR;
	} else if (operator == Operator.CONDITIONAL_AND) {
		return CONDITIONAL_AND;
	} else if (operator == Operator.OR) {
		return BITWISE_INCLUSIVE_OR;
	} else if (operator == Operator.XOR) {
		return BITWISE_EXCLUSIVE_OR;
	} else if (operator == Operator.AND) {
		return BITWISE_AND;
	} else if (operator == Operator.EQUALS || operator == Operator.NOT_EQUALS) {
		return EQUALITY;
	} else if (operator == Operator.LESS || operator == Operator.LESS_EQUALS || operator == Operator.GREATER || operator == Operator.GREATER_EQUALS) {
		return RELATIONAL;
	} else if (operator == Operator.LEFT_SHIFT || operator == Operator.RIGHT_SHIFT_SIGNED || operator == Operator.RIGHT_SHIFT_UNSIGNED) {
		return SHIFT;
	} else if (operator == Operator.PLUS || operator == Operator.MINUS) {
		return ADDITIVE;
	} else if (operator == Operator.REMAINDER || operator == Operator.DIVIDE || operator == Operator.TIMES) {
		return MULTIPLICATIVE;
	}
	return Integer.MAX_VALUE;
}
 
Example #14
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 #15
Source File: UseEqualsResolution.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
protected void repairBug(ASTRewrite rewrite, CompilationUnit workingUnit, BugInstance bug) throws BugResolutionException {
    Assert.isNotNull(rewrite);
    Assert.isNotNull(workingUnit);
    Assert.isNotNull(bug);

    InfixExpression[] stringEqualityChecks = findStringEqualityChecks(getASTNode(workingUnit, bug.getPrimarySourceLineAnnotation()));
    for (InfixExpression stringEqualityCheck : stringEqualityChecks) {
        Operator operator = stringEqualityCheck.getOperator();
        Expression replaceExpression;
        if (EQUALS.equals(operator)) {
            replaceExpression = createEqualsExpression(rewrite, stringEqualityCheck);
        } else if (NOT_EQUALS.equals(operator)) {
            replaceExpression = createNotEqualsExpression(rewrite, stringEqualityCheck);
        } else {
            throw new BugResolutionException("Illegal operator '" + operator + "' found.");
        }
        rewrite.replace(stringEqualityCheck, replaceExpression, null);
    }
}
 
Example #16
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 #17
Source File: InvertBooleanUtility.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static void breakInfixOperationAtOperation(ASTRewrite rewrite, Expression expression, Operator operator, int operatorOffset, boolean removeParentheses, Expression[] res) {
	if (expression.getStartPosition() + expression.getLength() <= operatorOffset) {
		// add to the left
		res[0] = combineOperands(rewrite, res[0], expression, removeParentheses, operator);
		return;
	}
	if (operatorOffset <= expression.getStartPosition()) {
		// add to the right
		res[1] = combineOperands(rewrite, res[1], expression, removeParentheses, operator);
		return;
	}
	if (!(expression instanceof InfixExpression)) {
		throw new IllegalArgumentException("Cannot break up non-infix expression"); //$NON-NLS-1$
	}
	InfixExpression infixExpression = (InfixExpression) expression;
	if (infixExpression.getOperator() != operator) {
		throw new IllegalArgumentException("Incompatible operator"); //$NON-NLS-1$
	}
	breakInfixOperationAtOperation(rewrite, infixExpression.getLeftOperand(), operator, operatorOffset, removeParentheses, res);
	breakInfixOperationAtOperation(rewrite, infixExpression.getRightOperand(), operator, operatorOffset, removeParentheses, res);

	List<Expression> extended = infixExpression.extendedOperands();
	for (int i = 0; i < extended.size(); i++) {
		breakInfixOperationAtOperation(rewrite, extended.get(i), operator, operatorOffset, removeParentheses, res);
	}
}
 
Example #18
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 #19
Source File: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean getPullNegationUpProposals(IInvocationContext context, ArrayList<ASTNode> coveredNodes, Collection<ICommandAccess> resultingCollections) {
	if (coveredNodes.size() != 1) {
		return false;
	}
	//
	ASTNode fullyCoveredNode= coveredNodes.get(0);

	Expression expression= getBooleanExpression(fullyCoveredNode);
	if (expression == null || (!(expression instanceof InfixExpression) && !(expression instanceof ConditionalExpression))) {
		return false;
	}
	//  we could produce quick assist
	if (resultingCollections == null) {
		return true;
	}
	//
	AST ast= expression.getAST();
	final ASTRewrite rewrite= ASTRewrite.create(ast);
	// prepared inverted expression
	Expression inversedExpression= getInversedExpression(rewrite, expression);
	// prepare ParenthesizedExpression
	ParenthesizedExpression parenthesizedExpression= ast.newParenthesizedExpression();
	parenthesizedExpression.setExpression(inversedExpression);
	// prepare NOT prefix expression
	PrefixExpression prefixExpression= ast.newPrefixExpression();
	prefixExpression.setOperator(PrefixExpression.Operator.NOT);
	prefixExpression.setOperand(parenthesizedExpression);
	// replace old expression
	rewrite.replace(expression, prefixExpression, null);
	// add correction proposal
	String label= CorrectionMessages.AdvancedQuickAssistProcessor_pullNegationUp;
	Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
	ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.PULL_NEGATION_UP, image);
	resultingCollections.add(proposal);
	return true;
}
 
Example #20
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 #21
Source File: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static Expression getInversedNotExpression(ASTRewrite rewrite, Expression expression, AST ast) {
	PrefixExpression prefixExpression= ast.newPrefixExpression();
	prefixExpression.setOperator(PrefixExpression.Operator.NOT);
	ParenthesizedExpression parenthesizedExpression= getParenthesizedExpression(ast, (Expression)rewrite.createCopyTarget(expression));
	prefixExpression.setOperand(parenthesizedExpression);
	return prefixExpression;
}
 
Example #22
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 #23
Source File: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean isNegated(Expression expression) {
	if (!(expression.getParent() instanceof ParenthesizedExpression))
		return false;

	ParenthesizedExpression parenthesis= (ParenthesizedExpression) expression.getParent();
	if (!(parenthesis.getParent() instanceof PrefixExpression))
		return false;

	PrefixExpression prefix= (PrefixExpression) parenthesis.getParent();
	if (!(prefix.getOperator() == PrefixExpression.Operator.NOT))
		return false;

	return true;
}
 
Example #24
Source File: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static Statement createAssignmentStatement(ASTRewrite rewrite, Assignment.Operator assignmentOperator, Expression origAssignee, Expression origAssigned) {
	AST ast= rewrite.getAST();
	Assignment elseAssignment= ast.newAssignment();
	elseAssignment.setOperator(assignmentOperator);
	elseAssignment.setLeftHandSide((Expression) rewrite.createCopyTarget(origAssignee));
	elseAssignment.setRightHandSide((Expression) rewrite.createCopyTarget(origAssigned));
	ExpressionStatement statement= ast.newExpressionStatement(elseAssignment);
	return statement;
}
 
Example #25
Source File: InvertBooleanUtility.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static Expression getInversedNotExpression(ASTRewrite rewrite, Expression expression, AST ast) {
	PrefixExpression prefixExpression = ast.newPrefixExpression();
	prefixExpression.setOperator(PrefixExpression.Operator.NOT);
	ParenthesizedExpression parenthesizedExpression = getParenthesizedExpression(ast, (Expression) rewrite.createCopyTarget(expression));
	prefixExpression.setOperand(parenthesizedExpression);
	return prefixExpression;
}
 
Example #26
Source File: GenerateHashCodeEqualsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private Statement createSimpleComparison(IVariableBinding binding) {
	if (isPrimitiveType(binding.getType(), PrimitiveType.FLOAT)) {
		return createReturningIfStatement(createFloatInvocation(getThisAccessForEquals(binding.getName())), createFloatInvocation(getOtherAccess(binding
				.getName())), Operator.NOT_EQUALS, false);
	} else if (isPrimitiveType(binding.getType(), PrimitiveType.DOUBLE)) {
		return createReturningIfStatement(createDoubleInvocation(getThisAccessForEquals(binding.getName())), createDoubleInvocation(getOtherAccess(binding
				.getName())), Operator.NOT_EQUALS, false);
	} else
		return createReturningIfStatement(getThisAccessForEquals(binding.getName()), getOtherAccess(binding.getName()), Operator.NOT_EQUALS, false);
}
 
Example #27
Source File: AbstractToStringGenerator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected InfixExpression createInfixExpression(Expression leftOperand, Operator operator, Expression rightOperand) {
	InfixExpression expression= fAst.newInfixExpression();
	expression.setLeftOperand(leftOperand);
	expression.setOperator(operator);
	expression.setRightOperand(rightOperand);
	return expression;
}
 
Example #28
Source File: StringConcatenationGenerator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private Expression createSumExpression(Expression left, Expression right) {
	if (right == null)
		return left;
	if (left == null)
		return right;
	return createInfixExpression(left, Operator.PLUS, right);
}
 
Example #29
Source File: StringConcatenationGenerator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void addMemberCheckNull(Object member, boolean addSeparator) {
	ConditionalExpression cExpression= fAst.newConditionalExpression();

	// member != null ?
	InfixExpression infExpression= fAst.newInfixExpression();
	infExpression.setLeftOperand(createMemberAccessExpression(member, true, true));
	infExpression.setRightOperand(fAst.newNullLiteral());
	infExpression.setOperator(Operator.NOT_EQUALS);
	cExpression.setExpression(infExpression);

	SumExpressionBuilder builder= new SumExpressionBuilder(null);
	String[] arrayString= getContext().getTemplateParser().getBody();
	for (int i= 0; i < arrayString.length; i++) {
		addElement(processElement(arrayString[i], member), builder);
	}
	if (addSeparator)
		addElement(getContext().getTemplateParser().getSeparator(), builder);
	cExpression.setThenExpression(builder.getExpression());

	StringLiteral literal= fAst.newStringLiteral();
	literal.setLiteralValue(getContext().isSkipNulls() ? "" : "null"); //$NON-NLS-1$ //$NON-NLS-2$
	cExpression.setElseExpression(literal);

	ParenthesizedExpression pExpression= fAst.newParenthesizedExpression();
	pExpression.setExpression(cExpression);
	toStringExpressionBuilder.addExpression(pExpression);
}
 
Example #30
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;
}