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


Java JavacTaskImpl.parse方法代码示例

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


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

示例1: findSource

import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
private Pair<JavacTask, CompilationUnitTree> findSource(String moduleName,
                                                        String binaryName) throws IOException {
    JavaFileObject jfo = fm.getJavaFileForInput(StandardLocation.SOURCE_PATH,
                                                binaryName,
                                                JavaFileObject.Kind.SOURCE);

    if (jfo == null)
        return null;

    List<JavaFileObject> jfos = Arrays.asList(jfo);
    JavaFileManager patchFM = moduleName != null
            ? new PatchModuleFileManager(baseFileManager, jfo, moduleName)
            : baseFileManager;
    JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, patchFM, d -> {}, null, null, jfos);
    Iterable<? extends CompilationUnitTree> cuts = task.parse();

    task.enter();

    return Pair.of(task, cuts.iterator().next());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:JavadocHelper.java

示例2: parse

import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
static List<? extends Tree> parse(String srcfile) throws Exception {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> fileObjects = fileManager.getJavaFileObjects(srcfile);
    String classPath = System.getProperty("java.class.path");
    List<String> options = Arrays.asList("-classpath", classPath);
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
    Context context = new Context();
    JavacTaskImpl task = (JavacTaskImpl) ((JavacTool) compiler).getTask(null, null,
            diagnostics, options, null, fileObjects, context);
    TrialParserFactory.instance(context);
    Iterable<? extends CompilationUnitTree> asts = task.parse();
    Iterator<? extends CompilationUnitTree> it = asts.iterator();
    if (it.hasNext()) {
        CompilationUnitTree cut = it.next();
        return cut.getTypeDecls();
    } else {
        throw new AssertionError("Expected compilation unit");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:JavacExtensionTest.java

示例3: testVariableInIfThen1

import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
@Test
void testVariableInIfThen1() throws IOException {

    String code = "package t; class Test { " +
            "private static void t(String name) { " +
            "if (name != null) String nn = name.trim(); } }";

    DiagnosticCollector<JavaFileObject> coll =
            new DiagnosticCollector<JavaFileObject>();

    JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null,
            null, Arrays.asList(new MyFileObject(code)));

    ct.parse();

    List<String> codes = new LinkedList<String>();

    for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
        codes.add(d.getCode());
    }

    assertEquals("testVariableInIfThen1",
            Arrays.<String>asList("compiler.err.variable.not.allowed"),
            codes);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:26,代码来源:JavacParserTest.java

示例4: testVariableInIfThen2

import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
@Test
void testVariableInIfThen2() throws IOException {

     String code = "package t; class Test { " +
             "private static void t(String name) { " +
             "if (name != null) class X {} } }";
     DiagnosticCollector<JavaFileObject> coll =
             new DiagnosticCollector<JavaFileObject>();
     JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null,
             null, Arrays.asList(new MyFileObject(code)));

     ct.parse();

     List<String> codes = new LinkedList<String>();

     for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
         codes.add(d.getCode());
     }

     assertEquals("testVariableInIfThen2",
             Arrays.<String>asList("compiler.err.class.not.allowed"), codes);
 }
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:23,代码来源:JavacParserTest.java

示例5: testVariableInIfThen3

import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
@Test
void testVariableInIfThen3() throws IOException {

    String code = "package t; class Test { "+
            "private static void t() { " +
            "if (true) abstract class F {} }}";
    DiagnosticCollector<JavaFileObject> coll =
            new DiagnosticCollector<JavaFileObject>();
    JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null,
            null, Arrays.asList(new MyFileObject(code)));

    ct.parse();

    List<String> codes = new LinkedList<String>();

    for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
        codes.add(d.getCode());
    }

    assertEquals("testVariableInIfThen3",
            Arrays.<String>asList("compiler.err.class.not.allowed"), codes);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:23,代码来源:JavacParserTest.java

示例6: testVariableInIfThen4

import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
@Test
void testVariableInIfThen4() throws IOException {

    String code = "package t; class Test { "+
            "private static void t(String name) { " +
            "if (name != null) interface X {} } }";
    DiagnosticCollector<JavaFileObject> coll =
            new DiagnosticCollector<JavaFileObject>();
    JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null,
            null, Arrays.asList(new MyFileObject(code)));

    ct.parse();

    List<String> codes = new LinkedList<String>();

    for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
        codes.add(d.getCode());
    }

    assertEquals("testVariableInIfThen4",
            Arrays.<String>asList("compiler.err.class.not.allowed"), codes);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:23,代码来源:JavacParserTest.java

