Search in sources :

Example 61 with Context

use of com.sun.tools.javac.util.Context in project ceylon-compiler by ceylon.

the class LanguageCompiler method getPhasedUnitsInstance.

/** Get the PhasedUnits instance for this context. */
public static PhasedUnits getPhasedUnitsInstance(final Context context) {
    PhasedUnits phasedUnits = context.get(phasedUnitsKey);
    if (phasedUnits == null) {
        com.redhat.ceylon.compiler.typechecker.context.Context ceylonContext = getCeylonContextInstance(context);
        phasedUnits = new PhasedUnits(ceylonContext, new ModuleManagerFactory() {

            @Override
            public ModuleManager createModuleManager(com.redhat.ceylon.compiler.typechecker.context.Context ceylonContext) {
                CompilerDelegate phasedUnitsManager = getCompilerDelegate(context);
                return phasedUnitsManager.getModuleManager();
            }

            @Override
            public ModuleSourceMapper createModuleManagerUtil(com.redhat.ceylon.compiler.typechecker.context.Context ceylonContext, ModuleManager moduleManager) {
                CompilerDelegate phasedUnitsManager = getCompilerDelegate(context);
                return phasedUnitsManager.getModuleSourceMapper();
            }
        });
        context.put(phasedUnitsKey, phasedUnits);
    }
    return phasedUnits;
}
Also used : Context(com.sun.tools.javac.util.Context) AttrContext(com.sun.tools.javac.comp.AttrContext) PhasedUnits(com.redhat.ceylon.compiler.typechecker.context.PhasedUnits) ModuleManagerFactory(com.redhat.ceylon.compiler.typechecker.util.ModuleManagerFactory) ModuleManager(com.redhat.ceylon.model.typechecker.util.ModuleManager)

Example 62 with Context

use of com.sun.tools.javac.util.Context in project ceylon-compiler by ceylon.

the class CeyloncTool method getStandardFileManager.

public JavacFileManager getStandardFileManager(Writer out, DiagnosticListener<? super JavaFileObject> diagnosticListener, Locale locale, Charset charset) {
    Context context = new Context();
    if (diagnosticListener != null)
        context.put(DiagnosticListener.class, diagnosticListener);
    // make sure we set the out before someone else sets a default one, or uses one
    if (context.get(Log.outKey) == null) {
        if (out == null)
            context.put(Log.outKey, new PrintWriter(System.err, true));
        else
            context.put(Log.outKey, new PrintWriter(out, true));
    }
    CeylonLog.preRegister(context);
    return new CeyloncFileManager(context, true, charset);
}
Also used : Context(com.sun.tools.javac.util.Context) DiagnosticListener(javax.tools.DiagnosticListener) PrintWriter(java.io.PrintWriter)

Example 63 with Context

use of com.sun.tools.javac.util.Context in project ceylon-compiler by ceylon.

the class Start method parseAndExecute.

/**
     * Main program - internal
     */
