本文整理汇总了Java中com.sun.source.tree.NewClassTree类的典型用法代码示例。如果您正苦于以下问题:Java NewClassTree类的具体用法?Java NewClassTree怎么用?Java NewClassTree使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
NewClassTree类属于com.sun.source.tree包,在下文中一共展示了NewClassTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: visitNewClass
import com.sun.source.tree.NewClassTree; //导入依赖的package包/类
public Boolean visitNewClass(NewClassTree node, TreePath p) {
if (p == null)
return super.visitNewClass(node, p);
NewClassTree t = (NewClassTree) p.getLeaf();
if (!scan(node.getIdentifier(), t.getIdentifier(), p))
return false;
if (!scan(node.getEnclosingExpression(), t.getEnclosingExpression(), p))
return false;
if (!checkLists(node.getTypeArguments(), t.getTypeArguments(), p))
return false;
if (!checkLists(node.getArguments(), t.getArguments(), p))
return false;
return scan(node.getClassBody(), t.getClassBody(), p);
}
示例2: testFieldChangeInInitValue
import com.sun.source.tree.NewClassTree; //导入依赖的package包/类
/**
* copy the initialization 'new String("NetBeers")' to 'new String()'.
* It tests only change in expression, no expression swap.
*/
public void testFieldChangeInInitValue() throws IOException {
System.err.println("testFieldChangeInInitValue");
process(
new Transformer<Void, Object>() {
public Void visitVariable(VariableTree node, Object p) {
super.visitVariable(node, p);
if ("initialValueReplacer".contentEquals(node.getName())) {
if (Tree.Kind.NEW_CLASS.equals(node.getInitializer().getKind())) {
NewClassTree nct = (NewClassTree) node.getInitializer();
NewClassTree njuNct = make.NewClass(
nct.getEnclosingExpression(),
(List<ExpressionTree>) nct.getTypeArguments(),
nct.getIdentifier(),
Collections.singletonList(make.Literal("NetBeans")),
nct.getClassBody()
);
copy.rewrite(nct, njuNct);
}
}
return null;
}
}
);
assertFiles("testFieldChangeInInitValue.pass");
}
示例3: testWrapMethod1
import com.sun.source.tree.NewClassTree; //导入依赖的package包/类
public void testWrapMethod1() throws Exception {
String code = "package hierbas.del.litoral;\n\n" +
"import java.util.concurrent.atomic.AtomicBoolean;\n\n" +
"public class Test {\n" +
"}\n";
runWrappingTest(code, new Task<WorkingCopy>() {
public void run(WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
TreeMaker make = workingCopy.getTreeMaker();
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
ExpressionTree parsed = workingCopy.getTreeUtilities().parseExpression("new Object() { private void test(int a, int b, int c) throws java.io.FileNotFound, java.net.MalformedURLException { } }", new SourcePositions[1]);
parsed = GeneratorUtilities.get(workingCopy).importFQNs(parsed);
MethodTree method = (MethodTree) ((NewClassTree) parsed).getClassBody().getMembers().get(0);
workingCopy.rewrite(clazz, make.addClassMember(clazz, method));
}
},
FmtOptions.wrapMethodParams, WrapStyle.WRAP_IF_LONG.name(),
FmtOptions.wrapThrowsKeyword, WrapStyle.WRAP_IF_LONG.name(),
FmtOptions.wrapThrowsList, WrapStyle.WRAP_IF_LONG.name());
}
示例4: testWrapMethod2
import com.sun.source.tree.NewClassTree; //导入依赖的package包/类
public void testWrapMethod2() throws Exception {
String code = "package hierbas.del.litoral;\n\n" +
"import java.util.concurrent.atomic.AtomicBoolean;\n\n" +
"public class Test {\n" +
"}\n";
runWrappingTest(code, new Task<WorkingCopy>() {
public void run(WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
TreeMaker make = workingCopy.getTreeMaker();
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
ExpressionTree parsed = workingCopy.getTreeUtilities().parseExpression("new Object() { private void test(int a, int b, int c) throws java.io.FileNotFoundException, java.net.MalformedURLException { } }", new SourcePositions[1]);
parsed = GeneratorUtilities.get(workingCopy).importFQNs(parsed);
MethodTree method = (MethodTree) ((NewClassTree) parsed).getClassBody().getMembers().get(0);
workingCopy.rewrite(clazz, make.addClassMember(clazz, method));
}
},
FmtOptions.wrapMethodParams, WrapStyle.WRAP_ALWAYS.name(),
FmtOptions.wrapThrowsKeyword, WrapStyle.WRAP_ALWAYS.name(),
FmtOptions.wrapThrowsList, WrapStyle.WRAP_ALWAYS.name());
}
示例5: testWrapMethod3
import com.sun.source.tree.NewClassTree; //导入依赖的package包/类
public void testWrapMethod3() throws Exception {
String code = "package hierbas.del.litoral;\n\n" +
"import java.util.concurrent.atomic.AtomicBoolean;\n\n" +
"public class Test {\n" +
"}\n";
runWrappingTest(code, new Task<WorkingCopy>() {
public void run(WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
TreeMaker make = workingCopy.getTreeMaker();
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
ExpressionTree parsed = workingCopy.getTreeUtilities().parseExpression("new Object() { public <T> void test(java.lang.Class<T> c) { } }", new SourcePositions[1]);
parsed = GeneratorUtilities.get(workingCopy).importFQNs(parsed);
MethodTree method = (MethodTree) ((NewClassTree) parsed).getClassBody().getMembers().get(0);
workingCopy.rewrite(clazz, make.addClassMember(clazz, method));
}
},
FmtOptions.wrapMethodParams, WrapStyle.WRAP_ALWAYS.name(),
FmtOptions.wrapThrowsKeyword, WrapStyle.WRAP_ALWAYS.name(),
FmtOptions.wrapThrowsList, WrapStyle.WRAP_ALWAYS.name());
}
示例6: visitNewClass
import com.sun.source.tree.NewClassTree; //导入依赖的package包/类
@Override
public Void visitNewClass(NewClassTree node, Stack<Tree> p) {
if (instantRename) {
return super.visitNewClass(node, p);
}
Element el = info.getTrees().getElement(getCurrentPath());
if (toFind.equals(el) && node.getIdentifier() != null) {
Token<JavaTokenId> t = Utilities.getToken(info, doc, new TreePath(getCurrentPath(), node.getIdentifier()));
if (t != null)
usages.add(t);
return null;
}
if (el != null && toFind.equals(el.getEnclosingElement())) {
return null;
}
return super.visitNewClass(node, p);
}
示例7: anonymousTooManyMethods
import com.sun.source.tree.NewClassTree; //导入依赖的package包/类
@Hint(
displayName = "#DN_AnonClassMethodCount",
description = "#DESC_AnonClassMethodCount",
category = "metrics",
options = { Hint.Options.HEAVY, Hint.Options.QUERY },
enabled = false
)
@TriggerPatterns({
@TriggerPattern("new $classname<$tparams$>($params$) { $members$; }"),
@TriggerPattern("$expr.new $classname<$tparams$>($params$) { $members$; }"),
@TriggerPattern("new $classname($params$) { $members$; }"),
@TriggerPattern("$expr.new $classname($params$) { $members$; }"),
})
@UseOptions({ OPTION_ANON_CLASS_METHODS_LIMIT})
public static ErrorDescription anonymousTooManyMethods(HintContext ctx) {
NewClassTree nct = (NewClassTree)ctx.getPath().getLeaf();
return checkTooManyMethods(ctx,
new TreePath(ctx.getPath(), nct.getClassBody()),
ctx.getPreferences().getInt(OPTION_ANON_CLASS_METHODS_LIMIT, DEFAULT_ANON_CLASS_METHODS_LIMIT),
true);
}
示例8: 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();
}
示例9: ConvertToLambdaPreconditionChecker
import com.sun.source.tree.NewClassTree; //导入依赖的package包/类
public ConvertToLambdaPreconditionChecker(TreePath pathToNewClassTree, CompilationInfo info) {
this.pathToNewClassTree = pathToNewClassTree;
this.newClassTree = (NewClassTree) pathToNewClassTree.getLeaf();
this.info = info;
this.types = info.getTypes();
Element el = info.getTrees().getElement(pathToNewClassTree);
if (el != null && el.getKind() == ElementKind.CONSTRUCTOR) {
createdClass = el.getEnclosingElement();
} else {
createdClass = null;
}
this.lambdaMethodTree = getMethodFromFunctionalInterface(this.pathToNewClassTree);
this.localScope = getScopeFromTree(this.pathToNewClassTree);
this.ownerClass = findFieldOwner();
}
示例10: 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;
}
示例11: performRewrite
import com.sun.source.tree.NewClassTree; //导入依赖的package包/类
@Override
protected void performRewrite(TransformationContext ctx) {
WorkingCopy copy = ctx.getWorkingCopy();
TreePath tp = ctx.getPath();
if (tp.getLeaf().getKind() != Tree.Kind.NEW_CLASS) {
//XXX: warning
return ;
}
NewClassTree nct = (NewClassTree) tp.getLeaf();
if (nct.getIdentifier().getKind() != Tree.Kind.PARAMETERIZED_TYPE) {
//XXX: warning
return ;
}
TreeMaker make = copy.getTreeMaker();
ParameterizedTypeTree ptt = (ParameterizedTypeTree) nct.getIdentifier();
ParameterizedTypeTree nue = make.ParameterizedType(ptt.getType(), Collections.<Tree>emptyList());
copy.rewrite(ptt, nue);
}
示例12: computeAnnonymousToLambda
import com.sun.source.tree.NewClassTree; //导入依赖的package包/类
@TriggerPatterns({
@TriggerPattern("new $clazz($params$) { $method; }") //NOI18N
})
@NbBundle.Messages("MSG_AnonymousConvertibleToLambda=This anonymous inner class creation can be turned into a lambda expression.")
public static ErrorDescription computeAnnonymousToLambda(HintContext ctx) {
ClassTree clazz = ((NewClassTree) ctx.getPath().getLeaf()).getClassBody();
ConvertToLambdaPreconditionChecker preconditionChecker =
new ConvertToLambdaPreconditionChecker(ctx.getPath(), ctx.getInfo());
if (!preconditionChecker.passesFatalPreconditions()) {
return null;
}
FixImpl fix = new FixImpl(ctx.getInfo(), ctx.getPath(), false);
if (ctx.getPreferences().getBoolean(KEY_PREFER_MEMBER_REFERENCES, DEF_PREFER_MEMBER_REFERENCES)
&& (preconditionChecker.foundMemberReferenceCandidate() || preconditionChecker.foundConstructorReferenceCandidate())) {
return ErrorDescriptionFactory.forTree(ctx, ((NewClassTree) ctx.getPath().getLeaf()).getIdentifier(), Bundle.MSG_AnonymousConvertibleToLambda(),
new FixImpl(ctx.getInfo(), ctx.getPath(), true).toEditorFix(), fix.toEditorFix());
}
return ErrorDescriptionFactory.forTree(ctx, ((NewClassTree) ctx.getPath().getLeaf()).getIdentifier(),
Bundle.MSG_AnonymousConvertibleToLambda(), fix.toEditorFix());
}
示例13: performRewrite
import com.sun.source.tree.NewClassTree; //导入依赖的package包/类
@Override
protected void performRewrite(TransformationContext ctx) throws Exception {
TreePath p = ctx.getPath();
if (p.getLeaf().getKind() != Tree.Kind.NEW_CLASS) {
return;
}
NewClassTree origNct = (NewClassTree)p.getLeaf();
if (origNct.getArguments().size() != 1) {
return;
}
NewClassTree nct = GeneratorUtilities.get(ctx.getWorkingCopy()).importComments(origNct, ctx.getWorkingCopy().getCompilationUnit());
ExpressionTree charArg = nct.getArguments().get(0);
TreeMaker mk = ctx.getWorkingCopy().getTreeMaker();
ExpressionTree newExpr = mk.NewClass(nct.getEnclosingExpression(), (List<ExpressionTree>)nct.getTypeArguments(), nct.getIdentifier(),
Collections.<ExpressionTree>emptyList(), nct.getClassBody());
Tree replace = mk.MethodInvocation(
Collections.<ExpressionTree>emptyList(),
mk.MemberSelect(newExpr, "append"), // NOI18N
Collections.singletonList(charArg));
ctx.getWorkingCopy().rewrite(nct, replace);
}
示例14: visitNewClass
import com.sun.source.tree.NewClassTree; //导入依赖的package包/类
@Override
public Boolean visitNewClass(NewClassTree node, Void p) {
TypeMirror tm = ci.getTrees().getTypeMirror(getCurrentPath());
if (tm == null || tm.getKind() != TypeKind.DECLARED) {
return false;
}
TypeElement el = (TypeElement)((DeclaredType)tm).asElement();
if (el == null) {
return false;
}
Name n = el.getQualifiedName();
boolean res = n.contentEquals("java.lang.StringBuilder") || n.contentEquals("java.lang.StringBuffer"); // NOI18N
// check if there is some initial contents
if (node.getArguments().size() == 1 &&
Utilities.isJavaString(ci, ci.getTrees().getTypeMirror(new TreePath(getCurrentPath(), node.getArguments().get(0))))) {
hasContents = true;
}
return res;
}
示例15: rewriteNewClass
import com.sun.source.tree.NewClassTree; //导入依赖的package包/类
private ExpressionTree rewriteNewClass(TreePath p) {
ExpressionTree expr = (ExpressionTree) p.getLeaf();
NewClassTree nct = (NewClassTree) expr;
Element el = wc.getTrees().getElement(p);
if (el != null && el.getKind() == ElementKind.CONSTRUCTOR) {
ExecutableElement ee = (ExecutableElement) el;
if (ee.getParameters().isEmpty()) {
// ctor without parameters, remove
return null;
}
TypeMirror argType = ee.getParameters().get(0).asType();
if (argType.getKind() == TypeKind.DECLARED) {
ExpressionTree a = nct.getArguments().get(0);
gu.copyComments(expr, a, true);
gu.copyComments(expr, a, false);
wc.rewrite(expr, a);
return a;
}
return null;
}
return expr;
}