Java Code Examples for org.eclipse.jdt.core.dom.ExpressionStatement#getExpression()

The following examples show how to use org.eclipse.jdt.core.dom.ExpressionStatement#getExpression() . 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: ExtractTempRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private IExpressionFragment getSelectedExpression() throws JavaModelException {
	if (fSelectedExpression != null) {
		return fSelectedExpression;
	}
	IASTFragment selectedFragment = ASTFragmentFactory.createFragmentForSourceRange(new SourceRange(fSelectionStart, fSelectionLength), fCompilationUnitNode, fCu);

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

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

	return fSelectedExpression;
}
 
Example 2
Source File: ExtractFieldRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private IExpressionFragment getSelectedExpression() throws JavaModelException {
	if (fSelectedExpression != null) {
		return fSelectedExpression;
	}
	IASTFragment selectedFragment = ASTFragmentFactory.createFragmentForSourceRange(new SourceRange(fSelectionStart, fSelectionLength), fCompilationUnitNode, fCu);

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

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

	return fSelectedExpression;
}
 
Example 3
Source File: ExtractTempRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private IExpressionFragment getSelectedExpression() throws JavaModelException {
	if (fSelectedExpression != null)
		return fSelectedExpression;
	IASTFragment selectedFragment= ASTFragmentFactory.createFragmentForSourceRange(new SourceRange(fSelectionStart, fSelectionLength), fCompilationUnitNode, fCu);

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

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

	return fSelectedExpression;
}
 
Example 4
Source File: MethodObject.java    From JDeodorant with MIT License 6 votes vote down vote up
public FieldInstructionObject isSetter() {
	if(getMethodBody() != null) {
 	List<AbstractStatement> abstractStatements = getMethodBody().getCompositeStatement().getStatements();
 	if(abstractStatements.size() == 1 && abstractStatements.get(0) instanceof StatementObject) {
 		StatementObject statementObject = (StatementObject)abstractStatements.get(0);
 		Statement statement = statementObject.getStatement();
 		if(statement instanceof ExpressionStatement) {
 			ExpressionStatement expressionStatement = (ExpressionStatement)statement;
 			if(expressionStatement.getExpression() instanceof Assignment && statementObject.getFieldInstructions().size() == 1 && statementObject.getMethodInvocations().size() == 0 &&
     				statementObject.getLocalVariableDeclarations().size() == 0 && statementObject.getLocalVariableInstructions().size() == 1 && this.constructorObject.parameterList.size() == 1) {
 				Assignment assignment = (Assignment)expressionStatement.getExpression();
 				if((assignment.getLeftHandSide() instanceof SimpleName || assignment.getLeftHandSide() instanceof FieldAccess) && assignment.getRightHandSide() instanceof SimpleName)
 					return statementObject.getFieldInstructions().get(0);
 			}
 		}
 	}
	}
	return null;
}
 
Example 5
Source File: RefactorProposalUtility.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static CUCorrectionProposal getAssignVariableProposal(CodeActionParams params, IInvocationContext context, boolean problemsAtLocation, Map formatterOptions, boolean returnAsCommand,
		IProblemLocationCore[] locations)
		throws CoreException {
	ASTNode node = context.getCoveringNode();
	Statement statement = ASTResolving.findParentStatement(node);
	if (!(statement instanceof ExpressionStatement)) {
		return null;
	}
	ExpressionStatement expressionStatement = (ExpressionStatement) statement;
	Expression expression = expressionStatement.getExpression();
	if (expression.getNodeType() == ASTNode.ASSIGNMENT) {
		return null;
	}
	ITypeBinding typeBinding = expression.resolveTypeBinding();
	typeBinding = Bindings.normalizeTypeBinding(typeBinding);
	if (typeBinding == null) {
		return null;
	}
	if (containsMatchingProblem(locations, IProblem.UnusedObjectAllocation)) {
		return null;
	}
	final ICompilationUnit cu = context.getCompilationUnit();
	int relevance;
	if (context.getSelectionLength() == 0) {
		relevance = IProposalRelevance.EXTRACT_LOCAL_ZERO_SELECTION;
	} else if (problemsAtLocation) {
		relevance = IProposalRelevance.EXTRACT_LOCAL_ERROR;
	} else {
		relevance = IProposalRelevance.EXTRACT_LOCAL;
	}
	if (returnAsCommand) {
		return new AssignToVariableAssistCommandProposal(cu, JavaCodeActionKind.REFACTOR_ASSIGN_VARIABLE, AssignToVariableAssistProposal.LOCAL, expressionStatement, typeBinding, relevance, APPLY_REFACTORING_COMMAND_ID,
				Arrays.asList(ASSIGN_VARIABLE_COMMAND, params));
	} else {
		return new AssignToVariableAssistProposal(cu, JavaCodeActionKind.REFACTOR_ASSIGN_VARIABLE, AssignToVariableAssistProposal.LOCAL, expressionStatement, typeBinding, relevance);
	}
}
 
