Java Code Examples for com.sun.source.tree.LambdaExpressionTree#getBodyKind()

The following examples show how to use com.sun.source.tree.LambdaExpressionTree#getBodyKind() . 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: NullAway.java    From NullAway with MIT License 5 votes vote down vote up
@Override
public Description matchLambdaExpression(LambdaExpressionTree tree, VisitorState state) {
  if (!matchWithinClass) {
    return Description.NO_MATCH;
  }
  Symbol.MethodSymbol funcInterfaceMethod =
      NullabilityUtil.getFunctionalInterfaceMethod(tree, state.getTypes());
  // we need to update environment mapping before running the handler, as some handlers
  // (like Rx nullability) run dataflow analysis
  updateEnvironmentMapping(tree, state);
  handler.onMatchLambdaExpression(this, tree, state, funcInterfaceMethod);
  if (NullabilityUtil.isUnannotated(funcInterfaceMethod, config)) {
    return Description.NO_MATCH;
  }
  Description description =
      checkParamOverriding(
          tree.getParameters().stream().map(ASTHelpers::getSymbol).collect(Collectors.toList()),
          funcInterfaceMethod,
          tree,
          null,
          state);
  if (description != Description.NO_MATCH) {
    return description;
  }
  // if the body has a return statement, that gets checked in matchReturn().  We need this code
  // for lambdas with expression bodies
  if (tree.getBodyKind() == LambdaExpressionTree.BodyKind.EXPRESSION
      && funcInterfaceMethod.getReturnType().getKind() != TypeKind.VOID) {
    ExpressionTree resExpr = (ExpressionTree) tree.getBody();
    return checkReturnExpression(tree, resExpr, funcInterfaceMethod, state);
  }
  return Description.NO_MATCH;
}
 
Example 2
Source File: MissingReturnStatement.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public List<Fix> run(CompilationInfo compilationInfo, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) {
    TreePath method = null;
    if (diagnosticKey != null && diagnosticKey.contains("/compiler.misc.incompatible.ret.type.in.lambda/")) { // NOI18N
        // PENDING: when issue #258201 is implemented, use the new method instead of this HACK
        offset++;
    }
    TreePath tp = compilationInfo.getTreeUtilities().pathFor(offset);

    while (tp != null && !TreeUtilities.CLASS_TREE_KINDS.contains(tp.getLeaf().getKind())) {
        Kind kind = tp.getLeaf().getKind();
        if (kind == Kind.METHOD || kind == Kind.LAMBDA_EXPRESSION) {
            method = tp;
            break;
        }

        tp = tp.getParentPath();
    }

    if (method == null) {
        return null;
    }
    
    if (method.getLeaf().getKind() == Kind.METHOD) {
        MethodTree mt = (MethodTree) tp.getLeaf();
        if (mt.getReturnType() == null) {
            return null;
        }
    } else if (method.getLeaf().getKind() == Kind.LAMBDA_EXPRESSION) {
        LambdaExpressionTree let = (LambdaExpressionTree)method.getLeaf();
        TreePath bodyPath = new TreePath(method, let.getBody());
        if (let.getBodyKind() == LambdaExpressionTree.BodyKind.EXPRESSION) {
            TypeMirror m = compilationInfo.getTrees().getTypeMirror(
                bodyPath);
            if (m == null) {
                return null;
            }
            if (m.getKind() == TypeKind.ERROR) {
                m = compilationInfo.getTrees().getOriginalType((ErrorType)m);
            }
            if (m.getKind() != TypeKind.VOID) {
                // do not offer to add return for something, which already has
                // some type
                return null;
            }
        } else if (Utilities.exitsFromAllBranchers(compilationInfo, bodyPath)) {
            // do not add return, returns are already there.
            return null;
        }
    }

    List<Fix> result = new ArrayList<Fix>(2);

    result.add(new FixImpl(compilationInfo.getSnapshot().getSource(), TreePathHandle.create(tp, compilationInfo)));
    if (method.getLeaf().getKind() == Kind.METHOD) {
        result.add(new ChangeMethodReturnType.FixImpl(compilationInfo, tp, TypeMirrorHandle.create(compilationInfo.getTypes().getNoType(TypeKind.VOID)), "void").toEditorFix());
    }

    return result;
}
 
Example 3
Source File: JavaInputAstVisitor.java    From google-java-format with Apache License 2.0 4 votes vote down vote up
@Override
public Void visitLambdaExpression(LambdaExpressionTree node, Void unused) {
  sync(node);
  boolean statementBody = node.getBodyKind() == LambdaExpressionTree.BodyKind.STATEMENT;
  boolean parens = builder.peekToken().equals(Optional.of("("));
  builder.open(parens ? plusFour : ZERO);
  if (parens) {
    token("(");
  }
  boolean first = true;
  for (VariableTree parameter : node.getParameters()) {
    if (!first) {
      token(",");
      builder.breakOp(" ");
    }
    scan(parameter, null);
    first = false;
  }
  if (parens) {
    token(")");
  }
  builder.close();
  builder.space();
  builder.op("->");
  builder.open(statementBody ? ZERO : plusFour);
  if (statementBody) {
    builder.space();
  } else {
    builder.breakOp(" ");
  }
  if (node.getBody().getKind() == Tree.Kind.BLOCK) {
    visitBlock(
        (BlockTree) node.getBody(),
        CollapseEmptyOrNot.YES,
        AllowLeadingBlankLine.NO,
        AllowTrailingBlankLine.NO);
  } else {
    scan(node.getBody(), null);
  }
  builder.close();
  return null;
}