Search in sources :

Example 6 with InputFile

use of com.google.devtools.j2objc.file.InputFile 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()) {
        ErrorUtil.warning("source debugging of jar files is not supported: " + filename);
    }
    GenerationUnit combinedUnit = null;
    if (options.getHeaderMap().combineSourceJars()) {
        combinedUnit = GenerationUnit.newCombinedJarUnit(filename, options);
    }
    try {
        ZipFile zfile = new ZipFile(f);
        try {
            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")) {
                    // 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);
                    }
                }
            }
        } 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 7 with InputFile

use of com.google.devtools.j2objc.file.InputFile in project j2objc by google.

the class InputFilePreprocessor method processPackageInfoSource.

private void processPackageInfoSource(ProcessingContext input) throws IOException {
    InputFile file = input.getFile();
    String source = options.fileUtil().readFile(file);
    CompilationUnit compilationUnit = parser.parse(FileUtil.getMainTypeName(file), file.getUnitName(), source);
    if (compilationUnit != null) {
        extractPackagePrefix(file, compilationUnit);
    }
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) InputFile(com.google.devtools.j2objc.file.InputFile) RegularInputFile(com.google.devtools.j2objc.file.RegularInputFile)

Example 8 with InputFile

use of com.google.devtools.j2objc.file.InputFile in project j2objc by google.

the class BuildClosureQueue method getFileForName.

private InputFile getFileForName(String name) {
    // Check if class exists on classpath.
    if (findClassFile(name)) {
        logger.finest("no source for " + name + ", class found");
        return null;
    }
    InputFile inputFile = null;
    try {
        inputFile = options.fileUtil().findOnSourcePath(name);
    } catch (IOException e) {
        ErrorUtil.warning(e.getMessage());
    }
    if (inputFile == null) {
        return null;
    }
    // Check if the source file is older than the generated header file.
    File headerSource = new File(options.fileUtil().getOutputDirectory(), name.replace('.', File.separatorChar) + ".h");
    if (headerSource.exists() && inputFile.lastModified() < headerSource.lastModified()) {
        return null;
    }
    return inputFile;
}
Also used : IOException(java.io.IOException) InputFile(com.google.devtools.j2objc.file.InputFile) File(java.io.File) InputFile(com.google.devtools.j2objc.file.InputFile)

Example 9 with InputFile

use of com.google.devtools.j2objc.file.InputFile in project j2objc by google.

the class BuildClosureQueue method getNextFile.

/**
   * Returns the next Java source file to be processed. Returns null if the
   * queue is empty.
   */
public InputFile getNextFile() {
    for (Iterator<String> iter = queuedNames.iterator(); iter.hasNext(); ) {
        String name = iter.next();
        iter.remove();
        processedNames.add(name);
        InputFile file = getFileForName(name);
        if (file != null) {
            return file;
        }
    }
    return null;
}
Also used : InputFile(com.google.devtools.j2objc.file.InputFile)

Example 10 with InputFile

use of com.google.devtools.j2objc.file.InputFile in project j2objc by google.

the class InputFilePreprocessor method processRegularSource.

private void processRegularSource(ProcessingContext input) throws IOException {
    InputFile file = input.getFile();
    String source = options.fileUtil().readFile(file);
    boolean shouldMapHeaders = options.getHeaderMap().useSourceDirectories();
    boolean doIncompatibleStripping = source.contains("J2ObjCIncompatible");
    if (!(shouldMapHeaders || doIncompatibleStripping)) {
        // No need to parse.
        return;
    }
    Parser.ParseResult parseResult = parser.parseWithoutBindings(file, source);
    if (parseResult == null) {
        // The parser found and reported one or more errors.
        return;
    }
    String qualifiedName = parseResult.mainTypeName();
    if (shouldMapHeaders) {
        options.getHeaderMap().put(qualifiedName, input.getGenerationUnit().getOutputPath() + ".h");
    }
    if (doIncompatibleStripping) {
        parseResult.stripIncompatibleSource();
        File strippedDir = getCreatedStrippedSourcesDir();
        String relativePath = qualifiedName.replace('.', File.separatorChar) + ".java";
        File strippedFile = new File(strippedDir, relativePath);
        Files.createParentDirs(strippedFile);
        Files.write(parseResult.getSource(), strippedFile, options.fileUtil().getCharset());
        input.setFile(new RegularInputFile(strippedFile.getPath(), relativePath));
    }
}
Also used : RegularInputFile(com.google.devtools.j2objc.file.RegularInputFile) InputFile(com.google.devtools.j2objc.file.InputFile) RegularInputFile(com.google.devtools.j2objc.file.RegularInputFile) File(java.io.File) InputFile(com.google.devtools.j2objc.file.InputFile) RegularInputFile(com.google.devtools.j2objc.file.RegularInputFile) Parser(com.google.devtools.j2objc.util.Parser)

Aggregations

InputFile (com.google.devtools.j2objc.file.InputFile)10 RegularInputFile (com.google.devtools.j2objc.file.RegularInputFile)5 File (java.io.File)4 IOException (java.io.IOException)4 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)2 ZipFile (java.util.zip.ZipFile)2 JarredInputFile (com.google.devtools.j2objc.file.JarredInputFile)1 GenerationUnit (com.google.devtools.j2objc.gen.GenerationUnit)1 Parser (com.google.devtools.j2objc.util.Parser)1 ZipEntry (java.util.zip.ZipEntry)1 ZipException (java.util.zip.ZipException)1