private boolean parseAndExecute(String... argv) throws IOException {
    long tm = System.currentTimeMillis();
    ListBuffer<String> javaNames = new ListBuffer<String>();
    // Preprocess @file arguments
    try {
        argv = CommandLine.parse(argv);
    } catch (FileNotFoundException e) {
        messager.error(null, "main.cant.read", e.getMessage());
        exit();
    } catch (IOException e) {
        e.printStackTrace();
        exit();
    }
    setDocletInvoker(argv);
    ListBuffer<String> subPackages = new ListBuffer<String>();
    ListBuffer<String> excludedPackages = new ListBuffer<String>();
    Context context = new Context();
    // Setup a new Messager, using the same initial parameters as the
    // existing Messager, except that this one will be able to use any
    // options that may be set up below.
    Messager.preRegister(context, messager.programName, messager.errWriter, messager.warnWriter, messager.noticeWriter);
    Options compOpts = Options.instance(context);
    boolean docClasses = false;
    // Parse arguments
    for (int i = 0; i < argv.length; i++) {
        String arg = argv[i];
        if (arg.equals("-subpackages")) {
            oneArg(argv, i++);
            addToList(subPackages, argv[i]);
        } else if (arg.equals("-exclude")) {
            oneArg(argv, i++);
            addToList(excludedPackages, argv[i]);
        } else if (arg.equals("-verbose")) {
            setOption(arg);
            compOpts.put("-verbose", "");
        } else if (arg.equals("-encoding")) {
            oneArg(argv, i++);
            encoding = argv[i];
            compOpts.put("-encoding", argv[i]);
        } else if (arg.equals("-breakiterator")) {
            breakiterator = true;
            setOption("-breakiterator");
        } else if (arg.equals("-quiet")) {
            quiet = true;
            setOption("-quiet");
        } else if (arg.equals("-help")) {
            usage();
            exit();
        } else if (arg.equals("-Xclasses")) {
            setOption(arg);
            docClasses = true;
        } else if (arg.equals("-Xwerror")) {
            setOption(arg);
            rejectWarnings = true;
        } else if (arg.equals("-private")) {
            setOption(arg);
            setFilter(ModifierFilter.ALL_ACCESS);
        } else if (arg.equals("-package")) {
            setOption(arg);
            setFilter(PUBLIC | PROTECTED | ModifierFilter.PACKAGE);
        } else if (arg.equals("-protected")) {
            setOption(arg);
            setFilter(PUBLIC | PROTECTED);
        } else if (arg.equals("-public")) {
            setOption(arg);
            setFilter(PUBLIC);
        } else if (arg.equals("-source")) {
            oneArg(argv, i++);
            if (compOpts.get("-source") != null) {
                usageError("main.option.already.seen", arg);
            }
            compOpts.put("-source", argv[i]);
        } else if (arg.equals("-prompt")) {
            compOpts.put("-prompt", "-prompt");
            messager.promptOnError = true;
        } else if (arg.equals("-sourcepath")) {
            oneArg(argv, i++);
            if (compOpts.get("-sourcepath") != null) {
                usageError("main.option.already.seen", arg);
            }
            compOpts.put("-sourcepath", argv[i]);
        } else if (arg.equals("-classpath")) {
            oneArg(argv, i++);
            if (compOpts.get("-classpath") != null) {
                usageError("main.option.already.seen", arg);
            }
            compOpts.put("-classpath", argv[i]);
        } else if (arg.equals("-sysclasspath")) {
            oneArg(argv, i++);
            if (compOpts.get("-bootclasspath") != null) {
                usageError("main.option.already.seen", arg);
            }
            compOpts.put("-bootclasspath", argv[i]);
        } else if (arg.equals("-bootclasspath")) {
            oneArg(argv, i++);
            if (compOpts.get("-bootclasspath") != null) {
                usageError("main.option.already.seen", arg);
            }
            compOpts.put("-bootclasspath", argv[i]);
        } else if (arg.equals("-extdirs")) {
            oneArg(argv, i++);
            if (compOpts.get("-extdirs") != null) {
                usageError("main.option.already.seen", arg);
            }
            compOpts.put("-extdirs", argv[i]);
        } else if (arg.equals("-overview")) {
            oneArg(argv, i++);
        } else if (arg.equals("-doclet")) {
            // handled in setDocletInvoker
            i++;
        } else if (arg.equals("-docletpath")) {
            // handled in setDocletInvoker
            i++;
        } else if (arg.equals("-locale")) {
            if (i != 0)
                usageError("main.locale_first");
            oneArg(argv, i++);
            docLocale = argv[i];
        } else if (arg.equals("-Xmaxerrs") || arg.equals("-Xmaxwarns")) {
            oneArg(argv, i++);
            if (compOpts.get(arg) != null) {
                usageError("main.option.already.seen", arg);
            }
            compOpts.put(arg, argv[i]);
        } else if (arg.equals("-X")) {
            Xusage();
            exit();
        } else if (arg.startsWith("-XD")) {
            String s = arg.substring("-XD".length());
            int eq = s.indexOf('=');
            String key = (eq < 0) ? s : s.substring(0, eq);
            String value = (eq < 0) ? s : s.substring(eq + 1);
            compOpts.put(key, value);
        } else // other arg starts with - is invalid
        if (arg.startsWith("-")) {
            int optionLength;
            optionLength = docletInvoker.optionLength(arg);
            if (optionLength < 0) {
                // error already displayed
                exit();
            } else if (optionLength == 0) {
                // option not found
                usageError("main.invalid_flag", arg);
            } else {
                // doclet added option
                if ((i + optionLength) > argv.length) {
                    usageError("main.requires_argument", arg);
                }
                ListBuffer<String> args = new ListBuffer<String>();
                for (int j = 0; j < optionLength - 1; ++j) {
                    args.append(argv[++i]);
                }
                setOption(arg, args.toList());
            }
        } else {
            javaNames.append(arg);
        }
    }
    if (javaNames.isEmpty() && subPackages.isEmpty()) {
        usageError("main.No_packages_or_classes_specified");
    }
    if (!docletInvoker.validOptions(options.toList())) {
        // error message already displayed
        exit();
    }
    JavadocTool comp = JavadocTool.make0(context);
    if (comp == null)
        return false;
    if (showAccess == null) {
        setFilter(defaultFilter);
    }
    LanguageVersion languageVersion = docletInvoker.languageVersion();
    RootDocImpl root = comp.getRootDocImpl(docLocale, encoding, showAccess, javaNames.toList(), options.toList(), breakiterator, subPackages.toList(), excludedPackages.toList(), docClasses, // legacy?
    languageVersion == null || languageVersion == LanguageVersion.JAVA_1_1, quiet);
    // pass off control to the doclet
    boolean ok = root != null;
    if (ok)
        ok = docletInvoker.start(root);
    Messager docletMessager = Messager.instance0(context);
    messager.nwarnings += docletMessager.nwarnings;
    messager.nerrors += docletMessager.nerrors;
    // We're done.
    if (compOpts.get("-verbose") != null) {
        tm = System.currentTimeMillis() - tm;
        messager.notice("main.done_in", Long.toString(tm));
    }
    return ok;
}
Also used : Context(com.sun.tools.javac.util.Context) Options(com.sun.tools.javac.util.Options) ListBuffer(com.sun.tools.javac.util.ListBuffer) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 64 with Context

