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