Java Code Examples for com.sun.source.tree.ReturnTree#getExpression()

The following examples show how to use com.sun.source.tree.ReturnTree#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: CreateElementUtilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static List<? extends TypeMirror> computeReturn(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
    ReturnTree rt = (ReturnTree) parent.getLeaf();
    
    if (rt.getExpression() == error) {
        TreePath method = findMethod(parent);
        
        if (method == null) {
            return null;
        }
        
        Element el = info.getTrees().getElement(method);
        
        if (el == null || el.getKind() != ElementKind.METHOD) {
            return null;
        }
        
        types.add(ElementKind.PARAMETER);
        types.add(ElementKind.LOCAL_VARIABLE);
        types.add(ElementKind.FIELD);
        
        return Collections.singletonList(((ExecutableElement) el).getReturnType());
    }
    
    return null;
}
 
Example 2
Source File: RemoveUnnecessary.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected void performRewrite(TransformationContext ctx) throws Exception {
    TreePath retPath = ctx.getPath();
    if (retPath.getLeaf().getKind() != Tree.Kind.RETURN) {
        return;
    }
    ReturnTree rtt = (ReturnTree)retPath.getLeaf();
    if (rtt.getExpression() == null) {
        return;
    }
    WorkingCopy wc = ctx.getWorkingCopy();
    ExpressionToStatement st = new ExpressionToStatement(wc.getTreeMaker(), wc);
    st.scan(new TreePath(retPath, rtt.getExpression()), null);
    if (st.remove || st.statements.isEmpty()) {
        // error, but I don't have an utility to properly remove the statement
        // from its parent now.
        return;
    }
    Utilities.replaceStatement(wc, retPath, st.statements);
}
 
Example 3
Source File: CreateElementUtilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static List<? extends TypeMirror> computeReturn(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
    ReturnTree rt = (ReturnTree) parent.getLeaf();
    
    if (rt.getExpression() == error) {
        TreePath method = findMethod(parent);
        
        if (method == null) {
            return null;
        }
        
        Element el = info.getTrees().getElement(method);
        
        if (el == null || el.getKind() != ElementKind.METHOD) {
            return null;
        }
        
        types.add(ElementKind.PARAMETER);
        types.add(ElementKind.LOCAL_VARIABLE);
        types.add(ElementKind.FIELD);
        
        return Collections.singletonList(((ExecutableElement) el).getReturnType());
    }
    
    return null;
}
 
Example 4
Source File: ExpectedTypeResolver.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public List<? extends TypeMirror> visitReturn(ReturnTree node, Object p) {
    if (node.getExpression() == null) {
        return null;
    }
    if (theExpression == null) {
        initExpression(node.getExpression());
    }
    TreePath parents = getCurrentPath();
    while (parents != null && parents.getLeaf().getKind() != Tree.Kind.METHOD) {
        parents = parents.getParentPath();
    }
    if (parents != null) {
        Tree returnTypeTree = ((MethodTree) parents.getLeaf()).getReturnType();
        if (returnTypeTree != null) {
            return Collections.singletonList(info.getTrees().getTypeMirror(new TreePath(parents, returnTypeTree)));
        }
    }
    return null;
}
 
Example 5
Source File: StreamNullabilityPropagator.java    From NullAway with MIT License 6 votes vote down vote up
@Override
public void onDataflowVisitReturn(
    ReturnTree tree, NullnessStore thenStore, NullnessStore elseStore) {
  if (returnToEnclosingMethodOrLambda.containsKey(tree)) {
    Tree filterTree = returnToEnclosingMethodOrLambda.get(tree);
    assert (filterTree instanceof MethodTree || filterTree instanceof LambdaExpressionTree);
    ExpressionTree retExpression = tree.getExpression();
    if (canBooleanExpressionEvalToTrue(retExpression)) {
      if (filterToNSMap.containsKey(filterTree)) {
        filterToNSMap.put(filterTree, filterToNSMap.get(filterTree).leastUpperBound(thenStore));
      } else {
        filterToNSMap.put(filterTree, thenStore);
      }
    }
  }
}
 
