当前位置: 首页>>代码示例>>Java>>正文


Java Trees.instance方法代码示例

本文整理汇总了Java中com.sun.source.util.Trees.instance方法的典型用法代码示例。如果您正苦于以下问题:Java Trees.instance方法的具体用法?Java Trees.instance怎么用?Java Trees.instance使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.sun.source.util.Trees的用法示例。


在下文中一共展示了Trees.instance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import com.sun.source.util.Trees; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject()));
    Iterable<? extends CompilationUnitTree> asts = task.parse();
    task.analyze();
    trees = Trees.instance(task);
    MyVisitor myVisitor = new MyVisitor();
    for (CompilationUnitTree ast : asts) {
        myVisitor.compilationUnit = ast;
        myVisitor.scan(ast, null);
    }

    if (!myVisitor.foundError) {
        throw new AssertionError("Expected error not found!");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:T6557752.java

示例2: getLocation

import com.sun.source.util.Trees; //导入方法依赖的package包/类
private Location getLocation(final String name, final TreePath treePath) {
    return new Location() {
        public String toString() {
            if (treePath == null)
                return name + " (Unknown Source)";
            // just like stack trace, we just print the file name and
            // not the whole path. The idea is that the package name should
            // provide enough clue on which directory it lives.
            CompilationUnitTree compilationUnit = treePath.getCompilationUnit();
            Trees trees = Trees.instance(env);
            long startPosition = trees.getSourcePositions().getStartPosition(compilationUnit, treePath.getLeaf());
            return name + "(" +
                    compilationUnit.getSourceFile().getName() + ":" + compilationUnit.getLineMap().getLineNumber(startPosition) +
                    ")";
        }
    };
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:18,代码来源:ApNavigator.java

示例3: process

import com.sun.source.util.Trees; //导入方法依赖的package包/类
public boolean process(Set<? extends TypeElement> annotations,
                       RoundEnvironment roundEnv) {
    new TestPathScanner<Void>() {
        @Override
        public void visit(Void t) {
        }
    };
    TypeElement currentClass = elements.getTypeElement("TypesCachesCleared");
    Trees trees = Trees.instance(processingEnv);
    TreePath path = trees.getPath(currentClass);
    new TreePathScanner<Void, Void>() {
        @Override public Void visitClass(ClassTree node, Void p) {
            trees.getElement(getCurrentPath());
            return super.visitClass(node, p);
        }
    }.scan(path, null);
    return false;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:19,代码来源:TypesCachesCleared.java

示例4: process

import com.sun.source.util.Trees; //导入方法依赖的package包/类
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    Trees trees = Trees.instance(processingEnv);
    Elements elements = processingEnv.getElementUtils();
    Element pack = elements.getTypeElement("Test").getEnclosingElement();
    for (Element root : pack.getEnclosedElements()) {
        TreePath tp = trees.getPath(root);
        new OwnerCheck(trees, pack).scan(tp.getCompilationUnit(), null);

    }
    return false;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:13,代码来源:LocalInAnonymous.java

示例5: init

import com.sun.source.util.Trees; //导入方法依赖的package包/类
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
    super.init(processingEnv);
    this.trees = Trees.instance(processingEnv);
    this.messager = processingEnv.getMessager();
    this.filer = processingEnv.getFiler();
    Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
    daoGenHelper = new DaoGenHelper(trees,context);
}
 
开发者ID:frankelau,项目名称:pndao,代码行数:10,代码来源:DaoGenProcessor.java

示例6: process

import com.sun.source.util.Trees; //导入方法依赖的package包/类
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    Trees trees = Trees.instance(processingEnv);
    if (round++ == 0) {
        for (Element e: roundEnv.getRootElements()) {
            TreePath p = trees.getPath(e);
            new Scanner().scan(p, trees);
        }
    }
    return false;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:12,代码来源:TestGetScope.java

示例7: testPositionBrokenSource126732b

import com.sun.source.util.Trees; //导入方法依赖的package包/类
void testPositionBrokenSource126732b() throws IOException {
    String[] commands = new String[]{
        "break",
        "break A",
        "continue ",
        "continue A",};

    for (String command : commands) {

        String code = "package test;\n"
                + "public class Test {\n"
                + "    public static void test() {\n"
                + "        while (true) {\n"
                + "            " + command + " {\n"
                + "                new Runnable() {\n"
                + "        };\n"
                + "        }\n"
                + "    }\n"
                + "}";

        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null,
                null, null, Arrays.asList(new MyFileObject(code)));
        CompilationUnitTree cut = ct.parse().iterator().next();

        ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
        MethodTree method = (MethodTree) clazz.getMembers().get(0);
        List<? extends StatementTree> statements =
                ((BlockTree) ((WhileLoopTree) method.getBody().getStatements().get(0)).getStatement()).getStatements();

        StatementTree ret = statements.get(0);
        StatementTree block = statements.get(1);

        Trees t = Trees.instance(ct);
        int len = code.indexOf(command + " {") + (command + " ").length();
        assertEquals(command, len,
                t.getSourcePositions().getEndPosition(cut, ret));
        assertEquals(command, len,
                t.getSourcePositions().getStartPosition(cut, block));
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:41,代码来源:JavacParserTest.java

示例8: process

import com.sun.source.util.Trees; //导入方法依赖的package包/类
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    if (roundEnv.processingOver())
        return true;

    Trees trees = Trees.instance(processingEnv);

    TypeElement testAnno = elements.getTypeElement("Check");
    for (Element elem: roundEnv.getElementsAnnotatedWith(testAnno)) {
        TreePath p = trees.getPath(elem);
        new IntersectionCastTester(trees).scan(p, null);
    }
    return true;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:15,代码来源:ModelChecker.java

示例9: testStartPositionEnumConstantInit

import com.sun.source.util.Trees; //导入方法依赖的package包/类
void testStartPositionEnumConstantInit() throws IOException {

        String code = "package t; enum Test { AAA; }";

        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
                null, Arrays.asList(new MyFileObject(code)));
        CompilationUnitTree cut = ct.parse().iterator().next();
        ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
        VariableTree enumAAA = (VariableTree) clazz.getMembers().get(0);
        Trees t = Trees.instance(ct);
        int start = (int) t.getSourcePositions().getStartPosition(cut,
                enumAAA.getInitializer());

        assertEquals("testStartPositionEnumConstantInit", -1, start);
    }
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:16,代码来源:JavacParserTest.java

示例10: verifyLambdaScopeCorrect

import com.sun.source.util.Trees; //导入方法依赖的package包/类
private static void verifyLambdaScopeCorrect(final String packageClause) throws Exception {
    JavacTool tool = JavacTool.create();
    JavaFileObject source = new SimpleJavaFileObject(URI.create("mem://Test.java"), Kind.SOURCE) {
        @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
            return packageClause + SOURCE_CODE;
        }
        @Override public boolean isNameCompatible(String simpleName, Kind kind) {
            return !"module-info".equals(simpleName);
        }
    };
    Iterable<? extends JavaFileObject> fos = Collections.singletonList(source);
    JavacTask task = tool.getTask(null, null, null, new ArrayList<String>(), null, fos);
    final Types types = JavacTypes.instance(((JavacTaskImpl) task).getContext());
    final Trees trees = Trees.instance(task);
    CompilationUnitTree cu = task.parse().iterator().next();

    task.analyze();

    new TreePathScanner<Void, Void>() {
        @Override public Void visitMemberSelect(MemberSelectTree node, Void p) {
            if (node.getIdentifier().contentEquals("correct")) {
                TypeMirror xType = trees.getTypeMirror(new TreePath(getCurrentPath(), node.getExpression()));
                Scope scope = trees.getScope(getCurrentPath());
                for (Element l : scope.getLocalElements()) {
                    if (!l.getSimpleName().contentEquals("x")) continue;
                    if (!types.isSameType(xType, l.asType())) {
                        throw new IllegalStateException("Incorrect variable type in scope: " + l.asType() + "; should be: " + xType);
                    }
                }
            }
            return super.visitMemberSelect(node, p);
        }
    }.scan(cu, null);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:35,代码来源:ScopeTest.java

示例11: init

import com.sun.source.util.Trees; //导入方法依赖的package包/类
@DefinedBy(Api.COMPILER_TREE)
public void init(JavacTask task, String... args) {
    BasicJavacTask impl = (BasicJavacTask)task;
    Context context = impl.getContext();
    log = Log.instance(context);
    trees = Trees.instance(task);
    task.addTaskListener(new PostAnalyzeTaskListener(
            new MutableFieldsAnalyzer(task),
            new AssertCheckAnalyzer(task),
            new DefinedByAnalyzer(task),
            new LegacyLogMethodAnalyzer(task)
    ));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:CodingRulesAnalyzerPlugin.java

示例12: process

import com.sun.source.util.Trees; //导入方法依赖的package包/类
public boolean process(Set<? extends TypeElement> annotations,
                       RoundEnvironment roundEnvironment)
{
    final Trees trees = Trees.instance(processingEnv);
    for (TypeElement e : typesIn(roundEnvironment.getRootElements())) {
        ClassTree node = trees.getTree(e);
        System.out.println(node.toString());
    }
    return true;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:11,代码来源:TestGetTree.java

示例13: performWildcardPositionsTest

import com.sun.source.util.Trees; //导入方法依赖的package包/类
void performWildcardPositionsTest(final String code,
        List<String> golden) throws IOException {

    final List<Diagnostic<? extends JavaFileObject>> errors =
            new LinkedList<Diagnostic<? extends JavaFileObject>>();

    JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm,
            new DiagnosticListener<JavaFileObject>() {
                public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
                    errors.add(diagnostic);
                }
            }, null, null, Arrays.asList(new MyFileObject(code)));

    final CompilationUnitTree cut = ct.parse().iterator().next();
    final List<String> content = new LinkedList<String>();
    final Trees trees = Trees.instance(ct);

    new TreeScanner<Void, Void>() {
        @Override
        public Void scan(Tree node, Void p) {
            if (node == null) {
                return null;
            }
            long start = trees.getSourcePositions().getStartPosition(cut, node);

            if (start == (-1)) {
                return null; // synthetic tree
            }
            long end = trees.getSourcePositions().getEndPosition(cut, node);
            String s = code.substring((int) start, (int) end);
            content.add(s);

            return super.scan(node, p);
        }
    }.scan(((MethodTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0)).getBody().getStatements().get(0), null);

    assertEquals("performWildcardPositionsTest",golden.toString(),
            content.toString());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:40,代码来源:JavacParserTest.java

示例14: run

import com.sun.source.util.Trees; //导入方法依赖的package包/类
private void run(String... args) throws IOException, URISyntaxException {
    //the first and only parameter must be the name of the file to be analyzed:
    if (args.length != 1) throw new IllegalStateException("Must provide source file!");
    File testSrc = new File(System.getProperty("test.src"));
    File testFile = new File(testSrc, args[0]);
    if (!testFile.canRead()) throw new IllegalStateException("Cannot read the test source");
    try (JavacFileManager fm = JavacTool.create().getStandardFileManager(null, null, null)) {

        //gather spans of the @TA annotations into typeAnnotationSpans:
        JavacTask task = JavacTool.create().getTask(null,
                                                    fm,
                                                    null,
                                                    Collections.<String>emptyList(),
                                                    null,
                                                    fm.getJavaFileObjects(testFile));
        final Trees trees = Trees.instance(task);
        final CompilationUnitTree cut = task.parse().iterator().next();
        final List<int[]> typeAnnotationSpans = new ArrayList<>();

        new TreePathScanner<Void, Void>() {
            @Override
            public Void visitAnnotation(AnnotationTree node, Void p) {
                if (node.getAnnotationType().getKind() == Kind.IDENTIFIER &&
                    ((IdentifierTree) node.getAnnotationType()).getName().contentEquals("TA")) {
                    int start = (int) trees.getSourcePositions().getStartPosition(cut, node);
                    int end = (int) trees.getSourcePositions().getEndPosition(cut, node);
                    typeAnnotationSpans.add(new int[] {start, end});
                }
                return null;
            }
        }.scan(cut, null);

        //sort the spans in the reverse order, to simplify removing them from the source:
        Collections.sort(typeAnnotationSpans, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o2[0] - o1[0];
            }
        });

        //verify the errors are produce correctly:
        String originalSource = cut.getSourceFile().getCharContent(false).toString();

        for (int[] toKeep : typeAnnotationSpans) {
            //prepare updated source code by removing all the annotations except the toKeep one:
            String updated = originalSource;

            for (int[] span : typeAnnotationSpans) {
                if (span == toKeep) continue;

                updated = updated.substring(0, span[0]) + updated.substring(span[1]);
            }

            //parse and verify:
            JavaFileObject updatedFile = new TestFO(cut.getSourceFile().toUri(), updated);
            DiagnosticCollector<JavaFileObject> errors = new DiagnosticCollector<>();
            JavacTask task2 = JavacTool.create().getTask(null,
                                                         fm,
                                                         errors,
                                                         Arrays.asList("-source", "7"),
                                                         null,
                                                         Arrays.asList(updatedFile));
            task2.parse();

            boolean found = false;

            for (Diagnostic<? extends JavaFileObject> d : errors.getDiagnostics()) {
                if (d.getKind() == Diagnostic.Kind.ERROR && EXPECTED_ERRORS.contains(d.getCode())) {
                    if (found) {
                        throw new IllegalStateException("More than one expected error found.");
                    }
                    found = true;
                }
            }

            if (!found)
                throw new IllegalStateException("Did not produce proper errors for: " + updated);
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:81,代码来源:CheckErrorsForSource7.java

示例15: started

import com.sun.source.util.Trees; //导入方法依赖的package包/类
public void started(TaskEvent e) {
    System.err.println("Started: " + e);
    Trees t = Trees.instance(task);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:5,代码来源:T6361619.java


注:本文中的com.sun.source.util.Trees.instance方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。