Java Programs

Java Reflection Get Method Information

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

Get Method Information :

[java]

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" + ")");
        }
    }
}

[/java]

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 :)

Found a mistake or something out of date? Tell us →

Written by

chandrashekhar

Founder of onlinetutorialspoint.com. Enterprise architect with 15+ years in healthcare technology, designing and running systems on AWS and Azure. Writes about building real things with AI, local AI hardware and cloud certifications.

Keep reading

Ask a question or leave a comment