Search in sources :

Example 1 with ProcessingContext

use of com.google.devtools.j2objc.pipeline.ProcessingContext in project j2objc by google.

the class GenerationTest method translateCombinedFiles.

protected String translateCombinedFiles(String outputPath, String extension, String... sources) throws IOException {
    List<ProcessingContext> inputs = new ArrayList<>();
    GenerationUnit genUnit = GenerationUnit.newCombinedJarUnit(outputPath + ".testfile", options);
    for (String sourceFile : sources) {
        inputs.add(new ProcessingContext(new RegularInputFile(tempDir + "/" + sourceFile, sourceFile), genUnit));
    }
    parser.setEnableDocComments(options.docCommentsEnabled());
    new InputFilePreprocessor(parser).processInputs(inputs);
    new TranslationProcessor(parser, CodeReferenceMap.builder().build()).processInputs(inputs);
    return getTranslatedFile(outputPath + extension);
}
Also used : ProcessingContext(com.google.devtools.j2objc.pipeline.ProcessingContext) GenerationUnit(com.google.devtools.j2objc.gen.GenerationUnit) RegularInputFile(com.google.devtools.j2objc.file.RegularInputFile) TranslationProcessor(com.google.devtools.j2objc.pipeline.TranslationProcessor) ArrayList(java.util.ArrayList) InputFilePreprocessor(com.google.devtools.j2objc.pipeline.InputFilePreprocessor)

Example 2 with ProcessingContext

use of com.google.devtools.j2objc.pipeline.ProcessingContext in project j2objc by google.

the class JavacParser method processAnnotations.

@Override
public ProcessingResult processAnnotations(Iterable<String> fileArgs, List<ProcessingContext> inputs) {
    final List<ProcessingContext> generatedInputs = Lists.newArrayList();
    PathClassLoader loader = new PathClassLoader(options.fileUtil().getClassPathEntries());
    loader.addPaths(options.getProcessorPathEntries());
    Iterator<Processor> serviceIterator = ServiceLoader.load(Processor.class, loader).iterator();
    if (serviceIterator.hasNext()) {
        List<File> inputFiles = new ArrayList<>();
        for (ProcessingContext input : inputs) {
            inputFiles.add(new File(input.getFile().getAbsolutePath()));
        }
        try {
            JavacEnvironment env = createEnvironment(inputFiles, null, true);
            List<CompilationUnitTree> units = new ArrayList<>();
            for (CompilationUnitTree unit : env.task().parse()) {
                units.add(unit);
            }
            // JavacTaskImpl.enter() parses and runs annotation processing, but
            // not type checking and attribution (that's done by analyze()).
            env.task().enter();
            processDiagnostics(env.diagnostics());
            // The source output directory is created and set in createEnvironment().
            File sourceOutputDirectory = env.fileManager().getLocation(StandardLocation.SOURCE_OUTPUT).iterator().next();
            collectGeneratedInputs(sourceOutputDirectory, "", generatedInputs);
            return new JavacProcessingResult(generatedInputs, sourceOutputDirectory);
        } catch (IOException e) {
            ErrorUtil.fatalError(e, "javac file manager error");
        }
    }
    // No annotation processors on classpath, or processing errors reported.
    return new JavacProcessingResult(generatedInputs, null);
}
Also used : ProcessingContext(com.google.devtools.j2objc.pipeline.ProcessingContext) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) Processor(javax.annotation.processing.Processor) ArrayList(java.util.ArrayList) IOException(java.io.IOException) PathClassLoader(com.google.devtools.j2objc.util.PathClassLoader) RegularInputFile(com.google.devtools.j2objc.file.RegularInputFile) InputFile(com.google.devtools.j2objc.file.InputFile) File(java.io.File)

Example 3 with ProcessingContext

use of com.google.devtools.j2objc.pipeline.ProcessingContext in project j2objc by google.

the class J2ObjC method run.

/**
   * Runs the entire J2ObjC pipeline.
   * @param fileArgs the files to process, same format as command-line args to {@link #main}.
   */