Example 6
Source File: RefactorProposalUtility.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static CUCorrectionProposal getAssignFieldProposal(CodeActionParams params, IInvocationContext context, boolean problemsAtLocation, Map formatterOptions, boolean returnAsCommand,
		IProblemLocationCore[] locations) throws CoreException {
	ASTNode node = context.getCoveringNode();
	Statement statement = ASTResolving.findParentStatement(node);
	if (!(statement instanceof ExpressionStatement)) {
		return null;
	}
	ExpressionStatement expressionStatement = (ExpressionStatement) statement;
	Expression expression = expressionStatement.getExpression();
	if (expression.getNodeType() == ASTNode.ASSIGNMENT) {
		return null;
	}
	ITypeBinding typeBinding = expression.resolveTypeBinding();
	typeBinding = Bindings.normalizeTypeBinding(typeBinding);
	if (typeBinding == null) {
		return null;
	}
	if (containsMatchingProblem(locations, IProblem.UnusedObjectAllocation)) {
		return null;
	}
	final ICompilationUnit cu = context.getCompilationUnit();
	ASTNode type = ASTResolving.findParentType(expression);
	if (type != null) {
		int relevance;
		if (context.getSelectionLength() == 0) {
			relevance = IProposalRelevance.EXTRACT_LOCAL_ZERO_SELECTION;
		} else if (problemsAtLocation) {
			relevance = IProposalRelevance.EXTRACT_LOCAL_ERROR;
		} else {
			relevance = IProposalRelevance.EXTRACT_LOCAL;
		}
		if (returnAsCommand) {
			return new AssignToVariableAssistCommandProposal(cu, JavaCodeActionKind.REFACTOR_ASSIGN_FIELD, AssignToVariableAssistProposal.FIELD, expressionStatement, typeBinding, relevance, APPLY_REFACTORING_COMMAND_ID,
					Arrays.asList(ASSIGN_FIELD_COMMAND, params));
		} else {
			return new AssignToVariableAssistProposal(cu, JavaCodeActionKind.REFACTOR_ASSIGN_FIELD, AssignToVariableAssistProposal.FIELD, expressionStatement, typeBinding, relevance);
		}
	}
	return null;
}
 
Example 7
Source File: NewVariableCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isAssigned(Statement statement, SimpleName name) {
	if (statement instanceof ExpressionStatement) {
		ExpressionStatement exstat= (ExpressionStatement) statement;
		if (exstat.getExpression() instanceof Assignment) {
			Assignment assignment= (Assignment) exstat.getExpression();
			return assignment.getLeftHandSide() == name;
		}
	}
	return false;
}
 
Example 8
Source File: QuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean getAssignToVariableProposals(IInvocationContext context, ASTNode node, IProblemLocation[] locations, Collection<ICommandAccess> resultingCollections) {
	Statement statement= ASTResolving.findParentStatement(node);
	if (!(statement instanceof ExpressionStatement)) {
		return false;
	}
	ExpressionStatement expressionStatement= (ExpressionStatement) statement;

	Expression expression= expressionStatement.getExpression();
	if (expression.getNodeType() == ASTNode.ASSIGNMENT) {
		return false; // too confusing and not helpful
	}

	ITypeBinding typeBinding= expression.resolveTypeBinding();
	typeBinding= Bindings.normalizeTypeBinding(typeBinding);
	if (typeBinding == null) {
		return false;
	}
	if (resultingCollections == null) {
		return true;
	}

	// don't add if already added as quick fix
	if (containsMatchingProblem(locations, IProblem.UnusedObjectAllocation))
		return false;
	
	ICompilationUnit cu= context.getCompilationUnit();

	AssignToVariableAssistProposal localProposal= new AssignToVariableAssistProposal(cu, AssignToVariableAssistProposal.LOCAL, expressionStatement, typeBinding, IProposalRelevance.ASSIGN_TO_LOCAL);
	localProposal.setCommandId(ASSIGN_TO_LOCAL_ID);
	resultingCollections.add(localProposal);

	ASTNode type= ASTResolving.findParentType(expression);
	if (type != null) {
		AssignToVariableAssistProposal fieldProposal= new AssignToVariableAssistProposal(cu, AssignToVariableAssistProposal.FIELD, expressionStatement, typeBinding, IProposalRelevance.ASSIGN_TO_FIELD);
		fieldProposal.setCommandId(ASSIGN_TO_FIELD_ID);
		resultingCollections.add(fieldProposal);
	}
	return true;

}
 
