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


Java JavacTask.analyze方法代码示例

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


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

示例1: run

import com.sun.source.util.JavacTask; //导入方法依赖的package包/类
void run() throws Exception {
    Context context = new Context();
    JavacFileManager.preRegister(context);
    final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, Arrays.asList(new JavaSource()));
    Iterable<? extends CompilationUnitTree> elements = ct.parse();
    ct.analyze();
    Assert.check(elements.iterator().hasNext());
    JCTree topLevel = (JCTree)elements.iterator().next();
    new TreeScanner() {
        @Override
        public void visitReference(JCMemberReference tree) {
            Assert.check(tree.getOverloadKind() != null);
        }
    }.scan(topLevel);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:FieldOverloadKindNotAssignedTest.java

示例2: testTreePathForModuleDecl

import com.sun.source.util.JavacTask; //导入方法依赖的package包/类
@Test
public void testTreePathForModuleDecl(Path base) throws Exception {

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {
        Path src = base.resolve("src");
        tb.writeJavaFiles(src, "/** Test module */ module m1x {}");

        Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(findJavaFiles(src));
        JavacTask task = (JavacTask) compiler.getTask(null, fm, null, null, null, files);

        task.analyze();
        JavacTrees trees = JavacTrees.instance(task);
        ModuleElement mdle = (ModuleElement) task.getElements().getModuleElement("m1x");

        TreePath path = trees.getPath(mdle);
        assertNotNull("path", path);

        ModuleElement mdle1 = (ModuleElement) trees.getElement(path);
        assertNotNull("mdle1", mdle1);

        DocCommentTree docCommentTree = trees.getDocCommentTree(mdle);
        assertNotNull("docCommentTree", docCommentTree);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:26,代码来源:ModuleInfoTreeAccess.java

示例3: run

import com.sun.source.util.JavacTask; //导入方法依赖的package包/类
void run() throws Exception {
    Context context = new Context();
    JavacFileManager.preRegister(context);
    Trees trees = JavacTrees.instance(context);
    StringWriter strOut = new StringWriter();
    PrintWriter pw = new PrintWriter(strOut);
    DPrinter dprinter = new DPrinter(pw, trees);
    final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, Arrays.asList(new JavaSource()));
    Iterable<? extends CompilationUnitTree> elements = ct.parse();
    ct.analyze();
    Assert.check(elements.iterator().hasNext());
    dprinter.treeTypes(true).printTree("", (JCTree)elements.iterator().next());
    String output = strOut.toString();
    Assert.check(!output.contains("java.lang.Object"), "there shouldn't be any type instantiated to Object");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:InferenceRegressionTest02.java

示例4: run

import com.sun.source.util.JavacTask; //导入方法依赖的package包/类
void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
    JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
            null, null, Arrays.asList(source));
    try {
        ct.analyze();
    } catch (Throwable ex) {
        fail("Error thrown when analyzing the following source:\n" + source.getCharContent(true));
    }
    check();
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:11,代码来源:FDTest.java

示例5: test

import com.sun.source.util.JavacTask; //导入方法依赖的package包/类
static void test(XlintOption xlint, SuppressLevel suppressLevel, ResourceUsage usage1,
            ResourceUsage usage2, ResourceUsage usage3) throws Exception {
    final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavaSource source = new JavaSource(suppressLevel, usage1, usage2, usage3);
    DiagnosticChecker dc = new DiagnosticChecker();
    JavacTask ct = (JavacTask)tool.getTask(null, fm, dc,
            Arrays.asList(xlint.getXlintOption()), null, Arrays.asList(source));
    ct.analyze();
    check(source, xlint, suppressLevel, usage1, usage2, usage3, dc);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:11,代码来源:UnusedResourcesTest.java

示例6: testClassesInProfiles

import com.sun.source.util.JavacTask; //导入方法依赖的package包/类
@Test
void testClassesInProfiles() throws Exception {
    for (Profile p: Profile.values()) {
        for (Map.Entry<Profile, List<JavaFileObject>> e: testClasses.entrySet()) {
            for (JavaFileObject fo: e.getValue()) {
                DiagnosticCollector<JavaFileObject> dl =
                        new DiagnosticCollector<JavaFileObject>();
                List<String> opts = (p == Profile.DEFAULT)
                        ? Collections.<String>emptyList()
                        : Arrays.asList("--release", "8", "-profile", p.name);
                JavacTask task = (JavacTask) javac.getTask(null, fm, dl, opts, null,
                        Arrays.asList(fo));
                task.analyze();

                List<String> expectDiagCodes = new ArrayList<>();
                if (fo.getName().equals("TPolicyFile.java")) {
                    expectDiagCodes.add("compiler.warn.has.been.deprecated.for.removal");
                }

                if (p.value < e.getKey().value) {
                    expectDiagCodes.add("compiler.err.not.in.profile");
                }

                checkDiags(opts + " " + fo.getName(), dl.getDiagnostics(), expectDiagCodes);
            }
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:29,代码来源:ProfileOptionTest.java

示例7: test

import com.sun.source.util.JavacTask; //导入方法依赖的package包/类
void test() throws Exception {
    System.out.println("\n====================================");
    System.out.println(fInterface + ", " +  context + ", " + methodReference);
    System.out.println(samSourceFile + "\n" + clientSourceFile);

    DiagnosticChecker dc = new DiagnosticChecker();
    JavacTask ct = (JavacTask)comp.getTask(null, fm, dc, null, null, Arrays.asList(samSourceFile, clientSourceFile));
    ct.analyze();
    if (dc.errorFound == checkSamConversion()) {
        throw new AssertionError(samSourceFile + "\n\n" + clientSourceFile);
    }
    count++;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:SamConversionComboTest.java

示例8: run

import com.sun.source.util.JavacTask; //导入方法依赖的package包/类
void run(List<JavaFileObject> sources) throws Exception {
    javax.tools.DiagnosticListener<JavaFileObject> dc = (diagnostic) -> {
        if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
            throw new AssertionError("unexpected diagnostic: " + diagnostic.getMessage(Locale.getDefault()));
        }
    };
    JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
    try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) {
        JavacTask ct = (JavacTask)comp.getTask(null, fm, dc,
                null, null, sources);
        ct.analyze();
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:T8000316.java

示例9: run

import com.sun.source.util.JavacTask; //导入方法依赖的package包/类
void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
    JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
            null, null, Arrays.asList(source));
    try {
        ct.analyze();
    } catch (Throwable ex) {
        throw new AssertionError("Error thron when compiling the following code:\n" + source.getCharContent(true));
    }
    check();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:11,代码来源:TestSelfRef.java

示例10: check

import com.sun.source.util.JavacTask; //导入方法依赖的package包/类
protected void check() throws Exception {
    String[] options = {
        "--should-stop:at=ATTR",
        "--debug:verboseResolution=success,failure,applicable,inapplicable,deferred-inference,predef"
    };

    AbstractProcessor[] processors = { new ResolveCandidateFinder(), null };

    @SuppressWarnings("unchecked")
    DiagnosticListener<? super JavaFileObject>[] diagListeners =
            new DiagnosticListener[] { new DiagnosticHandler(false), new DiagnosticHandler(true) };

    for (int i = 0 ; i < options.length ; i ++) {
        JavacTask ct = (JavacTask)comp.getTask(null, fm, diagListeners[i],
                Arrays.asList(options[i]), null, Arrays.asList(jfo));
        if (processors[i] != null) {
            ct.setProcessors(Collections.singleton(processors[i]));
        }
        ct.analyze();
    }

    //check diags
    for (Diagnostic<? extends JavaFileObject> diag : diags) {
        for (DiagnosticProcessor proc : diagProcessors) {
            if (proc.matches(diag)) {
                proc.process(diag);
                break;
            }
        }
    }
    //check all candidates have been used up
    for (Map.Entry<ElementKey, Candidate> entry : candidatesMap.entrySet()) {
        if (!seenCandidates.contains(entry.getKey())) {
            error("Redundant @Candidate annotation on method " + entry.getKey().elem + " sig = " + entry.getKey().elem.asType());
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:38,代码来源:ResolveHarness.java

示例11: testVisitor

import com.sun.source.util.JavacTask; //导入方法依赖的package包/类
@Test
public void testVisitor(Path base) throws Exception {
    Path src = base.resolve("src");
    Path src_m1 = src.resolve("m1x");
    tb.writeJavaFiles(src_m1,
                      "module m1x { "
                      + " requires m2x;"
                      + " exports p1;"
                      + " opens p2;"
                      + " uses p1.Service;"
                      + " provides p1.Service with p2.Impl;"
                      + "}",
                      "package p1; public interface Service { }",
                      "package p2; public class Impl implements p1.Service { }");
    Path src_m2 = src.resolve("m2x");
    tb.writeJavaFiles(src_m2,
                      "module m2x { }");

    Path modules = base.resolve("modules");
    tb.createDirectories(modules);

    JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
    try (StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null)) {
        Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(findJavaFiles(src));
        List<String> options = List.of(
                "--module-source-path", src.toString(),
                "-d", modules.toString()
        );
        JavacTask t = (JavacTask) javac.getTask(null, fm, null, options, null, files);
        t.analyze();
        ModuleElement e = t.getElements().getModuleElement("m1x");
        Set<DirectiveKind> kinds = EnumSet.<DirectiveKind>allOf(DirectiveKind.class);
        Visitor v = new Visitor();
        v.visit(e, kinds);
        if (!kinds.equals(EnumSet.<DirectiveKind>noneOf(DirectiveKind.class))) {
            error("Some kinds not found: " + kinds);
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:40,代码来源:DirectiveVisitorTest.java

示例12: main

import com.sun.source.util.JavacTask; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
    JavaFileObject sfo = new SimpleJavaFileObject(URI.create(""),Kind.SOURCE) {
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
            return "class Test<S> { <T extends S & Runnable> void test(){}}";
        }
    };
    List<? extends JavaFileObject> files = Arrays.asList(sfo);
    List<String> opts = Arrays.asList("-Xjcov");
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavacTask ct = (JavacTask)tool.getTask(null, null, null,opts,null,files);
    ct.analyze();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:13,代码来源:T6608214.java

示例13: runTest

import com.sun.source.util.JavacTask; //导入方法依赖的package包/类
void runTest(Stream<Path> inputs) {
    JavacTool tool = JavacTool.create();
    try (JavacFileManager fm = tool.getStandardFileManager(null, null, null)) {
        Path classes = Paths.get(System.getProperty("test.classes"));
        Iterable<? extends JavaFileObject> reconFiles =
                fm.getJavaFileObjectsFromFiles(inputs.sorted().map(p -> p.toFile()) :: iterator);
        List<String> options = Arrays.asList("-classpath", classes.toAbsolutePath().toString());
        JavacTask reconTask = tool.getTask(null, fm, null, options, null, reconFiles);
        Iterable<? extends CompilationUnitTree> reconUnits = reconTask.parse();
        JavacTrees reconTrees = JavacTrees.instance(reconTask);
        SearchAnnotations scanner = new SearchAnnotations(reconTrees,
                                                          reconTask.getElements());
        List<JavaFileObject> validateFiles = new ArrayList<>();

        reconTask.analyze();
        scanner.scan(reconUnits, null);

        for (CompilationUnitTree cut : reconUnits) {
            validateFiles.add(ClearAnnotations.clearAnnotations(reconTrees, cut));
        }

        Context validateContext = new Context();
        TestDependencies.preRegister(validateContext);
        JavacTask validateTask =
                tool.getTask(null, fm, null, options, null, validateFiles, validateContext);

        validateTask.analyze();

        TestDependencies deps = (TestDependencies) Dependencies.instance(validateContext);

        if (!scanner.topLevel2Expected.equals(deps.topLevel2Completing)) {
            throw new IllegalStateException(  "expected=" + scanner.topLevel2Expected +
                                            "; actual=" + deps.topLevel2Completing);
        }
    } catch (IOException ex) {
        throw new IllegalStateException(ex);
    } finally {
        inputs.close();
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:41,代码来源:DependenciesTest.java

示例14: runElementAndBreakIteratorTests

import com.sun.source.util.JavacTask; //导入方法依赖的package包/类
/**
 * Tests getting a DocCommentTree from an element, as well
 * as test if break iterator setter/getter works correctly.
 *
 * @param javaFileName a test file to be processed
 * @param expected the expected output
 * @throws java.io.IOException
 */
public void runElementAndBreakIteratorTests(String javaFileName, String expected) throws IOException {
    List<File> javaFiles = new ArrayList<>();
    javaFiles.add(new File(testSrc, javaFileName));

    List<File> dirs = new ArrayList<>();
    dirs.add(new File(testSrc));

    try (StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null)) {
        fm.setLocation(javax.tools.StandardLocation.SOURCE_PATH, dirs);
        Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(javaFiles);

        final JavacTask t = javac.getTask(null, fm, null, null, null, fos);
        final DocTrees trees = DocTrees.instance(t);

        Iterable<? extends Element> elements = t.analyze();

        Element klass = elements.iterator().next();
        DocCommentTree dcTree = trees.getDocCommentTree(klass);

        List<? extends DocTree> firstSentence = dcTree.getFirstSentence();
        StringWriter sw = new StringWriter();
        DocPretty pretty = new DocPretty(sw);
        pretty.print(firstSentence);
        check("getDocCommentTree(Element)", expected, sw.toString());

        BreakIterator bi = BreakIterator.getSentenceInstance(Locale.FRENCH);
        trees.setBreakIterator(bi);
        BreakIterator nbi = trees.getBreakIterator();
        if (bi.equals(nbi)) {
            pass++;
            check("getDocCommentTree(Element) with BreakIterator", expected, sw.toString());
        } else {
            fail++;
            System.err.println("BreakIterators don't match");
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:46,代码来源:DocCommentTreeApiTester.java

示例15: compileAndCheck

import com.sun.source.util.JavacTask; //导入方法依赖的package包/类
static void compileAndCheck(PackageKind pk, ConstructorKind ck, DiamondKind dk) throws Exception {
    FooClass foo = new FooClass(pk, ck);
    ClientClass client = new ClientClass(pk, dk);
    final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    ErrorListener el = new ErrorListener();
    JavacTask ct = (JavacTask)tool.getTask(null, null, el,
            null, null, Arrays.asList(foo, client));
    ct.analyze();
    if (el.errors > 0 == check(pk, ck, dk)) {
        String msg = el.errors > 0 ?
            "Error compiling files" :
            "No error when compiling files";
        throw new AssertionError(msg + ": \n" + foo.source + "\n" + client.source);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:16,代码来源:T6996914a.java


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