本文整理汇总了Java中com.sun.source.tree.NewClassTree.getClassBody方法的典型用法代码示例。如果您正苦于以下问题:Java NewClassTree.getClassBody方法的具体用法?Java NewClassTree.getClassBody怎么用?Java NewClassTree.getClassBody使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.source.tree.NewClassTree
的用法示例。
在下文中一共展示了NewClassTree.getClassBody方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: className
import com.sun.source.tree.NewClassTree; //导入方法依赖的package包/类
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();
}
示例2: visitNewClass
import com.sun.source.tree.NewClassTree; //导入方法依赖的package包/类
@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;
}
示例3: generateClassBody
import com.sun.source.tree.NewClassTree; //导入方法依赖的package包/类
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);
}
示例4: matchNewClass
import com.sun.source.tree.NewClassTree; //导入方法依赖的package包/类
@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
Type supertype = state.getTypes().supertype(methodSymbol.owner.type);
Symbol.MethodSymbol superConstructor =
findSuperConstructorInType(methodSymbol, supertype, state.getTypes());
if (superConstructor == null) {
throw new RuntimeException("must find constructor in supertype");
}
methodSymbol = superConstructor;
}
return handleInvocation(state, methodSymbol, actualParams);
}
示例5: visitNewClass
import com.sun.source.tree.NewClassTree; //导入方法依赖的package包/类
@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;
}
示例6: visitEnumConstantDeclaration
import com.sun.source.tree.NewClassTree; //导入方法依赖的package包/类
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);
}
}
示例7: visitNewClass
import com.sun.source.tree.NewClassTree; //导入方法依赖的package包/类
@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;
}
示例8: matches
import com.sun.source.tree.NewClassTree; //导入方法依赖的package包/类
@Override
public boolean matches(Tree tree, VisitorState visitorState) {
if (!(tree instanceof NewClassTree)) {
return false;
}
NewClassTree newClassTree = (NewClassTree) tree;
if (newClassTree.getClassBody() == null) {
return false;
}
return newClassTree
.getClassBody()
.getMembers()
.stream()
.filter(mem -> mem instanceof VariableTree)
.anyMatch(mem -> !isFinal(mem));
}
示例9: after
import com.sun.source.tree.NewClassTree; //导入方法依赖的package包/类
@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");
}
示例10: handlePossibleAnonymousInnerClass
import com.sun.source.tree.NewClassTree; //导入方法依赖的package包/类
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;
}
}
}
示例11: visitNewClass
import com.sun.source.tree.NewClassTree; //导入方法依赖的package包/类
@Override
public Boolean visitNewClass(NewClassTree node, EvaluationContext p) {
if (node.getClassBody() != null) {
return Boolean.FALSE;
}
return null;
}
示例12: visitNewClass
import com.sun.source.tree.NewClassTree; //导入方法依赖的package包/类
@Override
public Choice<State<JCNewClass>> visitNewClass(final NewClassTree node, State<?> state) {
if (node.getEnclosingExpression() != null
|| (node.getTypeArguments() != null && !node.getTypeArguments().isEmpty())
|| node.getClassBody() != null) {
return Choice.none();
}
return chooseSubtrees(
state,
s -> unifyExpression(node.getIdentifier(), s),
s -> unifyExpressions(node.getArguments(), s),
(ident, args) -> maker().NewClass(null, null, ident, args, null));
}
示例13: matchNewClass
import com.sun.source.tree.NewClassTree; //导入方法依赖的package包/类
@Override
public Description matchNewClass(NewClassTree newClassTree, VisitorState state) {
if (state.getPath().getParentPath().getLeaf().getKind() != Kind.EXPRESSION_STATEMENT) {
return Description.NO_MATCH;
}
if (newClassTree.getClassBody() == null) {
return Description.NO_MATCH;
}
if (!newClassTree.getArguments().isEmpty()) {
return Description.NO_MATCH;
}
for (Tree def : newClassTree.getClassBody().getMembers()) {
switch (def.getKind()) {
case VARIABLE:
{
VariableTree variableTree = (VariableTree) def;
if (variableTree.getInitializer() != null) {
return Description.NO_MATCH;
}
break;
}
case BLOCK:
return Description.NO_MATCH;
default:
break;
}
}
if (!sideEffectFreeConstructor(((JCTree) newClassTree.getIdentifier()).type.tsym, state)) {
return Description.NO_MATCH;
}
return describeMatch(newClassTree);
}
示例14: matchNewClass
import com.sun.source.tree.NewClassTree; //导入方法依赖的package包/类
@Override
public Description matchNewClass(NewClassTree tree, VisitorState state) {
ClassTree body = tree.getClassBody();
if (body == null) {
return NO_MATCH;
}
ImmutableList<? extends Tree> members =
body.getMembers()
.stream()
.filter(
m ->
!(m instanceof MethodTree && ASTHelpers.isGeneratedConstructor((MethodTree) m)))
.collect(toImmutableList());
if (members.size() != 1) {
return NO_MATCH;
}
Tree member = Iterables.getOnlyElement(members);
if (!(member instanceof BlockTree)) {
return NO_MATCH;
}
BlockTree block = (BlockTree) member;
Optional<CollectionTypes> collectionType =
Stream.of(CollectionTypes.values())
.filter(type -> type.constructorMatcher.matches(tree, state))
.findFirst();
if (!collectionType.isPresent()) {
return NO_MATCH;
}
Description.Builder description = buildDescription(tree);
collectionType.get().maybeFix(tree, state, block).ifPresent(description::addFix);
return description.build();
}
示例15: getMethodFromFunctionalInterface
import com.sun.source.tree.NewClassTree; //导入方法依赖的package包/类
private MethodTree getMethodFromFunctionalInterface(NewClassTree newClassTree) {
//ignore first member, which is a synthetic constructor call
ClassTree classTree = newClassTree.getClassBody();
return (MethodTree) classTree.getMembers().get(1);
}