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);
}
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);
}
Aggregations