View Javadoc
1   package com.simpligility.maven.plugins.android.configuration;
2   
3   import org.apache.maven.plugin.AbstractMojo;
4   import org.apache.maven.plugin.MojoExecutionException;
5   
6   import java.lang.reflect.Array;
7   import java.lang.reflect.Field;
8   
9   /**
10   * Helper for parsing the embedded configuration of a mojo.
11   *
12   * @author Pappy STÄ‚NESCU - pappy.stanescu@gmail.com
13   */
14  public final class ConfigHelper
15  {
16  
17      public static void copyValues( AbstractMojo mojo, String confFieldName ) throws MojoExecutionException
18      {
19          try
20          {
21              final Class<? extends AbstractMojo> mojoClass = mojo.getClass();
22              final Field confField = mojoClass.getDeclaredField( confFieldName );
23  
24              confField.setAccessible( true );
25  
26              final Object conf = confField.get( mojo );
27  
28              if ( conf == null )
29              {
30                  return;
31              }
32  
33              for ( final Field field : conf.getClass().getDeclaredFields() )
34              {
35                  field.setAccessible( true );
36  
37                  final Object value = field.get( conf );
38  
39                  if ( value == null )
40                  {
41                      continue;
42                  }
43  
44                  final Class<?> cls = value.getClass();
45  
46                  if ( ( cls == String.class ) && ( ( ( String ) value ).length() == 0 ) )
47                  {
48                      continue;
49                  }
50                  if ( cls.isArray() && ( Array.getLength( value ) == 0 ) )
51                  {
52                      continue;
53                  }
54  
55                  String mojoFieldName = field.getName();
56  
57                  mojoFieldName = Character.toUpperCase( mojoFieldName.charAt( 0 ) ) + mojoFieldName.substring( 1 );
58                  mojoFieldName = confFieldName + mojoFieldName;
59  
60                  try
61                  {
62                      final Field mojoField = mojoClass.getDeclaredField( mojoFieldName );
63  
64                      mojoField.setAccessible( true );
65                      mojoField.set( mojo, value );
66                  }
67                  catch ( final NoSuchFieldException e )
68                  {
69                      // swallow
70                  }
71  
72                  //  handle deprecated parameters
73                  try
74                  {
75                      final Field mojoField = mojoClass.getDeclaredField( field.getName() );
76  
77                      mojoField.setAccessible( true );
78                      mojoField.set( mojo, value );
79                  }
80                  catch ( final NoSuchFieldException e )
81                  {
82                      // swallow
83                  }
84                  catch ( final IllegalArgumentException e )
85                  {
86                      // probably not a deprecated parameter, see Proguard configuration;
87                  }
88              }
89          }
90          catch ( final Exception e )
91          {
92              throw new MojoExecutionException( e.getMessage(), e );
93          }
94      }
95  }