使用JavaParser进行java源码解析
2014-02-11 09:25
477 查看
JavaParser下载地址:http://code.google.com/p/javaparser/downloads/list
刚开始接触的时候觉得和JDT里的Parser类不太一样。查了一下WiKi,发现它访问编译单元成员,需要调用相应子元素对应的Visitor类的visit()方法。此方法可以通过复写来实现自己需要的功能。我需要的功能是将编译单元里的所有MethodDeclaration取出,因此利用传递句柄的方式,实现代码如下:
官方还提供了不通过Visitor改变一个类成员方法的示例(相对Visitor更复杂):
最后附上WiKi里给出的编译单元创建示例:
刚开始接触的时候觉得和JDT里的Parser类不太一样。查了一下WiKi,发现它访问编译单元成员,需要调用相应子元素对应的Visitor类的visit()方法。此方法可以通过复写来实现自己需要的功能。我需要的功能是将编译单元里的所有MethodDeclaration取出,因此利用传递句柄的方式,实现代码如下:
public static List<MethodDeclaration> getMethodList(CompilationUnit cu) { List<MethodDeclaration> methodList = new ArrayList<MethodDeclaration>(); new MethodGetterVisitor().visit(cu, methodList); return methodList; } private static class MethodGetterVisitor extends VoidVisitorAdapter<Object> { @SuppressWarnings("unchecked") @Override public void visit(MethodDeclaration n, Object arg) { List<MethodDeclaration> methodList = new ArrayList<MethodDeclaration>(); methodList = (List<MethodDeclaration>) arg; methodList.add(n); } }
官方还提供了不通过Visitor改变一个类成员方法的示例(相对Visitor更复杂):
public class MethodChanger { public static void main(String[] args) throws Exception { // creates an input stream for the file to be parsed FileInputStream in = new FileInputStream("test.java"); CompilationUnit cu; try { // parse the file cu = JavaParser.parse(in); } finally { in.close(); } // change the methods names and parameters changeMethods(cu); // prints the changed compilation unit System.out.println(cu.toString()); } private static void changeMethods(CompilationUnit cu) { List<TypeDeclaration> types = cu.getTypes(); for (TypeDeclaration type : types) { List<BodyDeclaration> members = type.getMembers(); for (BodyDeclaration member : members) { if (member instanceof MethodDeclaration) { MethodDeclaration method = (MethodDeclaration) member; changeMethod(method); } } } } private static void changeMethod(MethodDeclaration n) { // change the name of the method to upper case n.setName(n.getName().toUpperCase()); // create the new parameter Parameter newArg = ASTHelper.createParameter(ASTHelper.INT_TYPE, "value"); // add the parameter to the method ASTHelper.addParameter(n, newArg); } }
最后附上WiKi里给出的编译单元创建示例:
public class ClassCreator { public static void main(String[] args) throws Exception { // creates the compilation unit CompilationUnit cu = createCU(); // prints the created compilation unit System.out.println(cu.toString()); } /** * creates the compilation unit */ private static CompilationUnit createCU() { CompilationUnit cu = new CompilationUnit(); // set the package cu.setPakage(new PackageDeclaration(ASTHelper.createNameExpr("java.parser.test"))); // create the type declaration ClassOrInterfaceDeclaration type = new ClassOrInterfaceDeclaration(ModifierSet.PUBLIC, false, "GeneratedClass"); ASTHelper.addTypeDeclaration(cu, type); // create a method MethodDeclaration method = new MethodDeclaration(ModifierSet.PUBLIC, ASTHelper.VOID_TYPE, "main"); method.setModifiers(ModifierSet.addModifier(method.getModifiers(), ModifierSet.STATIC)); ASTHelper.addMember(type, method); // add a parameter to the method Parameter param = ASTHelper.createParameter(ASTHelper.createReferenceType("String", 0), "args"); param.setVarArgs(true); ASTHelper.addParameter(method, param); // add a body to the method BlockStmt block = new BlockStmt(); method.setBody(block); // add a statement do the method body NameExpr clazz = new NameExpr("System"); FieldAccessExpr field = new FieldAccessExpr(clazz, "out"); MethodCallExpr call = new MethodCallExpr(field, "println"); ASTHelper.addArgument(call, new StringLiteralExpr("Hello World!")); ASTHelper.addStmt(block, call); return cu; } }
相关文章推荐
- java中使用net.sf.json对json进行解析
- Java【集合系列】-11- Hashtable详细介绍(源码解析)和使用示例
- Java 集合系列06之 Vector详细介绍(源码解析)和使用示例
- Java 集合系列Stack详细介绍(源码解析)和使用示例
- Java【集合系列】-15- HashSet详细介绍(源码解析)和使用示例
- Java解析HTML之HTMLParser使用与详解
- Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例
- Java解析HTML之HTMLParser使用与详解
- Java 集合系列17之 TreeSet详细介绍(源码解析)和使用示例
- Java 集合系列 06 Stack详细介绍(源码解析)和使用示例
- Java 集合系列 05 Vector详细介绍(源码解析)和使用示例
- Java解析HTML之HTMLParser使用与详解
- Java 集合系列10之 HashMap详细介绍(源码解析)和使用示例
- Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例
- Java 集合系列06之 Vector详细介绍(源码解析)和使用示例
- Java 集合系列11之 Hashtable详细介绍(源码解析)和使用示例
- Java 集合系列07之 Stack详细介绍(源码解析)和使用示例
- Java【集合系列】-06-Vector详细介绍(源码解析)和使用示例
- Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例
- Java 集合系列12之 TreeMap详细介绍(源码解析)和使用示例