Search in sources :

Example 31 with Context

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

the class T7021650 method run.

/**
     * Perform a compilation with custom factories registered in the context,
     * and verify that corresponding objects are created in each round.
     */
void run() throws Exception {
    Counter demoCounter = new Counter();
    Counter myAttrCounter = new Counter();
    Context context = new Context();
    // Use a custom file manager which creates classloaders for annotation
    // processors with a sensible delegation parent, so that all instances
    // of test classes come from the same class loader. This is important
    // because the test performs class checks on the instances of classes
    // found in the context for each round or processing.
    context.put(JavaFileManager.class, new Context.Factory<JavaFileManager>() {

        public JavaFileManager make(Context c) {
            return new JavacFileManager(c, true, null) {

                @Override
                protected ClassLoader getClassLoader(URL[] urls) {
                    return new URLClassLoader(urls, T7021650.class.getClassLoader());
                }
            };
        }
    });
    Demo.preRegister(context, demoCounter);
    MyAttr.preRegister(context, myAttrCounter);
    String[] args = { "-d", ".", "-processor", T7021650.class.getName(), "-XprintRounds", new File(testSrc, T7021650.class.getName() + ".java").getPath() };
    compile(context, args);
    // Expect to create Demo for initial round, then MAX_ROUNDS in which
    // GenX files are generated, then standard final round of processing.
    checkEqual("demoCounter", demoCounter.count, MAX_ROUNDS + 2);
    // Expect to create MyAttr for same processing rounds as for Demo,
    // plus additional context for final compilation.
    checkEqual("myAttrCounter", myAttrCounter.count, MAX_ROUNDS + 3);
}
Also used : Context(com.sun.tools.javac.util.Context) JavacFileManager(com.sun.tools.javac.file.JavacFileManager)

Example 32 with Context

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

the class CeylonCompileTool method addJavacArguments.

private void addJavacArguments(List<String> arguments) {
    Options options = Options.instance(new Context());
    for (String argument : javac) {
        HELPER.lastError = null;
        String value = null;
        int index = argument.indexOf('=');
        if (index != -1) {
            value = index < argument.length() ? argument.substring(index + 1) : "";
            argument = argument.substring(0, index);
        }
        JavacOption javacOpt = getJavacOpt(argument.replaceAll(":.*", ":"));
        if (javacOpt == null) {
            throw new IllegalArgumentException(CeylonCompileMessages.msg("option.error.javac", argument));
        }
        if (value != null) {
            if (!javacOpt.hasArg()) {
                throw new IllegalArgumentException(CeylonCompileMessages.msg("option.error.syntax.javac", argument, "Unexpected argument given"));
            }
            if (!javacOpt.matches(argument)) {
                throw new IllegalArgumentException(CeylonCompileMessages.msg("option.error.javac", argument));
            }
            if (javacOpt.process(options, argument, value)) {
                throw new IllegalArgumentException(CeylonCompileMessages.msg("option.error.syntax.javac", argument, HELPER.lastError));
            }
        } else {
            if (javacOpt.hasArg()) {
                throw new IllegalArgumentException(CeylonCompileMessages.msg("option.error.syntax.javac", argument, "Missing expected argument"));
            }
            if (!javacOpt.matches(argument)) {
                throw new IllegalArgumentException(CeylonCompileMessages.msg("option.error.javac", argument));
            }
            if (javacOpt.process(options, argument)) {
                throw new IllegalArgumentException(CeylonCompileMessages.msg("option.error.syntax.javac", argument, HELPER.lastError));
            }
        }
        arguments.add(argument);
        if (value != null) {
            arguments.add(value);
        }
    }
}
Also used : Context(com.sun.tools.javac.util.Context) DefaultToolOptions(com.redhat.ceylon.common.config.DefaultToolOptions) RecognizedOptions(com.sun.tools.javac.main.RecognizedOptions) Options(com.sun.tools.javac.util.Options) JavacOption(com.sun.tools.javac.main.JavacOption)

Example 33 with Context

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

the class CeyloncTool method getTask.

public CeyloncTaskImpl getTask(Writer out, JavaFileManager fileManager, DiagnosticListener<? super JavaFileObject> diagnosticListener, Iterable<String> options, Iterable<String> classes, Iterable<? extends JavaFileObject> compilationUnits) {
    //        final String kindMsg = "All compilation units must be of SOURCE kind";
    if (options != null)
        for (String option : options) // null check
        option.getClass();
    if (classes != null) {
        for (String cls : classes) if (// implicit null check
        !SourceVersion.isName(cls) && // FIX for ceylon because default is not a valid name for Java
        !"default".equals(cls))
            throw new IllegalArgumentException("Not a valid class name: " + cls);
    }
    if (fileManager == null)
        fileManager = getStandardFileManager(out, diagnosticListener, null, null);
    Context context = ((CeyloncFileManager) fileManager).getContext();
    if (diagnosticListener != null && context.get(DiagnosticListener.class) == null)
        context.put(DiagnosticListener.class, diagnosticListener);
    context.put(JavaFileManager.class, fileManager);
    JavacTool.processOptions(context, fileManager, options);
    Main compiler = new Main("ceyloncTask", context.get(Log.outKey));
    return new CeyloncTaskImpl(compiler, options, context, classes, compilationUnits);
}
Also used : Context(com.sun.tools.javac.util.Context) DiagnosticListener(javax.tools.DiagnosticListener) Main(com.redhat.ceylon.compiler.java.launcher.Main)

Example 34 with Context

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

the class T7018098 method process.

