jdt之java源文件解析

 
1、加入jdt的相关依赖
  
  
  
  
  1. <dependency> 
  2.             <groupId>org.eclipse.tycho</groupId> 
  3.             <artifactId>org.eclipse.jdt.core</artifactId> 
  4.             <version>3.6.2.v_A76_R36x</version> 
  5.         </dependency> 
  6.         <dependency> 
  7.             <groupId>org.eclipse.core</groupId> 
  8.             <artifactId>org.eclipse.core.jobs</artifactId> 
  9.             <version>3.5.0.v20100515</version> 
  10.         </dependency> 
  11.         <dependency> 
  12.             <groupId>org.eclipse.core</groupId> 
  13.             <artifactId>org.eclipse.core.contenttype</artifactId> 
  14.             <version>3.4.100.v20100505-1235</version> 
  15.         </dependency> 
  16.         <dependency> 
  17.             <groupId>org.eclipse.core</groupId> 
  18.             <artifactId>org.eclipse.core.resources</artifactId> 
  19.             <version>3.6.0.v20100526-0737</version> 
  20.         </dependency> 
  21.         <dependency> 
  22.             <groupId>org.eclipse.core</groupId> 
  23.             <artifactId>org.eclipse.core.runtime</artifactId> 
  24.             <version>3.6.0.v20100505</version> 
  25.         </dependency> 
  26.         <dependency> 
  27.             <groupId>org.eclipse.osgi</groupId> 
  28.             <artifactId>org.eclipse.osgi</artifactId> 
  29.             <version>3.6.0.v20100517</version> 
  30.         </dependency> 
2、JLS2和JLS3的差别,参考: http://publib.boulder.ibm.com/infocenter/iadthelp/v6r0/index.jsp?topic=/org.eclipse.jdt.doc.isv/reference/api/overview-summary.html
原文解释为:
 
Method declaration AST node type. A method declaration is the union of a method declaration and a constructor declaration. For JLS2:
 MethodDeclaration:
    [ Javadoc ] { Modifier } ( Type | void ) Identifier (
        [ FormalParameter 
 		     { , FormalParameter } ] ) {[ ] }
        [ throws TypeName { , TypeName } ] ( Block | ; )
 ConstructorDeclaration:
    [ Javadoc ] { Modifier } Identifier (
 		  [ FormalParameter
 			 { , FormalParameter } ] )
        [throws TypeName { , TypeName } ] Block
 
For JLS3, type parameters and reified modifiers (and annotations) were added:
 MethodDeclaration:
    [ Javadoc ] { ExtendedModifier }
		  [ < TypeParameter { , TypeParameter } > ]
        ( Type | void ) Identifier (
        [ FormalParameter 
 		     { , FormalParameter } ] ) {[ ] }
        [ throws TypeName { , TypeName } ] ( Block | ; )
 ConstructorDeclaration:
    [ Javadoc ] { ExtendedModifier } Identifier (
 		  [ FormalParameter
 			 { , FormalParameter } ] )
 
