Java Code Examples for com.sun.source.tree.Tree.Kind#LAMBDA_EXPRESSION
The following examples show how to use
com.sun.source.tree.Tree.Kind#LAMBDA_EXPRESSION .
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: MagicSurroundWithTryCatchFix.java From netbeans with Apache License 2.0 | 6 votes |
static TreePath enclosingTry(TreePath from) { TreePath tryPath = from; while (tryPath != null && tryPath.getLeaf().getKind() != Kind.TRY && !TreeUtilities.CLASS_TREE_KINDS.contains(tryPath.getLeaf().getKind()) && tryPath.getLeaf().getKind() != Kind.CATCH && tryPath.getLeaf().getKind() != Kind.LAMBDA_EXPRESSION) tryPath = tryPath.getParentPath(); if (tryPath.getLeaf().getKind() == Kind.TRY) { TryTree tt = (TryTree) tryPath.getLeaf(); //#104085: if the statement to be wrapped is inside a finally block of the try-catch, //do not attempt to extend existing catches...: for (Tree t : from) { if (tt.getFinallyBlock() == t) { return null; } } return tryPath; } return null; }
Example 2
Source File: Lambda.java From netbeans with Apache License 2.0 | 6 votes |
@Hint(displayName="#DN_lambda2Class", description="#DESC_lambda2Class", category="suggestions", hintKind=Hint.Kind.ACTION, minSourceVersion = "8") @Messages({ "DN_lambda2Class=Convert Lambda Expression to Anonymous Innerclass", "DESC_lambda2Class=Converts lambda expressions to anonymous inner classes", "ERR_lambda2Class=Anonymous class can be used" }) @TriggerTreeKind(Kind.LAMBDA_EXPRESSION) public static ErrorDescription lambda2Class(HintContext ctx) { TypeMirror samType = ctx.getInfo().getTrees().getTypeMirror(ctx.getPath()); if (samType == null || samType.getKind() != TypeKind.DECLARED) { return null; } return ErrorDescriptionFactory.forTree(ctx, ctx.getPath(), Bundle.ERR_lambda2Class(), new Lambda2Anonymous(ctx.getInfo(), ctx.getPath()).toEditorFix()); }
Example 3
Source File: Lambda.java From netbeans with Apache License 2.0 | 6 votes |
@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 4
Source File: NPECheck.java From netbeans with Apache License 2.0 | 5 votes |
@Override public State scan(Tree tree, Void p) { resume(tree, resumeBefore); State r = super.scan(tree, p); TypeMirror currentType = tree != null ? info.getTrees().getTypeMirror(new TreePath(getCurrentPath(), tree)) : null; if ((tree != null && tree.getKind() == Kind.LAMBDA_EXPRESSION) || (currentType != null && currentType.getKind().isPrimitive())) { r = State.NOT_NULL; } if (r != null && !doNotRecord) { // expressionState.put(tree, r); expressionState.put(tree, mergeIn(expressionState, tree, r)); } resume(tree, resumeAfter); Collection<VariableElement> varsOutScope = scopedVariables.get(tree); if (varsOutScope != null) { for (VariableElement ve : varsOutScope) { State s = variable2State.get(ve); if (s != null) { variable2StateFinal.put(ve, s); } } variable2State.keySet().removeAll(varsOutScope); } return r; }
Example 5
Source File: Utilities.java From netbeans with Apache License 2.0 | 5 votes |
/** * Finds the top-level block or expression that contains the 'from' path. * The result could be a * <ul> * <li>BlockTree representing method body * <li>ExpressionTree representing field initializer * <li>BlockTree representing class initializer * <li>ExpressionTree representing lambda expression * <li>BlockTree representing lambda expression * </ul> * @param from start from * @return nearest enclosing top-level block/expression as defined above. */ public static TreePath findTopLevelBlock(TreePath from) { if (from.getLeaf().getKind() == Tree.Kind.COMPILATION_UNIT) { return null; } TreePath save = null; while (from != null) { Tree.Kind k = from.getParentPath().getLeaf().getKind(); if (k == Kind.METHOD || k == Kind.LAMBDA_EXPRESSION) { return from; } else if (k == Kind.VARIABLE) { save = from; } else if (TreeUtilities.CLASS_TREE_KINDS.contains(k)) { if (save != null) { // variable initializer from the previous iteration return save; } if (from.getLeaf().getKind() == Kind.BLOCK) { // parent is class, from is block -> initializer return from; } return null; } else { save = null; } from = from.getParentPath(); } return null; }
Example 6
Source File: OrigSurroundWithTryCatchFix.java From netbeans with Apache License 2.0 | 5 votes |
private TreePath findStatement(TreePath path) { while (path != null && !StatementTree.class.isAssignableFrom(path.getLeaf().getKind().asInterface()) && (path.getParentPath() == null || path.getParentPath().getLeaf().getKind() != Kind.LAMBDA_EXPRESSION)) { path = path.getParentPath(); } return path; }
Example 7
Source File: Lambda.java From netbeans with Apache License 2.0 | 5 votes |
@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 8
Source File: Lambda.java From netbeans with Apache License 2.0 | 5 votes |
@Override protected void performRewrite(JavaFix.TransformationContext ctx) throws Exception { if (ctx.getPath().getLeaf().getKind() == Kind.LAMBDA_EXPRESSION) { LambdaExpressionTree let = (LambdaExpressionTree) ctx.getPath().getLeaf(); TreeMaker make = ctx.getWorkingCopy().getTreeMaker(); let.getParameters().forEach((var) -> { ctx.getWorkingCopy().rewrite(var.getType(), make.Type("var")); // NOI18N }); } }
Example 9
Source File: MissingReturnStatement.java From netbeans with Apache License 2.0 | 4 votes |
@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 10
Source File: Lambda.java From netbeans with Apache License 2.0 | 4 votes |
@Hint(displayName="#DN_lambda2MemberReference", description="#DESC_lambda2MemberReference", category="suggestions", hintKind=Hint.Kind.ACTION, minSourceVersion = "8") @Messages({ "DN_lambda2MemberReference=Convert Lambda Expression to Member Reference", "DESC_lambda2MemberReference=Converts lambda expressions to member references", "ERR_lambda2MemberReference=Member reference can be used", "FIX_lambda2MemberReference=Use member reference" }) @TriggerTreeKind(Kind.LAMBDA_EXPRESSION) public static ErrorDescription lambda2MemberReference(HintContext ctx) { TypeMirror samType = ctx.getInfo().getTrees().getTypeMirror(ctx.getPath()); if (samType == null || samType.getKind() != TypeKind.DECLARED) { return null; } LambdaExpressionTree lambda = (LambdaExpressionTree) ctx.getPath().getLeaf(); Tree tree = lambda.getBody(); if (tree == null) { return null; } if (tree.getKind() == Tree.Kind.BLOCK) { if (((BlockTree)tree).getStatements().size() == 1) { tree = ((BlockTree)tree).getStatements().get(0); if (tree.getKind() == Tree.Kind.EXPRESSION_STATEMENT) { tree = ((ExpressionStatementTree)tree).getExpression(); } else if (tree.getKind() == Tree.Kind.RETURN) { tree = ((ReturnTree)tree).getExpression(); } else { return null; } if (tree == null) { return null; } } else { return null; } } if (tree.getKind() != Tree.Kind.METHOD_INVOCATION) { return null; } boolean check = true; Iterator<? extends VariableTree> paramsIt = lambda.getParameters().iterator(); ExpressionTree methodSelect = ((MethodInvocationTree)tree).getMethodSelect(); if (paramsIt.hasNext() && methodSelect.getKind() == Tree.Kind.MEMBER_SELECT) { ExpressionTree expr = ((MemberSelectTree) methodSelect).getExpression(); if (expr.getKind() == Tree.Kind.IDENTIFIER) { if (!((IdentifierTree)expr).getName().contentEquals(paramsIt.next().getName())) { paramsIt = lambda.getParameters().iterator(); } } } Iterator<? extends ExpressionTree> argsIt = ((MethodInvocationTree)tree).getArguments().iterator(); while (check && argsIt.hasNext() && paramsIt.hasNext()) { ExpressionTree arg = argsIt.next(); if (arg.getKind() != Tree.Kind.IDENTIFIER || !paramsIt.next().getName().contentEquals(((IdentifierTree)arg).getName())) { check = false; } } if (!check || paramsIt.hasNext() || argsIt.hasNext()) { return null; } return ErrorDescriptionFactory.forTree(ctx, ctx.getPath(), Bundle.ERR_lambda2MemberReference(), new Lambda2MemberReference(ctx.getInfo(), ctx.getPath()).toEditorFix()); }