public static void run(List<String> fileArgs, Options options) {
    File preProcessorTempDir = null;
    File strippedSourcesDir = null;
    Parser parser = null;
    try {
        List<ProcessingContext> inputs = Lists.newArrayList();
        GenerationBatch batch = new GenerationBatch(options);
        batch.processFileArgs(fileArgs);
        inputs.addAll(batch.getInputs());
        if (ErrorUtil.errorCount() > 0) {
            return;
        }
        parser = createParser(options);
        Parser.ProcessingResult processingResult = parser.processAnnotations(fileArgs, inputs);
        List<ProcessingContext> generatedInputs = processingResult.getGeneratedSources();
        // Ensure all generatedInputs are at end of input list.
        inputs.addAll(generatedInputs);
        preProcessorTempDir = processingResult.getSourceOutputDirectory();
        if (ErrorUtil.errorCount() > 0) {
            return;
        }
        if (preProcessorTempDir != null) {
            parser.addSourcepathEntry(preProcessorTempDir.getAbsolutePath());
        }
        InputFilePreprocessor inputFilePreprocessor = new InputFilePreprocessor(parser);
        inputFilePreprocessor.processInputs(inputs);
        if (ErrorUtil.errorCount() > 0) {
            return;
        }
        strippedSourcesDir = inputFilePreprocessor.getStrippedSourcesDir();
        if (strippedSourcesDir != null) {
            parser.prependSourcepathEntry(strippedSourcesDir.getPath());
        }
        options.getHeaderMap().loadMappings();
        TranslationProcessor translationProcessor = new TranslationProcessor(parser, loadDeadCodeMap());
        translationProcessor.processInputs(inputs);
        translationProcessor.processBuildClosureDependencies();
        if (ErrorUtil.errorCount() > 0) {
            return;
        }
        translationProcessor.postProcess();
        options.getHeaderMap().printMappings();
    } finally {
        if (parser != null) {
            try {
                parser.close();
            } catch (IOException e) {
                ErrorUtil.error(e.getMessage());
            }
        }
        Set<String> tempDirs = options.fileUtil().getTempDirs();
        for (String dir : tempDirs) {
            FileUtil.deleteTempDir(new File(dir));
        }
        FileUtil.deleteTempDir(preProcessorTempDir);
        FileUtil.deleteTempDir(strippedSourcesDir);
    }
}
Also used : ProcessingContext(com.google.devtools.j2objc.pipeline.ProcessingContext) TranslationProcessor(com.google.devtools.j2objc.pipeline.TranslationProcessor) InputFilePreprocessor(com.google.devtools.j2objc.pipeline.InputFilePreprocessor) IOException(java.io.IOException) File(java.io.File) GenerationBatch(com.google.devtools.j2objc.pipeline.GenerationBatch) ProGuardUsageParser(com.google.devtools.j2objc.util.ProGuardUsageParser) Parser(com.google.devtools.j2objc.util.Parser)

Aggregations

ProcessingContext (com.google.devtools.j2objc.pipeline.ProcessingContext)3 RegularInputFile (com.google.devtools.j2objc.file.RegularInputFile)2 InputFilePreprocessor (com.google.devtools.j2objc.pipeline.InputFilePreprocessor)2 TranslationProcessor (com.google.devtools.j2objc.pipeline.TranslationProcessor)2 File (java.io.File)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 InputFile (com.google.devtools.j2objc.file.InputFile)1 GenerationUnit (com.google.devtools.j2objc.gen.GenerationUnit)1 GenerationBatch (com.google.devtools.j2objc.pipeline.GenerationBatch)1 Parser (com.google.devtools.j2objc.util.Parser)1 PathClassLoader (com.google.devtools.j2objc.util.PathClassLoader)1 ProGuardUsageParser (com.google.devtools.j2objc.util.ProGuardUsageParser)1 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)1 Processor (javax.annotation.processing.Processor)1