Java Code Examples for com.sun.source.tree.NewClassTree#getClassBody()
The following examples show how to use
com.sun.source.tree.NewClassTree#getClassBody() .
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: ImplementAllAbstractMethods.java From netbeans with Apache License 2.0 | 6 votes |
protected boolean generateClassBody(TreePath p) throws Exception { Element e = copy.getTrees().getElement(p); boolean isUsableElement = e != null && (e.getKind().isClass() || e.getKind().isInterface()); if (isUsableElement) { return true; } if (e.getKind() == ElementKind.ENUM_CONSTANT) { VariableTree var = (VariableTree) p.getLeaf(); if (var.getInitializer() != null && var.getInitializer().getKind() == Kind.NEW_CLASS) { NewClassTree nct = (NewClassTree) var.getInitializer(); if (nct.getClassBody() != null) { return true; } } } return !generateClassBody2(copy, p); }
Example 2
Source File: JavaInputAstVisitor.java From google-java-format with Apache License 2.0 | 6 votes |
private void visitEnumConstantDeclaration(VariableTree enumConstant) { for (AnnotationTree annotation : enumConstant.getModifiers().getAnnotations()) { scan(annotation, null); builder.forcedBreak(); } visit(enumConstant.getName()); NewClassTree init = ((NewClassTree) enumConstant.getInitializer()); if (init.getArguments().isEmpty()) { builder.guessToken("("); builder.guessToken(")"); } else { addArguments(init.getArguments(), plusFour); } if (init.getClassBody() != null) { addBodyDeclarations( init.getClassBody().getMembers(), BracesOrNot.YES, FirstDeclarationsOrNot.YES); } }
Example 3
Source File: JavaInputAstVisitor.java From google-java-format with Apache License 2.0 | 6 votes |
@Override public Void visitNewClass(NewClassTree node, Void unused) { sync(node); builder.open(ZERO); if (node.getEnclosingExpression() != null) { scan(node.getEnclosingExpression(), null); builder.breakOp(); token("."); } token("new"); builder.space(); addTypeArguments(node.getTypeArguments(), plusFour); if (node.getClassBody() != null) { builder.addAll( visitModifiers( node.getClassBody().getModifiers(), Direction.HORIZONTAL, Optional.empty())); } scan(node.getIdentifier(), null); addArguments(node.getArguments(), plusFour); builder.close(); if (node.getClassBody() != null) { addBodyDeclarations( node.getClassBody().getMembers(), BracesOrNot.YES, FirstDeclarationsOrNot.YES); } return null; }
Example 4
Source File: AnonymousClassScanner.java From annotation-tools with MIT License | 6 votes |
@Override public Void visitNewClass(NewClassTree node, Integer level) { // if (level < 2) { if (!found && anonclass.getKind() == Tree.Kind.NEW_CLASS) { if (anonclass == node) { found = true; } else if (node.getClassBody() != null) { // Need to make sure you actually are creating anonymous inner class, // not just object creation. index++; } else { return null; } } super.visitNewClass(node, level + 1); // } return null; }
Example 5
Source File: JavaInputAstVisitor.java From javaide with GNU General Public License v3.0 | 6 votes |
private void visitEnumConstantDeclaration(VariableTree enumConstant) { for (AnnotationTree annotation : enumConstant.getModifiers().getAnnotations()) { scan(annotation, null); builder.forcedBreak(); } visit(enumConstant.getName()); NewClassTree init = ((NewClassTree) enumConstant.getInitializer()); if (init.getArguments().isEmpty()) { builder.guessToken("("); builder.guessToken(")"); } else { addArguments(init.getArguments(), plusFour); } if (init.getClassBody() != null) { addBodyDeclarations( init.getClassBody().getMembers(), BracesOrNot.YES, FirstDeclarationsOrNot.YES); } }
Example 6
Source File: JavaInputAstVisitor.java From javaide with GNU General Public License v3.0 | 6 votes |
@Override public Void visitNewClass(NewClassTree node, Void unused) { sync(node); builder.open(ZERO); if (node.getEnclosingExpression() != null) { scan(node.getEnclosingExpression(), null); builder.breakOp(); token("."); } token("new"); builder.space(); addTypeArguments(node.getTypeArguments(), plusFour); if (node.getClassBody() != null) { List<Op> ops = visitModifiers(node.getClassBody().getModifiers(), Direction.HORIZONTAL, Optional.<BreakTag>absent()); builder.addAll(ops); } scan(node.getIdentifier(), null); addArguments(node.getArguments(), plusFour); builder.close(); if (node.getClassBody() != null) { addBodyDeclarations( node.getClassBody().getMembers(), BracesOrNot.YES, FirstDeclarationsOrNot.YES); } return null; }
Example 7
Source File: NullAway.java From NullAway with MIT License | 6 votes |
@Override public Description matchNewClass(NewClassTree tree, VisitorState state) { if (!matchWithinClass) { return Description.NO_MATCH; } Symbol.MethodSymbol methodSymbol = ASTHelpers.getSymbol(tree); if (methodSymbol == null) { throw new RuntimeException("not expecting unresolved method here"); } List<? extends ExpressionTree> actualParams = tree.getArguments(); if (tree.getClassBody() != null && actualParams.size() > 0) { // passing parameters to constructor of anonymous class // this constructor just invokes the constructor of the superclass, and // in the AST does not have the parameter nullability annotations from the superclass. // so, treat as if the superclass constructor is being invoked directly // see https://github.com/uber/NullAway/issues/102 methodSymbol = getSymbolOfSuperConstructor(methodSymbol, state); } return handleInvocation(tree, state, methodSymbol, actualParams); }
Example 8
Source File: JavaInputAstVisitor.java From java-n-IDE-for-Android with Apache License 2.0 | 6 votes |
@Override public Void visitNewClass(NewClassTree node, Void unused) { sync(node); builder.open(ZERO); if (node.getEnclosingExpression() != null) { scan(node.getEnclosingExpression(), null); builder.breakOp(); token("."); } token("new"); builder.space(); addTypeArguments(node.getTypeArguments(), plusFour); if (node.getClassBody() != null) { List<Op> ops = visitModifiers(node.getClassBody().getModifiers(), Direction.HORIZONTAL, Optional.<BreakTag>absent()); builder.addAll(ops); } scan(node.getIdentifier(), null); addArguments(node.getArguments(), plusFour); builder.close(); if (node.getClassBody() != null) { addBodyDeclarations( node.getClassBody().getMembers(), BracesOrNot.YES, FirstDeclarationsOrNot.YES); } return null; }
Example 9
Source File: ConvertToLambdaPreconditionChecker.java From netbeans with Apache License 2.0 | 6 votes |
@Override public Tree visitNewClass(NewClassTree node, Trees p) { Tree t = super.visitNewClass(node, p); // new class tree > expression statement tree > block. Does not accept anonymous classes for ctor references. if (node.getClassBody() == null && singleStatementLambdaMethodBody == getCurrentPath().getParentPath().getParentPath().getLeaf()) { Tree parent = getCurrentPath().getParentPath().getLeaf(); Element el = info.getTrees().getElement(getCurrentPath()); if (el == null || el.getKind() != ElementKind.CONSTRUCTOR || !el.getEnclosingElement().getKind().isClass()) { return t; } el = el.getEnclosingElement(); if (parent.getKind() == Tree.Kind.EXPRESSION_STATEMENT || parent.getKind() == Tree.Kind.RETURN) { ExpressionTree et = node.getEnclosingExpression(); if (et != null) { if (el.getModifiers().contains(Modifier.STATIC) || !isMeaninglessQualifier(new TreePath(getCurrentPath().getParentPath(), et))) { return t; } } foundConstructorReferenceCandidate = true; } } return t; }
Example 10
Source File: BreadCrumbsNodeImpl.java From netbeans with Apache License 2.0 | 6 votes |
private static String className(TreePath path) { ClassTree ct = (ClassTree) path.getLeaf(); if (path.getParentPath().getLeaf().getKind() == Kind.NEW_CLASS) { NewClassTree nct = (NewClassTree) path.getParentPath().getLeaf(); if (nct.getClassBody() == ct) { return simpleName(nct.getIdentifier()); } } else if (path.getParentPath().getLeaf() == path.getCompilationUnit()) { ExpressionTree pkg = path.getCompilationUnit().getPackageName(); String pkgName = pkg != null ? pkg.toString() : null; if (pkgName != null && !pkgName.contentEquals(ERR_NAME)) { return pkgName + '.' + ct.getSimpleName().toString(); } } return ct.getSimpleName().toString(); }
Example 11
Source File: JavaInputAstVisitor.java From java-n-IDE-for-Android with Apache License 2.0 | 6 votes |
private void visitEnumConstantDeclaration(VariableTree enumConstant) { for (AnnotationTree annotation : enumConstant.getModifiers().getAnnotations()) { scan(annotation, null); builder.forcedBreak(); } visit(enumConstant.getName()); NewClassTree init = ((NewClassTree) enumConstant.getInitializer()); if (init.getArguments().isEmpty()) { builder.guessToken("("); builder.guessToken(")"); } else { addArguments(init.getArguments(), plusFour); } if (init.getClassBody() != null) { addBodyDeclarations( init.getClassBody().getMembers(), BracesOrNot.YES, FirstDeclarationsOrNot.YES); } }
Example 12
Source File: Unbalanced.java From netbeans with Apache License 2.0 | 5 votes |
@TriggerPattern(value="$mods$ $type $name = $init$;") public static ErrorDescription after(HintContext ctx) { if (testElement(ctx) == null) return null; TreePath init = ctx.getVariables().get("$init$"); if (init != null) { if (init.getLeaf().getKind() != Kind.NEW_CLASS) return null; NewClassTree nct = (NewClassTree) init.getLeaf(); if (nct.getClassBody() != null || nct.getArguments().size() > 1) return null; if (nct.getArguments().size() == 1) { TypeMirror tm = ctx.getInfo().getTrees().getTypeMirror(new TreePath(init, nct.getArguments().get(0))); if (tm == null || tm.getKind() != TypeKind.INT) return null; } } if ( ctx.getPath().getParentPath().getLeaf().getKind() == Kind.ENHANCED_FOR_LOOP && ((EnhancedForLoopTree) ctx.getPath().getParentPath().getLeaf()).getVariable() == ctx.getPath().getLeaf()) { return null; } return produceWarning(ctx, "ERR_UnbalancedCollection"); }
Example 13
Source File: GoToSupport.java From netbeans with Apache License 2.0 | 5 votes |
private static Element handlePossibleAnonymousInnerClass(CompilationInfo info, final Element el) { Element encl = el.getEnclosingElement(); Element doubleEncl = encl != null ? encl.getEnclosingElement() : null; if ( doubleEncl != null && !doubleEncl.getKind().isClass() && !doubleEncl.getKind().isInterface() && doubleEncl.getKind() != ElementKind.PACKAGE && encl.getKind() == ElementKind.CLASS) { TreePath enclTreePath = info.getTrees().getPath(encl); Tree enclTree = enclTreePath != null ? enclTreePath.getLeaf() : null; if (enclTree != null && TreeUtilities.CLASS_TREE_KINDS.contains(enclTree.getKind()) && enclTreePath.getParentPath().getLeaf().getKind() == Tree.Kind.NEW_CLASS) { NewClassTree nct = (NewClassTree) enclTreePath.getParentPath().getLeaf(); if (nct.getClassBody() != null) { Element parentElement = info.getTrees().getElement(new TreePath(enclTreePath, nct.getIdentifier())); if (parentElement == null || parentElement.getKind().isInterface()) { return parentElement; } else { //annonymous innerclass extending a class. Find out which constructor is used: TreePath superConstructorCall = new FindSuperConstructorCall().scan(enclTreePath, null); if (superConstructorCall != null) { return info.getTrees().getElement(superConstructorCall); } } } } return null;//prevent jumps to incorrect positions } else { if (encl != null) { return encl; } else { return el; } } }
Example 14
Source File: CanInterpretVisitor.java From netbeans with Apache License 2.0 | 5 votes |
@Override public Boolean visitNewClass(NewClassTree node, EvaluationContext p) { if (node.getClassBody() != null) { return Boolean.FALSE; } return null; }
Example 15
Source File: TreeUtilitiesTest.java From netbeans with Apache License 2.0 | 5 votes |
public void testIsSyntheticNewClassImplements() throws Exception { prepareTest("Test", "package test; import java.io.*; public class Test { void t() { new Serializable() { }; } }"); TreePath tp = info.getTreeUtilities().pathFor(65); NewClassTree nct = (NewClassTree) tp.getLeaf(); TreePath toTest = new TreePath(new TreePath(tp, nct.getClassBody()), nct.getClassBody().getImplementsClause().get(0)); assertTrue(info.getTreeUtilities().isSynthetic(toTest)); }
Example 16
Source File: ConvertToVarHint.java From netbeans with Apache License 2.0 | 4 votes |
private static boolean isValidVarType(HintContext ctx) { TreePath treePath = ctx.getPath(); TreePath initTreePath = ctx.getVariables().get("$init"); //NOI18N TreePath expressionTreePath = ctx.getVariables().get("$expression"); //NOI18N TreePath typeTreePath = ctx.getVariables().get("$type"); //NOI18N if (initTreePath != null) { Tree.Kind kind = initTreePath.getLeaf().getKind(); switch (kind) { case NEW_CLASS: NewClassTree nct = (NewClassTree) (initTreePath.getLeaf()); //anonymous class type if (nct.getClassBody() != null) { return false; } break; case NEW_ARRAY: NewArrayTree nat = (NewArrayTree) ((VariableTree) treePath.getLeaf()).getInitializer(); //array initializer expr type if (nat.getType() == null) { return false; } break; case LAMBDA_EXPRESSION: return false; default: break; } // variable initializer type should be same as variable type. TypeMirror initTypeMirror = ctx.getInfo().getTrees().getTypeMirror(initTreePath); TypeMirror variableTypeMirror = ctx.getInfo().getTrees().getElement(treePath).asType(); if ((!Utilities.isValidType(initTypeMirror)) || (!ctx.getInfo().getTypes().isSameType(variableTypeMirror, Utilities.resolveCapturedType(ctx.getInfo(), initTypeMirror)))) { return false; } return true; } else if (expressionTreePath != null) { ExecutableElement iterator = ExpandEnhancedForLoop.findIterable(ctx.getInfo()); TypeMirror expTypeMirror = ctx.getInfo().getTrees().getTypeMirror(expressionTreePath); TypeMirror typeTypeMirror = ctx.getInfo().getTrees().getTypeMirror(typeTreePath); if (expTypeMirror.getKind() == TypeKind.DECLARED) { DeclaredType dt = (DeclaredType) expTypeMirror; if (dt.getTypeArguments().size() > 0) { TypeMirror paramType = dt.getTypeArguments().get(0); if ((!Utilities.isValidType(typeTypeMirror)) || (!ctx.getInfo().getTypes().isSameType(typeTypeMirror, paramType))) { return false; } } } else { ArrayType arrayTypeExp = (ArrayType) Utilities.resolveCapturedType(ctx.getInfo(), expTypeMirror); Type arrayTypeExpType = arrayTypeExp.getComponentType(); if ((!Utilities.isValidType(typeTypeMirror)) || (!ctx.getInfo().getTypes().isSameType(typeTypeMirror, arrayTypeExpType))) { return false; } } return (iterator != null); } else { return false; } }
Example 17
Source File: ConvertToLambdaConverter.java From netbeans with Apache License 2.0 | 4 votes |
private MethodTree getMethodFromFunctionalInterface(NewClassTree newClassTree) { //ignore first member, which is a synthetic constructor call ClassTree classTree = newClassTree.getClassBody(); return (MethodTree) classTree.getMembers().get(1); }