Eclipse AST 实现一个类信息统计小程序
生活随笔
收集整理的這篇文章主要介紹了
Eclipse AST 实现一个类信息统计小程序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這個是對Eclipse AST 獲取與訪問的介紹,http://blog.csdn.net/LoveLion/article/details/19050155 ?(我的老師的博客)
本文的工具類完全來自于該文章,其他的類有這篇文章的影子
首先是 工具類,不多講,請看老師的介紹
package com.kyc.rec;import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException;import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTParser; import org.eclipse.jdt.core.dom.CompilationUnit;public class JdtAstUtil {/*** get compilation unit of source code* @param javaFilePath * @return CompilationUnit*/public static CompilationUnit getCompilationUnit(String javaFilePath){byte[] input = null;try {BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(javaFilePath));input = new byte[bufferedInputStream.available()];bufferedInputStream.read(input);bufferedInputStream.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}ASTParser astParser = ASTParser.newParser(AST.JLS3);astParser.setSource(new String(input).toCharArray());astParser.setKind(ASTParser.K_COMPILATION_UNIT);CompilationUnit result = (CompilationUnit) (astParser.createAST(null));return result;} }接下來是DTO用來存儲類中方法的相關信息 package com.kyc.rec;public class MethodDTO {private String name;//方法名private int codeLength;//方法的代碼長度private int numOfParam;//方法參數個數public MethodDTO(String name,int codeLength,int numOfParam){setName(name);setCodeLength(codeLength);setNumOfParam(numOfParam);}public MethodDTO(){}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getCodeLength() {return codeLength;}public void setCodeLength(int codeLength) {this.codeLength = codeLength;}public int getNumOfParam() {return numOfParam;}public void setNumOfParam(int numOfParam) {this.numOfParam = numOfParam;}} 下面開始訪問,其他訪問都不難,只是在獲取參數最多的方法的名字時,對我來說有點小麻煩,,因為我對AST的了解還很膚淺,不知道有沒有更先進的技術可以使我的方法變得簡單 package com.kyc.rec;import java.util.ArrayList;import org.eclipse.jdt.core.dom.ASTVisitor; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.FieldDeclaration; import org.eclipse.jdt.core.dom.MethodDeclaration; import org.eclipse.jdt.core.dom.SingleVariableDeclaration; import org.eclipse.jdt.core.dom.TypeDeclaration;public class DemoVisitor extends ASTVisitor {/*類中方法的個數、* 屬性的個數、* 源代碼行數、* 代碼行最多的方法名以及* 代碼行數、* 參數個數最多的方法名* 及其參數的個數等信息*/public int numOfMethod=0;//方法個數public int numOffield=0;//屬性個數public int codeLength=0;//代碼總行數public int index=-1;//用于獲取參數的時候,指向上一個方法;public int[] listOfParam=new int[100];private int numOfParam=0;private ArrayList<MethodDTO> arrayList =new ArrayList<MethodDTO>();//存儲類中方法的相關信息public void print(){System.out.println("屬性個數為:"+numOfMethod);System.out.println("方法個數為:"+numOffield);System.out.println("代碼行數為:"+codeLength);System.out.println("代碼行最多的方法:"+findLongestCodeLength().getName()+"行數為:"+findLongestCodeLength().getCodeLength());System.out.println("參數最多的方法:"+findMaxNumOfParam().getName()+"方法數為:"+findMaxNumOfParam().getNumOfParam()); // for(int i=0;i<arrayList.size();i++){ // System.out.println(arrayList.get(i).getName()+" - "+arrayList.get(i).getNumOfParam()); // }}@Overridepublic boolean visit(FieldDeclaration node) {numOffield++;return true;}@Overridepublic boolean visit(MethodDeclaration node) {if(index!=-1){arrayList.get(index).setNumOfParam(numOfParam);numOfParam=0;}index++;//System.out.println("Method:\t" + node.getName()+" "+node.getLength());numOfMethod++;CountLength cl=new CountLength();cl.countLength(node.toString(), "\n");addToArrayList(new MethodDTO(node.getName().toString(), cl.getCodeLength(),0));return true;}@Overridepublic boolean visit(TypeDeclaration node) {//該方法可訪問類名//System.out.println("Class:\t" + node.getName()); return true;}@Overridepublic boolean visit(CompilationUnit node){//該方法可獲得代碼的總行數CountLength cl=new CountLength();cl.countLength(node.toString(), "\n");codeLength=cl.getCodeLength();return true;}public void addToArrayList(MethodDTO meDto) {arrayList.add(meDto);}public MethodDTO findLongestCodeLength(){//獲取代碼行數最多的方法int maxLength=0;MethodDTO methodDTO=new MethodDTO();for(int i=0;i<arrayList.size();i++){if(arrayList.get(i).getCodeLength()>maxLength){maxLength=arrayList.get(i).getCodeLength();}}for(int i=0;i<arrayList.size();i++){if(arrayList.get(i).getCodeLength()==maxLength){methodDTO=arrayList.get(i);break;}}return methodDTO;}public MethodDTO findMaxNumOfParam(){//獲取參數最多的方法//這個方法與上一個方法存在明顯的代碼的壞味道(重復代碼),可以對此進行優化int maxNum=0;MethodDTO methodDTO=new MethodDTO();for(int i=0;i<arrayList.size();i++){if(arrayList.get(i).getNumOfParam()>maxNum){maxNum=arrayList.get(i).getNumOfParam();}}for(int i=0;i<arrayList.size();i++){if(arrayList.get(i).getNumOfParam()==maxNum){methodDTO=arrayList.get(i);break;}}return methodDTO;}@Overridepublic boolean visit(SingleVariableDeclaration node){//本方法在執行visit(MethodDeclaration node)后執行 Method中的參數次;numOfParam++;return true;}public void dotheEndParam(){arrayList.get(index).setNumOfParam(numOfParam);}}統計行數類 package com.kyc.rec;public class CountLength {//此類用于獲取字符串中換行字符的個數,str2代表子字符串private int codeLength=0;public int getCodeLength() {return codeLength;}public void setCodeLength(int codeLength) {this.codeLength = codeLength;}public int countLength(String str1, String str2) { if (str1.indexOf(str2) == -1) { return 0; } else if (str1.indexOf(str2) != -1) { codeLength++; countLength(str1.substring(str1.indexOf(str2) + str2.length()), str2); return codeLength ; }return 0; } }
test ?類 package com.kyc.rec;import org.eclipse.jdt.core.dom.CompilationUnit;public class DemoVisitorTest {public DemoVisitorTest(String path) {CompilationUnit comp = JdtAstUtil.getCompilationUnit(path);DemoVisitor visitor = new DemoVisitor();comp.accept(visitor);visitor.dotheEndParam();visitor.print();}public static void main(String[] args) {new DemoVisitorTest("DemoVisitor.java");} }結果如下:
jar包可私信
總結
以上是生活随笔為你收集整理的Eclipse AST 实现一个类信息统计小程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JDeodorant 的使用
- 下一篇: 在eclipse导入SSH项目