Class ReflectionHelper

java.lang.Object
com.pnfsoftware.jeb.util.reflect.ReflectionHelper

public class ReflectionHelper extends Object
Helper routines using the Java Reflection API.
  • Constructor Details

    • ReflectionHelper

      public ReflectionHelper()
  • Method Details

    • hasNoArgConstructor

      public static boolean hasNoArgConstructor(Class<?> c)
      Determine whether a class declares a no-argument constructor.
      Parameters:
      c - class to inspect
      Returns:
      true if the class declares a no-argument constructor
    • newInstance2

      Instantiate an object using a no-argument constructor, temporarily making it accessible if necessary.
      Parameters:
      constructor - constructor to invoke
      Returns:
      the new instance
      Throws:
      InstantiationException - if the declaring class cannot be instantiated
      IllegalAccessException - if the constructor cannot be accessed
      IllegalArgumentException - if the constructor invocation arguments are invalid
      InvocationTargetException - if the constructor throws an exception
    • invoke2

      Invoke a method, temporarily making it accessible if necessary.
      Parameters:
      m - method to invoke
      o - target object, or null for a static method
      args - invocation arguments
      Returns:
      the invocation result
      Throws:
      IllegalAccessException - if the method cannot be accessed
      IllegalArgumentException - if the target object or arguments are invalid
      InvocationTargetException - if the method throws an exception
    • set2

      public static void set2(Field f, Object o, Object value) throws IllegalAccessException, IllegalArgumentException
      Set a field value, temporarily making the field accessible if necessary.
      Parameters:
      f - field to set
      o - target object, or null for a static field
      value - value to set
      Throws:
      IllegalAccessException - if the field cannot be accessed
      IllegalArgumentException - if the target object or value is invalid
    • get2

      Retrieve a field value, temporarily making the field accessible if necessary.
      Parameters:
      f - field to read
      o - target object, or null for a static field
      Returns:
      the field value
      Throws:
      IllegalAccessException - if the field cannot be accessed
      IllegalArgumentException - if the target object is invalid
    • removeDimensions

      public static Class<?> removeDimensions(Class<?> c)
      Remove dimensions of an array type.
      • non-array => non-array
      • type[]...[] => type
      Parameters:
      c - a type
      Returns:
      the dimension-less type
    • reduceDimensions

      public static Class<?> reduceDimensions(Class<?> c)
      Reduce dimensions of an array type. Careful, caveat for primitives:
      • non-array => non-array
      • Object[]...[] => Object
      • prim[]...[] => prim[]
      Parameters:
      c - a type
      Returns:
      the reduced type
    • instantiateSafe

      public static <T> T instantiateSafe(Class<? extends T> c)
      Instantiate a class using its public no-argument constructor.
      Type Parameters:
      T - object type
      Parameters:
      c - class to instantiate
      Returns:
      the new instance, or null if instantiation failed
    • isInstance

      public static <T> boolean isInstance(Object o, Collection<Class<? extends T>> classObjects)
      A multi-element variant of Class.isInstance(Object).
      Parameters:
      o - the object to be tested
      classObjects - a collection of Class objects
      Returns:
      true if o can be cast to one of the provided class objects
    • isInstance

      public static boolean isInstance(Object o, Class<?>... classObjects)
      A multi-element variant of Class.isInstance(Object).
      Parameters:
      o - the object to be tested
      classObjects - a collection of Class objects
      Returns:
      true if o can be cast to one of the provided class objects
    • subtypeOf

      public static boolean subtypeOf(Class<?> c, Class<?> parent)
      Determine if a type is a descendant of the provided ancestor.
      Parameters:
      c - a type (class or interface)
      parent - a candidate ancestor type (class or interface)
      Returns:
      true or false
    • openAllModules

      public static void openAllModules(Class<?> clzInUnnamedModule) throws Exception
      Grant reflection access to non-public members of Java named modules to an unnamed module. Required for JDK 17+, since --illegal-access= is no longer an option.

      Implementation note: this code relies on Module.implAddExportsOrOpens(); therefore access to java.lang must have been granted to the JVM (e.g. via --add-opens java.base/java.lang=ALL-UNNAMED on the command line).

      Parameters:
      clzInUnnamedModule - a class in the unnamed module
      Throws:
      Exception - raised if any reflection error or illegal access error happens