View Javadoc

1   package org.apache.maven.shared.tools.test;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.lang.reflect.Field;
23  import java.lang.reflect.InvocationTargetException;
24  import java.lang.reflect.Method;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import org.codehaus.plexus.util.ReflectionUtils;
29  import org.codehaus.plexus.util.StringUtils;
30  
31  /**
32   * @version $Id: ReflectiveSetter.java 677117 2008-07-16 00:29:56Z vsiveton $
33   */
34  public class ReflectiveSetter
35  {
36      private Map cachedPropertySetters = new HashMap();
37  
38      private final Class targetClass;
39  
40      /**
41       * @param targetClass
42       */
43      public ReflectiveSetter( Class targetClass )
44      {
45          this.targetClass = targetClass;
46      }
47  
48      /**
49       * @param propertyName
50       * @param value
51       * @param target
52       * @throws Throwable
53       */
54      public void setProperty( String propertyName, Object value, Object target )
55          throws Throwable
56      {
57  
58          String preferredMethodName = "set" + StringUtils.capitalizeFirstLetter( propertyName );
59  
60          Setter setter = null;
61  
62          Method method = ReflectionUtils.getSetter( preferredMethodName, targetClass );
63  
64          if ( method != null )
65          {
66              setter = new MethodSetter( propertyName, method );
67          }
68          else
69          {
70              Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses( propertyName, targetClass );
71  
72              setter = new FieldSetter( propertyName, field );
73          }
74  
75          if ( setter == null )
76          {
77              throw new IllegalArgumentException( "No such property: " + propertyName + " in: " + targetClass
78                  + ". Searched for: {method:" + preferredMethodName + ", method:" + propertyName + ", field:"
79                  + propertyName + "}" );
80          }
81  
82          cachedPropertySetters.put( setter.getProperty(), setter );
83  
84          try
85          {
86              setter.set( value, target );
87          }
88          catch ( InvocationTargetException e )
89          {
90              throw e.getTargetException();
91          }
92      }
93  
94      private interface Setter
95      {
96          void set( Object value, Object target )
97              throws IllegalArgumentException, IllegalAccessException, InvocationTargetException;
98  
99          String getProperty();
100     }
101 
102     private static class MethodSetter
103         implements Setter
104     {
105         private Method method;
106 
107         private String name;
108 
109         MethodSetter( String name, Method method )
110         {
111             this.name = name;
112             this.method = method;
113         }
114 
115         /** {@inheritDoc} */
116         public String getProperty()
117         {
118             return name;
119         }
120 
121         /** {@inheritDoc} */
122         public void set( Object value, Object target )
123             throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
124         {
125             boolean wasAccessible = method.isAccessible();
126 
127             method.setAccessible( true );
128             try
129             {
130                 method.invoke( target, new Object[] { value } );
131             }
132             finally
133             {
134                 method.setAccessible( wasAccessible );
135             }
136         }
137     }
138 
139     private static class FieldSetter
140         implements Setter
141     {
142         private Field field;
143 
144         private String name;
145 
146         FieldSetter( String name, Field field )
147         {
148             this.name = name;
149             this.field = field;
150         }
151 
152         /** {@inheritDoc} */
153         public String getProperty()
154         {
155             return name;
156         }
157 
158         /** {@inheritDoc} */
159         public void set( Object value, Object target )
160             throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
161         {
162             boolean wasAccessible = field.isAccessible();
163 
164             field.setAccessible( true );
165             try
166             {
167                 field.set( target, value );
168             }
169             finally
170             {
171                 field.setAccessible( wasAccessible );
172             }
173         }
174     }
175 }