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> 
原文解释为:
 
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丢失


本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/615291,如需转载请自行联系原作者
相关文章
|
15天前
|
Java
Java中ReentrantLock释放锁代码解析
Java中ReentrantLock释放锁代码解析
25 8
|
1天前
|
Java
Java输入输出流详细解析
Java输入输出流详细解析
Java输入输出流详细解析
|
2天前
|
存储 Java C++
Java集合篇之深度解析Queue,单端队列、双端队列、优先级队列、阻塞队列
Java集合篇之深度解析Queue,单端队列、双端队列、优先级队列、阻塞队列
12 0
|
9天前
|
Java API 数据库
深入解析:使用JPA进行Java对象关系映射的实践与应用
【4月更文挑战第17天】Java Persistence API (JPA) 是Java EE中的ORM规范,简化数据库操作,让开发者以面向对象方式处理数据,提高效率和代码可读性。它定义了Java对象与数据库表的映射,通过@Entity等注解标记实体类,如User类映射到users表。JPA提供持久化上下文和EntityManager,管理对象生命周期,支持Criteria API和JPQL进行数据库查询。同时,JPA包含事务管理功能,保证数据一致性。使用JPA能降低开发复杂性,但需根据项目需求灵活应用,结合框架如Spring Data JPA,进一步提升开发便捷性。
|
13天前
|
Java
Java 15 神秘登场:隐藏类解析未知领域
Java 15 神秘登场:隐藏类解析未知领域
17 0
|
13天前
|
安全 Java 编译器
接口之美,内部之妙:深入解析Java的接口与内部类
接口之美,内部之妙:深入解析Java的接口与内部类
35 0
接口之美,内部之妙:深入解析Java的接口与内部类
|
1月前
|
Java 程序员 C#
静态构造方法解析,Java新手必看技能
静态构造方法解析,Java新手必看技能
9 0
|
2天前
|
安全 Java 调度
Java线程:深入理解与实战应用
Java线程:深入理解与实战应用
20 0
|
1天前
|
缓存 Java
【Java基础】简说多线程(上)
【Java基础】简说多线程(上)
5 0
|
1天前
|
并行计算 算法 安全
Java从入门到精通:2.1.3深入学习Java核心技术——掌握Java多线程编程
Java从入门到精通:2.1.3深入学习Java核心技术——掌握Java多线程编程

推荐镜像

更多