Java Reflection: Methods and Modifiers

java-reflect

Reflection API provides us various methods to play around with class, its members and their associated modifiers also. In this post we will go through some of those methods.

Reflection on Methods:

Reflection API provide us methods to fetch list of methods available of a class and can be invoked accordingly. Methods are represented as Method.java class.

getMethods(): This method returns the list of all public methods of a class including inherited methods.
getDeclaredMethods(): This method returns the list of all declared methods in a class excluding inherited methods.
getMethod(“<method_name”, “<argument_types>”): This method returns the Method.java object of the method which matches method_ name and argument_types combination. It will consider methods which are inherited and public.
getDeclaredMethod(“<method_name”, “<argument_types>”): This method returns the Method.java object of the method which matches method_ name and argument_types combination. It will consider only declared methods and will not work for inherited methods.
setAccessible(true): We can use this method to invoke methods which are otherwise not accessible due to access modifiers restrictions like private methods.
invoke(<object_instance>): This method in Method.java class is used to invoke the method on the given instance.

Refer the code below –

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MethodReflection {

  public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
    Method[] methods = MethodDemo.class.getDeclaredMethods();

    for (Method method : methods) {
      System.out.println(method.getName() + "parameter count :: " + method.getParameterCount());
      if(method.getParameterCount() == 2)
        method.invoke(new MethodDemo(), 1,2);
    }

    Method privateMethod = MethodDemo.class.getDeclaredMethod("privateMethod");
    privateMethod.setAccessible(true);
    privateMethod.invoke(new MethodDemo());

  }
}

class MethodDemo {
  void oneParamMethod(int param1) {
    System.out.println("111 :: " + param1);
  }

  void twoParamMethod(int param1, int param2) {
    System.out.println("222 :: " + param1);
    System.out.println("222 :: " + param2);
  }

  void noParamMethod() {
    System.out.println("No Param");
  }
  private void privateMethod() {
    System.out.println("Private method invoked.");
  }
}
Reflection on Modifiers:

Reflection API provide us methods to understand modifiers used in class and its members. Modifiers are represented as Modifier.java class.

Class.java, Field.java and Method.java all have getModifers() method which returns an integer value. This value can be used with methods provided by Modifier.java class to identify the access modifiers implemented.

Refer the code below –

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class ModifiersReflection {

  public static void main(String[] args) {

    System.out.println(Modifier.isPublic(ModifierDemo.class.getModifiers()));
    for(Field field : ModifierDemo.class.getDeclaredFields()) {
      if(Modifier.isPrivate(field.getModifiers()))
        System.out.println("Private :: " + field.getName());
    }

    for(Method method : ModifierDemo.class.getDeclaredMethods()) {
      if(Modifier.isSynchronized(method.getModifiers()))
        System.out.println("Synchronized :: " + method.getName());
      if(Modifier.isPrivate(method.getModifiers()))
        System.out.println("Private :: " + method.getName());
      if(Modifier.isStatic(method.getModifiers()))
        System.out.println("Static :: " + method.getName());
    }
  }
}

class ModifierDemo {
  private int field1;
  int field2;

  synchronized private void method1() {
    System.out.println("test method");
  }

  public static void method2() {
    System.out.println("Method 2");
  }

}

Leave a Comment

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