use of japa.parser.ParseException in project Japid by branaway.
the class JavaSyntaxValidatorTest method testBuildValidAst.
@Test
public void testBuildValidAst() {
String validSrc = "package testdir.d1;\r\n" + "\r\n" + "public class A {\r\n" + " Boo 不不 = new Boo('不'); \n" + "}\r\n" + "";
CompilationUnit cu = null;
try {
cu = JavaSyntaxTool.parse(validSrc);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
assertNotNull(cu);
}
use of japa.parser.ParseException in project Japid by branaway.
the class JavaSyntaxTool method parseCode.
public static List<CodeNode> parseCode(String code) {
try {
final List<CodeNode> nodes = new LinkedList();
CompilationUnit cu = parse(code);
VoidVisitorAdapter visitor = new VoidVisitorAdapter() {
int nested = 0;
@Override
public void visit(AnnotationDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(AnnotationMemberDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ArrayAccessExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ArrayCreationExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ArrayInitializerExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(AssertStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(AssignExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(BinaryExpr n, Object arg) {
if (n.getOperator() == Operator.binOr) {
nodes.add(new CodeNode(nested++, new BinaryOrExpr(n)));
} else {
nodes.add(new CodeNode(nested++, n));
}
super.visit(n, arg);
nested--;
}
@Override
public void visit(BlockComment n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(BlockStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(BooleanLiteralExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(BreakStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(CastExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(CatchClause n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(CharLiteralExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ClassExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ClassOrInterfaceDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ClassOrInterfaceType n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(CompilationUnit n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ConditionalExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ConstructorDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ContinueStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(DoStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(DoubleLiteralExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(EmptyMemberDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(EmptyStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(EmptyTypeDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(EnclosedExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(EnumConstantDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(EnumDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ExplicitConstructorInvocationStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ExpressionStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(FieldAccessExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(FieldDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ForeachStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ForStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(IfStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ImportDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(InitializerDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(InstanceOfExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(IntegerLiteralExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(IntegerLiteralMinValueExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(JavadocComment n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(LabeledStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(LineComment n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(LongLiteralExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(LongLiteralMinValueExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(MarkerAnnotationExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(MemberValuePair n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(MethodCallExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(MethodDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(NameExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(NormalAnnotationExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(NullLiteralExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ObjectCreationExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(PackageDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(Parameter n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(PrimitiveType n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(QualifiedNameExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ReferenceType n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ReturnStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(SingleMemberAnnotationExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(StringLiteralExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(SuperExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(SwitchEntryStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(SwitchStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(SynchronizedStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ThisExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ThrowStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(TryStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(TypeDeclarationStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(TypeParameter n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(UnaryExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(VariableDeclarationExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(VariableDeclarator n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(VariableDeclaratorId n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(VoidType n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(WhileStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(WildcardType n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
};
cu.accept(visitor, null);
return nodes;
} catch (ParseException e) {
throw new RuntimeException("invalid Java code: " + code + ". " + e);
}
}
use of japa.parser.ParseException in project Japid by branaway.
the class JavaSyntaxTool method isValidMethDecl.
/**
* verify that line is a valid method declaration part, excluding method body and the {}
* @param line: something like foo(int a, String b)
*/
public static void isValidMethDecl(String line) {
final String classTempForMeth = "class T { %s{} }";
String classString = String.format(classTempForMeth, line);
try {
CompilationUnit cu = parse(classString);
VoidVisitorAdapter visitor = new VoidVisitorAdapter() {
};
cu.accept(visitor, null);
} catch (ParseException e) {
throw new RuntimeException("the line does not seem to be a valid method declaration: " + line + ". Was expecting something like foo(int a, String b).");
}
}
use of japa.parser.ParseException in project Japid by branaway.
the class JavaSyntaxTool method parseParams.
/**
* parse a line of text that is supposed to be parameter list for a method
* declaration.
*
* TODO: the parameter annotation, modifiers, etc ignored. should do it!
*
* @param line
* @return
*/
public static List<Parameter> parseParams(String line) {
final List<Parameter> ret = new ArrayList<Parameter>();
if (line == null || line.trim().length() == 0)
return ret;
// make it tolerant of lowercase default
line = line.replace("@default(", "@Default(");
String cl = String.format(classTempForParams, line);
try {
CompilationUnit cu = parse(cl);
VoidVisitorAdapter visitor = new VoidVisitorAdapter() {
@Override
public void visit(Parameter p, Object arg) {
ret.add(p);
}
};
cu.accept(visitor, null);
} catch (ParseException e) {
throw new RuntimeException("the line does not seem to be a valid param list declaration: " + line);
}
return ret;
}
use of japa.parser.ParseException in project Japid by branaway.
the class JavaSyntaxTool method isOpenIf.
public static boolean isOpenIf(String part) {
part = part.trim();
if (part.endsWith("{"))
part = part.substring(0, part.length() - 1);
String classString = String.format(IF_PREDICATE_OPEN, part);
try {
CompilationUnit cu = parse(classString);
return true;
} catch (ParseException e) {
return false;
}
}
Aggregations