下面列出了org.eclipse.jdt.core.dom.ReturnStatement#getExpression ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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$
}
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);
}
}
@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;
}
@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);
}
}
}
}
}
}
@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);
}
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$
}
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;
}
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;
}
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);
}
}
}
}
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;
}
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;
}
@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;
}
@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);
}
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;
}
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);
}
}
}
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;
}
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);
}
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;
}
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;
}
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;
}