Search in sources :

Example 21 with JavaCompiler

use of com.sun.tools.javac.main.JavaCompiler in project checker-framework by typetools.

the class StubGenerator method main.

/**
 * The main entry point to StubGenerator.
 *
 * @param args command-line arguments
 */
// User-supplied arguments to main
@SuppressWarnings("signature")
public static void main(String[] args) {
    if (args.length != 1) {
        System.out.println("Usage:");
        System.out.println("    java StubGenerator [class or package name]");
        return;
    }
    Context context = new Context();
    Options options = Options.instance(context);
    if (SystemUtil.getJreVersion() == 8) {
        options.put(Option.SOURCE, "8");
        options.put(Option.TARGET, "8");
    }
    JavaCompiler javac = JavaCompiler.instance(context);
    javac.initModules(com.sun.tools.javac.util.List.nil());
    javac.enterDone();
    ProcessingEnvironment env = JavacProcessingEnvironment.instance(context);
    StubGenerator generator = new StubGenerator();
    if (env.getElementUtils().getPackageElement(args[0]) != null) {
        generator.stubFromPackage(env.getElementUtils().getPackageElement(args[0]));
    } else if (env.getElementUtils().getTypeElement(args[0]) != null) {
        generator.stubFromType(env.getElementUtils().getTypeElement(args[0]));
    } else {
        error("Couldn't find a package or a class named " + args[0]);
    }
}
Also used : Context(com.sun.tools.javac.util.Context) Options(com.sun.tools.javac.util.Options) JavaCompiler(com.sun.tools.javac.main.JavaCompiler) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment)

Example 22 with JavaCompiler

use of com.sun.tools.javac.main.JavaCompiler in project checker-framework by typetools.

the class AbstractTypeProcessor method init.

/**
 * {@inheritDoc}
 *
 * <p>Register a TaskListener that will get called after FLOW.
 */
@Override
public synchronized 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);
    compiler.shouldStopPolicyIfError = CompileState.max(compiler.shouldStopPolicyIfError, 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)

Example 23 with JavaCompiler

use of com.sun.tools.javac.main.JavaCompiler in project error-prone by google.

the class VisitorState method getTypeFromStringInternal.

private Type getTypeFromStringInternal(String typeStr) {
    validateTypeStr(typeStr);
    if (isPrimitiveType(typeStr)) {
        return getPrimitiveType(typeStr);
    }
    if (isVoidType(typeStr)) {
        return getVoidType();
    }
    Name typeName = getName(typeStr);
    try {
        ClassSymbol typeSymbol = getSymtab().getClass(inferModule(typeName), typeName);
        if (typeSymbol == null) {
            JavaCompiler compiler = JavaCompiler.instance(context);
            Symbol sym = compiler.resolveIdent(inferModule(typeName), typeStr);
            if (!(sym instanceof ClassSymbol)) {
                return null;
            }
            typeSymbol = (ClassSymbol) sym;
        }
        Type type = typeSymbol.asType();
        // Throws CompletionFailure if the source/class file for this type is not available.
        // This is hacky but the best way I can think of to handle this case.
        type.complete();
        if (type.isErroneous()) {
            return null;
        }
        return type;
    } catch (CompletionFailure failure) {
        // during the compilation.
        return null;
    }
}
Also used : ClassType(com.sun.tools.javac.code.Type.ClassType) ArrayType(com.sun.tools.javac.code.Type.ArrayType) Type(com.sun.tools.javac.code.Type) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ModuleSymbol(com.sun.tools.javac.code.Symbol.ModuleSymbol) Symbol(com.sun.tools.javac.code.Symbol) CompletionFailure(com.sun.tools.javac.code.Symbol.CompletionFailure) JavaCompiler(com.sun.tools.javac.main.JavaCompiler) Name(com.sun.tools.javac.util.Name)

Example 24 with JavaCompiler

use of com.sun.tools.javac.main.JavaCompiler in project ceylon by eclipse.

the class T6400303 method main.

public static void main(String... args) {
    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("Test$1").complete();
    } catch (CompletionFailure ex) {
        System.err.println("Got expected completion failure: " + ex.getLocalizedMessage());
        return;
    }
    throw new AssertionError("No error reported");
}
Also used : JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) CompletionFailure(com.sun.tools.javac.code.Symbol.CompletionFailure) JavaCompiler(com.sun.tools.javac.main.JavaCompiler)

Example 25 with JavaCompiler

use of com.sun.tools.javac.main.JavaCompiler in project ceylon by eclipse.

the class T6358168 method testNoAnnotationProcessing.

static void testNoAnnotationProcessing(JavacFileManager fm, JavaFileObject f) throws Throwable {
    Context context = new Context();
    fm.setContext(context);
    Main compilerMain = new Main("javac", new PrintWriter(System.err, true));
    compilerMain.setOptions(Options.instance(context));
    compilerMain.filenames = new ListBuffer<File>();
    compilerMain.processArgs(new String[] { "-d", "." });
    JavaCompiler compiler = JavaCompiler.instance(context);
    compiler.compile(List.of(f));
    try {
        compiler.compile(List.of(f));
        throw new Error("Error: AssertionError not thrown after second call of compile");
    } catch (AssertionError e) {
        System.err.println("Exception from compiler (expected): " + e);
    }
}
Also used : JavaCompiler(com.sun.tools.javac.main.JavaCompiler)

Aggregations

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