3、代码样例
 
  
  
  
  
  1. import java.io.File; 
  2. import java.io.FileInputStream; 
  3. import java.io.IOException; 
  4. import java.util.List; 
  5.  
  6. import org.eclipse.jdt.core.dom.AST; 
  7. import org.eclipse.jdt.core.dom.ASTParser; 
  8. import org.eclipse.jdt.core.dom.CompilationUnit; 
  9. import org.eclipse.jdt.core.dom.FieldDeclaration; 
  10. import org.eclipse.jdt.core.dom.ImportDeclaration; 
  11. import org.eclipse.jdt.core.dom.MethodDeclaration; 
  12. import org.eclipse.jdt.core.dom.Modifier; 
  13. import org.eclipse.jdt.core.dom.PackageDeclaration; 
  14. import org.eclipse.jdt.core.dom.TypeDeclaration; 
  15. import org.eclipse.jdt.core.dom.VariableDeclarationFragment; 
  
  
  
  
  1. public static void main(String[] args) throws Exception { 
  2.       String content = read("D:\\HelloWorld.java"); //java源文件
  3.       //创建解析器  
  4.       ASTParser parsert = ASTParser.newParser(AST.JLS3); 
  5.       //设定解析器的源代码字符  
  6.       parsert.setSource(content.toCharArray()); 
  7.       //使用解析器进行解析并返回AST上下文结果(CompilationUnit为根节点)  
  8.       CompilationUnit result = (CompilationUnit) parsert.createAST(null); 
  9.  
  10.       //获取类型  
  11.       List types = result.types(); 
  12.       //取得类型声明  
  13.       TypeDeclaration typeDec = (TypeDeclaration) types.get(0); 
  14.  
  15.       //##############获取源代码结构信息#################  
  16.       //引用import  
  17.       List importList = result.imports(); 
  18.       //取得包名  
  19.       PackageDeclaration packetDec = result.getPackage(); 
  20.       //取得类名  
  21.       String className = typeDec.getName().toString(); 
  22.       //取得函数(Method)声明列表  
  23.       MethodDeclaration methodDec[] = typeDec.getMethods(); 
  24.       //取得函数(Field)声明列表  
  25.       FieldDeclaration fieldDec[] = typeDec.getFields(); 
  26.  
  27.       //输出包名  
  28.       System.out.println("包:"); 
  29.       System.out.println(packetDec.getName()); 
  30.       //输出引用import  
  31.       System.out.println("引用import:"); 
  32.       for (Object obj : importList) { 
  33.           ImportDeclaration importDec = (ImportDeclaration) obj; 
  34.           System.out.println(importDec.getName()); 
  35.       } 
  36.       //输出类名  
  37.       System.out.println("类:"); 
  38.       System.out.println(className); 
  39.       //循环输出函数名称  
  40.       System.out.println("========================"); 
  41.       System.out.println("函数:"); 
  42.       for (MethodDeclaration method : methodDec) { 
  43.          /* System.out.println(method.getName()); 
  44.           System.out.println("body:"); 
  45.           System.out.println(method.getBody()); 
  46.           System.out.println("Javadoc:" + method.getJavadoc()); 
  47.  
  48.           System.out.println("Body:" + method.getBody()); 
  49.  
  50.           System.out.println("ReturnType:" + method.getReturnType());*/ 
  51.           System.out.println("============="); 
  52.           System.out.println(method); 
  53.       } 
  54.  
  55.       //循环输出变量  
  56.       System.out.println("变量:"); 
  57.       for (FieldDeclaration fieldDecEle : fieldDec) { 
  58.           //public static  
  59.           for (Object modifiObj : fieldDecEle.modifiers()) { 
  60.               Modifier modify = (Modifier) modifiObj; 
  61.               System.out.print(modify + "-"); 
  62.           } 
  63.           System.out.println(fieldDecEle.getType()); 
  64.           for (Object obj : fieldDecEle.fragments()) { 
  65.               VariableDeclarationFragment frag = (VariableDeclarationFragment) obj; 
  66.               System.out.println("[FIELD_NAME:]" + frag.getName()); 
  67.           } 
  68.       } 
  69.   } 
  70.  
  71.   private static String read(String filename) throws IOException { 
  72.       File file = new File(filename); 
  73.       byte[] b = new byte[(int) file.length()]; 
  74.       FileInputStream fis = new FileInputStream(file); 
  75.       fis.read(b); 
  76.       return new String(b); 
  77.  
  78.   } 
运行代码:
  
  
  
  
  1. 函数: 
  2. ============= 
  3. /**  
  4.  * 测试相关类 jdt api测试 
  5.  * @param null 
  6.  * @expected 1 
  7.  */ 
  8. @Test public void test1(){ 
  9.   assertEquals(1,1); 
  10.  
  11. ============= 
  12. /**  
  13.  * 测试ignore情况 
  14.  */ 
  15. @Test @Ignore public void test2(){ 
  16.   assertEquals(1,1); 
4、结论:jdt的AST.JLS3已经满足对java method处理的需求,满足tc转换工具的使用要求。
 
5、jdt解析java文件的BlockComment、LineComment丢失
http://txy920.iteye.com/blog/237107




 

你可能感兴趣的:(职场,jdt,休闲)