Search in sources :

Example 6 with JavacTaskImpl

use of com.sun.tools.javac.api.JavacTaskImpl in project error-prone by google.

the class SuggestedFixes method compilesWithFix.

/**
 * Returns true if the current compilation would succeed with the given fix applied. Note that
 * calling this method is very expensive as it requires rerunning the entire compile, so it should
 * be used with restraint.
 */
public static boolean compilesWithFix(Fix fix, VisitorState state) {
    if (fix.isEmpty()) {
        return true;
    }
    JCCompilationUnit compilationUnit = (JCCompilationUnit) state.getPath().getCompilationUnit();
    JavaFileObject modifiedFile = compilationUnit.getSourceFile();
    JavacTaskImpl javacTask = (JavacTaskImpl) state.context.get(JavacTask.class);
    if (javacTask == null) {
        throw new IllegalArgumentException("No JavacTask in context.");
    }
    Arguments arguments = Arguments.instance(javacTask.getContext());
    List<JavaFileObject> fileObjects = new ArrayList<>(arguments.getFileObjects());
    for (int i = 0; i < fileObjects.size(); i++) {
        final JavaFileObject oldFile = fileObjects.get(i);
        if (modifiedFile.toUri().equals(oldFile.toUri())) {
            DescriptionBasedDiff diff = DescriptionBasedDiff.create(compilationUnit, ImportOrganizer.STATIC_FIRST_ORGANIZER);
            diff.handleFix(fix);
            SourceFile fixSource;
            try {
                fixSource = new SourceFile(modifiedFile.getName(), modifiedFile.getCharContent(false));
            } catch (IOException e) {
                return false;
            }
            diff.applyDifferences(fixSource);
            fileObjects.set(i, new SimpleJavaFileObject(modifiedFile.toUri(), Kind.SOURCE) {

                @Override
                public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
                    return fixSource.getAsSequence();
                }
            });
            break;
        }
    }
    DiagnosticCollector<JavaFileObject> diagnosticListener = new DiagnosticCollector<>();
    Context context = new Context();
    Options.instance(context).putAll(Options.instance(javacTask.getContext()));
    context.put(Arguments.class, arguments);
    JavacTask newTask = JavacTool.create().getTask(CharStreams.nullWriter(), state.context.get(JavaFileManager.class), diagnosticListener, ImmutableList.of(), arguments.getClassNames(), fileObjects, context);
    try {
        newTask.analyze();
    } catch (Throwable e) {
        e.printStackTrace();
    }
    return countErrors(diagnosticListener) == 0;
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) DescriptionBasedDiff(com.google.errorprone.apply.DescriptionBasedDiff) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) Context(com.sun.tools.javac.util.Context) JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) JavaFileManager(javax.tools.JavaFileManager) Arguments(com.sun.tools.javac.main.Arguments) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) DiagnosticCollector(javax.tools.DiagnosticCollector) SourceFile(com.google.errorprone.apply.SourceFile) JavacTask(com.sun.source.util.JavacTask)

Example 7 with JavacTaskImpl

use of com.sun.tools.javac.api.JavacTaskImpl in project error-prone by google.

the class BaseErrorProneJavaCompiler method getTask.

@Override
public CompilationTask getTask(Writer out, JavaFileManager fileManager, DiagnosticListener<? super JavaFileObject> diagnosticListener, Iterable<String> options, Iterable<String> classes, Iterable<? extends JavaFileObject> compilationUnits) {
    ErrorProneOptions errorProneOptions = ErrorProneOptions.processArgs(options);
    List<String> remainingOptions = Arrays.asList(errorProneOptions.getRemainingArgs());
    ImmutableList<String> javacOpts = ImmutableList.copyOf(remainingOptions);
    javacOpts = defaultToLatestSupportedLanguageLevel(javacOpts);
    javacOpts = setCompilePolicyToByFile(javacOpts);
    final JavacTaskImpl task = (JavacTaskImpl) javacTool.getTask(out, fileManager, diagnosticListener, javacOpts, classes, compilationUnits);
    setupMessageBundle(task.getContext());
    RefactoringCollection[] refactoringCollection = { null };
    task.addTaskListener(createAnalyzer(scannerSupplier, errorProneOptions, task.getContext(), refactoringCollection));
    if (refactoringCollection[0] != null) {
        task.addTaskListener(new RefactoringTask(task.getContext(), refactoringCollection[0]));
    }
    task.addTaskListener(new CFCacheClearingListener());
    return task;
}
Also used : JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl)