//---------------
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
    FSInfo fsInfo = context.get(FSInfo.class);
    round++;
    if (round == 1) {
        boolean expect = Boolean.valueOf(options.get("expect"));
        checkEqual("cache result", fsInfo.isDirectory(testDir), expect);
        initialFSInfo = fsInfo;
    } else {
        checkEqual("fsInfo", fsInfo, initialFSInfo);
    }
    return true;
}
Also used : Context(com.sun.tools.javac.util.Context) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) FSInfo(com.sun.tools.javac.file.FSInfo)

Example 35 with Context

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

the class CeylonCompileTool method initialize.

@Override
public void initialize(CeylonTool mainTool) throws IOException {
    compiler = new Main("ceylon compile");
    Options options = Options.instance(new Context());
    if (modulesOrFiles.isEmpty() && !javac.contains("-help") && !javac.contains("-X") && !javac.contains("-version")) {
        throw new IllegalStateException("Argument moduleOrFile should appear at least 1 time(s)");
    }
    arguments = new ArrayList<>();
    if (cwd != null) {
        arguments.add("-cwd");
        arguments.add(cwd.getPath());
    }
    for (File source : applyCwd(this.sources)) {
        arguments.add("-src");
        arguments.add(source.getPath());
        options.addMulti(OptionName.SOURCEPATH, source.getPath());
    }
    for (File resource : applyCwd(this.resources)) {
        arguments.add("-res");
        arguments.add(resource.getPath());
    //options.addMulti(OptionName.RESOURCEPATH, resource.getPath());
    }
    if (resourceRoot != null) {
        arguments.add("-resroot");
        arguments.add(resourceRoot);
    }
    if (continueOnErrors) {
        arguments.add("-continue");
    }
    if (progress) {
        arguments.add("-progress");
    }
    if (offline) {
        arguments.add("-offline");
    }
    if (timeout != -1) {
        arguments.add("-timeout");
        arguments.add(String.valueOf(timeout));
    }
    if (flatClasspath) {
        arguments.add("-flat-classpath");
    }
    if (autoExportMavenDependencies) {
        arguments.add("-auto-export-maven-dependencies");
    }
    if (overrides != null) {
        arguments.add("-overrides");
        if (overrides.startsWith("classpath:")) {
            arguments.add(overrides);
        } else {
            arguments.add(applyCwd(new File(overrides)).getPath());
        }
    }
    if (noOsgi) {
        arguments.add("-noosgi");
    }
    if (osgiProvidedBundles != null && !osgiProvidedBundles.isEmpty()) {
        arguments.add("-osgi-provided-bundles");
        arguments.add(osgiProvidedBundles);
    }
    if (noPom) {
        arguments.add("-nopom");
    }
    if (pack200) {
        arguments.add("-pack200");
    }
    if (verbose != null) {
        if (verbose.isEmpty()) {
            arguments.add("-verbose");
        } else {
            arguments.add("-verbose:" + verbose);
        }
    }
    if (out != null) {
        arguments.add("-out");
        arguments.add(out);
    }
    if (user != null) {
        arguments.add("-user");
        arguments.add(user);
    }
    if (pass != null) {
        arguments.add("-pass");
        arguments.add(pass);
    }
    String fileEncoding = encoding;
    if (fileEncoding == null) {
        fileEncoding = DefaultToolOptions.getDefaultEncoding();
    }
    if (fileEncoding != null) {
        JavacOption encodingOpt = getJavacOpt(OptionName.ENCODING.toString());
        validateWithJavac(options, encodingOpt, OptionName.ENCODING.toString(), fileEncoding, "option.error.syntax.encoding");
        arguments.add(OptionName.ENCODING.toString());
        arguments.add(fileEncoding);
    }
    if (systemRepo != null) {
        arguments.add("-sysrep");
        arguments.add(systemRepo);
    }
    if (cacheRepo != null) {
        arguments.add("-cacherep");
        arguments.add(cacheRepo);
    }
    if (noDefRepos) {
        arguments.add("-nodefreps");
    }
    if (repo != null) {
        for (URI uri : this.repo) {
            arguments.add("-rep");
            arguments.add(uri.toString());
        }
    }
    if (suppressWarnings != null) {
        arguments.add("-suppress-warnings");
        arguments.add(EnumUtil.enumsToString(suppressWarnings));
    }
    addJavacArguments(arguments);
    List<File> srcs = applyCwd(this.sources);
    List<String> expandedModulesOrFiles = ModuleWildcardsHelper.expandWildcards(srcs, this.modulesOrFiles, Backend.Java);
    if (expandedModulesOrFiles.isEmpty()) {
        throw new ToolUsageError("No modules or source files to compile");
    }
    JavacOption sourceFileOpt = getJavacOpt(OptionName.SOURCEFILE.toString());
    if (sourceFileOpt != null) {
        for (String moduleOrFile : expandedModulesOrFiles) {
            validateWithJavac(options, sourceFileOpt, moduleOrFile, moduleOrFile, "argument.error");
        }
    }
    validateSourceArguments(expandedModulesOrFiles);
    arguments.addAll(expandedModulesOrFiles);
    if (verbose != null) {
        System.out.println(arguments);
        System.out.flush();
    }
}
Also used : Context(com.sun.tools.javac.util.Context) DefaultToolOptions(com.redhat.ceylon.common.config.DefaultToolOptions) RecognizedOptions(com.sun.tools.javac.main.RecognizedOptions) Options(com.sun.tools.javac.util.Options) JavacOption(com.sun.tools.javac.main.JavacOption) ToolUsageError(com.redhat.ceylon.common.tool.ToolUsageError) Main(com.redhat.ceylon.compiler.java.launcher.Main) File(java.io.File) URI(java.net.URI)

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