use of japa.parser.ast.CompilationUnit in project japid42 by branaway.
the class JavaSyntaxTool method parse.
public static CompilationUnit parse(String src) throws ParseException {
ByteArrayInputStream in;
try {
in = new ByteArrayInputStream(src.getBytes(UTF_8));
CompilationUnit cu;
cu = JavaParser.parse(in, UTF_8);
return cu;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TokenMgrError e) {
throw new ParseException(e.getMessage());
}
return null;
}
use of japa.parser.ast.CompilationUnit in project japid42 by branaway.
the class JavaSyntaxTool method parseArgs.
// XXX this method does not properly parse thingsl like A<t> a
// it does not detect the error
@SuppressWarnings("unchecked")
public static List<String> parseArgs(String line) {
final List<String> ret = new ArrayList<String>();
if (line == null || line.trim().length() == 0)
return ret;
@SuppressWarnings("rawtypes") VoidVisitorAdapter visitor = new VoidVisitorAdapter() {
@Override
public void visit(MethodCallExpr n, Object arg) {
List<Expression> args = n.getArgs();
// api issue: args can be null in case of empty arg list
if (args != null)
for (Expression e : args) {
ret.add(e.toString());
}
}
};
line = line.trim();
if (line.startsWith("(")) {
// perhaps it's in the form of (...)
String cl = String.format(classTempForArgsNoParenthesis, line);
try {
CompilationUnit cu = parse(cl);
cu.accept(visitor, null);
return ret;
} catch (ParseException e) {
// perhaps not really (...). fall through
}
}
String cl = String.format(classTempForArgs, line);
try {
CompilationUnit cu = parse(cl);
cu.accept(visitor, null);
} catch (ParseException e) {
throw new RuntimeException("the line does not seem to be a valid arg list: " + line);
}
return ret;
}
use of japa.parser.ast.CompilationUnit in project enumerable by hraberg.
the class InMemoryCompilerTest method inMemoryCompilationFromJavaParserAST.
@Test
public void inMemoryCompilationFromJavaParserAST() throws Exception {
CompilationUnit cu = createCU();
assertEquals("Hello World from AST", invokeMain(compiler.compile(getFullyQualifiedName(cu), cu.toString())).trim());
}
use of japa.parser.ast.CompilationUnit in project Japid by branaway.
the class CompilerTests method testTagline.
@Test
public void testTagline() throws IOException, ParseException {
String srcFile = "tests/tagline.html";
String src = readFile(srcFile);
JapidTemplate bt = new JapidTemplate("tagline.html", src);
JapidAbstractCompiler cp = new JapidTemplateCompiler();
cp.compile(bt);
String code = bt.javaSource;
System.out.println(code);
CompilationUnit cu = JavaSyntaxTool.parse(code);
// System.out.println(cu);
}
use of japa.parser.ast.CompilationUnit in project Japid by branaway.
the class CompilerTests method testTemplateWithCallbackTagCalls.
@Test
public void testTemplateWithCallbackTagCalls() throws IOException, ParseException {
String src = readFile("JapidSample/app/japidviews/templates/AllPost.html");
JapidTemplate bt = new JapidTemplate("japidviews/templates/AllPost.html", src);
JapidAbstractCompiler cp = new JapidTemplateCompiler();
cp.compile(bt);
System.out.println(bt.javaSource);
CompilationUnit cu = JavaSyntaxTool.parse(bt.javaSource);
System.out.println(cu);
// assertTrue("invalid java code", JavaSyntaxValidator.isValid(bt.javaSource));
}
Aggregations