Java Reflection: List All Constructors,Methods and Fields

 
Please read Inspect Java Class using Reflection before proceeding further here.
List public Methods – including inherited methods
Class<ReflectionExample> clazz = ReflectionExample.class;
Method[] methods = clazz.getMethods();
List of declared Methods(including public, protected, default access, and private methods) – excluding inherited methods
Class<ReflectionExample> clazz = ReflectionExample.class;
Method[] declaredMethods = clazz.getDeclaredMethods();
 
List public constructors
Class<ReflectionExample> clazz = ReflectionExample.class;
Constructor<?>[] constructors = clazz.getConstructors();
 
List of declared constructors(including public, protected, default access, and private constructors)
Class<ReflectionExample> clazz = ReflectionExample.class;
Constructor<?>[] declaredConstructors = clazz.getDeclaredConstructors();
 
List public Fields – including inherited fields
Class<ReflectionExample> clazz = ReflectionExample.class;
Field[] fields = clazz.getFields();
 
List all declared Fields(including public, protected, default access, and private fields) – excluding inherited fields
Class<ReflectionExample> clazz = ReflectionExample.class;
Field[] declaredFields = clazz.getDeclaredFields();
package com.java.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

@SuppressWarnings("unused")
public class ReflectionExample {

    public String publicStr = "";
    public static String staticStr = "";


    private ReflectionExample(int id, String name) {
        this.id = id;
        this.name = name;
    }

    protected ReflectionExample(String name) {
        this.name = name;
    }

    private int id;

    private String name;

    private void privateMethod() {}

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public ReflectionExample() {}

    public ReflectionExample(int id) {
        this.id = id;
    }

    public static void main(String[] args) {

        Class < ReflectionExample > clazz = ReflectionExample.class;

        //List public Methods - including inherited methods
        Method[] methods = clazz.getMethods();
        System.out.println("Printing all public Methods");
        Arrays.asList(methods).stream().forEach(x - > System.out.println(x.toString()));

        //List of declared Methods(including public, protected, default access, and private methods) - excluding inherited methods
        Method[] declaredMethods = clazz.getDeclaredMethods();
        System.out.println("Printing all declared Methods");
        Arrays.asList(declaredMethods).stream().forEach(x - > System.out.println(x.toString()));

        //List public constructors
        Constructor << ? > [] constructors = clazz.getConstructors();
        System.out.println("Printing all public constructors");
        Arrays.asList(constructors).stream().forEach(x - > System.out.println(x.toString()));

        //List of declared constructors(including public, protected, default access, and private constructors)
        Constructor << ? > [] declaredConstructors = clazz.getDeclaredConstructors();
        System.out.println("Printing all declared Constructors");
        Arrays.asList(declaredConstructors).stream().forEach(x - > System.out.println(x.toString()));

        // List public Fields - including inherited fields
        Field[] fields = clazz.getFields();
        System.out.println("Printing all public fields");
        Arrays.asList(fields).stream().forEach(x - > System.out.println(x.toString()));

        // List all declared Fields(including public, protected, default access, and private fields) - excluding inherited fields
        Field[] declaredFields = clazz.getDeclaredFields();
        System.out.println("Printing all declared fields");
        Arrays.asList(declaredFields).stream().forEach(x - > System.out.println(x.toString()));
    }

}
Printing all public Methods
public static void com.java.reflection.ReflectionExample.main(java.lang.String[])
public int com.java.reflection.ReflectionExample.getId()
public void com.java.reflection.ReflectionExample.setId(int)
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
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 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()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
Printing all declared Methods
public static void com.java.reflection.ReflectionExample.main(java.lang.String[])
public int com.java.reflection.ReflectionExample.getId()
private void com.java.reflection.ReflectionExample.privateMethod()
public void com.java.reflection.ReflectionExample.setId(int)
private static void com.java.reflection.ReflectionExample.lambda$0(java.lang.reflect.Method)
private static void com.java.reflection.ReflectionExample.lambda$1(java.lang.reflect.Method)
private static void com.java.reflection.ReflectionExample.lambda$2(java.lang.reflect.Constructor)
private static void com.java.reflection.ReflectionExample.lambda$3(java.lang.reflect.Constructor)
private static void com.java.reflection.ReflectionExample.lambda$4(java.lang.reflect.Field)
private static void com.java.reflection.ReflectionExample.lambda$5(java.lang.reflect.Field)
Printing all public constructors
public com.java.reflection.ReflectionExample()
public com.java.reflection.ReflectionExample(int)
Printing all declared Constructors
public com.java.reflection.ReflectionExample()
protected com.java.reflection.ReflectionExample(java.lang.String)
private com.java.reflection.ReflectionExample(int,java.lang.String)
public com.java.reflection.ReflectionExample(int)
Printing all public fields
public java.lang.String com.java.reflection.ReflectionExample.publicStr
public static java.lang.String com.java.reflection.ReflectionExample.staticStr
Printing all declared fields
public java.lang.String com.java.reflection.ReflectionExample.publicStr
public static java.lang.String com.java.reflection.ReflectionExample.staticStr
private int com.java.reflection.ReflectionExample.id
private java.lang.String com.java.reflection.ReflectionExample.name

Leave a Reply

Your email address will not be published. Required fields are marked *