Java Code Examples for org.eclipse.jdt.core.dom.Expression#getStartPosition()

The following examples show how to use org.eclipse.jdt.core.dom.Expression#getStartPosition() . 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: 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 2
Source File: IntroduceParameterRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void initializeSelectedExpression(CompilationUnitRewrite cuRewrite) throws JavaModelException {
	IASTFragment fragment= ASTFragmentFactory.createFragmentForSourceRange(
			new SourceRange(fSelectionStart, fSelectionLength), cuRewrite.getRoot(), cuRewrite.getCu());

	if (! (fragment instanceof IExpressionFragment))
		return;

	//TODO: doesn't handle selection of partial Expressions
	Expression expression= ((IExpressionFragment) fragment).getAssociatedExpression();
	if (fragment.getStartPosition() != expression.getStartPosition()
			|| fragment.getLength() != expression.getLength())
		return;

	if (Checks.isInsideJavadoc(expression))
		return;
	//TODO: exclude invalid selections
	if (Checks.isEnumCase(expression.getParent()))
		return;

	fSelectedExpression= expression;
}
 
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 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 4
Source File: ConditionalLoop.java    From JDeodorant with MIT License 6 votes vote down vote up
private static List<ASTNode> getAllVariableModifiersInParentBlock(SimpleName variable, Block block)
{
	List<ASTNode> bodyVariableModifiers = new ArrayList<ASTNode>();
	ExpressionExtractor expressionExtractor = new ExpressionExtractor();
	bodyVariableModifiers.addAll(expressionExtractor.getAssignments(block));
	// remove all variable updaters that are not modifying the specified variable or are after the position of the variable in use
	Iterator<ASTNode> it = bodyVariableModifiers.iterator();
	while (it.hasNext())
	{
		ASTNode currentNode = it.next();
		if (currentNode instanceof Expression)
		{
			Expression currentExpression = (Expression) currentNode;
			if (!AbstractLoopUtilities.isUpdatingVariable(currentExpression, variable) || currentExpression.getStartPosition() >= variable.getStartPosition())
			{
				it.remove();
			}
		}
	}
	return bodyVariableModifiers;
}
 
Example 5
Source File: JavaMethodParameterCodeMining.java    From jdt-codemining with Eclipse Public License 1.0 5 votes vote down vote up
private JavaMethodParameterCodeMining(ASTNode node, Expression parameter, int parameterIndex, CompilationUnit cu,
		ICodeMiningProvider provider, boolean showName, boolean showType, boolean showParameterByUsingFilters) {
	super(new Position(parameter.getStartPosition(), 1), provider, null);
	this.cu = cu;
	this.node = node;
	this.parameter = parameter;
	this.parameterIndex = parameterIndex;
	this.showName = showName;
	this.showType = showType;
	this.showParameterByUsingFilters = showParameterByUsingFilters;
}
 
Example 6
Source File: PreconditionExaminer.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean isInsideDifference(SimpleName simpleName, Expression difference) {
	int startOffset = simpleName.getStartPosition();
	int endOffset = simpleName.getStartPosition() + simpleName.getLength();
	int differenceStartOffset = difference.getStartPosition();
	int differenceEndOffset = difference.getStartPosition() + difference.getLength();
	if(startOffset >= differenceStartOffset && endOffset <= differenceEndOffset)
		return true;
	return false;
}
 
Example 7
Source File: ControlVariable.java    From JDeodorant with MIT License 5 votes vote down vote up
private static List<ASTNode> getAllVariableModifiersInParentMethod(SimpleName variable)
{
	List<ASTNode> bodyVariableModifiers = new ArrayList<ASTNode>();
	MethodDeclaration parentMethod = AbstractLoopUtilities.findParentMethodDeclaration(variable);
	if (parentMethod != null)
	{
		Block parentMethodBody = parentMethod.getBody();
		if (parentMethodBody != null)
		{
			ExpressionExtractor expressionExtractor = new ExpressionExtractor();
			bodyVariableModifiers.addAll(expressionExtractor.getVariableModifiers(parentMethodBody));
			// remove all variable updaters that are not modifying the specified variable or are after the position of the variable in use
			Iterator<ASTNode> it = bodyVariableModifiers.iterator();
			while (it.hasNext())
			{
				ASTNode currentNode = it.next();
				if (currentNode instanceof Expression)
				{
					Expression currentExpression = (Expression) currentNode;
					if (!AbstractLoopUtilities.isUpdatingVariable(currentExpression, variable) || currentExpression.getStartPosition() >= variable.getStartPosition())
					{
						it.remove();
					}
				}
			}
			// add the variable's declaration
			VariableDeclaration variableDeclaration = AbstractLoopUtilities.getVariableDeclaration(variable);
			if (variableDeclaration != null)
			{
				bodyVariableModifiers.add(0, variableDeclaration);
			}
		}
	}
	return bodyVariableModifiers;
}
 
Example 8
Source File: AssociativeInfixExpressionFragment.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static ISourceRange getRangeOfOperands(List<Expression> operands) {
	Expression first = operands.get(0);
	Expression last = operands.get(operands.size() - 1);
	return new SourceRange(first.getStartPosition(), last.getStartPosition() + last.getLength() - first.getStartPosition());
}
 
Example 9
Source File: ChangeTypeRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void setSelectionRanges(Expression exp){
	fEffectiveSelectionStart= exp.getStartPosition();
	fEffectiveSelectionLength= exp.getLength();
	fSelectionBinding= ExpressionVariable.resolveBinding(exp);
	setOriginalType(exp.resolveTypeBinding());
}
 
Example 10
Source File: AssociativeInfixExpressionFragment.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static ISourceRange getRangeOfOperands(List<Expression> operands) {
	Expression first= operands.get(0);
	Expression last= operands.get(operands.size() - 1);
	return new SourceRange(first.getStartPosition(), last.getStartPosition() + last.getLength() - first.getStartPosition());
}
 
Example 11
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;
}