Example 9
Source File: QuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static ITypeBinding extractExpressionType(ExpressionStatement statement) {
	Expression expression= statement.getExpression();
	if (expression.getNodeType() == ASTNode.METHOD_INVOCATION
			|| expression.getNodeType() == ASTNode.FIELD_ACCESS) {
		return expression.resolveTypeBinding();
	}
	return null;
}
 
Example 10
Source File: NewVariableCorrectionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isAssigned(Statement statement, SimpleName name) {
	if (statement instanceof ExpressionStatement) {
		ExpressionStatement exstat= (ExpressionStatement) statement;
		if (exstat.getExpression() instanceof Assignment) {
			Assignment assignment= (Assignment) exstat.getExpression();
			return assignment.getLeftHandSide() == name;
		}
	}
	return false;
}
 
Example 11
Source File: MethodDeclarationUtility.java    From JDeodorant with MIT License 5 votes vote down vote up
public static SimpleName isSetter(MethodDeclaration methodDeclaration) {
	Block methodBody = methodDeclaration.getBody();
	List<SingleVariableDeclaration> parameters = methodDeclaration.parameters();
	if(methodBody != null) {
		List<Statement> statements = methodBody.statements();
		if(statements.size() == 1 && parameters.size() == 1) {
			Statement statement = statements.get(0);
    		if(statement instanceof ExpressionStatement) {
    			ExpressionStatement expressionStatement = (ExpressionStatement)statement;
    			Expression expressionStatementExpression = expressionStatement.getExpression();
    			if(expressionStatementExpression instanceof Assignment) {
    				Assignment assignment = (Assignment)expressionStatementExpression;
    				Expression rightHandSide = assignment.getRightHandSide();
    				if(rightHandSide instanceof SimpleName) {
    					SimpleName rightHandSideSimpleName = (SimpleName)rightHandSide;
    					if(rightHandSideSimpleName.resolveBinding().isEqualTo(parameters.get(0).resolveBinding())) {
    						Expression leftHandSide = assignment.getLeftHandSide();
    						if(leftHandSide instanceof SimpleName) {
    		    				return (SimpleName)leftHandSide;
    		    			}
    		    			else if(leftHandSide instanceof FieldAccess) {
    		    				FieldAccess fieldAccess = (FieldAccess)leftHandSide;
    		    				return fieldAccess.getName();
    		    			}
    					}
    				}
    			}
    		}
		}
	}
	return null;
}
 
Example 12
Source File: ASTNodeMatcher.java    From JDeodorant with MIT License 4 votes vote down vote up
public boolean match(ExpressionStatement node, Object other) {
	if (AbstractControlStructureUtilities.hasOneConditionalExpression(node) != null && other instanceof IfStatement)
	{
		TernaryControlStructure nodeTernaryControlStructure = new TernaryControlStructure(node);
		return ifMatch(nodeTernaryControlStructure, other);
	}
	else if(node.getExpression() instanceof Assignment && other instanceof VariableDeclarationStatement) {
		VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement)other;
		List fragments = variableDeclarationStatement.fragments();
		if(fragments.size() == 1) {
			VariableDeclarationFragment fragment = (VariableDeclarationFragment)fragments.get(0);
			Assignment assignment = (Assignment)node.getExpression();
			Expression leftHandSide = assignment.getLeftHandSide();
			if(leftHandSide instanceof SimpleName) {
				SimpleName simpleName = (SimpleName)leftHandSide;
				boolean variableMatch = safeSubtreeMatch(simpleName, fragment.getName());
				boolean variableTypeMatch = false;
				IBinding simpleNameBinding = simpleName.resolveBinding();
				IBinding fragmentNameBinding = fragment.getName().resolveBinding();
				if(simpleNameBinding.getKind() == IBinding.VARIABLE && fragmentNameBinding.getKind() == IBinding.VARIABLE) {
					IVariableBinding simpleNameVariableBinding = (IVariableBinding)simpleNameBinding;
					IVariableBinding fragmentNameVariableBinding = (IVariableBinding)fragmentNameBinding;
					variableTypeMatch = simpleNameVariableBinding.getType().isEqualTo(fragmentNameVariableBinding.getType()) &&
							simpleNameVariableBinding.getType().getQualifiedName().equals(fragmentNameVariableBinding.getType().getQualifiedName());
				}
				boolean initializerMatch = false;
				boolean initializerTypeMatch = false;
				Expression initializer = fragment.getInitializer();
				Expression rightHandSide = assignment.getRightHandSide();
				if(initializer != null && initializer.getNodeType() == rightHandSide.getNodeType()) {
					initializerMatch = safeSubtreeMatch(rightHandSide, initializer);
					initializerTypeMatch = initializer.resolveTypeBinding().isEqualTo(rightHandSide.resolveTypeBinding()) &&
							initializer.resolveTypeBinding().getQualifiedName().equals(rightHandSide.resolveTypeBinding().getQualifiedName());
				}
				if(variableMatch && variableTypeMatch && initializerMatch && initializerTypeMatch) {
					VariableDeclaration variableDeclaration = AbstractLoopUtilities.getVariableDeclaration(simpleName);
					if(variableDeclaration != null && hasEmptyInitializer(variableDeclaration)) {
						safeSubtreeMatch(variableDeclaration.getName(), fragment.getName());
						List<ASTNode> astNodes = new ArrayList<ASTNode>();
						astNodes.add(variableDeclaration);
						ASTInformationGenerator.setCurrentITypeRoot(typeRoot1);
						reportAdditionalFragments(astNodes, this.additionallyMatchedFragments1);
						return true;
					}
				}
			}
		}
	}
	return super.match(node, other);
}
 
