-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParserExample.java
61 lines (50 loc) · 1.86 KB
/
ParserExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import japa.parser.JavaParser;
import japa.parser.ast.CompilationUnit;
import japa.parser.ast.body.MethodDeclaration;
import japa.parser.ast.expr.LiteralExpr;
import japa.parser.ast.expr.StringLiteralExpr;
import japa.parser.ast.visitor.GenericVisitorAdapter;
import japa.parser.ast.visitor.VoidVisitorAdapter;
import java.io.FileInputStream;
public class ParserExample {
public static void main(String[] args) throws Exception {
// creates an input stream for the file to be parsed
if (args.length < 1) {
System.err.println("Usage: <<filename.java>>");
return;
}
FileInputStream in = new FileInputStream(args[0]);
CompilationUnit cu;
try {
// parse the file
cu = JavaParser.parse(in);
} finally {
in.close();
}
// visit and print the methods names
new MethodVisitor().visit(cu, null);
// visit and print the string literals
new MyGenericVisitor().visit(cu, null);
// // prints the resulting compilation unit to default system output
// System.out.println(cu.toString());
}
/**
* Simple visitor implementation for visiting MethodDeclaration nodes.
*/
private static class MethodVisitor extends VoidVisitorAdapter {
@Override
public void visit(MethodDeclaration n, Object arg) {
// here you can access the attributes of the method.
// this method will be called for all methods in this
// CompilationUnit, including inner class methods
System.out.println(n.getName());
}
}
private static class MyGenericVisitor extends GenericVisitorAdapter {
@Override
public Object visit(StringLiteralExpr n, Object arg) {
System.out.println(n.getValue());
return arg;
}
}
}