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

The following examples show how to use org.eclipse.jdt.core.dom.ReturnStatement#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: ReturnTypeSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public ITypeBinding getTypeBinding(AST ast) {
	boolean couldBeObject= false;
	for (int i= 0; i < fResult.size(); i++) {
		ReturnStatement node= fResult.get(i);
		Expression expr= node.getExpression();
		if (expr != null) {
			ITypeBinding binding= Bindings.normalizeTypeBinding(expr.resolveTypeBinding());
			if (binding != null) {
				return binding;
			} else {
				couldBeObject= true;
			}
		} else {
			return ast.resolveWellKnownType("void"); //$NON-NLS-1$
		}
	}
	if (couldBeObject) {
		return ast.resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
	}
	return ast.resolveWellKnownType("void"); //$NON-NLS-1$
}
 
Example 2
Source File: ReturnTypeSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static void addMethodReturnsVoidProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) throws JavaModelException {
	CompilationUnit astRoot= context.getASTRoot();
	ASTNode selectedNode= problem.getCoveringNode(astRoot);
	if (!(selectedNode instanceof ReturnStatement)) {
		return;
	}
	ReturnStatement returnStatement= (ReturnStatement) selectedNode;
	Expression expression= returnStatement.getExpression();
	if (expression == null) {
		return;
	}
	BodyDeclaration decl= ASTResolving.findParentBodyDeclaration(selectedNode);
	if (decl instanceof MethodDeclaration) {
		MethodDeclaration methDecl= (MethodDeclaration) decl;
		Type retType= methDecl.getReturnType2();
		if (retType == null || retType.resolveBinding() == null) {
			return;
		}
		TypeMismatchSubProcessor.addChangeSenderTypeProposals(context, expression, retType.resolveBinding(), false, IProposalRelevance.METHOD_RETURNS_VOID, proposals);
	}
}
 
Example 3
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean visit(final ReturnStatement node) {
  this.appendToBuffer("return");
  Expression _expression = node.getExpression();
  boolean _tripleNotEquals = (_expression != null);
  if (_tripleNotEquals) {
    this.appendSpaceToBuffer();
    node.getExpression().accept(this);
    this.appendSpaceToBuffer();
  } else {
    final ASTNode parent = node.getParent();
    final boolean isIfElse = ((parent instanceof IfStatement) && (((IfStatement) parent).getElseStatement() != null));
    if (((!isIfElse) && (!(parent instanceof SwitchStatement)))) {
      this.appendToBuffer(";");
    }
  }
  return false;
}
 
Example 4
Source File: SuperTypeConstraintsCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public final void endVisit(final ReturnStatement node) {
	final Expression expression= node.getExpression();
	if (expression != null) {
		final ConstraintVariable2 descendant= (ConstraintVariable2) expression.getProperty(PROPERTY_CONSTRAINT_VARIABLE);
		if (descendant != null) {
			final MethodDeclaration declaration= fCurrentMethods.peek();
			if (declaration != null) {
				final IMethodBinding binding= declaration.resolveBinding();
				if (binding != null) {
					final ConstraintVariable2 ancestor= fModel.createReturnTypeVariable(binding);
					if (ancestor != null) {
						node.setProperty(PROPERTY_CONSTRAINT_VARIABLE, ancestor);
						fModel.createSubtypeConstraint(descendant, ancestor);
					}
				}
			}
		}
	}
}
 
Example 5
Source File: InferTypeArgumentsConstraintCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void endVisit(ReturnStatement node) {
	Expression expression= node.getExpression();
	if (expression == null)
		return;
	ConstraintVariable2 expressionCv= getConstraintVariable(expression);
	if (expressionCv == null)
		return;

	MethodDeclaration methodDeclaration= (MethodDeclaration) ASTNodes.getParent(node, ASTNode.METHOD_DECLARATION);
	if (methodDeclaration == null)
		return;
	IMethodBinding methodBinding= methodDeclaration.resolveBinding();
	if (methodBinding == null)
		return;
	ReturnTypeVariable2 returnTypeCv= fTCModel.makeReturnTypeVariable(methodBinding);
	if (returnTypeCv == null)
		return;

	fTCModel.createElementEqualsConstraints(returnTypeCv, expressionCv);
}
 
