Search in sources :

Example 1 with Visitor

use of edu.rice.cs.caper.bayou.core.dom_driver.Visitor in project bayou by capergroup.

the class Driver method execute.

public void execute(String classpath) throws IOException {
    CompilationUnit cu = createCompilationUnit(classpath);
    Visitor visitor = new Visitor(cu, options);
    cu.accept(visitor);
    String json = visitor.buildJson();
    if (json == null)
        return;
    PrintWriter output;
    if (options.cmdLine.hasOption("output-file"))
        output = new PrintWriter(options.cmdLine.getOptionValue("output-file"));
    else
        output = new PrintWriter(System.out);
    output.write(json);
    output.flush();
    output.close();
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Visitor(edu.rice.cs.caper.bayou.core.dom_driver.Visitor) PrintWriter(java.io.PrintWriter)

Example 2 with Visitor

use of edu.rice.cs.caper.bayou.core.dom_driver.Visitor 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 3 with Visitor

use of edu.rice.cs.caper.bayou.core.dom_driver.Visitor 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)

Aggregations

Visitor (edu.rice.cs.caper.bayou.core.dom_driver.Visitor)3 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)3 Options (edu.rice.cs.caper.bayou.core.dom_driver.Options)2 ASTParser (org.eclipse.jdt.core.dom.ASTParser)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 JSONObject (org.json.JSONObject)1 Test (org.junit.Test)1