Count the Number of Classes and Methods contains in a program.




Here iam using eclipse platform for checking my programs.

All programs are coming under package Count
Iam going to check number of classes and number of methods inside a program( just for example, here it is a class test.java)

My application program is written in class classMethodCount.  

Before starting, you have to copy your test.java from package explorer ( see the figure 1) and paste it on your project folder (see figure 2), here faizal is my project folder. After this run this program.

For Counting number of class iam using a simple logic, it is nothing but just check the word existence like "public class","private class","protected class","class" if it is found it means there is a class declaration, for confirming again we have to check its position like index[0] (in my program all class declaration is start from most left side of the page).
 

package Count;


import java.io.*;
import java.lang.reflect.Method;
import java.util.*;

public class classMethodCount
{

       public static void method(String str)
       { 
         try
               {
               int Mcount=0,MthdLen=0;
              
              
               System.out.println("Enter package name :");
               Scanner s = new Scanner(System.in);
               String str2 = s.nextLine();
               Class cls = Class.forName(str2+"."+str);
               int a;
               Method methlist[]= cls.getDeclaredMethods();
               for (int i = 0; i < methlist.length;i++)
                 {
                   Method m = methlist[i];
                   Mcount = Mcount + 1;
                 }
               System.out.println("Method count = " + Mcount);
               }
               catch (Throwable e)
               {
                       System.err.println(e);
               }
       }

       public static void main(String[] args)
      {
        try 
         {
            Scanner s = new Scanner(System.in);
            System.out.println("Enter file name :");
            String str = s.nextLine();
            System.out.println("Enter extension name( Eg: java ) :");
            String str1=s.nextLine();
         
            String strc= str+"."+str1;
                     
            File file = new File(strc);
            
        if (file.exists())
             
           {
               FileReader fr = new FileReader(file);
               LineNumberReader ln = new LineNumberReader(fr);

               int count = 0; // to count the class
               int count1=0; // to count the number of spaces
               int c=0;  // to count the number of lines

               String strLine;
// reading file line by line

               while ((strLine = ln.readLine()) != null)
                 {
                   if (strLine.trim().length() != 0)
                     {
                      c++;// new line found

String[] str3 ={"public class","private class","protected class","class"};
                       for(String d : str3)
                         {
                           Boolean i = strLine.contains(d);
                           if(i)
                             {  
                              int j = strLine.indexOf("public");

                               if(j==0)
                                   count++;//  class found

                              }
                                        
                          }
                      }
                        else
                            count1++;// new space found
                          
                     } // end of while loop

     System.out.println("Total number of lines: " + c);
     System.out.println("Total number of class: " + count);
     System.out.println("Total no of space line: "+ count1);
     method(str); // call another method to count number of methods
     ln.close();
                  }
                      else
                              System.out.println("File not exist");
             } // end of try
             
            catch(IOException e)
                      e.printStackTrace();
          
      }

   
}


Output :( from Eclipse Console)

Enter file name :
test  // test is my class name that i have to count the number of class
Enter extension name( Eg: java ) :
java
Total number of lines: 130
Total number of class: 1
Total no of space line: 62
Enter package name :
Count  // this is my package name.
Method count = 3

                                                                     Figure 1
                                    



                                                                         Figure 2



Post a Comment

Copyright © Rough Record. Designed by OddThemes