1、加入jdt的相关依赖
- <dependency>
- <groupId>org.eclipse.tycho</groupId>
- <artifactId>org.eclipse.jdt.core</artifactId>
- <version>3.6.2.v_A76_R36x</version>
- </dependency>
- <dependency>
- <groupId>org.eclipse.core</groupId>
- <artifactId>org.eclipse.core.jobs</artifactId>
- <version>3.5.0.v20100515</version>
- </dependency>
- <dependency>
- <groupId>org.eclipse.core</groupId>
- <artifactId>org.eclipse.core.contenttype</artifactId>
- <version>3.4.100.v20100505-1235</version>
- </dependency>
- <dependency>
- <groupId>org.eclipse.core</groupId>
- <artifactId>org.eclipse.core.resources</artifactId>
- <version>3.6.0.v20100526-0737</version>
- </dependency>
- <dependency>
- <groupId>org.eclipse.core</groupId>
- <artifactId>org.eclipse.core.runtime</artifactId>
- <version>3.6.0.v20100505</version>
- </dependency>
- <dependency>
- <groupId>org.eclipse.osgi</groupId>
- <artifactId>org.eclipse.osgi</artifactId>
- <version>3.6.0.v20100517</version>
- </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、代码样例
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.util.List;
- import org.eclipse.jdt.core.dom.AST;
- import org.eclipse.jdt.core.dom.ASTParser;
- import org.eclipse.jdt.core.dom.CompilationUnit;
- import org.eclipse.jdt.core.dom.FieldDeclaration;
- import org.eclipse.jdt.core.dom.ImportDeclaration;
- import org.eclipse.jdt.core.dom.MethodDeclaration;
- import org.eclipse.jdt.core.dom.Modifier;
- import org.eclipse.jdt.core.dom.PackageDeclaration;
- import org.eclipse.jdt.core.dom.TypeDeclaration;
- import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
- public static void main(String[] args) throws Exception {
- String content = read("D:\\HelloWorld.java"); //java源文件
- //创建解析器
- ASTParser parsert = ASTParser.newParser(AST.JLS3);
- //设定解析器的源代码字符
- parsert.setSource(content.toCharArray());
- //使用解析器进行解析并返回AST上下文结果(CompilationUnit为根节点)
- CompilationUnit result = (CompilationUnit) parsert.createAST(null);
- //获取类型
- List types = result.types();
- //取得类型声明
- TypeDeclaration typeDec = (TypeDeclaration) types.get(0);
- //##############获取源代码结构信息#################
- //引用import
- List importList = result.imports();
- //取得包名
- PackageDeclaration packetDec = result.getPackage();
- //取得类名
- String className = typeDec.getName().toString();
- //取得函数(Method)声明列表
- MethodDeclaration methodDec[] = typeDec.getMethods();
- //取得函数(Field)声明列表
- FieldDeclaration fieldDec[] = typeDec.getFields();
- //输出包名
- System.out.println("包:");
- System.out.println(packetDec.getName());
- //输出引用import
- System.out.println("引用import:");
- for (Object obj : importList) {
- ImportDeclaration importDec = (ImportDeclaration) obj;
- System.out.println(importDec.getName());
- }
- //输出类名
- System.out.println("类:");
- System.out.println(className);
- //循环输出函数名称
- System.out.println("========================");
- System.out.println("函数:");
- for (MethodDeclaration method : methodDec) {
- /* System.out.println(method.getName());
- System.out.println("body:");
- System.out.println(method.getBody());
- System.out.println("Javadoc:" + method.getJavadoc());
- System.out.println("Body:" + method.getBody());
- System.out.println("ReturnType:" + method.getReturnType());*/
- System.out.println("=============");
- System.out.println(method);
- }
- //循环输出变量
- System.out.println("变量:");
- for (FieldDeclaration fieldDecEle : fieldDec) {
- //public static
- for (Object modifiObj : fieldDecEle.modifiers()) {
- Modifier modify = (Modifier) modifiObj;
- System.out.print(modify + "-");
- }
- System.out.println(fieldDecEle.getType());
- for (Object obj : fieldDecEle.fragments()) {
- VariableDeclarationFragment frag = (VariableDeclarationFragment) obj;
- System.out.println("[FIELD_NAME:]" + frag.getName());
- }
- }
- }
- private static String read(String filename) throws IOException {
- File file = new File(filename);
- byte[] b = new byte[(int) file.length()];
- FileInputStream fis = new FileInputStream(file);
- fis.read(b);
- return new String(b);
- }
运行代码:
- 函数:
- =============
- /**
- * 测试相关类 jdt api测试
- * @param null
- * @expected 1
- */
- @Test public void test1(){
- assertEquals(1,1);
- }
- =============
- /**
- * 测试ignore情况
- */
- @Test @Ignore public void test2(){
- assertEquals(1,1);
- }
4、结论:jdt的AST.JLS3已经满足对java method处理的需求,满足tc转换工具的使用要求。
5、jdt解析java文件的BlockComment、LineComment丢失
本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/615291,如需转载请自行联系原作者