示例7: testVariableInIfThen5

import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
@Test
void testVariableInIfThen5() throws IOException {

    String code = "package t; class Test { "+
            "private static void t() { " +
            "if (true) } }";
    DiagnosticCollector<JavaFileObject> coll =
            new DiagnosticCollector<JavaFileObject>();
    JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null,
            null, Arrays.asList(new MyFileObject(code)));

    ct.parse();

    List<String> codes = new LinkedList<String>();

    for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
        codes.add(d.getCode());
    }

    assertEquals("testVariableInIfThen5",
            Arrays.<String>asList("compiler.err.illegal.start.of.stmt"),
            codes);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:JavacParserTest.java

示例8: runField

import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
private void runField(String code) throws IOException {
    String src = prefix +
            code + "; }" +
            postfix;

    try (JavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
                null, Arrays.asList(new MyFileObject(src)));

        for (CompilationUnitTree cut : ct.parse()) {
            JCTree.JCVariableDecl var =
                    (JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
            checkMatch(code, var);
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:TypeAnnotationsPretty.java

示例9: runMethod

import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
private void runMethod(String code) throws IOException {
    String src = prefix +
            code + "}" +
            postfix;

    try (JavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
                null, Arrays.asList(new MyFileObject(src)));


        for (CompilationUnitTree cut : ct.parse()) {
            JCTree.JCMethodDecl meth =
                    (JCTree.JCMethodDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
            checkMatch(code, meth);
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:18,代码来源:TypeAnnotationsPretty.java

示例10: run

import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
void run(String code) throws IOException {
    String src = "public class Test {" + code + ";}";

    JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
            null, Arrays.asList(new MyFileObject(src)));

    for (CompilationUnitTree cut : ct.parse()) {
        JCTree.JCVariableDecl var =
                (JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);

        if (!code.equals(var.toString())) {
            System.err.println("Expected: " + code);
            System.err.println("Obtained: " + var.toString());
            throw new RuntimeException("strings do not match!");
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:18,代码来源:NewArrayPretty.java

示例11: testCorrectAnonymousIndicesForMethodInvocations

import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
public void testCorrectAnonymousIndicesForMethodInvocations() throws Exception {
    String code = "package test;\n" +
                  "public class Test {\n" +
                  "    public Test main(Object o) {\n" +
                  "        return new Test().main(new Runnable() {\n" +
                  "            public void run() {\n" +
                  "                throw new UnsupportedOperationException();\n" +
                  "            }\n" +
                  "        }).main(new Iterable() {\n" +
                  "            public java.util.Iterator iterator() {\n" +
                  "                throw new UnsupportedOperationException();\n" +
                  "            }\n" +
                  "        });\n" +
                  "    }\n" +
                  "}";

    JavacTaskImpl ct = Utilities.createJavac(null, Utilities.fileObjectFor(code));
    
    Iterable<? extends CompilationUnitTree> cuts = ct.parse();
    Iterable<? extends Element> analyze = ct.analyze();
    
    Symtab symTab = Symtab.instance(ct.getContext());
    Modules modules = Modules.instance(ct.getContext());
    TypeElement first = symTab.getClass(modules.getDefaultModule(), (Name)ct.getElements().getName("test.Test$1"));
    TypeElement second = symTab.getClass(modules.getDefaultModule(), (Name)ct.getElements().getName("test.Test$2"));

    assertEquals("java.lang.Iterable", ((TypeElement) ((DeclaredType) first.getInterfaces().get(0)).asElement()).getQualifiedName().toString());
    assertEquals("java.lang.Runnable", ((TypeElement) ((DeclaredType) second.getInterfaces().get(0)).asElement()).getQualifiedName().toString());
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:30,代码来源:AnonymousNumberingTest.java

示例12: testParseEnterAnalyze

import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
@Test
public void testParseEnterAnalyze(Path base) throws Exception {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {
        Path moduleSrc = base.resolve("module-src");
        Path m1 = moduleSrc.resolve("m1x");

        tb.writeJavaFiles(m1, "module m1x { }",
                              "package p;",
                              "package p; class T { }");

        Path classes = base.resolve("classes");
        Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(findJavaFiles(moduleSrc));
        List<String> options = Arrays.asList("-d", classes.toString(), "-Xpkginfo:always");
        JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, fm, null, options, null, files);

        Iterable<? extends CompilationUnitTree> parsed = task.parse();
        Iterable<? extends Element> entered = task.enter(parsed);
        Iterable<? extends Element> analyzed = task.analyze(entered);
        Iterable<? extends JavaFileObject> generatedFiles = task.generate(analyzed);

        Set<String> generated = new HashSet<>();

        for (JavaFileObject jfo : generatedFiles) {
            generated.add(jfo.getName());
        }

        Set<String> expected = new HashSet<>(
                Arrays.asList(Paths.get("testParseEnterAnalyze", "classes", "p", "package-info.class").toString(),
                              Paths.get("testParseEnterAnalyze", "classes", "module-info.class").toString(),
                              Paths.get("testParseEnterAnalyze", "classes", "p", "T.class").toString())
        );

        if (!Objects.equals(expected, generated))
            throw new AssertionError("Incorrect generated files: " + generated);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:38,代码来源:EdgeCases.java

示例13: testMethodReference

import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
public void testMethodReference() throws Exception {
    final FileObject libFile = FileUtil.toFileObject(TestFileUtils.writeFile(
         new File(FileUtil.toFile(src),"Lib.java"), //NOI18N
         "public class Lib {\n" +                   //NOI18N
         "    public static void foo(){}\n" +       //NOI18N
         "}"));                                     //NOI18N
    final FileObject javaFile = FileUtil.toFileObject(TestFileUtils.writeFile(
         new File(FileUtil.toFile(src),"Test.java"),        //NOI18N
         "public class Test {   \n" +                       //NOI18N
         "    public static void main(String[] args) {\n" + //NOI18N
         "        Runnable r = Lib::foo;\n" +               //NOI18N
         "    }\n" +                                        //NOI18N
         "}"));                                             //NOI18N

    final DiagnosticListener<JavaFileObject> diag = new DiagnosticListener<JavaFileObject>() {

        private final Queue<Diagnostic<? extends JavaFileObject>> problems = new ArrayDeque<Diagnostic<? extends JavaFileObject>>();

        @Override
        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
            problems.offer(diagnostic);
        }
    };
    TransactionContext.beginStandardTransaction(src.toURL(), true, false, true);
    try {
        final ClasspathInfo cpInfo = ClasspathInfoAccessor.getINSTANCE().create(
            src,
            null,
            true,
            true,
            false,
            true);
        final JavaFileObject jfo = FileObjects.sourceFileObject(javaFile, src);
        final JavacTaskImpl jt = JavacParser.createJavacTask(
            cpInfo,
            diag,
            SourceLevelQuery.getSourceLevel(src),  //NOI18N
            SourceLevelQuery.Profile.DEFAULT,
            null, null, null, null, Arrays.asList(jfo));
        final Iterable<? extends CompilationUnitTree> trees = jt.parse();
        jt.enter();
        jt.analyze();
        final SourceAnalyzerFactory.SimpleAnalyzer sa = SourceAnalyzerFactory.createSimpleAnalyzer();
        List<Pair<Pair<BinaryName, String>, Object[]>> data = sa.analyseUnit(trees.iterator().next(), jt);
        assertEquals(1, data.size());
        assertTrue(((Collection)data.iterator().next().second()[0]).contains(
            DocumentUtil.encodeUsage("Lib", EnumSet.<ClassIndexImpl.UsageType>of(   //NOI18N
                ClassIndexImpl.UsageType.METHOD_REFERENCE,
                ClassIndexImpl.UsageType.TYPE_REFERENCE))));
    } finally {
        TransactionContext.get().rollBack();
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:54,代码来源:SourceAnalyzerTest.java

示例14: testConstructorReference

import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
public void testConstructorReference() throws Exception {
    final FileObject libFile = FileUtil.toFileObject(TestFileUtils.writeFile(
         new File(FileUtil.toFile(src),"Lib.java"), //NOI18N
         "public class Lib {\n" +                   //NOI18N
         "}"));                                     //NOI18N
    final FileObject javaFile = FileUtil.toFileObject(TestFileUtils.writeFile(
         new File(FileUtil.toFile(src),"Test.java"),        //NOI18N
         "public class Test {   \n" +                       //NOI18N
         "    public static void main(String[] args) {\n" + //NOI18N
         "        Runnable r = Lib::new;\n" +               //NOI18N
         "    }\n" +                                        //NOI18N
         "}"));                                             //NOI18N

    final DiagnosticListener<JavaFileObject> diag = new DiagnosticListener<JavaFileObject>() {

        private final Queue<Diagnostic<? extends JavaFileObject>> problems = new ArrayDeque<Diagnostic<? extends JavaFileObject>>();

        @Override
        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
            problems.offer(diagnostic);
        }
    };
    TransactionContext.beginStandardTransaction(src.toURL(), true, false, true);
    try {
        final ClasspathInfo cpInfo = ClasspathInfoAccessor.getINSTANCE().create(
            src,
            null,
            true,
            true,
            false,
            true);
        final JavaFileObject jfo = FileObjects.sourceFileObject(javaFile, src);
        final JavacTaskImpl jt = JavacParser.createJavacTask(
            cpInfo,
            diag,
            SourceLevelQuery.getSourceLevel(src),  //NOI18N
            SourceLevelQuery.Profile.DEFAULT,
            null, null, null, null, Arrays.asList(jfo));
        final Iterable<? extends CompilationUnitTree> trees = jt.parse();
        jt.enter();
        jt.analyze();
        final SourceAnalyzerFactory.SimpleAnalyzer sa = SourceAnalyzerFactory.createSimpleAnalyzer();
        List<Pair<Pair<BinaryName, String>, Object[]>> data = sa.analyseUnit(trees.iterator().next(), jt);
        assertEquals(1, data.size());
        assertTrue(((Collection)data.iterator().next().second()[0]).contains(
            DocumentUtil.encodeUsage("Lib", EnumSet.<ClassIndexImpl.UsageType>of(   //NOI18N
                ClassIndexImpl.UsageType.METHOD_REFERENCE,
                ClassIndexImpl.UsageType.TYPE_REFERENCE))));
    } finally {
        TransactionContext.get().rollBack();
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:53,代码来源:SourceAnalyzerTest.java

示例15: testBrokenReference

import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
public void testBrokenReference() throws Exception {
    final FileObject javaFile = FileUtil.toFileObject(TestFileUtils.writeFile(
         new File(FileUtil.toFile(src),"Test.java"),        //NOI18N
         "public class Test {   \n" +                       //NOI18N
         "    public static void main(String[] args) {\n" + //NOI18N
         "        Runnable r = Lib::foo;\n" +               //NOI18N
         "    }\n" +                                        //NOI18N
         "}"));                                             //NOI18N

    final DiagnosticListener<JavaFileObject> diag = new DiagnosticListener<JavaFileObject>() {

        private final Queue<Diagnostic<? extends JavaFileObject>> problems = new ArrayDeque<Diagnostic<? extends JavaFileObject>>();

        @Override
        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
            problems.offer(diagnostic);
        }
    };
    TransactionContext.beginStandardTransaction(src.toURL(), true, false, true);
    try {
        final ClasspathInfo cpInfo = ClasspathInfoAccessor.getINSTANCE().create(
            src,
            null,
            true,
            true,
            false,
            true);
        final JavaFileObject jfo = FileObjects.sourceFileObject(javaFile, src);
        final JavacTaskImpl jt = JavacParser.createJavacTask(
            cpInfo,
            diag,
            SourceLevelQuery.getSourceLevel(src),  //NOI18N
            SourceLevelQuery.Profile.DEFAULT,
            null, null, null, null, Arrays.asList(jfo));
        final Iterable<? extends CompilationUnitTree> trees = jt.parse();
        jt.enter();
        jt.analyze();
        final SourceAnalyzerFactory.SimpleAnalyzer sa = SourceAnalyzerFactory.createSimpleAnalyzer();
        List<Pair<Pair<BinaryName, String>, Object[]>> data = sa.analyseUnit(trees.iterator().next(), jt);
        assertEquals(1, data.size());
        assertTrue(((Collection)data.iterator().next().second()[0]).contains(
            DocumentUtil.encodeUsage("Lib", EnumSet.<ClassIndexImpl.UsageType>of(   //NOI18N
                ClassIndexImpl.UsageType.TYPE_REFERENCE))));
    } finally {
        TransactionContext.get().rollBack();
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:48,代码来源:SourceAnalyzerTest.java


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