Search in sources :

Example 1 with TranslationEnvironment

use of com.google.devtools.j2objc.util.TranslationEnvironment 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 2 with TranslationEnvironment

use of com.google.devtools.j2objc.util.TranslationEnvironment 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 3 with TranslationEnvironment

use of com.google.devtools.j2objc.util.TranslationEnvironment in project j2objc by google.

the class TreeConverter method convertCompilationUnit.

public static CompilationUnit convertCompilationUnit(Options options, JavacEnvironment env, JCTree.JCCompilationUnit javacUnit) {
    String sourceFilePath = getPath(javacUnit.getSourceFile());
    try {
        TreeConverter converter = new TreeConverter(javacUnit, env);
        JavaFileObject sourceFile = javacUnit.getSourceFile();
        String source = sourceFile.getCharContent(false).toString();
        String mainTypeName = FileUtil.getMainTypeName(sourceFile);
        converter.newUnit = new CompilationUnit(new TranslationEnvironment(options, env), sourceFilePath, mainTypeName, source);
        PackageElement pkg = javacUnit.packge != null ? javacUnit.packge : env.defaultPackage();
        converter.newUnit.setPackage(converter.convertPackage(pkg, Trees.instance(env.task())));
        for (JCTree type : javacUnit.getTypeDecls()) {
            TreeNode newNode = converter.convert(type);
            if (newNode.getKind() != TreeNode.Kind.EMPTY_STATEMENT) {
                converter.newUnit.addType((AbstractTypeDeclaration) newNode);
            }
        }
        addOcniComments(converter.newUnit, options.jsniWarnings());
        return converter.newUnit;
    } catch (Throwable e) {
        ErrorUtil.fatalError(e, sourceFilePath);
        return null;
    }
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) JavaFileObject(javax.tools.JavaFileObject) TranslationEnvironment(com.google.devtools.j2objc.util.TranslationEnvironment) TreeNode(com.google.devtools.j2objc.ast.TreeNode) JCTree(com.sun.tools.javac.tree.JCTree) PackageElement(javax.lang.model.element.PackageElement)

Aggregations

TranslationEnvironment (com.google.devtools.j2objc.util.TranslationEnvironment)3 RegularInputFile (com.google.devtools.j2objc.file.RegularInputFile)2 ParserEnvironment (com.google.devtools.j2objc.util.ParserEnvironment)2 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)2 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)1 TreeNode (com.google.devtools.j2objc.ast.TreeNode)1 JCTree (com.sun.tools.javac.tree.JCTree)1 IOException (java.io.IOException)1 PackageElement (javax.lang.model.element.PackageElement)1 JavaFileObject (javax.tools.JavaFileObject)1 ASTParser (org.eclipse.jdt.core.dom.ASTParser)1 FileASTRequestor (org.eclipse.jdt.core.dom.FileASTRequestor)1