Example 6
Source File: JavaInputAstVisitor.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
@Override
public Void visitReturn(ReturnTree node, Void unused) {
    sync(node);
    token("return");
    if (node.getExpression() != null) {
        builder.space();
        scan(node.getExpression(), null);
    }
    token(";");
    return null;
}
 
Example 7
Source File: XPFlagCleaner.java    From piranha with Apache License 2.0 5 votes vote down vote up
@Override
public Description matchReturn(ReturnTree tree, VisitorState state) {
  if (overLaps(tree, state)) {
    return Description.NO_MATCH;
  }
  ExpressionTree et = tree.getExpression();
  if (et != null && et.getKind().equals(Kind.BOOLEAN_LITERAL)) {
    return Description.NO_MATCH;
  }

  Value x = evalExpr(et, state);
  boolean update = false;
  String replacementString = EMPTY;

  if (x.equals(Value.TRUE)) {
    update = true;
    replacementString = TRUE;
  } else if (x.equals(Value.FALSE)) {
    update = true;
    replacementString = FALSE;
  }

  if (update) {
    Description.Builder builder = buildDescription(tree);
    SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
    fixBuilder.replace(et, replacementString);
    decrementAllSymbolUsages(et, state, fixBuilder);
    builder.addFix(fixBuilder.build());
    endPos = state.getEndPosition(tree);
    return builder.build();
  }
  return Description.NO_MATCH;
}
 
Example 8
Source File: Refactorer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private List<ProspectiveOperation> getIfListRepresentation( StatementTree tree, boolean last) {
    IfTree ifTree = (IfTree) tree;
    List<ProspectiveOperation> ls = new ArrayList<ProspectiveOperation>();
    if (ifTree.getElseStatement() == null) {

        StatementTree then = ifTree.getThenStatement();
        if (isOneStatementBlock(then)) {
            then = ((BlockTree) then).getStatements().get(0);
        }
        if (then.getKind() == Tree.Kind.RETURN) {
            ReturnTree returnTree = (ReturnTree) then;
            ExpressionTree returnExpression = returnTree.getExpression();
            if (returnExpression.getKind() == Tree.Kind.BOOLEAN_LITERAL && ((LiteralTree) returnExpression).getValue().equals(true)) {
                ls.addAll(ProspectiveOperation.createOperator(ifTree, ProspectiveOperation.OperationType.ANYMATCH, this.preconditionsChecker, this.workingCopy));
            } else if (returnExpression.getKind() == Tree.Kind.BOOLEAN_LITERAL && ((LiteralTree) returnExpression).getValue().equals(false)) {
                ls.addAll(ProspectiveOperation.createOperator(ifTree, ProspectiveOperation.OperationType.NONEMATCH, this.preconditionsChecker, this.workingCopy));
            }
        } else {
            ls.addAll(ProspectiveOperation.createOperator(ifTree, ProspectiveOperation.OperationType.FILTER, this.preconditionsChecker, this.workingCopy));
            ls.addAll(getListRepresentation(ifTree.getThenStatement(), last));
        }
    } else {

        ls.addAll(ProspectiveOperation.createOperator(ifTree, ProspectiveOperation.OperationType.MAP, this.preconditionsChecker, this.workingCopy));
    }
    return ls;
}
 
Example 9
Source File: PreconditionsChecker.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Tree visitReturn(ReturnTree that, Trees trees) {
    ExpressionTree thatExpression = that.getExpression();
    if (!this.hasMatcherReturn && thatExpression != null && thatExpression.getKind() == Tree.Kind.BOOLEAN_LITERAL
            && thisIsMatcherReturn(that, this.getCurrentPath())) {
        this.hasMatcherReturn = true;
    } else {
        this.hasReturns = true;
    }
    return super.visitReturn(that, trees);

}
 
Example 10
Source File: NullAway.java    From NullAway with MIT License 5 votes vote down vote up
/**
 * We are trying to see if (1) we are in a method guaranteed to return something non-null, and (2)
 * this return statement can return something null.
 */
