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

com.jayway.maven.plugins.android.configuration.ConfigHelper Maven / Gradle / Ivy

There is a newer version: 4.0.0-rc.2
Show newest version
package com.jayway.maven.plugins.android.configuration;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;

import java.lang.reflect.Array;
import java.lang.reflect.Field;

/**
 * Helper for parsing the embedded configuration of a mojo.
 *
 * @author Pappy Răzvan STĂNESCU <pa314159@gmail.com>
 */
public final class ConfigHelper
{

    public static void copyValues( AbstractMojo mojo, String confFieldName ) throws MojoExecutionException
    {
        try
        {
            final Class mojoClass = mojo.getClass();
            final Field confField = mojoClass.getDeclaredField( confFieldName );

            confField.setAccessible( true );

            final Object conf = confField.get( mojo );

            if ( conf == null )
            {
                return;
            }

            for ( final Field field : conf.getClass().getDeclaredFields() )
            {
                field.setAccessible( true );

                final Object value = field.get( conf );

                if ( value == null )
                {
                    continue;
                }

                final Class cls = value.getClass();

                if ( ( cls == String.class ) && ( ( ( String ) value ).length() == 0 ) )
                {
                    continue;
                }
                if ( cls.isArray() && ( Array.getLength( value ) == 0 ) )
                {
                    continue;
                }

                String mojoFieldName = field.getName();

                mojoFieldName = Character.toUpperCase( mojoFieldName.charAt( 0 ) ) + mojoFieldName.substring( 1 );
                mojoFieldName = confFieldName + mojoFieldName;

                try
                {
                    final Field mojoField = mojoClass.getDeclaredField( mojoFieldName );

                    mojoField.setAccessible( true );
                    mojoField.set( mojo, value );
                }
                catch ( final NoSuchFieldException e )
                {
                    // swallow
                }

                //  handle deprecated parameters
                try
                {
                    final Field mojoField = mojoClass.getDeclaredField( field.getName() );

                    mojoField.setAccessible( true );
                    mojoField.set( mojo, value );
                }
                catch ( final NoSuchFieldException e )
                {
                    // swallow
                }
                catch ( final IllegalArgumentException e )
                {
                    // probably not a deprecated parameter, see Proguard configuration;
                }
            }
        }
        catch ( final Exception e )
        {
            throw new MojoExecutionException( e.getMessage(), e );
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy