Thu. Mar 28th, 2024

I have not done any work prior to using reflection in my Java projects.  A project I was working on I needed to be able to access the Getter/Setter of an object.  I honestly never dreamed it was this easy.  Four simple methods that make it extremely easy to access the fields of any class.  Now you do have to know the name of the field you wish to set, but there are so many ways to code and plan for this to make it easy.

package name.mymiller.extensions.lang.reflect;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;

/**
 * Collection of Utility functions around Reflection to assist with creating, setting
 * and getting values on an object.
 *
 * @author jmiller
 */
public class ReflectionUtil {
    /**
     * Instantiate a class via it's name. Must have a default constructor otherwise it will fail.
     *
     * @param className Full package name plus Class Name for the Class to instantiate.
     * @return Object of the type Class
     * @throws InstantiationException Failed to instantiate the class, most often the class is missing
     *                                the default constructor.
     * @throws IllegalAccessException Lack the security access to instantiate that class.
     * @throws ClassNotFoundException Class not found.
     */
    static public Object instantiateClass(String className) throws InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
        return Class.forName(className).getDeclaredConstructor().newInstance();
    }

    /**
     * Set the value on a field of a class.  The field must have an mutator.
     *
     * @param object    Object containing the field to set.
     * @param fieldName String containing the name of the field to set.
     * @param value     Value to set the field to.
     * @throws IntrospectionException    Unable to identify the proper method.
     * @throws IllegalAccessException    Lack the security access to set the field.
     * @throws IllegalArgumentException  Unable to find the field indicated.
     * @throws InvocationTargetException Object passed in the invocation is not of the right type.
     */
    static public void setField(Object object, String fieldName, Object value) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        ReflectionUtil.getPropertyDescriptor(object, fieldName).getWriteMethod().invoke(object, value);
    }

    /**
     * Get the value on a field of a class.  The field must have an accessor.
     *
     * @param object    Object containing the field to set.
     * @param fieldName String containing the name of the field to set.
     * @throws IntrospectionException    Unable to identify the proper method.
     * @throws IllegalAccessException    Lack the security access to set the field.
     * @throws IllegalArgumentException  Unable to find the field indicated.
     * @throws InvocationTargetException Object passed in the invocation is not of the right type.
     */
    static public Object getField(Object object, String fieldName) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        return ReflectionUtil.getPropertyDescriptor(object, fieldName).getReadMethod().invoke(object);
    }

    /**
     * Get the value on a field of a class.  The field must have an accessor.
     *
     * @param object    Object containing the field to set.
     * @param fieldName String containing the name of the field to set.
     * @throws IntrospectionException Unable to identify the proper method.
     */
    static private PropertyDescriptor getPropertyDescriptor(Object object, String fieldName) throws IntrospectionException {
        return new PropertyDescriptor(fieldName, object.getClass());
    }
}

Simple inline methods for all of it. I have had a number of instances I could have done things much simpler than I had in the past.  Unfortunately, I never took the time to really look at reflection. Yes shame on me.  However, I won’t make that mistake anymore.  I hope you find this useful.

bob

By Jeffery Miller

I am known for being able to quickly decipher difficult problems to assist development teams in producing a solution. I have been called upon to be the Team Lead for multiple large-scale projects. I have a keen interest in learning new technologies, always ready for a new challenge.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d