View Javadoc
1   /*
2    * Copyright 2007 Kasper B. Graversen
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.supercsv.util;
17  
18  import java.lang.reflect.Method;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.supercsv.exception.SuperCsvReflectionException;
23  
24  /**
25   * Provides useful utility methods for reflection.
26   * 
27   * @author James Bassett
28   * @since 2.0.0
29   */
30  public final class ReflectionUtils {
31  	
32  	public static final String SET_PREFIX = "set";
33  	public static final String GET_PREFIX = "get";
34  	public static final String IS_PREFIX = "is";
35  	
36  	/**
37  	 * A map of primitives and their associated wrapper classes, to cater for autoboxing.
38  	 */
39  	private static final Map<Class<?>, Class<?>> AUTOBOXING_CONVERTER = new HashMap<Class<?>, Class<?>>();
40  	static {
41  		AUTOBOXING_CONVERTER.put(long.class, Long.class);
42  		AUTOBOXING_CONVERTER.put(Long.class, long.class);
43  		AUTOBOXING_CONVERTER.put(int.class, Integer.class);
44  		AUTOBOXING_CONVERTER.put(Integer.class, int.class);
45  		AUTOBOXING_CONVERTER.put(char.class, Character.class);
46  		AUTOBOXING_CONVERTER.put(Character.class, char.class);
47  		AUTOBOXING_CONVERTER.put(byte.class, Byte.class);
48  		AUTOBOXING_CONVERTER.put(Byte.class, byte.class);
49  		AUTOBOXING_CONVERTER.put(short.class, Short.class);
50  		AUTOBOXING_CONVERTER.put(Short.class, short.class);
51  		AUTOBOXING_CONVERTER.put(boolean.class, Boolean.class);
52  		AUTOBOXING_CONVERTER.put(Boolean.class, boolean.class);
53  		AUTOBOXING_CONVERTER.put(double.class, Double.class);
54  		AUTOBOXING_CONVERTER.put(Double.class, double.class);
55  		AUTOBOXING_CONVERTER.put(float.class, Float.class);
56  		AUTOBOXING_CONVERTER.put(Float.class, float.class);
57  	}
58  	
59  	// no instantiation
60  	private ReflectionUtils() {
61  	}
62  	
63  	/**
64  	 * Returns the getter method associated with the object's field.
65  	 * 
66  	 * @param object
67  	 *            the object
68  	 * @param fieldName
69  	 *            the name of the field
70  	 * @return the getter method
71  	 * @throws NullPointerException
72  	 *             if object or fieldName is null
73  	 * @throws SuperCsvReflectionException
74  	 *             if the getter doesn't exist or is not visible
75  	 */
76  	public static Method findGetter(final Object object, final String fieldName) {
77  		if( object == null ) {
78  			throw new NullPointerException("object should not be null");
79  		} else if( fieldName == null ) {
80  			throw new NullPointerException("fieldName should not be null");
81  		}
82  		
83  		final Class<?> clazz = object.getClass();
84  		
85  		// find a standard getter
86  		final String standardGetterName = getMethodNameForField(GET_PREFIX, fieldName);
87  		Method getter = findGetterWithCompatibleReturnType(standardGetterName, clazz, false);
88  		
89  		// if that fails, try for an isX() style boolean getter
90  		if( getter == null ) {
91  			final String booleanGetterName = getMethodNameForField(IS_PREFIX, fieldName);
92  			getter = findGetterWithCompatibleReturnType(booleanGetterName, clazz, true);
93  		}
94  		
95  		if( getter == null ) {
96  			throw new SuperCsvReflectionException(
97  				String
98  					.format(
99  						"unable to find getter for field %s in class %s - check that the corresponding nameMapping element matches the field name in the bean",
100 						fieldName, clazz.getName()));
101 		}
102 		
103 		return getter;
104 	}
105 	
106 	/**
107 	 * Helper method for findGetter() that finds a getter with the supplied name, optionally enforcing that the method
108 	 * must have a Boolean/boolean return type. Developer note: this method could have accepted an actual return type to
109 	 * enforce, but it was more efficient to cater for only Booleans (as they're the only type that has differently
110 	 * named getters).
111 	 * 
112 	 * @param getterName
113 	 *            the getter name
114 	 * @param clazz
115 	 *            the class
116 	 * @param enforceBooleanReturnType
117 	 *            if true, the method must return a Boolean/boolean, otherwise it's return type doesn't matter
118 	 * @return the getter, or null if none is found
119 	 */
120 	private static Method findGetterWithCompatibleReturnType(final String getterName, final Class<?> clazz,
121 		final boolean enforceBooleanReturnType) {
122 		
123 		for( final Method method : clazz.getMethods() ) {
124 			
125 			if( !getterName.equalsIgnoreCase(method.getName()) || method.getParameterTypes().length != 0
126 				|| method.getReturnType().equals(void.class) ) {
127 				continue; // getter must have correct name, 0 parameters and a return type
128 			}
129 			
130 			if( !enforceBooleanReturnType || boolean.class.equals(method.getReturnType())
131 				|| Boolean.class.equals(method.getReturnType()) ) {
132 				return method;
133 			}
134 			
135 		}
136 		
137 		return null;
138 	}
139 	
140 	/**
141 	 * Returns the setter method associated with the object's field.
142 	 * <p>
143 	 * This method handles any autoboxing/unboxing of the argument passed to the setter (e.g. if the setter type is a
144 	 * primitive {@code int} but the argument passed to the setter is an {@code Integer}) by looking for a setter with
145 	 * the same type, and failing that checking for a setter with the corresponding primitive/wrapper type.
146 	 * <p>
147 	 * It also allows for an argument type that is a subclass or implementation of the setter type (when the setter type
148 	 * is an {@code Object} or {@code interface} respectively).
149 	 * 
150 	 * @param object
151 	 *            the object
152 	 * @param fieldName
153 	 *            the name of the field
154 	 * @param argumentType
155 	 *            the type to be passed to the setter
156 	 * @return the setter method
157 	 * @throws NullPointerException
158 	 *             if object, fieldName or fieldType is null
159 	 * @throws SuperCsvReflectionException
160 	 *             if the setter doesn't exist or is not visible
161 	 */
162 	public static Method findSetter(final Object object, final String fieldName, final Class<?> argumentType) {
163 		if( object == null ) {
164 			throw new NullPointerException("object should not be null");
165 		} else if( fieldName == null ) {
166 			throw new NullPointerException("fieldName should not be null");
167 		} else if( argumentType == null ) {
168 			throw new NullPointerException("argumentType should not be null");
169 		}
170 		
171 		final String setterName = getMethodNameForField(SET_PREFIX, fieldName);
172 		final Class<?> clazz = object.getClass();
173 		
174 		// find a setter compatible with the supplied argument type
175 		Method setter = findSetterWithCompatibleParamType(clazz, setterName, argumentType);
176 		
177 		// if that failed, try the corresponding primitive/wrapper if it's a type that can be autoboxed/unboxed
178 		if( setter == null && AUTOBOXING_CONVERTER.containsKey(argumentType) ) {
179 			setter = findSetterWithCompatibleParamType(clazz, setterName, AUTOBOXING_CONVERTER.get(argumentType));
180 		}
181 		
182 		if( setter == null ) {
183 			throw new SuperCsvReflectionException(
184 				String
185 					.format(
186 						"unable to find method %s(%s) in class %s - check that the corresponding nameMapping element matches the field name in the bean, "
187 							+ "and the cell processor returns a type compatible with the field", setterName,
188 						argumentType.getName(), clazz.getName()));
189 		}
190 		
191 		return setter;
192 	}
193 	
194 	/**
195 	 * Helper method for findSetter() that returns the setter method of the supplied name, whose parameter type is
196 	 * compatible with the supplied argument type (will allow an object of that type to be used when invoking the
197 	 * setter), or returns <tt>null</tt> if no match is found. Preference is given to setters whose parameter type is an
198 	 * exact match, but if there is none, then the first compatible method found is returned.
199 	 * 
200 	 * @param clazz
201 	 *            the class containing the setter
202 	 * @param setterName
203 	 *            the name of the setter
204 	 * @param argumentType
205 	 *            the type to be passed to the setter
206 	 * @return the setter method, or null if none is found
207 	 */
208 	private static Method findSetterWithCompatibleParamType(final Class<?> clazz, final String setterName,
209 		final Class<?> argumentType) {
210 		
211 		Method compatibleSetter = null;
212 		for( final Method method : clazz.getMethods() ) {
213 			
214 			if( !setterName.equalsIgnoreCase(method.getName()) || method.getParameterTypes().length != 1 ) {
215 				continue; // setter must have correct name and only 1 parameter
216 			}
217 			
218 			final Class<?> parameterType = method.getParameterTypes()[0];
219 			if( parameterType.equals(argumentType) ) {
220 				compatibleSetter = method;
221 				break; // exact match
222 				
223 			} else if( parameterType.isAssignableFrom(argumentType) ) {
224 				compatibleSetter = method; // potential match, but keep looking for exact match
225 			}
226 			
227 		}
228 		
229 		return compatibleSetter;
230 	}
231 	
232 	/**
233 	 * Gets the camelcase getter/setter method name for a field.
234 	 * 
235 	 * @param prefix
236 	 *            the method prefix
237 	 * @param fieldName
238 	 *            the field name
239 	 * @return the method name
240 	 */
241 	private static String getMethodNameForField(final String prefix, final String fieldName) {
242 		return prefix + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
243 	}
244 }