use of com.sun.tools.javac.util.Context in project ceylon-compiler by ceylon.

the class CompileTest method test.

void test(String[] opts, String className) throws Exception {
    count++;
    System.err.println("Test " + count + " " + Arrays.asList(opts) + " " + className);
    Path testSrcDir = Paths.get(System.getProperty("test.src"));
    Path testClassesDir = Paths.get(System.getProperty("test.classes"));
    Path classes = Files.createDirectory(Paths.get("classes." + count));
    Context ctx = new Context();
    PathFileManager fm = new JavacPathFileManager(ctx, true, null);
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    List<String> options = new ArrayList<String>();
    options.addAll(Arrays.asList(opts));
    options.addAll(Arrays.asList("-verbose", "-XDverboseCompilePolicy", "-d", classes.toString(), "-g"));
    Iterable<? extends JavaFileObject> compilationUnits = fm.getJavaFileObjects(testSrcDir.resolve(className + ".java"));
    StringWriter sw = new StringWriter();
    PrintWriter out = new PrintWriter(sw);
    JavaCompiler.CompilationTask t = compiler.getTask(out, fm, null, options, null, compilationUnits);
    boolean ok = t.call();
    System.err.println(sw.toString());
    if (!ok) {
        throw new Exception("compilation failed");
    }
    File expect = new File("classes." + count + "/" + className + ".class");
    if (!expect.exists())
        throw new Exception("expected file not found: " + expect);
    // Note that we explicitly specify -g for compiling both the actual class and the expected class.
    // This isolates the expected class from javac options that might be given to jtreg.
    long expectedSize = new File(testClassesDir.toString(), className + ".class").length();
    long actualSize = expect.length();
    if (expectedSize != actualSize)
        throw new Exception("wrong size found: " + actualSize + "; expected: " + expectedSize);
}
Also used : Context(com.sun.tools.javac.util.Context)

Example 65 with Context

use of com.sun.tools.javac.util.Context in project ceylon-compiler by ceylon.

the class TestContext method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    round++;
    JavacProcessingEnvironment jpe = (JavacProcessingEnvironment) processingEnv;
    Context c = jpe.getContext();
    check(c.get(JavacElements.class), eltUtils);
    check(c.get(JavacTypes.class), typeUtils);
    check(c.get(JavacTrees.class), treeUtils);
    final int MAXROUNDS = 3;
    if (round < MAXROUNDS)
        generateSource("Gen" + round);
    return true;
}
Also used : Context(com.sun.tools.javac.util.Context) JavacTypes(com.sun.tools.javac.model.JavacTypes) JavacTrees(com.sun.tools.javac.api.JavacTrees) JavacElements(com.sun.tools.javac.model.JavacElements) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment)

Aggregations

Context (com.sun.tools.javac.util.Context)65 JavacFileManager (com.sun.tools.javac.file.JavacFileManager)19 Test (org.junit.Test)9 Options (com.sun.tools.javac.util.Options)8 IOException (java.io.IOException)8 PrintWriter (java.io.PrintWriter)8 JavaFileObject (javax.tools.JavaFileObject)7 ListBuffer (com.sun.tools.javac.util.ListBuffer)6 JavacTask (com.sun.source.util.JavacTask)5 JavacProcessingEnvironment (com.sun.tools.javac.processing.JavacProcessingEnvironment)5 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)5 JavaFileManager (javax.tools.JavaFileManager)5 OutputStreamWriter (java.io.OutputStreamWriter)4 Path (java.nio.file.Path)4 DiagnosticListener (javax.tools.DiagnosticListener)4 ImmutableList (com.google.common.collect.ImmutableList)3 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)3 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)3 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)3 StringWriter (java.io.StringWriter)3