@Override
public Description matchReturn(ReturnTree tree, VisitorState state) {
  if (!matchWithinClass) {
    return Description.NO_MATCH;
  }
  handler.onMatchReturn(this, tree, state);
  ExpressionTree retExpr = tree.getExpression();
  // let's do quick checks on returned expression first
  if (retExpr == null) {
    return Description.NO_MATCH;
  }
  // now let's check the enclosing method
  TreePath enclosingMethodOrLambda =
      NullabilityUtil.findEnclosingMethodOrLambdaOrInitializer(state.getPath());
  if (enclosingMethodOrLambda == null) {
    throw new RuntimeException("no enclosing method, lambda or initializer!");
  }
  if (!(enclosingMethodOrLambda.getLeaf() instanceof MethodTree
      || enclosingMethodOrLambda.getLeaf() instanceof LambdaExpressionTree)) {
    throw new RuntimeException(
        "return statement outside of a method or lambda! (e.g. in an initializer block)");
  }
  Tree leaf = enclosingMethodOrLambda.getLeaf();
  Symbol.MethodSymbol methodSymbol;
  if (leaf instanceof MethodTree) {
    MethodTree enclosingMethod = (MethodTree) leaf;
    methodSymbol = ASTHelpers.getSymbol(enclosingMethod);
  } else {
    // we have a lambda
    methodSymbol =
        NullabilityUtil.getFunctionalInterfaceMethod(
            (LambdaExpressionTree) leaf, state.getTypes());
  }
  return checkReturnExpression(tree, retExpr, methodSymbol, state);
}
 
Example 11
Source File: JavaInputAstVisitor.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Void visitReturn(ReturnTree node, Void unused) {
    sync(node);
    token("return");
    if (node.getExpression() != null) {
        builder.space();
        scan(node.getExpression(), null);
    }
    token(";");
    return null;
}
 
Example 12
Source File: ExpressionToTypeInfo.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public TreePath visitReturn(ReturnTree node, Boolean isTargetContext) {
    ExpressionTree tree = node.getExpression();
    TreePath tp = new TreePath(getCurrentPath(), tree);
    if (isTargetContext) {
        throw new Result(tp);
    } else {
        return null;
    }
}
 
Example 13
Source File: JavaInputAstVisitor.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
@Override
public Void visitReturn(ReturnTree node, Void unused) {
  sync(node);
  token("return");
  if (node.getExpression() != null) {
    builder.space();
    scan(node.getExpression(), null);
  }
  token(";");
  return null;
}
 
Example 14
Source File: ScanStatement.java    From netbeans with Apache License 2.0 4 votes vote down vote up
String verifyExits(boolean exitsFromAllBranches) {
    int i = 0;
    i += hasReturns ? 1 : 0;
    i += hasBreaks ? 1 : 0;
    i += hasContinues ? 1 : 0;
    if (i > 1) {
        return "ERR_Too_Many_Different_Exits"; // NOI18N
    }
    if ((exitsFromAllBranches ? 0 : i) + usedAfterSelection.size() > 1) {
        return "ERR_Too_Many_Return_Values"; // NOI18N
    }
    Tree breakOrContinueTarget = null;
    boolean returnValueComputed = false;
    TreePath returnValue = null;
    for (TreePath tp : selectionExits) {
        if (tp.getLeaf().getKind() == Tree.Kind.RETURN) {
            if (!exitsFromAllBranches) {
                ReturnTree rt = (ReturnTree) tp.getLeaf();
                TreePath currentReturnValue = rt.getExpression() != null ? new TreePath(tp, rt.getExpression()) : null;
                if (!returnValueComputed) {
                    returnValue = currentReturnValue;
                    returnValueComputed = true;
                } else {
                    if (returnValue != null && currentReturnValue != null) {
                        Set<TreePath> candidates = SourceUtils.computeDuplicates(info, returnValue, currentReturnValue, cancel);
                        if (candidates.size() != 1 || candidates.iterator().next().getLeaf() != rt.getExpression()) {
                            return "ERR_Different_Return_Values"; // NOI18N
                        }
                    } else {
                        if (returnValue != currentReturnValue) {
                            return "ERR_Different_Return_Values"; // NOI18N
                        }
                    }
                }
            }
        } else {
            Tree target = info.getTreeUtilities().getBreakContinueTargetTree(tp);
            if (breakOrContinueTarget == null) {
                breakOrContinueTarget = target;
            }
            if (breakOrContinueTarget != target) {
                return "ERR_Break_Mismatch"; // NOI18N
            }
        }
    }
    return null;
}