Search in sources :

Example 51 with ASTParser

use of org.eclipse.jdt.core.dom.ASTParser in project bayou by capergroup.

the class Driver method createCompilationUnit.

private CompilationUnit createCompilationUnit(String classpath) throws IOException {
    ASTParser parser = ASTParser.newParser(AST.JLS8);
    File input = new File(options.cmdLine.getOptionValue("input-file"));
    parser.setSource(FileUtils.readFileToString(input, "utf-8").toCharArray());
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    parser.setUnitName("Program.java");
    parser.setEnvironment(new String[] { classpath != null ? classpath : "" }, new String[] { "" }, new String[] { "UTF-8" }, true);
    parser.setResolveBindings(true);
    return (CompilationUnit) parser.createAST(null);
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ASTParser(org.eclipse.jdt.core.dom.ASTParser) File(java.io.File)

Example 52 with ASTParser

use of org.eclipse.jdt.core.dom.ASTParser in project bayou by capergroup.

the class TrialsRunner method makeSketchFromSource.

/*
     * Build a sketch representation of the given source and unit name.
     *
     * Patterned after edu.rice.cs.caper.bayou.application.dom_driver.Driver
     */
private static DSubTree makeSketchFromSource(String source, String unitName) {
    if (source == null)
        throw new IllegalArgumentException("source");
    CompilationUnit cu;
    {
        ASTParser parser = ASTParser.newParser(AST.JLS8);
        parser.setSource(source.toCharArray());
        parser.setKind(ASTParser.K_COMPILATION_UNIT);
        // removing this always make the sketch null
        parser.setUnitName(unitName);
        parser.setEnvironment(new String[] { "" }, new String[] { "" }, new String[] { "UTF-8" }, true);
        parser.setResolveBindings(true);
        cu = (CompilationUnit) parser.createAST(null);
    }
    Options options = new Options();
    String sketch;
    try {
        Visitor visitor = new Visitor(cu, options);
        cu.accept(visitor);
        sketch = visitor.buildJson();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    if (sketch == null)
        return null;
    JSONObject sketchObject = new JSONObject(sketch);
    JSONObject program = sketchObject.getJSONArray("programs").getJSONObject(0);
    JSONObject astNode = program.getJSONObject("ast");
    RuntimeTypeAdapterFactory<DASTNode> nodeAdapter = RuntimeTypeAdapterFactory.of(DASTNode.class, "node").registerSubtype(DAPICall.class).registerSubtype(DBranch.class).registerSubtype(DExcept.class).registerSubtype(DLoop.class).registerSubtype(DSubTree.class);
    Gson gson = new GsonBuilder().serializeNulls().registerTypeAdapterFactory(nodeAdapter).create();
    return gson.fromJson(astNode.toString(), DSubTree.class);
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Options(edu.rice.cs.caper.bayou.core.dom_driver.Options) Visitor(edu.rice.cs.caper.bayou.core.dom_driver.Visitor) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) IOException(java.io.IOException) JSONObject(org.json.JSONObject) ASTParser(org.eclipse.jdt.core.dom.ASTParser)

Example 53 with ASTParser

use of org.eclipse.jdt.core.dom.ASTParser in project bayou by capergroup.

the class VisitorTest method testBuildJson.

@Test
public void testBuildJson() throws IOException {
    String source = "import java.io.BufferedReader;\n" + "import java.io.InputStreamReader;\n" + "import java.io.FileReader;\n" + "import java.io.File;\n" + "\n" + "class Test {\n" + "    BufferedReader br;\n" + "    public Test(File file) {\n" + "        br = new BufferedReader(new FileReader(file));\n" + "    }\n" + "}";
    String unitName = "Test.java";
    CompilationUnit cu;
    {
        ASTParser parser = ASTParser.newParser(AST.JLS8);
        parser.setSource(source.toCharArray());
        parser.setKind(ASTParser.K_COMPILATION_UNIT);
        parser.setUnitName(unitName);
        parser.setEnvironment(new String[] { "" }, new String[] { "" }, new String[] { "UTF-8" }, true);
        parser.setResolveBindings(true);
        cu = (CompilationUnit) parser.createAST(null);
    }
    Options options = new Options();
    Visitor visitor = new Visitor(cu, options);
    cu.accept(visitor);
    String sketch = visitor.buildJson();
    Assert.assertNotNull(sketch);
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Options(edu.rice.cs.caper.bayou.core.dom_driver.Options) Visitor(edu.rice.cs.caper.bayou.core.dom_driver.Visitor) ASTParser(org.eclipse.jdt.core.dom.ASTParser) Test(org.junit.Test)

Example 54 with ASTParser

use of org.eclipse.jdt.core.dom.ASTParser in project eclipse-pmd by acanda.

the class ASTQuickFixTestCase method createAST.

private CompilationUnit createAST(final org.eclipse.jface.text.Document document, final ASTQuickFix<ASTNode> quickFix) {
    final ASTParser astParser = ASTParser.newParser(AST.JLS4);
    astParser.setSource(document.get().toCharArray());
    astParser.setKind(ASTParser.K_COMPILATION_UNIT);
    astParser.setResolveBindings(quickFix.needsTypeResolution());
    astParser.setEnvironment(new String[0], new String[0], new String[0], true);
    final String name = last(params.pmdReferenceId.or("QuickFixTest").split("/"));
    astParser.setUnitName(format("{0}.java", name));
    final String version = last(params.language.or("java 1.7").split("\\s+"));
    astParser.setCompilerOptions(ImmutableMap.<String, String>builder().put(JavaCore.COMPILER_SOURCE, version).put(JavaCore.COMPILER_COMPLIANCE, version).put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, version).build());
    final CompilationUnit ast = (CompilationUnit) astParser.createAST(null);
    ast.recordModifications();
    return ast;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ASTParser(org.eclipse.jdt.core.dom.ASTParser)

Aggregations

ASTParser (org.eclipse.jdt.core.dom.ASTParser)54 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)43 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)27 ASTNode (org.eclipse.jdt.core.dom.ASTNode)12 RefactoringASTParser (org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser)9 IBinding (org.eclipse.jdt.core.dom.IBinding)7 ArrayList (java.util.ArrayList)6 IProblem (org.eclipse.jdt.core.compiler.IProblem)6 HashMap (java.util.HashMap)5 IType (org.eclipse.jdt.core.IType)5 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)4 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)4 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)4 File (java.io.File)3 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)3 ISourceRange (org.eclipse.jdt.core.ISourceRange)3 ASTRequestor (org.eclipse.jdt.core.dom.ASTRequestor)3 ArrayType (org.eclipse.jdt.core.dom.ArrayType)3 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)3