Example usage for org.eclipse.jdt.core.dom ASTParser createASTs

List of usage examples for org.eclipse.jdt.core.dom ASTParser createASTs

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom ASTParser createASTs.

Prototype

public void createASTs(String[] sourceFilePaths, String[] encodings, String[] bindingKeys,
        FileASTRequestor requestor, IProgressMonitor monitor) 

Source Link

Document

Creates ASTs for a batch of compilation units.

Usage

From source file:br.uff.ic.mergeguider.javaparser.JavaParser.java

private Storage generateASTs(String path) {
    ASTParser parser = ASTParser.newParser(AST.JLS8);

    parser.setResolveBindings(true);// w ww. ja  v a2 s .  c o  m
    parser.setBindingsRecovery(true);

    Map options = JavaCore.getOptions();

    options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_8);
    options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8);
    options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8);
    parser.setCompilerOptions(options);

    String[] srcDirs = FileUtils.getAllDirs(path);
    String[] javaFiles = FileUtils.getAllJavaFiles(path);

    parser.setEnvironment(null, srcDirs, null, true);

    Storage storage = new Storage();
    parser.createASTs(javaFiles, null, new String[0], storage, null);
    return storage;
}

From source file:com.google.dart.java2dart.Context.java

License:Open Source License

/**
 * @return the Java AST of the given Java {@link File} in context of {@link #sourceFolders}.
 *//*  w  w w  . j a va  2s . c  o  m*/
private Map<File, CompilationUnit> parseJavaFiles(final List<File> javaFiles) throws Exception {
    String paths[] = new String[javaFiles.size()];
    final Map<String, File> pathToFile = Maps.newHashMap();
    for (int i = 0; i < javaFiles.size(); i++) {
        File javaFile = javaFiles.get(i);
        String javaPath = javaFile.getAbsolutePath();
        paths[i] = javaPath;
        pathToFile.put(javaPath, javaFile);
    }
    // prepare Java parser
    ASTParser parser = ASTParser.newParser(AST.JLS4);
    {
        String[] classpathEntries = new String[classpathFiles.size()];
        for (int i = 0; i < classpathFiles.size(); i++) {
            classpathEntries[i] = classpathFiles.get(i).getAbsolutePath();
        }
        String[] sourceEntries = new String[sourceFolders.size()];
        for (int i = 0; i < sourceFolders.size(); i++) {
            sourceEntries[i] = sourceFolders.get(i).getAbsolutePath();
        }
        parser.setEnvironment(classpathEntries, sourceEntries, null, true);
    }
    parser.setResolveBindings(true);
    parser.setCompilerOptions(ImmutableMap.of(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5,
            JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED));
    // do parse
    final Map<File, CompilationUnit> units = Maps.newLinkedHashMap();
    parser.createASTs(paths, null, ArrayUtils.EMPTY_STRING_ARRAY, new FileASTRequestor() {
        @Override
        public void acceptAST(String sourceFilePath, org.eclipse.jdt.core.dom.CompilationUnit javaUnit) {
            File astFile = pathToFile.get(sourceFilePath);
            CompilationUnit dartUnit = SyntaxTranslator.translate(Context.this, javaUnit);
            units.put(astFile, dartUnit);
        }
    }, null);
    return units;
}

From source file:com.google.devtools.j2objc.jdt.JdtParser.java

License:Apache License

@Override
public void parseFiles(Collection<String> paths, final Handler handler, SourceVersion sourceVersion) {
    ASTParser parser = newASTParser(true, sourceVersion);
    FileASTRequestor astRequestor = new FileASTRequestor() {
        @Override// w  w  w  .jav  a2 s  . co m
        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);
}

From source file:com.google.devtools.j2objc.util.JdtParser.java

License:Apache License

public void parseFiles(List<InputFile> files, final Handler handler) {
    // We need the whole SourceFile to correctly handle a parsed ADT, so we keep track of it here.
    final Map<String, InputFile> reverseMap = new LinkedHashMap<String, InputFile>();
    for (InputFile file : files) {
        reverseMap.put(file.getPath(), file);
    }//from   w  ww. ja  v  a 2 s  . com

    ASTParser parser = newASTParser(true);
    FileASTRequestor astRequestor = new FileASTRequestor() {
        @Override
        public void acceptAST(String sourceFilePath, CompilationUnit ast) {
            logger.fine("acceptAST: " + sourceFilePath);
            if (checkCompilationErrors(sourceFilePath, ast)) {
                handler.handleParsedUnit(reverseMap.get(sourceFilePath), ast);
            }
        }
    };
    // 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[] paths = reverseMap.keySet().toArray(new String[reverseMap.size()]);
    parser.createASTs(paths, getEncodings(paths.length), paths, astRequestor, null);
}

From source file:com.ibm.wala.cast.java.translator.jdt.ecj.ECJSourceModuleTranslator.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//  w  ww. ja  v a 2  s .c  om
public void loadAllSources(Set<ModuleEntry> modules) {
    List<String> sources = new LinkedList<String>();
    Map<String, ModuleEntry> sourceMap = HashMapFactory.make();
    for (ModuleEntry m : modules) {
        if (m.isSourceFile()) {
            SourceFileModule s = (SourceFileModule) m;
            sourceMap.put(s.getAbsolutePath(), s);
            sources.add(s.getAbsolutePath());
        }
    }

    String[] sourceFiles = sources.toArray(new String[sources.size()]);
    final ASTParser parser = ASTParser.newParser(AST.JLS8);
    parser.setResolveBindings(true);
    parser.setEnvironment(libs, this.sources, null, false);
    Hashtable options = JavaCore.getOptions();
    options.put(JavaCore.COMPILER_SOURCE, "1.8");
    parser.setCompilerOptions(options);
    parser.createASTs(sourceFiles, null, new String[0], new ECJAstToIR(sourceMap), new NullProgressMonitor());
}

From source file:com.ibm.wala.cast.java.translator.jdt.ejc.ECJSourceModuleTranslator.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from  w  ww . j  a  v  a2s  . c o  m*/
public void loadAllSources(Set<ModuleEntry> modules) {
    List<String> sources = new LinkedList<String>();
    Map<String, ModuleEntry> sourceMap = HashMapFactory.make();
    for (ModuleEntry m : modules) {
        if (m.isSourceFile()) {
            SourceFileModule s = (SourceFileModule) m;
            sourceMap.put(s.getAbsolutePath(), s);
            sources.add(s.getAbsolutePath());
        }
    }

    String[] sourceFiles = sources.toArray(new String[sources.size()]);
    final ASTParser parser = ASTParser.newParser(AST.JLS8);
    parser.setResolveBindings(true);
    parser.setEnvironment(libs, null, null, false);
    Hashtable options = JavaCore.getOptions();
    options.put(JavaCore.COMPILER_SOURCE, "1.8");
    parser.setCompilerOptions(options);
    parser.createASTs(sourceFiles, null, new String[0], new ECJAstToIR(sourceMap), new NullProgressMonitor());
}

From source file:com.ibm.wala.cast.java.translator.jdt.ejc.EJCSourceModuleTranslator.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*  w  w w.jav  a2s  . c o m*/
public void loadAllSources(Set<ModuleEntry> modules) {
    List<String> sources = new LinkedList<String>();
    Map<String, ModuleEntry> sourceMap = HashMapFactory.make();
    for (ModuleEntry m : modules) {
        if (m.isSourceFile()) {
            SourceFileModule s = (SourceFileModule) m;
            sourceMap.put(s.getAbsolutePath(), s);
            sources.add(s.getAbsolutePath());
        }
    }

    String[] sourceFiles = sources.toArray(new String[sources.size()]);
    final ASTParser parser = ASTParser.newParser(AST.JLS8);
    parser.setResolveBindings(true);
    parser.setEnvironment(libs, null, null, false);
    Hashtable options = JavaCore.getOptions();
    options.put(JavaCore.COMPILER_SOURCE, "1.8");
    parser.setCompilerOptions(options);
    parser.createASTs(sourceFiles, null, new String[0], new EjcAstToIR(sourceMap), new NullProgressMonitor());
}