Example 8 with JavacTaskImpl

use of com.sun.tools.javac.api.JavacTaskImpl in project ceylon-compiler by ceylon.

the class T6457284 method main.

public static void main(String[] args) throws IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject()));
    MyMessages.preRegister(task.getContext());
    task.parse();
    for (Element e : task.analyze()) {
        if (!e.getEnclosingElement().toString().equals("compiler.misc.unnamed.package"))
            throw new AssertionError(e.getEnclosingElement());
        System.out.println("OK: " + e.getEnclosingElement());
        return;
    }
    throw new AssertionError("No top-level classes!");
}
Also used : JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) Element(javax.lang.model.element.Element)

Example 9 with JavacTaskImpl

use of com.sun.tools.javac.api.JavacTaskImpl in project ceylon-compiler by ceylon.

the class T6330997 method main.

public static void main(String... args) {
    increaseMajor("T1.class", 1);
    increaseMajor("T2.class", 2);
    javax.tools.JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavacTaskImpl task = (JavacTaskImpl) tool.getTask(null, null, null, null, null, null);
    JavaCompiler compiler = JavaCompiler.instance(task.getContext());
    try {
        compiler.resolveIdent("T1").complete();
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Failed: unexpected exception while reading class T1");
    }
    try {
        compiler.resolveIdent("T2").complete();
    } catch (BadClassFile e) {
        System.err.println("Passed: expected completion failure " + e.getClass().getName());
        return;
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Failed: unexpected exception while reading class T2");
    }
    throw new RuntimeException("Failed: no error reported");
}
Also used : JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) BadClassFile(com.sun.tools.javac.jvm.ClassReader.BadClassFile) JavaCompiler(com.sun.tools.javac.main.JavaCompiler)

Example 10 with JavacTaskImpl

use of com.sun.tools.javac.api.JavacTaskImpl in project ceylon-compiler by ceylon.

the class TestJavacTask method getTask.

static JavacTaskImpl getTask(JavaCompiler compiler, File... file) {
    StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjectsFromFiles(Arrays.asList(file));
    return (JavacTaskImpl) compiler.getTask(null, fm, null, null, null, files);
}
Also used : JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) StandardJavaFileManager(javax.tools.StandardJavaFileManager)

Aggregations

JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)29 JavaCompiler (javax.tools.JavaCompiler)10 JavaCompiler (com.sun.tools.javac.main.JavaCompiler)8 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)7 IOException (java.io.IOException)5 JavaFileObject (javax.tools.JavaFileObject)5 StandardJavaFileManager (javax.tools.StandardJavaFileManager)5 VariableTree (com.sun.source.tree.VariableTree)4 BadClassFile (com.sun.tools.javac.jvm.ClassReader.BadClassFile)4 Context (com.sun.tools.javac.util.Context)4 Element (javax.lang.model.element.Element)4 TypeElement (javax.lang.model.element.TypeElement)4 DiagnosticCollector (javax.tools.DiagnosticCollector)4 DescriptionBasedDiff (com.google.errorprone.apply.DescriptionBasedDiff)3 SourceFile (com.google.errorprone.apply.SourceFile)3 JCTree (com.sun.tools.javac.tree.JCTree)3 File (java.io.File)3 BinaryTree (com.sun.source.tree.BinaryTree)2 ClassTree (com.sun.source.tree.ClassTree)2 MethodTree (com.sun.source.tree.MethodTree)2