Java Reflection Get Method Information

By | 2018-03-21T21:22:21+05:30 June 21st, 2016|java programs|

Here we are going to write a program, how to get the Method Information using Java Reflection mechanism.

Get Method Information :


package com.onlinetutorialspoint.ref;

import java.lang.reflect.Method;

public class Reflection_Method_Info {
    public static void main(String[] args) {
        try {
            if (args.length == 0) {
                System.out.println("PLEASE PASS THE CLASS NAME..!");
            } else {
                Class c = Class.forName(args[0]);
                printMethods(c);
            }
        } catch (ClassNotFoundException cnfe) {
            System.out.println(args[0] + " DOES NOT EXISTS...");
        }
    }

    static void printMethods(Class c) {
        Method m[] = c.getMethods();
        System.out.println("NUMBER OF METHODS = " + m.length);
        System.out.println("NAME OF THE CLASS : " + c.getName());
        for (int i = 0; i < m.length; i++) {
            Class c1 = m[i].getReturnType();
            String rtype = c1.getName();
            String mname = m[i].getName();
            System.out.print(rtype + " " + mname + "(");
            Class mp[] = m[i].getParameterTypes();
            for (int j = 0; j < mp.length; j++) {
                String ptype = mp[j].getName();
                System.out.print(ptype + ",");
            }
            System.out.println("\b" + ")");
        }
    }
}

Output :

javac Reflection_Method_Info.java
java Reflection_Method_Info 

java.lang.reflect.Method
NUMBER OF METHODS = 43
NAME OF THE CLASS : java.lang.reflect.Method
java.lang.Object invoke(java.lang.Object,[Ljava.lang.Object;)
boolean equals(java.lang.Object)
java.lang.String toString)
int hashCode)
int getModifiers)
java.lang.String getName)
java.lang.String toGenericString)
boolean isSynthetic)
[Ljava.lang.reflect.TypeVariable; getTypeParameters)
java.lang.Class getDeclaringClass)
java.lang.annotation.Annotation getAnnotation(java.lang.Class)
[Ljava.lang.annotation.Annotation; getDeclaredAnnotations)
java.lang.Class getReturnType)
[Ljava.lang.Class; getParameterTypes)
boolean isDefault)
boolean isVarArgs)
int getParameterCount)
[[Ljava.lang.annotation.Annotation; getParameterAnnotations)
java.lang.reflect.Type getGenericReturnType)
[Ljava.lang.reflect.Type; getGenericParameterTypes)
[Ljava.lang.Class; getExceptionTypes)
[Ljava.lang.reflect.Type; getGenericExceptionTypes)
boolean isBridge)
java.lang.Object getDefaultValue)
java.lang.reflect.AnnotatedType getAnnotatedReturnType)
[Ljava.lang.annotation.Annotation; getAnnotationsByType(java.lang.Class)
[Ljava.lang.reflect.AnnotatedType; getAnnotatedParameterTypes)
[Ljava.lang.reflect.Parameter; getParameters)
java.lang.reflect.AnnotatedType getAnnotatedReceiverType)
[Ljava.lang.reflect.AnnotatedType; getAnnotatedExceptionTypes)
boolean isAnnotationPresent(java.lang.Class)
[Ljava.lang.annotation.Annotation; getAnnotations)
java.lang.annotation.Annotation getDeclaredAnnotation(java.lang.Class)
[Ljava.lang.annotation.Annotation; getDeclaredAnnotationsByType(java.lang.Class)
void setAccessible([Ljava.lang.reflect.AccessibleObject;,boolean)
void setAccessible(boolean)
boolean isAccessible)
void wait(long,int)
void wait(long)
void wait)
java.lang.Class getClass)
void notify)
void notifyAll)

On the above out put we can see all the methods available in java.lang.reflect.Method class.

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment