下面列出了com.sun.source.tree.NewClassTree#getClassBody ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
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);
}
}
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();
}
@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;
}
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);
}
@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);
}
@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;
}
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);
}
}
@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;
}
@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;
}
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);
}
}
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));
}
@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");
}
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;
}
}
}
@Override
public Boolean visitNewClass(NewClassTree node, EvaluationContext p) {
if (node.getClassBody() != null) {
return Boolean.FALSE;
}
return null;
}
private MethodTree getMethodFromFunctionalInterface(NewClassTree newClassTree) {
//ignore first member, which is a synthetic constructor call
ClassTree classTree = newClassTree.getClassBody();
return (MethodTree) classTree.getMembers().get(1);
}
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;
}
}