View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.util.method;
35  
36  import org.apache.commons.lang.StringUtils;
37  
38  import java.lang.reflect.InvocationTargetException;
39  import java.lang.reflect.Method;
40  
41  
42  /**
43   *
44   * MethodUtils
45   *
46   */
47  public final class MethodUtil
48  {
49      private static final String PREFIX_GET = "get";
50      private static final String PREFIX_SET = "set";
51  
52      /**
53       * Instantiates a new method utils.
54       */
55      private MethodUtil(  )
56      {
57      }
58  
59      /**
60       * Sets the attribute.
61       * <br />
62       * <strong>Warning :</warning> This method does not handle setter that :
63       * <ul>
64       * <li>has no parameter or has more than one parameter</li>
65       * <li>has array parameter (ie : String[] or int[] ...)</li>
66       * </ul>
67       *
68       * @param <A> the generic type of the instance
69       * @param <B> the generic type of the value to set
70       * @param instance the instance to set
71       * @param strAttributeName the attribute name
72       * @param value the value of the attribute to set
73       * @throws SecurityException the security exception
74       * @throws NoSuchMethodException the no such method exception
75       * @throws IllegalArgumentException the illegal argument exception
76       * @throws IllegalAccessException the illegal access exception
77       * @throws InvocationTargetException the invocation target exception
78       */
79      public static <A, B> void set( A instance, String strAttributeName, B value )
80          throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
81              InvocationTargetException
82      {
83          if ( StringUtils.isNotBlank( strAttributeName ) && ( instance != null ) && ( value != null ) )
84          {
85              Method methodSetter = getSetter( instance, strAttributeName, value.getClass(  ) );
86  
87              if ( methodSetter != null )
88              {
89                  methodSetter.invoke( instance, new Object[] { value } );
90              }
91              else
92              {
93                  throw new NoSuchMethodException(  );
94              }
95          }
96          else
97          {
98              throw new IllegalArgumentException( "One on the parameters is null/blank." );
99          }
100     }
101 
102     /**
103      * Gets the method.
104      *
105      * @param <A> the generic type of the instance
106      * @param strMethodPrefix the str method prefix
107      * @param instance the instance
108      * @param strAttributeName the str attribute name
109      * @param clazz the clazz
110      * @return the method
111      * @throws SecurityException the security exception
112      * @throws NoSuchMethodException the no such method exception
113      */
114     public static <A> Method getMethod( String strMethodPrefix, A instance, String strAttributeName, Class<?> clazz )
115         throws SecurityException, NoSuchMethodException
116     {
117         String strFirstLetter = strAttributeName.substring( 0, 1 ).toUpperCase(  );
118 
119         String strMethodName = strMethodPrefix + strFirstLetter +
120             strAttributeName.substring( 1, strAttributeName.length(  ) );
121 
122         try
123         {
124             return instance.getClass(  ).getMethod( strMethodName, new Class[] { clazz } );
125         }
126         catch ( NoSuchMethodException e )
127         {
128             return getPrimitiveMethod( strMethodName, instance, clazz );
129         }
130     }
131 
132     /**
133      * Gets the primitive method.
134      *
135      * @param <A> the generic type of the instance
136      * @param strMethodName the str method name
137      * @param instance the instance
138      * @param clazz the clazz
139      * @return the primitive method
140      * @throws SecurityException the security exception
141      * @throws NoSuchMethodException the no such method exception
142      */
143     public static <A> Method getPrimitiveMethod( String strMethodName, A instance, Class<?> clazz )
144         throws SecurityException, NoSuchMethodException
145     {
146         if ( clazz.equals( Integer.class ) )
147         {
148             return instance.getClass(  ).getMethod( strMethodName, new Class[] { int.class } );
149         }
150         else if ( clazz.equals( Long.class ) )
151         {
152             return instance.getClass(  ).getMethod( strMethodName, new Class[] { long.class } );
153         }
154         else if ( clazz.equals( Double.class ) )
155         {
156             return instance.getClass(  ).getMethod( strMethodName, new Class[] { double.class } );
157         }
158         else if ( clazz.equals( Short.class ) )
159         {
160             return instance.getClass(  ).getMethod( strMethodName, new Class[] { short.class } );
161         }
162         else if ( clazz.equals( Byte.class ) )
163         {
164             return instance.getClass(  ).getMethod( strMethodName, new Class[] { byte.class } );
165         }
166         else if ( clazz.equals( Float.class ) )
167         {
168             return instance.getClass(  ).getMethod( strMethodName, new Class[] { float.class } );
169         }
170         else if ( clazz.equals( Character.class ) )
171         {
172             return instance.getClass(  ).getMethod( strMethodName, new Class[] { char.class } );
173         }
174         else if ( clazz.equals( Boolean.class ) )
175         {
176             return instance.getClass(  ).getMethod( strMethodName, new Class[] { boolean.class } );
177         }
178 
179         throw new NoSuchMethodException(  );
180     }
181 
182     /**
183      * Gets the setter.
184      *
185      * @param <A> the generic type
186      * @param instance the instance
187      * @param strAttributeName the str attribute name
188      * @param clazz the clazz
189      * @return the setter
190      * @throws SecurityException the security exception
191      * @throws NoSuchMethodException the no such method exception
192      */
193     public static <A> Method getSetter( A instance, String strAttributeName, Class<?> clazz )
194         throws SecurityException, NoSuchMethodException
195     {
196         return getMethod( PREFIX_SET, instance, strAttributeName, clazz );
197     }
198 
199     /**
200      * Gets the setter.
201      *
202      * @param <A> the generic type of the instance
203      * @param instance the instance
204      * @param strAttributeName the str attribute name
205      * @param clazz the clazz
206      * @return the setter
207      * @throws SecurityException the security exception
208      * @throws NoSuchMethodException the no such method exception
209      */
210     public static <A> Method getGetter( A instance, String strAttributeName, Class<?> clazz )
211         throws SecurityException, NoSuchMethodException
212     {
213         return getMethod( PREFIX_GET, instance, strAttributeName, clazz );
214     }
215 }