Example 13
Source File: ASTNodeMatcher.java    From JDeodorant with MIT License 4 votes vote down vote up
public boolean match(VariableDeclarationStatement node, Object other) {
	List fragments = node.fragments();
	if(fragments.size() == 1 && other instanceof ExpressionStatement) {
		VariableDeclarationFragment fragment = (VariableDeclarationFragment)fragments.get(0);
		ExpressionStatement expressionStatement = (ExpressionStatement)other;
		Expression expression = expressionStatement.getExpression();
		if(expression instanceof Assignment) {
			Assignment assignment = (Assignment)expression;
			Expression leftHandSide = assignment.getLeftHandSide();
			if(leftHandSide instanceof SimpleName) {
				SimpleName simpleName = (SimpleName)leftHandSide;
				boolean variableMatch = safeSubtreeMatch(fragment.getName(), simpleName);
				boolean variableTypeMatch = false;
				IBinding simpleNameBinding = simpleName.resolveBinding();
				IBinding fragmentNameBinding = fragment.getName().resolveBinding();
				if(simpleNameBinding.getKind() == IBinding.VARIABLE && fragmentNameBinding.getKind() == IBinding.VARIABLE) {
					IVariableBinding simpleNameVariableBinding = (IVariableBinding)simpleNameBinding;
					IVariableBinding fragmentNameVariableBinding = (IVariableBinding)fragmentNameBinding;
					variableTypeMatch = simpleNameVariableBinding.getType().isEqualTo(fragmentNameVariableBinding.getType()) &&
							simpleNameVariableBinding.getType().getQualifiedName().equals(fragmentNameVariableBinding.getType().getQualifiedName());;
				}
				boolean initializerMatch = false;
				boolean initializerTypeMatch = false;
				Expression initializer = fragment.getInitializer();
				Expression rightHandSide = assignment.getRightHandSide();
				if(initializer != null && initializer.getNodeType() == rightHandSide.getNodeType()) {
					initializerMatch = safeSubtreeMatch(initializer, rightHandSide);
					initializerTypeMatch = initializer.resolveTypeBinding().isEqualTo(rightHandSide.resolveTypeBinding()) &&
							initializer.resolveTypeBinding().getQualifiedName().equals(rightHandSide.resolveTypeBinding().getQualifiedName());
				}
				if(variableMatch && variableTypeMatch && initializerMatch && initializerTypeMatch) {
					VariableDeclaration variableDeclaration = AbstractLoopUtilities.getVariableDeclaration(simpleName);
					if(variableDeclaration != null && hasEmptyInitializer(variableDeclaration)) {
						safeSubtreeMatch(fragment.getName(), variableDeclaration.getName());
						List<ASTNode> astNodes = new ArrayList<ASTNode>();
						astNodes.add(variableDeclaration);
						ASTInformationGenerator.setCurrentITypeRoot(typeRoot2);
						reportAdditionalFragments(astNodes, this.additionallyMatchedFragments2);
						return true;
					}
				}
			}
		}
	}
	return super.match(node, other);
}