View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of 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.lang3.StringUtils;
37  
38  import java.lang.reflect.InvocationTargetException;
39  import java.lang.reflect.Method;
40  
41  /**
42   *
43   * MethodUtils
44   *
45   */
46  public final class MethodUtil
47  {
48      private static final String PREFIX_GET = "get";
49      private static final String PREFIX_SET = "set";
50  
51      /**
52       * Instantiates a new method utils.
53       */
54      private MethodUtil( )
55      {
56      }
57  
58      /**
59       * Sets the attribute. <br>
60       * <strong>Warning :</strong> This method does not handle setter that :
61       * <ul>
62       * <li>has no parameter or has more than one parameter</li>
63       * <li>has array parameter (ie : String[] or int[] ...)</li>
64       * </ul>
65       *
66       * @param <A>
67       *            the generic type of the instance
68       * @param <B>
69       *            the generic type of the value to set
70       * @param instance
71       *            the instance to set
72       * @param strAttributeName
73       *            the attribute name
74       * @param value
75       *            the value of the attribute to set
76       * @throws SecurityException
77       *             the security exception
78       * @throws NoSuchMethodException
79       *             the no such method exception
80       * @throws IllegalArgumentException
81       *             the illegal argument exception
82       * @throws IllegalAccessException
83       *             the illegal access exception
84       * @throws InvocationTargetException
85       *             the invocation target exception
86       */
87      public static <A, B> void set( A instance, String strAttributeName, B value )
88              throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
89      {
90          if ( StringUtils.isNotBlank( strAttributeName ) && ( instance != null ) && ( value != null ) )
91          {
92              Method methodSetter = getSetter( instance, strAttributeName, value.getClass( ) );
93  
94              if ( methodSetter != null )
95              {
96                  methodSetter.invoke( instance, value );
97              }
98              else
99              {
100                 throw new NoSuchMethodException( );
101             }
102         }
103         else
104         {
105             throw new IllegalArgumentException( "One on the parameters is null/blank." );
106         }
107     }
108 
109     /**
110      * Gets the method.
111      *
112      * @param <A>
113      *            the generic type of the instance
114      * @param strMethodPrefix
115      *            the str method prefix
116      * @param instance
117      *            the instance
118      * @param strAttributeName
119      *            the str attribute name
120      * @param clazz
121      *            the clazz
122      * @return the method
123      * @throws SecurityException
124      *             the security exception
125      * @throws NoSuchMethodException
126      *             the no such method exception
127      */
128     public static <A> Method getMethod( String strMethodPrefix, A instance, String strAttributeName, Class<?> clazz ) throws NoSuchMethodException
129     {
130         String strFirstLetter = strAttributeName.substring( 0, 1 ).toUpperCase( );
131 
132         String strMethodName = strMethodPrefix + strFirstLetter + strAttributeName.substring( 1, strAttributeName.length( ) );
133 
134         try
135         {
136             return instance.getClass( ).getMethod( strMethodName, clazz );
137         }
138         catch( NoSuchMethodException e )
139         {
140             return getPrimitiveMethod( strMethodName, instance, clazz );
141         }
142     }
143 
144     /**
145      * Gets the primitive method.
146      *
147      * @param <A>
148      *            the generic type of the instance
149      * @param strMethodName
150      *            the str method name
151      * @param instance
152      *            the instance
153      * @param clazz
154      *            the clazz
155      * @return the primitive method
156      * @throws SecurityException
157      *             the security exception
158      * @throws NoSuchMethodException
159      *             the no such method exception
160      */
161     public static <A> Method getPrimitiveMethod( String strMethodName, A instance, Class<?> clazz ) throws NoSuchMethodException
162     {
163         if ( clazz.equals( Integer.class ) )
164         {
165             return instance.getClass( ).getMethod( strMethodName, int.class );
166         }
167         else
168             if ( clazz.equals( Long.class ) )
169             {
170                 return instance.getClass( ).getMethod( strMethodName, long.class );
171             }
172             else
173                 if ( clazz.equals( Double.class ) )
174                 {
175                     return instance.getClass( ).getMethod( strMethodName, double.class );
176                 }
177                 else
178                     if ( clazz.equals( Short.class ) )
179                     {
180                         return instance.getClass( ).getMethod( strMethodName, short.class );
181                     }
182                     else
183                         if ( clazz.equals( Byte.class ) )
184                         {
185                             return instance.getClass( ).getMethod( strMethodName, byte.class );
186                         }
187                         else
188                             if ( clazz.equals( Float.class ) )
189                             {
190                                 return instance.getClass( ).getMethod( strMethodName, float.class );
191                             }
192                             else
193                                 if ( clazz.equals( Character.class ) )
194                                 {
195                                     return instance.getClass( ).getMethod( strMethodName, char.class );
196                                 }
197                                 else
198                                     if ( clazz.equals( Boolean.class ) )
199                                     {
200                                         return instance.getClass( ).getMethod( strMethodName, boolean.class );
201                                     }
202 
203         throw new NoSuchMethodException( );
204     }
205 
206     /**
207      * Gets the setter.
208      *
209      * @param <A>
210      *            the generic type
211      * @param instance
212      *            the instance
213      * @param strAttributeName
214      *            the str attribute name
215      * @param clazz
216      *            the clazz
217      * @return the setter
218      * @throws SecurityException
219      *             the security exception
220      * @throws NoSuchMethodException
221      *             the no such method exception
222      */
223     public static <A> Method getSetter( A instance, String strAttributeName, Class<?> clazz ) throws NoSuchMethodException
224     {
225         return getMethod( PREFIX_SET, instance, strAttributeName, clazz );
226     }
227 
228     /**
229      * Gets the setter.
230      *
231      * @param <A>
232      *            the generic type of the instance
233      * @param instance
234      *            the instance
235      * @param strAttributeName
236      *            the str attribute name
237      * @param clazz
238      *            the clazz
239      * @return the setter
240      * @throws SecurityException
241      *             the security exception
242      * @throws NoSuchMethodException
243      *             the no such method exception
244      */
245     public static <A> Method getGetter( A instance, String strAttributeName, Class<?> clazz ) throws NoSuchMethodException
246     {
247         return getMethod( PREFIX_GET, instance, strAttributeName, clazz );
248     }
249 }