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

The following examples show how to use com.sun.source.tree.LambdaExpressionTree#getParameters() . 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: Lambda.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Hint(displayName = "#DN_ConvertVarLambdaParameters", description = "#DESC_ConvertVarLambdaParameters", category = "suggestions", hintKind = Hint.Kind.ACTION, minSourceVersion = "11")
@TriggerTreeKind(Kind.LAMBDA_EXPRESSION)
public static ErrorDescription implicitVarParameterTypes(HintContext ctx) {
    // hint will be enable only for JDK-11 or above.
    if (ctx.getInfo().getSourceVersion().compareTo(SourceVersion.RELEASE_9) < 2) {
        return null;
    }
    // Check invalid lambda parameter declaration
    if (ctx.getInfo().getTreeUtilities().hasError(ctx.getPath().getLeaf())) {
        return null;
    }
    // Check var parameter types
    LambdaExpressionTree let = (LambdaExpressionTree) ctx.getPath().getLeaf();
    if (let.getParameters() == null || let.getParameters().isEmpty()) {
        return null;
    }
    VariableTree var = let.getParameters().get(0);
    if (ctx.getInfo().getTreeUtilities().isVarType(new TreePath(ctx.getPath(), var))) {
        return null;
    }

    return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), NbBundle.getMessage(Lambda.class, "ERR_ConvertVarLambdaParameters"), new AddVarLambdaParameterTypes(ctx.getInfo(), ctx.getPath()).toEditorFix());
}
 
Example 2
Source File: Lambda.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Hint(displayName="#DN_addExplicitLambdaParameters", description="#DESC_addExplicitLambdaParameters", category="suggestions", hintKind=Hint.Kind.ACTION)
@Messages({
    "DN_addExplicitLambdaParameters=Convert Lambda to Use Explicit Parameter Types",
    "DESC_addExplicitLambdaParameters=Converts lambdas to use explicit parameter types",
    "ERR_addExplicitLambdaParameters=",
    "FIX_addExplicitLambdaParameters=Use explicit parameter types"
})
@TriggerTreeKind(Kind.LAMBDA_EXPRESSION)
public static ErrorDescription explicitParameterTypes(HintContext ctx) {
    LambdaExpressionTree let = (LambdaExpressionTree) ctx.getPath().getLeaf();

    if (ctx.getInfo().getTreeUtilities().hasError(let)) {
        return null;
    }

    boolean hasSyntheticParameterName = false;
    
    for (VariableTree var : let.getParameters()) {
        hasSyntheticParameterName |= var.getType() == null || ctx.getInfo().getTreeUtilities().isSynthetic(TreePath.getPath(ctx.getPath(), var.getType()));
    }
    
    if (!hasSyntheticParameterName) {
        return null;
    }
    
    return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_addExplicitLambdaParameters(), new AddExplicitLambdaParameterTypes(ctx.getInfo(), ctx.getPath()).toEditorFix());
}
 
Example 3
Source File: Lambda.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected void performRewrite(TransformationContext ctx) throws Exception {
    LambdaExpressionTree let = (LambdaExpressionTree) ctx.getPath().getLeaf();

    for (VariableTree var : let.getParameters()) {
        TreePath typePath = TreePath.getPath(ctx.getPath(), var.getType());
        if (ctx.getWorkingCopy().getTreeUtilities().isSynthetic(typePath)) {
            Tree imported = ctx.getWorkingCopy().getTreeMaker().Type(ctx.getWorkingCopy().getTrees().getTypeMirror(typePath));
            ctx.getWorkingCopy().rewrite(var.getType(), imported);
        }
    }
}
 
Example 4
Source File: TreeConverter.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private TreeNode convertLambda(LambdaExpressionTree node, TreePath parent) {
  TreePath path = getTreePath(parent, node);
  LambdaExpression newNode = new LambdaExpression();
  convertFunctionalExpression((JCFunctionalExpression) node, parent, newNode);
  for (VariableTree param : node.getParameters()) {
    newNode.addParameter((VariableDeclaration) convert(param, path));
  }
  return newNode.setBody(convert(node.getBody(), path));
}
 
Example 5
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;
}