All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.codehaus.plexus.util.ReflectionUtils Maven / Gradle / Ivy

There is a newer version: 4.1.4
Show newest version
package org.codehaus.plexus.util;

/*
 * Copyright The Codehaus Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.AccessibleObject;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * Operations on a class' fields and their setters.
 * 
 * @author Michal Maczka
 * @author Jesse McConnell
 * @author Trygve Laugstøl
 * @version $Id$
 */
public final class ReflectionUtils
{
    // ----------------------------------------------------------------------
    // Field utils
    // ----------------------------------------------------------------------

    public static Field getFieldByNameIncludingSuperclasses( String fieldName, Class clazz )
    {
        Field retValue = null;

        try
        {
            retValue = clazz.getDeclaredField( fieldName );
        }
        catch ( NoSuchFieldException e )
        {
            Class superclass = clazz.getSuperclass();

            if ( superclass != null )
            {
                retValue = getFieldByNameIncludingSuperclasses( fieldName, superclass );
            }
        }

        return retValue;
    }

    public static List getFieldsIncludingSuperclasses( Class clazz )
    {
        List fields = new ArrayList( Arrays.asList( clazz.getDeclaredFields() ) );

        Class superclass = clazz.getSuperclass();

        if ( superclass != null )
        {
            fields.addAll( getFieldsIncludingSuperclasses( superclass ) );
        }

        return fields;
    }

    // ----------------------------------------------------------------------
    // Setter utils
    // ----------------------------------------------------------------------

    /**
     * Finds a setter in the given class for the given field. It searches interfaces and superclasses too.
     *
     * @param fieldName the name of the field (i.e. 'fooBar'); it will search for a method named 'setFooBar'.
     * @param clazz The class to find the method in.
     * @return null or the method found.
     */
    public static Method getSetter( String fieldName, Class clazz )
    {
        Method[] methods = clazz.getMethods();

        fieldName = "set" + StringUtils.capitalizeFirstLetter( fieldName );

        for ( Method method : methods )
        {
            if ( method.getName().equals( fieldName ) && isSetter( method ) )
            {
                return method;
            }
        }

        return null;
    }

    /**
     * Finds all setters in the given class and super classes.
     */
    public static List getSetters( Class clazz )
    {
        Method[] methods = clazz.getMethods();

        List list = new ArrayList();

        for ( Method method : methods )
        {
            if ( isSetter( method ) )
            {
                list.add( method );
            }
        }

        return list;
    }

    /**
     * Returns the class of the argument to the setter. Will throw an RuntimeException if the method isn't a setter.
     */
    public static Class getSetterType( Method method )
    {
        if ( !isSetter( method ) )
        {
            throw new RuntimeException( "The method " + method.getDeclaringClass().getName() + "." + method.getName()
                + " is not a setter." );
        }

        return method.getParameterTypes()[0];
    }

    // ----------------------------------------------------------------------
    // Value accesstors
    // ----------------------------------------------------------------------

    /**
     * attempts to set the value to the variable in the object passed in
     *
     * @param object
     * @param variable
     * @param value
     * @throws IllegalAccessException
     */
    public static void setVariableValueInObject( Object object, String variable, Object value )
        throws IllegalAccessException
    {
        Field field = getFieldByNameIncludingSuperclasses( variable, object.getClass() );

        field.setAccessible( true );

        field.set( object, value );
    }

    /**
     * Generates a map of the fields and values on a given object, also pulls from superclasses
     *
     * @param object the object to generate the list of fields from
     * @return map containing the fields and their values
     */
    public static Object getValueIncludingSuperclasses( String variable, Object object )
        throws IllegalAccessException
    {

        Field field = getFieldByNameIncludingSuperclasses( variable, object.getClass() );

        field.setAccessible( true );

        return field.get( object );
    }

    /**
     * Generates a map of the fields and values on a given object, also pulls from superclasses
     *
     * @param object the object to generate the list of fields from
     * @return map containing the fields and their values
     */
    public static Map getVariablesAndValuesIncludingSuperclasses( Object object )
        throws IllegalAccessException
    {
        Map map = new HashMap();

        gatherVariablesAndValuesIncludingSuperclasses( object, map );

        return map;
    }

    // ----------------------------------------------------------------------
    // Private
    // ----------------------------------------------------------------------

    public static boolean isSetter( Method method )
    {
        return method.getReturnType().equals( Void.TYPE ) && // FIXME: needed /required?
            !Modifier.isStatic( method.getModifiers() ) && method.getParameterTypes().length == 1;
    }

    /**
     * populates a map of the fields and values on a given object, also pulls from superclasses
     *
     * @param object the object to generate the list of fields from
     * @param map to populate
     */
    private static void gatherVariablesAndValuesIncludingSuperclasses( Object object, Map map )
        throws IllegalAccessException
    {

        Class clazz = object.getClass();

        Field[] fields = clazz.getDeclaredFields();

        AccessibleObject.setAccessible( fields, true );

        for ( Field field : fields )
        {
            map.put( field.getName(), field.get( object ) );

        }

        Class superclass = clazz.getSuperclass();

        if ( !Object.class.equals( superclass ) )
        {
            gatherVariablesAndValuesIncludingSuperclasses( superclass, map );
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy