The Eclipse AST implements a class information statistics applet

Source: Internet
Author: User

This is an introduction to the Eclipse AST acquisition and access, http://blog.csdn.net/LoveLion/article/details/19050155 (my Teacher's blog)

The tool classes in this article are entirely from this article, other classes have the shadow of this article


First is the tool class, not much, please see the teacher's introduction

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 {/** * g ET compilation Unit of source code * @param javafilepath * @return Compilationunit */public static Compilati Onunit getcompilationunit (String javafilepath) {byte[] input = null;try {Bufferedinputstream Bufferedinputstrea    m = 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; }}
Next is the information about the DTO used to store the methods in the class

Package Com.kyc.rec;public class Methoddto {Private String name;//method name private int codelength;//method code length private int numofparam;//method parameter number 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;}}
It's not hard to get access to the rest of the way, except for the name of the method that gets the most parameters, which is a bit of a hassle for me, because my understanding of AST is superficial and I don't know if there are any more advanced techniques that will make my approach simple.

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 the number of methods in the Astvisitor {/* class, number of * attributes, number of source lines, * code lines, Number of method names and number of * code lines, * Number of parameters with maximum number of arguments * and the number of parameter */public int numofmethod=0;//method number public int numoffield=0;//property number public int Codelen gth=0;//code total number of public int index=-1;//used to get parameters, point to previous method; public int[] Listofparam=new int[100];p rivate int numofparam=0; Private arraylist<methoddto> ArrayList =new arraylist<methoddto> ();//information about methods in the storage class public void print () { System.out.println ("Number of attributes:" +numofmethod); System.out.println ("Number of methods:" +numoffield); System.out.println ("Lines of Code:" +codelength); SYSTEM.OUT.PRINTLN ("Most code line method:" +findlongestcodelength (). GetName () + "number of rows:" +findlongestcodelength (). GetcodelenGth ()); System.out.println ("method with the most parameters:" +findmaxnumofparam (). GetName () + "method number:" +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) {//The method can access the class name//system.out.println ("class:\t" + node.getname ()); return true;} @Overridepublic Boolean visit (compilationunit node) {//The method can get the total number of lines of code 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 () {//Gets the maximum number of lines of code in the method 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 () {//Get the most parameters method//This method with the previous method has obvious code bad taste (duplicate code), can be optimized for this 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) {//This method executes the parameter in method after executing visit (methoddeclaration node) ; Numofparam++;return true;} public void Dotheendparam () {arraylist.get (index). Setnumofparam (Numofparam);}}

Statistics row Number Class

Package Com.kyc.rec;public class Countlength {//This class is used to get the number of newline characters in a string, str2 represents a substring 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.i Ndexof (str2)! =-1) {          codelength++;          Countlength (Str1.substring (Str1.indexof (str2) +                     str2.length ()), str2);                 return codelength;         }            return 0;      }  }

Test class

Package Com.kyc.rec;import Org.eclipse.jdt.core.dom.compilationunit;public class Demovisitortest {public Demovisitortest (String path) {Compilationunit comp = jdtastutil.getcompilationunit (path);D Emovisitor visitor = new Demovisitor (); comp.accept (visitor); Visitor.dotheendparam (); Visitor.print ();} public static void Main (string[] args) {new Demovisitortest ("Demovisitor.java");}}
The results are as follows:

Jar packages can be private messages



The Eclipse AST implements a class information statistics applet

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.