View Javadoc

1   package org.ops4j.pax.construct.util;
2   
3   /*
4    * Copyright 2007 Stuart McCulloch
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.lang.reflect.Field;
20  import java.security.AccessController;
21  import java.security.PrivilegedAction;
22  
23  import org.apache.maven.plugin.AbstractMojo;
24  import org.apache.maven.plugin.logging.Log;
25  
26  /**
27   * Provide access to private inherited mojo fields
28   */
29  public final class ReflectMojo
30  {
31      /**
32       * Maven mojo instance
33       */
34      private final AbstractMojo m_mojo;
35  
36      /**
37       * Inherited super-mojo
38       */
39      private final Class m_clazz;
40  
41      /**
42       * @param mojo maven mojo instance
43       * @param clazz inherited super-mojo
44       */
45      public ReflectMojo( AbstractMojo mojo, Class clazz )
46      {
47          m_mojo = mojo;
48          m_clazz = clazz;
49      }
50  
51      /**
52       * @param name field name
53       * @return reflected field
54       * @throws NoSuchFieldException
55       */
56      Field getMojoField( String name )
57          throws NoSuchFieldException
58      {
59          return m_clazz.getDeclaredField( name );
60      }
61  
62      /**
63       * @return mojo instance
64       */
65      AbstractMojo getMojoInstance()
66      {
67          return m_mojo;
68      }
69  
70      /**
71       * @return mojo logger
72       */
73      Log getMojoLogger()
74      {
75          return m_mojo.getLog();
76      }
77  
78      /**
79       * @param name name of the field member
80       * @return true if the field exists, otherwise false
81       */
82      public boolean hasField( final String name )
83      {
84          return null != AccessController.doPrivileged( new PrivilegedAction()
85          {
86              public Object run()
87              {
88                  try
89                  {
90                      return getMojoField( name );
91                  }
92                  catch( NoSuchFieldException e )
93                  {
94                      return null;
95                  }
96                  catch( SecurityException e )
97                  {
98                      return null;
99                  }
100             }
101         } );
102     }
103 
104     /**
105      * @param name name of the field member
106      * @param value the new value for the field
107      */
108     public void setField( final String name, final Object value )
109     {
110         AccessController.doPrivileged( new PrivilegedAction()
111         {
112             public Object run()
113             {
114                 try
115                 {
116                     final Object safeValue;
117                     Field f = getMojoField( name );
118 
119                     if( boolean.class.equals( f.getType() ) )
120                     {
121                         safeValue = Boolean.valueOf( value.toString() );
122                     }
123                     else
124                     {
125                         safeValue = value;
126                     }
127 
128                     f.setAccessible( true );
129                     f.set( getMojoInstance(), safeValue );
130                 }
131                 catch( NoSuchFieldException e )
132                 {
133                     getMojoLogger().error( "Unknown field " + name, e );
134                 }
135                 catch( IllegalAccessException e )
136                 {
137                     getMojoLogger().error( "Cannot set field " + name, e );
138                 }
139 
140                 return null;
141             }
142         } );
143     }
144 
145     /**
146      * @param name name of the field member
147      * @return the current value in the field
148      */
149     public Object getField( final String name )
150     {
151         return AccessController.doPrivileged( new PrivilegedAction()
152         {
153             public Object run()
154             {
155                 try
156                 {
157                     Field f = getMojoField( name );
158                     f.setAccessible( true );
159                     return f.get( getMojoInstance() );
160                 }
161                 catch( NoSuchFieldException e )
162                 {
163                     getMojoLogger().error( "Unknown field " + name, e );
164                 }
165                 catch( IllegalAccessException e )
166                 {
167                     getMojoLogger().error( "Cannot get field " + name, e );
168                 }
169 
170                 return null;
171             }
172         } );
173     }
174 }