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 <a href="mailto:michal@codehaus.org">Michal Maczka</a>
 * @author <a href="mailto:jesse@codehaus.org">Jesse McConnell</a>
 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
 *
 */
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<Field> getFieldsIncludingSuperclasses( Class<?> clazz )
    {
        List<Field> 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;
    }

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

        List<Method> list = new ArrayList<>();

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

        return list;
    }

    /**
     * @param method the method
     * @return  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 see name
     * @param variable see name
     * @param value see name
     * @throws IllegalAccessException if error
     */
    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 variable field name
     * @param object the object to generate the list of fields from
     * @return map containing the fields and their values
     * @throws IllegalAccessException cannot access
     */
    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
     * @throws IllegalAccessException cannot access
     */
    public static Map<String, Object> getVariablesAndValuesIncludingSuperclasses( Object object )
        throws IllegalAccessException
    {
        Map<String, Object> 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<String, Object> 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 );
        }
    }
}

@@ -1169,15 +1169,12 @@
 /a%3E%0A
- *%0A
  */%0A
+
 publ
@@ -3765,17 +3765,16 @@
 @return 
- 
 the clas
@@ -5034,24 +5034,32 @@
 uperclasses%0A
+     * %0A
      * @para
@@ -6952,32 +6952,32 @@
 xception%0A    %7B%0A%0A
-
         Class%3C?%3E
@@ -6997,32 +6997,432 @@
 ct.getClass();%0A%0A
+        if ( Float.parseFloat( System.getProperty( %22java.specification.version%22 ) ) %3E= 11%0A            && Class.class.getCanonicalName().equals( clazz.getCanonicalName() ) )%0A        %7B%0A            // Updating Class fields accessibility is forbidden on Java 16 (and throws warning from version 11)%0A            // No concrete use case to modify accessibility at this level%0A            return;%0A        %7D%0A%0A
         Field%5B%5D 

Fix #116 : Java 16 support for ReflectionUtils (and Java 11 no warning) (#123)