Search in sources :

Example 1 with GenerationUnit

use of com.google.devtools.j2objc.gen.GenerationUnit 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 GenerationUnit

use of com.google.devtools.j2objc.gen.GenerationUnit in project j2objc by google.

the class GenerationBatch method processJarFile.

private void processJarFile(String filename) {
    File f = findJarFile(filename);
    if (f == null) {
        ErrorUtil.error("No such file: " + filename);
        return;
    }
    // don't support Java-like source paths.
    if (options.emitLineDirectives() && options.isVerbose()) {
        ErrorUtil.warning("source debugging of jar files is not supported: " + filename);
    }
    GenerationUnit combinedUnit = null;
    if (globalCombinedUnit != null) {
        combinedUnit = globalCombinedUnit;
    } else if (options.getHeaderMap().combineSourceJars()) {
        combinedUnit = GenerationUnit.newCombinedJarUnit(filename, options);
    }
    try {
        ZipFile zfile = new ZipFile(f);
        try {
            boolean containsJavaFile = false;
            Enumeration<? extends ZipEntry> enumerator = zfile.entries();
            File tempDir = FileUtil.createTempDir(J2OBJC_TEMP_DIR_PREFIX);
            String tempDirPath = tempDir.getAbsolutePath();
            options.fileUtil().addTempDir(tempDirPath);
            options.fileUtil().appendSourcePath(tempDirPath);
            while (enumerator.hasMoreElements()) {
                ZipEntry entry = enumerator.nextElement();
                String internalPath = entry.getName();
                if (internalPath.endsWith(".java") || (options.translateClassfiles() && internalPath.endsWith(".class"))) {
                    // Extract JAR file to a temporary directory
                    File outputFile = options.fileUtil().extractZipEntry(tempDir, zfile, entry);
                    InputFile newFile = new RegularInputFile(outputFile.getAbsolutePath(), internalPath);
                    if (combinedUnit != null) {
                        inputs.add(new ProcessingContext(newFile, combinedUnit));
                    } else {
                        addExtractedJarSource(newFile, filename, internalPath);
                    }
                    containsJavaFile = true;
                }
            }
            if (!options.translateClassfiles() && !containsJavaFile) {
                ErrorUtil.error(filename + " does not contain any Java source files.");
            }
        } finally {
            // Also closes input stream.
            zfile.close();
        }
    } catch (ZipException e) {
        // Also catches JarExceptions
        logger.fine(e.getMessage());
        ErrorUtil.error("Error reading file " + filename + " as a zip or jar file.");
    } catch (IOException e) {
        ErrorUtil.error(e.getMessage());
    }
}
Also used : GenerationUnit(com.google.devtools.j2objc.gen.GenerationUnit) ZipFile(java.util.zip.ZipFile) RegularInputFile(com.google.devtools.j2objc.file.RegularInputFile) ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) InputFile(com.google.devtools.j2objc.file.InputFile) RegularInputFile(com.google.devtools.j2objc.file.RegularInputFile) File(java.io.File) ZipFile(java.util.zip.ZipFile) InputFile(com.google.devtools.j2objc.file.InputFile) RegularInputFile(com.google.devtools.j2objc.file.RegularInputFile)

Example 3 with GenerationUnit

use of com.google.devtools.j2objc.gen.GenerationUnit in project j2objc by google.

the class ProcessingContext method fromFile.

public static ProcessingContext fromFile(InputFile file, Options options) {
    Options.CombinedOutput globalOutput = options.globalCombinedOutput();
    GenerationUnit newGenUnit = globalOutput != null ? globalOutput.globalGenerationUnit() : GenerationUnit.newSingleFileUnit(file, options);
    return new ProcessingContext(file, newGenUnit);
}
Also used : Options(com.google.devtools.j2objc.Options) GenerationUnit(com.google.devtools.j2objc.gen.GenerationUnit)

Example 4 with GenerationUnit

use of com.google.devtools.j2objc.gen.GenerationUnit in project j2objc by google.

the class ProcessingContext method fromExtractedJarEntry.

public static ProcessingContext fromExtractedJarEntry(InputFile file, String sourceName, Options options) {
    Options.CombinedOutput globalOutput = options.globalCombinedOutput();
    GenerationUnit newGenUnit = globalOutput != null ? globalOutput.globalGenerationUnit() : GenerationUnit.newSingleExtractedJarEntryUnit(file, sourceName, options);
    return new ProcessingContext(file, newGenUnit);
}
Also used : Options(com.google.devtools.j2objc.Options) GenerationUnit(com.google.devtools.j2objc.gen.GenerationUnit)

Example 5 with GenerationUnit

use of com.google.devtools.j2objc.gen.GenerationUnit in project j2objc by google.

the class TranslationProcessor method processConvertedTree.

@Override
protected void processConvertedTree(ProcessingContext input, CompilationUnit unit) {
    String unitName = input.getOriginalSourcePath();
    if (logger.isLoggable(Level.INFO)) {
        System.out.println("translating " + unitName);
    }
    TimeTracker ticker = TimeTracker.getTicker(unitName, options.timingLevel());
    if (options.dumpAST()) {
        // Dump compilation unit to an .ast output file instead of translating.
        DebugASTDump.dumpUnit(unit);
    } else {
        applyMutations(unit, deadCodeMap, options.externalAnnotations(), ticker);
        ticker.tick("Tree mutations");
        ticker.printResults(System.out);
        GenerationUnit genUnit = input.getGenerationUnit();
        genUnit.addCompilationUnit(unit);
        outputs.add(genUnit);
        // Add out-of-date dependencies to translation list.
        if (closureQueue != null) {
            checkDependencies(unit);
        }
    }
    processedCount++;
}
Also used : GenerationUnit(com.google.devtools.j2objc.gen.GenerationUnit) TimeTracker(com.google.devtools.j2objc.util.TimeTracker)

Aggregations

GenerationUnit (com.google.devtools.j2objc.gen.GenerationUnit)6 Options (com.google.devtools.j2objc.Options)2 RegularInputFile (com.google.devtools.j2objc.file.RegularInputFile)2 InputFile (com.google.devtools.j2objc.file.InputFile)1 InputFilePreprocessor (com.google.devtools.j2objc.pipeline.InputFilePreprocessor)1 ProcessingContext (com.google.devtools.j2objc.pipeline.ProcessingContext)1 TranslationProcessor (com.google.devtools.j2objc.pipeline.TranslationProcessor)1 TimeTracker (com.google.devtools.j2objc.util.TimeTracker)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ZipEntry (java.util.zip.ZipEntry)1 ZipException (java.util.zip.ZipException)1 ZipFile (java.util.zip.ZipFile)1