Search in sources :

Example 1 with Options

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

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

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