Search in sources :

Example 6 with RegularInputFile

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

the class JdtParser method parseFiles.

@Override
public void parseFiles(Collection<String> paths, final Handler handler, SourceVersion sourceVersion) {
    ASTParser parser = newASTParser(true, sourceVersion);
    FileASTRequestor astRequestor = new FileASTRequestor() {

        @Override
        public void acceptAST(String sourceFilePath, CompilationUnit ast) {
            logger.fine("acceptAST: " + sourceFilePath);
            if (checkCompilationErrors(sourceFilePath, ast)) {
                RegularInputFile file = new RegularInputFile(sourceFilePath);
                try {
                    String source = options.fileUtil().readFile(file);
                    ParserEnvironment parserEnv = new JdtParserEnvironment(ast.getAST());
                    TranslationEnvironment env = new TranslationEnvironment(options, parserEnv);
                    com.google.devtools.j2objc.ast.CompilationUnit unit = TreeConverter.convertCompilationUnit(env, ast, sourceFilePath, FileUtil.getMainTypeName(file), source);
                    handler.handleParsedUnit(sourceFilePath, unit);
                } catch (IOException e) {
                    ErrorUtil.error("Error reading file " + file.getOriginalLocation() + ": " + e.getMessage());
                }
            }
        }
    };
    // JDT fails to resolve all secondary bindings unless there are the same
    // number of "binding key" strings as source files. It doesn't appear to
    // matter what the binding key strings should be (as long as they're non-
    // null), so the paths array is reused.
    String[] pathsArray = paths.toArray(new String[paths.size()]);
    parser.createASTs(pathsArray, getEncodings(pathsArray.length), pathsArray, astRequestor, null);
}
Also used : FileASTRequestor(org.eclipse.jdt.core.dom.FileASTRequestor) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IOException(java.io.IOException) TranslationEnvironment(com.google.devtools.j2objc.util.TranslationEnvironment) RegularInputFile(com.google.devtools.j2objc.file.RegularInputFile) ASTParser(org.eclipse.jdt.core.dom.ASTParser) ParserEnvironment(com.google.devtools.j2objc.util.ParserEnvironment)

Example 7 with RegularInputFile

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

the class GenerationBatch method processJavaFile.

private void processJavaFile(String filename) {
    InputFile inputFile;
    try {
        inputFile = new RegularInputFile(filename, filename);
        if (!inputFile.exists()) {
            // Convert to a qualified name and search on the sourcepath.
            String qualifiedName = filename.substring(0, filename.length() - 5).replace(File.separatorChar, '.');
            inputFile = options.fileUtil().findOnSourcePath(qualifiedName);
            if (inputFile == null) {
                ErrorUtil.error("No such file: " + filename);
                return;
            }
        }
    } catch (IOException e) {
        ErrorUtil.warning(e.getMessage());
        return;
    }
    addSource(inputFile);
}
Also used : RegularInputFile(com.google.devtools.j2objc.file.RegularInputFile) IOException(java.io.IOException) InputFile(com.google.devtools.j2objc.file.InputFile) RegularInputFile(com.google.devtools.j2objc.file.RegularInputFile)

Example 8 with RegularInputFile

use of com.google.devtools.j2objc.file.RegularInputFile 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 9 with RegularInputFile

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

the class JdtParser method parse.

@Override
public com.google.devtools.j2objc.ast.CompilationUnit parse(String mainTypeName, String path, String source) {
    int errors = ErrorUtil.errorCount();
    org.eclipse.jdt.core.dom.CompilationUnit unit = parse(path, source, true);
    if (ErrorUtil.errorCount() > errors) {
        return null;
    }
    if (mainTypeName == null) {
        RegularInputFile file = new RegularInputFile(path);
        mainTypeName = FileUtil.getQualifiedMainTypeName(file, unit);
    }
    ParserEnvironment parserEnv = new JdtParserEnvironment(unit.getAST());
    TranslationEnvironment env = new TranslationEnvironment(options, parserEnv);
    return TreeConverter.convertCompilationUnit(env, unit, path, mainTypeName, source);
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) TranslationEnvironment(com.google.devtools.j2objc.util.TranslationEnvironment) RegularInputFile(com.google.devtools.j2objc.file.RegularInputFile) ParserEnvironment(com.google.devtools.j2objc.util.ParserEnvironment)

Example 10 with RegularInputFile

use of com.google.devtools.j2objc.file.RegularInputFile 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

RegularInputFile (com.google.devtools.j2objc.file.RegularInputFile)12 File (java.io.File)5 InputFile (com.google.devtools.j2objc.file.InputFile)4 Parser (com.google.devtools.j2objc.util.Parser)3 IOException (java.io.IOException)3 GenerationUnit (com.google.devtools.j2objc.gen.GenerationUnit)2 InputFilePreprocessor (com.google.devtools.j2objc.pipeline.InputFilePreprocessor)2 ParserEnvironment (com.google.devtools.j2objc.util.ParserEnvironment)2 TranslationEnvironment (com.google.devtools.j2objc.util.TranslationEnvironment)2 ZipFile (java.util.zip.ZipFile)2 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)2 JarredInputFile (com.google.devtools.j2objc.file.JarredInputFile)1 GenerationBatch (com.google.devtools.j2objc.pipeline.GenerationBatch)1 ProcessingContext (com.google.devtools.j2objc.pipeline.ProcessingContext)1 TranslationProcessor (com.google.devtools.j2objc.pipeline.TranslationProcessor)1 ProGuardUsageParser (com.google.devtools.j2objc.util.ProGuardUsageParser)1 ArrayList (java.util.ArrayList)1 ZipEntry (java.util.zip.ZipEntry)1 ZipException (java.util.zip.ZipException)1 ASTParser (org.eclipse.jdt.core.dom.ASTParser)1