1、参考文档:http://code.google.com/p/javaparser/wiki/UsingThisParser
2、添加maven依赖:
download源代码:
svn checkouthttp://javaparser.googlecode.com/svn/trunk/ javaparser-read-only
3、常用API的使用
public class CuPrinter { 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 filecu = JavaParser.parse(in); } finally { in.close(); } // prints the resulting compilation unit to default system output System.out.println(cu.toString()); }
public class MethodPrinter { 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 filecu = JavaParser.parse(in); } finally { in.close(); } // visit and print the methods names new MethodVisitor().visit(cu, null); } /**
* 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()); } }
}
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 filecu = JavaParser.parse(in); } finally { in.close(); } // visit and change the methods names and parameters new MethodChangerVisitor().visit(cu, null); // prints the changed compilation unit System.out.println(cu.toString()); } /**
* Simple visitor implementation for visiting MethodDeclaration nodes.
*/ private static class MethodChangerVisitor extends VoidVisitorAdapter { @Override public void visit(MethodDeclaration n, Object arg) { // 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); } }
}
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 filecu = 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); }
}
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; }
}
4、结论:操作相比jdt更加的容易
原网址: 访问
创建于: 2021-12-28 20:12:52
目录: default
标签: 无
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论