From source file:com.j2swift.util.JdtParser.java

License:Apache License

public void parseFiles(Collection<String> paths, final Handler handler) {
    ASTParser parser = newASTParser(true);
    FileASTRequestor astRequestor = new FileASTRequestor() {
        @Override/*  w  w w  . ja  v  a  2 s  .  c o m*/
        public void acceptAST(String sourceFilePath, CompilationUnit ast) {
            logger.fine("acceptAST: " + sourceFilePath);
            if (checkCompilationErrors(sourceFilePath, ast)) {
                handler.handleParsedUnit(sourceFilePath, ast);
            }
        }
    };
    // 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);
}

From source file:fr.labri.harmony.rta.junit.jdt.JDTGeneratorRTA.java

License:Open Source License

private boolean generate(String file) {

    if (!this.modifiedUrls.contains(file) && !this.modifiedUrls.isEmpty())
        return false;

    ASTParser parser = ASTParser.newParser(AST.JLS4);
    parser.setEnvironment(classPath, sourcePath, null, true);
    parser.setResolveBindings(true);//from w  w w .j  a va2s .c o m
    parser.setBindingsRecovery(true);
    parser.setStatementsRecovery(true);
    parser.setIgnoreMethodBodies(false);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);

    Map<String, String> pOptions = JavaCore.getOptions();
    pOptions.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_7);
    pOptions.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_7);
    pOptions.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_7);
    parser.setCompilerOptions(pOptions);

    JDTRequestorRTA req = null;
    String shortPath = file.substring(localPath.length());
    if (!shortPath.startsWith("/"))
        shortPath = "/" + shortPath;

    JavaFile jf = new JavaFile(file, shortPath, null);
    JDTVisitorRTA.currentFile = jf;

    if (file.contains("/test/") || file.contains("/tests/") || file.contains("/test-sources/")
            || file.contains("/testsrc/"))
        req = new JDTRequestorRTA(file, hashes, true);
    else
        req = new JDTRequestorRTA(file, hashes, false);
    parser.createASTs(new String[] { file }, null, new String[] {}, req, null);

    JavaFile j = new JavaFile(JDTVisitorRTA.currentFile.getFullPath(), JDTVisitorRTA.currentFile.getShortPath(),
            JDTVisitorRTA.currentFile.getEnclosedClass());
    JDTVisitorRTA.files.add(j);

    return true;
}

From source file:org.codejuicer.java2csharp.Java2CsharpMojo.java

License:Open Source License

private void createCompilationUnitsForJavaFiles(final File inputFolder) {
    // first process input folder to find java file
    processFolderToFindJavaClass(inputFolder);

    // now prepare ASTParser to process al java file found
    ASTParser parser = ASTParser.newParser(AST.JLS4);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    parser.setResolveBindings(true);//from w  ww  . j a  va 2s  . c  o  m
    // parser.setBindingsRecovery(true);
    parser.setEnvironment(null, new String[] { inputFolder.getAbsolutePath() }, new String[] { "UTF-8" }, true);

    // add compiler compliance rules to convert enums
    @SuppressWarnings("unchecked")
    Hashtable<String, String> options = JavaCore.getOptions();
    options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_5);
    options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_5);
    options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5);

    parser.setCompilerOptions(options);
    FileASTRequestor requestor = new CustomFileASTRequestor(getLog(), inputFolder.getAbsolutePath(),
            sourcePathEntry);
    parser.createASTs(sourcePathEntry.keySet().toArray(new String[0]), charsetEntry.toArray(new String[0]),
            new String[] { "" }, requestor, null);
}