Example 6
Source File: PreconditionExaminer.java    From JDeodorant with MIT License 6 votes vote down vote up
private void extractReturnTypeBinding(PDGNode pdgNode, List<ITypeBinding> returnedTypeBindings) {
	if(pdgNode instanceof PDGExitNode) {
		PDGExitNode exitNode = (PDGExitNode)pdgNode;
		ReturnStatement returnStatement = (ReturnStatement)exitNode.getASTStatement();
		Expression returnedExpression = returnStatement.getExpression();
		if(returnedExpression != null && !(returnedExpression instanceof NullLiteral)) {
			ITypeBinding typeBinding = returnedExpression.resolveTypeBinding();
			if(typeBinding != null) {
				boolean alreadyContained = false;
				for(ITypeBinding binding : returnedTypeBindings) {
					if(binding.isEqualTo(typeBinding)) {
						alreadyContained = true;
						break;
					}
				}
				if(!alreadyContained)
					returnedTypeBindings.add(typeBinding);
			}
		}
	}
}
 
Example 7
Source File: ReturnTypeSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public ITypeBinding getTypeBinding(AST ast) {
	boolean couldBeObject= false;
	for (int i= 0; i < fResult.size(); i++) {
		ReturnStatement node= fResult.get(i);
		Expression expr= node.getExpression();
		if (expr != null) {
			ITypeBinding binding= Bindings.normalizeTypeBinding(expr.resolveTypeBinding());
			if (binding != null) {
				return binding;
			} else {
				couldBeObject= true;
			}
		} else {
			return ast.resolveWellKnownType("void"); //$NON-NLS-1$
		}
	}
	if (couldBeObject) {
		return ast.resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
	}
	return ast.resolveWellKnownType("void"); //$NON-NLS-1$
}
 
Example 8
Source File: MethodObject.java    From JDeodorant with MIT License 6 votes vote down vote up
public FieldInstructionObject isGetter() {
	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 ReturnStatement) {
 			ReturnStatement returnStatement = (ReturnStatement) statement;
 			if((returnStatement.getExpression() instanceof SimpleName || returnStatement.getExpression() instanceof FieldAccess) && statementObject.getFieldInstructions().size() == 1 && statementObject.getMethodInvocations().size() == 0 &&
  				statementObject.getLocalVariableDeclarations().size() == 0 && statementObject.getLocalVariableInstructions().size() == 0 && this.constructorObject.parameterList.size() == 0) {
 				return statementObject.getFieldInstructions().get(0);
 			}
 		}
 	}
	}
	return null;
}
 
Example 9
Source File: MethodDeclarationUtility.java    From JDeodorant with MIT License 6 votes vote down vote up
public static SimpleName isGetter(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() == 0) {
			Statement statement = statements.get(0);
    		if(statement instanceof ReturnStatement) {
    			ReturnStatement returnStatement = (ReturnStatement)statement;
    			Expression returnStatementExpression = returnStatement.getExpression();
    			if(returnStatementExpression instanceof SimpleName) {
    				return (SimpleName)returnStatementExpression;
    			}
    			else if(returnStatementExpression instanceof FieldAccess) {
    				FieldAccess fieldAccess = (FieldAccess)returnStatementExpression;
    				return fieldAccess.getName();
    			}
    		}
		}
	}
	return null;
}
 
