1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
54
55 private MethodUtil( )
56 {
57 }
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
104
105
106
107
108
109
110
111
112
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
134
135
136
137
138
139
140
141
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
184
185
186
187
188
189
190
191
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
201
202
203
204
205
206
207
208
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 }