开发者

Program to find the number of methods in a java program

开发者 https://www.devze.com 2023-02-20 12:38 出处:网络
I used the following code to identify the number of functions in a class. Similar way can any one help me to identify the number of functions in a java program. In my program i gave input file as a cl

I used the following code to identify the number of functions in a class. Similar way can any one help me to identify the number of functions in a java program. In my program i gave input file as a class. Guide me with the code to give input as a java program and to find the number of declared functions in it.

import java.lang.reflect.*;
import java.io.*;
import java.lang.String.*;
   public class Method1 {
      private int f1(
       Object p, int x) throws NullPointerException
      {
         if (p == null)
            throw new NullPointerException();
         return x;
      }

      public static void main(String args[])throws Exception
      {
         int Mcount=0,MthdLen=0;
         try {
           Class cls = Class.forName("Madhu");
        int a;
            Method methlist[]= cls.getDeclaredMethods();
            for (int i = 0; i < methlist.length;i++)
       开发者_开发技巧     {  
               Method m = methlist[i];
               Mcount = Mcount + 1;
               MthdLen=MthdLen+(m.getName().length());
            }
         }
         catch (Throwable e) {
            System.err.println(e);
         }
        System.out.println("Length = " + MthdLen);
        System.out.println("Mcount = " + Mcount);
      }
   }


First of all,

 Class cls = Class.forName("Madhu");

Requires the fully qualified name of the desired class. e.g Class.forName("java.lang.Thread")'.

Secondly,

 Method methlist[]= cls.getDeclaredMethods();

returns public, protected, private and default method of that specific class only (it excludes inherited methods).

Thirdly,

MthdLen=MthdLen+(m.getName().length());

Sums up the string length of the method name. What do you need this for? You could simply do a count as follows:

int MCount = cls.getDeclaredMethods().length; //If the "getDeclaredMethods()` doesn't return a null.

Finally, if you need to get all inherited public & protected methods of that class, you would do

Class<?> class2 = cls.getSuperClass();

//Get all methods using
Method[] methods2 = class2.getDeclaredMethods();

//Iterate through methods2 and retrieve all public, protected methods and add it to MCount.

Hope this helps.


Java doesn't have any functions so the answer is 0. ;)

If you are looking for the number of methods, you have to ask yourself, do you want to

  • include inherited methods.
  • count overriden methods once ore multiple times.
  • include all the methods in Object or not.

e.g.

public class Main {
    static class A {
        public String toString() {
            return super.toString();
        }
    }
    static class B extends A {
        public String toString() {
            return super.toString();
        }
    }

    public static void main(String args[]) {
        for(Class clazz = B.class;clazz != null;clazz = clazz.getSuperclass()) {
            for(Method m: clazz.getDeclaredMethods())
                System.out.println(m);
        }
    }
}

prints

public java.lang.String Main$B.toString()
public java.lang.String Main$A.toString()
protected void java.lang.Object.finalize() throws java.lang.Throwable
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
protected native java.lang.Object java.lang.Object.clone() throws java.lang.CloneNotSupportedException
private static native void java.lang.Object.registerNatives()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()


In JDK6+, you could compile the file at runtime using JavaCompiler, and use your old code to find the number of methods.

EDIT:
Bonus: Replace your code with

System.out.println("Total number of methods: " +  
              java.beans.Introspector.getBeanInfo( //your class name here
                                              ).getMethodDescriptors().length);


i use this code to find out count of method in a class inside of another class.

public class test1 {
    public static void main(String[] args)
//if use in class 
//Method[] s = getClass.getDeclaredMethods();
        Method[] s = SmokeTestTests.class.getDeclaredMethods();
        int methodCounter = 0;
        for (Method method :
                s) {
            ++methodCounter;
        }
        System.out.println(methodCounter);
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消