Example 10
Source File: ASTNodeMatcher.java    From JDeodorant with MIT License 5 votes vote down vote up
private static AbstractControlStructure generateAbstractControlStructure(Object object)
{
	if (object instanceof IfStatement)
	{
		return new IfControlStructure((IfStatement) object);
	}
	else if (object instanceof SwitchStatement)
	{
		return new SwitchControlStructure((SwitchStatement) object);
	}
	else if (object instanceof ExpressionStatement)
	{
		ExpressionStatement expressionStatement = (ExpressionStatement) object;
		if (AbstractControlStructureUtilities.hasOneConditionalExpression(expressionStatement) != null)
		{
			return new TernaryControlStructure(expressionStatement);
		}
	}
	else if (object instanceof ReturnStatement)
	{
		ReturnStatement returnStatement = (ReturnStatement) object;
		if (returnStatement.getExpression() instanceof ConditionalExpression)
		{
			return new TernaryControlStructure(returnStatement);
		}
	}
	return null;
}
 
Example 11
Source File: CodeBlock.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
private ReturnStmt visit(ReturnStatement node) {
	int startLine = _cunit.getLineNumber(node.getStartPosition());
	int endLine = _cunit.getLineNumber(node.getStartPosition() + node.getLength());
	ReturnStmt returnStmt = new ReturnStmt(startLine, endLine, node);
	
	if(node.getExpression() != null){
		Expr expression = (Expr) process(node.getExpression());
		expression.setParent(returnStmt);
		returnStmt.setExpression(expression);
	}
	
	return returnStmt;
}
 
Example 12
Source File: ASTNodeMatcher.java    From JDeodorant with MIT License 5 votes vote down vote up
public boolean match(ReturnStatement node, Object other) {
	if (node.getExpression() instanceof ConditionalExpression && other instanceof IfStatement)
	{
		TernaryControlStructure nodeTernaryControlStructure = new TernaryControlStructure(node);
		return ifMatch(nodeTernaryControlStructure, other);
	}
	return super.match(node, other);
}
 
Example 13
Source File: ControlDependenceTreeGenerator.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean isExpressionStatementWithConditionalExpression(PDGNode node) {
	Statement statement = node.getASTStatement();
	if(statement instanceof ExpressionStatement) {
		ExpressionExtractor expressionExtractor = new ExpressionExtractor();
		List<Expression> conditionalExpressions = expressionExtractor.getConditionalExpressions(statement);
		if(conditionalExpressions.size() == 1) {
			ConditionalExpression conditional = (ConditionalExpression)conditionalExpressions.get(0);
			ASTNode parent = conditional.getParent();
			ASTNode grandParent = parent.getParent();
			if(grandParent != null && grandParent.equals(statement)) {
				return true;
			}
			if(parent instanceof ParenthesizedExpression) {
				if(grandParent != null) {
					if(grandParent.getParent() != null && grandParent.getParent().equals(statement)) {
						return true;
					}
				}
			}
		}
	}
	else if (statement instanceof ReturnStatement) {
		ReturnStatement returnStatement = (ReturnStatement)statement;
		if (returnStatement.getExpression() instanceof ConditionalExpression) {
			return true;
		}
	}
	return false;
}
 
Example 14
Source File: PreconditionExaminer.java    From JDeodorant with MIT License 5 votes vote down vote up
private void conditionalReturnStatement(NodeMapping nodeMapping, PDGNode node) {
	CFGNode cfgNode = node.getCFGNode();
	if(cfgNode instanceof CFGExitNode) {
		ReturnStatement returnStatement = (ReturnStatement)cfgNode.getASTStatement();
		if(returnStatement.getExpression() == null) {
			PreconditionViolation violation = new StatementPreconditionViolation(node.getStatement(),
					PreconditionViolationType.CONDITIONAL_RETURN_STATEMENT);
			nodeMapping.addPreconditionViolation(violation);
			preconditionViolations.add(violation);
		}
	}
}
 
Example 15
Source File: StyledStringVisitor.java    From JDeodorant with MIT License 5 votes vote down vote up
public boolean visit(ReturnStatement stmnt) {
	/*
	 * ReturnStatement: return [ Expression ] ;
	 */
	styledString.append("return", new StyledStringStyler(keywordStyle));
	Expression expression = stmnt.getExpression();
	if (expression != null){
		appendSpace();
		handleExpression(expression);
	}
	appendSemicolon();
	return false;
}
 
