Search in sources :

Example 1 with JavaCompiler

use of com.sun.tools.javac.main.JavaCompiler in project lombok by rzwitserloot.

the class CommentCatcher method create.

public static CommentCatcher create(Context context) {
    registerCommentsCollectingScannerFactory(context);
    JavaCompiler compiler = new JavaCompiler(context);
    setInCompiler(compiler, context);
    compiler.keepComments = true;
    compiler.genEndPos = true;
    return new CommentCatcher(compiler);
}
Also used : JavaCompiler(com.sun.tools.javac.main.JavaCompiler)

Example 2 with JavaCompiler

use of com.sun.tools.javac.main.JavaCompiler in project lombok by rzwitserloot.

the class CommentCollectingParserFactory method setInCompiler.

public static void setInCompiler(JavaCompiler compiler, Context context) {
    context.put(CommentCollectingParserFactory.key(), (Parser.Factory) null);
    Field field;
    try {
        field = JavaCompiler.class.getDeclaredField("parserFactory");
        field.setAccessible(true);
        field.set(compiler, new CommentCollectingParserFactory(context));
    } catch (Exception e) {
        throw new IllegalStateException("Could not set comment sensitive parser in the compiler", e);
    }
}
Also used : Field(java.lang.reflect.Field) JavaCompiler(com.sun.tools.javac.main.JavaCompiler) Parser(com.sun.tools.javac.parser.Parser)

Example 3 with JavaCompiler

use of com.sun.tools.javac.main.JavaCompiler in project lombok by rzwitserloot.

the class CommentCollectingParserFactory method setInCompiler.

public static void setInCompiler(JavaCompiler compiler, Context context) {
    context.put(CommentCollectingParserFactory.key(), (ParserFactory) null);
    Field field;
    try {
        field = JavaCompiler.class.getDeclaredField("parserFactory");
        field.setAccessible(true);
        field.set(compiler, new CommentCollectingParserFactory(context));
    } catch (Exception e) {
        throw new IllegalStateException("Could not set comment sensitive parser in the compiler", e);
    }
}
Also used : Field(java.lang.reflect.Field) JavaCompiler(com.sun.tools.javac.main.JavaCompiler)

Example 4 with JavaCompiler

use of com.sun.tools.javac.main.JavaCompiler in project bazel by bazelbuild.

the class JavaSource2CFGDOT method getMethodTreeAndCompilationUnit.

/**
     * @return The AST of a specific method in a specific class as well as the
     *         {@link CompilationUnitTree} in a specific file (or null they do
     *         not exist).
     */
public static Entry</*@Nullable*/
MethodTree, /*@Nullable*/
CompilationUnitTree> getMethodTreeAndCompilationUnit(String file, final String method, String clas) {
    final Holder<MethodTree> m = new Holder<>();
    final Holder<CompilationUnitTree> c = new Holder<>();
    BasicTypeProcessor typeProcessor = new BasicTypeProcessor() {

        @Override
        protected TreePathScanner<?, ?> createTreePathScanner(CompilationUnitTree root) {
            c.value = root;
            return new TreePathScanner<Void, Void>() {

                @Override
                public Void visitMethod(MethodTree node, Void p) {
                    ExecutableElement el = TreeUtils.elementFromDeclaration(node);
                    if (el.getSimpleName().contentEquals(method)) {
                        m.value = node;
                        // compilation).
                        throw new RuntimeException();
                    }
                    return null;
                }
            };
        }
    };
    Context context = new Context();
    JavaCompiler javac = new JavaCompiler(context);
    javac.attrParseOnly = true;
    JavacFileManager fileManager = (JavacFileManager) context.get(JavaFileManager.class);
    JavaFileObject l = fileManager.getJavaFileObjectsFromStrings(List.of(file)).iterator().next();
    PrintStream err = System.err;
    try {
        // redirect syserr to nothing (and prevent the compiler from issuing
        // warnings about our exception.
        System.setErr(new PrintStream(new OutputStream() {

            @Override
            public void write(int b) throws IOException {
            }
        }));
        javac.compile(List.of(l), List.of(clas), List.of(typeProcessor));
    } catch (Throwable e) {
    // ok
    } finally {
        System.setErr(err);
    }
    return new Entry<MethodTree, CompilationUnitTree>() {

        @Override
        public CompilationUnitTree setValue(CompilationUnitTree value) {
            return null;
        }

        @Override
        public CompilationUnitTree getValue() {
            return c.value;
        }

        @Override
        public MethodTree getKey() {
            return m.value;
        }
    };
}
Also used : Context(com.sun.tools.javac.util.Context) PrintStream(java.io.PrintStream) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) MethodTree(com.sun.source.tree.MethodTree) JavaFileManager(javax.tools.JavaFileManager) Holder(javax.xml.ws.Holder) ExecutableElement(javax.lang.model.element.ExecutableElement) OutputStream(java.io.OutputStream) TreePathScanner(com.sun.source.util.TreePathScanner) JavaCompiler(com.sun.tools.javac.main.JavaCompiler) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) JavaFileObject(javax.tools.JavaFileObject) Entry(java.util.Map.Entry) BasicTypeProcessor(org.checkerframework.javacutil.BasicTypeProcessor)

Example 5 with JavaCompiler

use of com.sun.tools.javac.main.JavaCompiler in project bazel by bazelbuild.

the class AbstractTypeProcessor method init.

/**
     * {@inheritDoc}
     *
     * Register a TaskListener that will get called after FLOW.
     */
@Override
public void init(ProcessingEnvironment env) {
    super.init(env);
    JavacTask.instance(env).addTaskListener(listener);
    Context ctx = ((JavacProcessingEnvironment) processingEnv).getContext();
    JavaCompiler compiler = JavaCompiler.instance(ctx);
    compiler.shouldStopPolicyIfNoError = CompileState.max(compiler.shouldStopPolicyIfNoError, CompileState.FLOW);
}
Also used : Context(com.sun.tools.javac.util.Context) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) JavaCompiler(com.sun.tools.javac.main.JavaCompiler)

Aggregations

JavaCompiler (com.sun.tools.javac.main.JavaCompiler)30 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)8 Field (java.lang.reflect.Field)6 Context (com.sun.tools.javac.util.Context)5 JavaFileObject (javax.tools.JavaFileObject)5 BadClassFile (com.sun.tools.javac.jvm.ClassReader.BadClassFile)4 JavaFileManager (javax.tools.JavaFileManager)4 CompletionFailure (com.sun.tools.javac.code.Symbol.CompletionFailure)3 JavacFileManager (com.sun.tools.javac.file.JavacFileManager)3 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)2 MethodTree (com.sun.source.tree.MethodTree)2 TreePathScanner (com.sun.source.util.TreePathScanner)2 JavacProcessingEnvironment (com.sun.tools.javac.processing.JavacProcessingEnvironment)2 File (java.io.File)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 PrintStream (java.io.PrintStream)2 EnvironmentException (com.redhat.ceylon.compiler.EnvironmentException)1 RepositoryException (com.redhat.ceylon.model.cmr.RepositoryException)1 TaskEvent (com.sun.source.util.TaskEvent)1