Example 16
Source File: FullConstraintCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public ITypeConstraint[] create(ReturnStatement returnStatement){
	if (returnStatement.getExpression() == null)
		return new ITypeConstraint[0];

	ConstraintVariable returnTypeVariable= fConstraintVariableFactory.makeReturnTypeVariable(returnStatement);
	return fTypeConstraintFactory.createSubtypeConstraint(
			fConstraintVariableFactory.makeExpressionOrTypeVariable(returnStatement.getExpression(), getContext()),
			returnTypeVariable);
}
 
Example 17
Source File: SourceProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(ReturnStatement node) {
	Expression expression= node.getExpression();
	if (!(ASTNodes.isLiteral(expression) || expression instanceof Name)) {
		fMustEvalReturnedExpression= true;
	}
	if (Invocations.isInvocation(expression) || expression instanceof ClassInstanceCreation) {
		fReturnValueNeedsLocalVariable= false;
	}
	fReturnExpressions.add(expression);
	return false;
}
 
Example 18
Source File: ReturnFlowInfo.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static int getReturnFlag(ReturnStatement node) {
	Expression expression = node.getExpression();
	if (expression == null || expression.resolveTypeBinding() == node.getAST().resolveWellKnownType("void")) {
		return VOID_RETURN;
	}
	return VALUE_RETURN;
}
 
Example 19
Source File: ReplaceTypeCodeWithStateStrategy.java    From JDeodorant with MIT License 4 votes vote down vote up
private boolean typeObjectGetterMethodAlreadyExists() {
	InheritanceTree tree = typeCheckElimination.getInheritanceTreeMatchingWithStaticTypes();
	if(tree != null) {
		MethodDeclaration[] contextMethods = sourceTypeDeclaration.getMethods();
		DefaultMutableTreeNode rootNode = tree.getRootNode();
		String rootClassName = (String)rootNode.getUserObject();
		DefaultMutableTreeNode leaf = rootNode.getFirstLeaf();
		List<String> subclassNames = new ArrayList<String>();
		while(leaf != null) {
			subclassNames.add((String)leaf.getUserObject());
			leaf = leaf.getNextLeaf();
		}
		for(MethodDeclaration contextMethod : contextMethods) {
			Type returnType = contextMethod.getReturnType2();
			if(returnType != null) {
				if(returnType.resolveBinding().getQualifiedName().equals(rootClassName)) {
					Block contextMethodBody = contextMethod.getBody();
					if(contextMethodBody != null) {
						List<Statement> statements = contextMethodBody.statements();
						if(statements.size() > 0 && statements.get(0) instanceof SwitchStatement) {
							SwitchStatement switchStatement = (SwitchStatement)statements.get(0);
							List<Statement> statements2 = switchStatement.statements();
							int matchCounter = 0;
							for(Statement statement2 : statements2) {
								if(statement2 instanceof ReturnStatement) {
									ReturnStatement returnStatement = (ReturnStatement)statement2;
									Expression returnStatementExpression = returnStatement.getExpression();
									if(returnStatementExpression instanceof ClassInstanceCreation) {
										ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation)returnStatementExpression;
										Type classInstanceCreationType = classInstanceCreation.getType();
										if(subclassNames.contains(classInstanceCreationType.resolveBinding().getQualifiedName())) {
											matchCounter++;
										}
									}
								}
							}
							if(matchCounter == subclassNames.size())
								return true;
						}
					}
				}
			}
		}
	}
	return false;
}
 
Example 20
Source File: ReturnFlowInfo.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static int getReturnFlag(ReturnStatement node) {
	Expression expression= node.getExpression();
	if (expression == null || expression.resolveTypeBinding() == node.getAST().resolveWellKnownType("void")) //$NON-NLS-1$
		return VOID_RETURN;
	return VALUE_RETURN;
}