|
35 | 35 |
|
36 | 36 | import java.lang.reflect.Field; |
37 | 37 | import java.lang.reflect.Method; |
| 38 | +import java.lang.reflect.ParameterizedType; |
38 | 39 | import java.lang.reflect.Type; |
| 40 | +import java.lang.reflect.TypeVariable; |
39 | 41 | import java.util.List; |
| 42 | +import java.util.Stack; |
40 | 43 |
|
41 | 44 | /** |
42 | 45 | * Useful methods for working with {@link Type} objects, particularly generic |
@@ -225,4 +228,158 @@ public static Type getTypeParameter(final Type type, final Class<?> c, |
225 | 228 | c.getTypeParameters()[paramNo]); |
226 | 229 | } |
227 | 230 |
|
| 231 | + /** |
| 232 | + * Obtains a string for the given field when viewed through the specified |
| 233 | + * subclass, which matches the syntax of Java source code. |
| 234 | + * <p> |
| 235 | + * Many thanks to <a |
| 236 | + * href="http://stackoverflow.com/questions/28143029">rgettman on |
| 237 | + * StackOverflow</a> for developing this logic. |
| 238 | + * </p> |
| 239 | + * |
| 240 | + * @param c The class from which to view the field in question. |
| 241 | + * @param field The field for which to generate a type string. |
| 242 | + * @return A type string describing the method, which matches the syntax of |
| 243 | + * Java source code. |
| 244 | + */ |
| 245 | + public static String getFieldTypeString(final Class<?> c, final Field field) { |
| 246 | + final Type genericType = field.getGenericType(); |
| 247 | + // Declared as actual type name... |
| 248 | + if (genericType instanceof Class) { |
| 249 | + final Class<?> genericTypeClass = (Class<?>) genericType; |
| 250 | + return genericTypeClass.getName(); |
| 251 | + } |
| 252 | + // .. or as a generic type? |
| 253 | + else if (genericType instanceof TypeVariable) { |
| 254 | + final TypeVariable<?> typeVariable = (TypeVariable<?>) genericType; |
| 255 | + final Class<?> declaringClass = field.getDeclaringClass(); |
| 256 | + |
| 257 | + // Create a Stack of classes going from c up to, but not including, |
| 258 | + // the declaring class. |
| 259 | + final Stack<Class<?>> stack = new Stack<Class<?>>(); |
| 260 | + Class<?> currClass = c; |
| 261 | + while (!currClass.equals(declaringClass)) { |
| 262 | + stack.push(currClass); |
| 263 | + currClass = currClass.getSuperclass(); |
| 264 | + } |
| 265 | + // Get the original type parameter from the declaring class. |
| 266 | + int typeVariableIndex = -1; |
| 267 | + String typeVariableName = typeVariable.getName(); |
| 268 | + TypeVariable<?>[] currTypeParameters = currClass.getTypeParameters(); |
| 269 | + for (int i = 0; i < currTypeParameters.length; i++) { |
| 270 | + final TypeVariable<?> currTypeVariable = currTypeParameters[i]; |
| 271 | + if (currTypeVariable.getName().equals(typeVariableName)) { |
| 272 | + typeVariableIndex = i; |
| 273 | + break; |
| 274 | + } |
| 275 | + } |
| 276 | + |
| 277 | + if (typeVariableIndex == -1) { |
| 278 | + throw new IllegalStateException("Expected Type variable \"" + |
| 279 | + typeVariable.getName() + "\" in class " + c + |
| 280 | + "; but it was not found."); |
| 281 | + } |
| 282 | + |
| 283 | + // If the type parameter is from the same class, don't bother walking down |
| 284 | + // a non-existent hierarchy. |
| 285 | + if (declaringClass.equals(c)) { |
| 286 | + return getTypeVariableString(typeVariable); |
| 287 | + } |
| 288 | + |
| 289 | + // Pop them in order, keeping track of which index is the type variable. |
| 290 | + while (!stack.isEmpty()) { |
| 291 | + currClass = stack.pop(); |
| 292 | + // Must be ParameterizedType, not Class, because type arguments must be |
| 293 | + // supplied to the generic superclass. |
| 294 | + final ParameterizedType superclassParameterizedType = |
| 295 | + (ParameterizedType) currClass.getGenericSuperclass(); |
| 296 | + final Type currType = |
| 297 | + superclassParameterizedType.getActualTypeArguments()[typeVariableIndex]; |
| 298 | + if (currType instanceof Class) { |
| 299 | + // Type argument is an actual Class, e.g. |
| 300 | + // "extends ArrayList<Integer>". |
| 301 | + currClass = (Class<?>) currType; |
| 302 | + return currClass.getName(); |
| 303 | + } |
| 304 | + else if (currType instanceof TypeVariable) { |
| 305 | + // e.g., "T" |
| 306 | + TypeVariable<?> currTypeVariable = (TypeVariable<?>) currType; |
| 307 | + typeVariableName = currTypeVariable.getName(); |
| 308 | + // Reached passed-in class (bottom of hierarchy)? Report it. |
| 309 | + if (currClass.equals(c)) { |
| 310 | + return getTypeVariableString(currTypeVariable); |
| 311 | + } |
| 312 | + // Not at bottom? Find the type parameter to set up for next loop. |
| 313 | + typeVariableIndex = -1; |
| 314 | + currTypeParameters = currClass.getTypeParameters(); |
| 315 | + for (int i = 0; i < currTypeParameters.length; i++) { |
| 316 | + currTypeVariable = currTypeParameters[i]; |
| 317 | + if (currTypeVariable.getName().equals(typeVariableName)) { |
| 318 | + typeVariableIndex = i; |
| 319 | + break; |
| 320 | + } |
| 321 | + } |
| 322 | + |
| 323 | + if (typeVariableIndex == -1) { |
| 324 | + // Shouldn't get here. |
| 325 | + throw new IllegalStateException("Expected Type variable \"" + |
| 326 | + typeVariable.getName() + "\" in class " + currClass.getName() + |
| 327 | + "; but it was not found."); |
| 328 | + } |
| 329 | + } |
| 330 | + else if (currType instanceof ParameterizedType) { |
| 331 | + final ParameterizedType pType = (ParameterizedType) currType; |
| 332 | + // e.g., "List<T>" |
| 333 | + return pType.toString(); |
| 334 | + } |
| 335 | + } |
| 336 | + } |
| 337 | + // Shouldn't get here. |
| 338 | + final String genericTypeClassName = genericType == null ? "null" : genericType.getClass().getName(); |
| 339 | + throw new IllegalStateException("Unsupported type of Type: " + genericTypeClassName); |
| 340 | + } |
| 341 | + |
| 342 | + /** Gets a string describing a generic type parameter including its bounds. */ |
| 343 | + public static String |
| 344 | + getTypeVariableString(final TypeVariable<?> typeVariable) |
| 345 | + { |
| 346 | + final StringBuilder buf = new StringBuilder(); |
| 347 | + buf.append(typeVariable.getName()); |
| 348 | + final Type[] bounds = typeVariable.getBounds(); |
| 349 | + boolean first = true; |
| 350 | + // Don't report explicit "extends Object". |
| 351 | + if (bounds.length == 1 && bounds[0].equals(Object.class)) { |
| 352 | + return buf.toString(); |
| 353 | + } |
| 354 | + for (final Type bound : bounds) { |
| 355 | + if (first) { |
| 356 | + buf.append(" extends "); |
| 357 | + first = false; |
| 358 | + } |
| 359 | + else { |
| 360 | + buf.append(" & "); |
| 361 | + } |
| 362 | + if (bound instanceof Class) { |
| 363 | + // e.g., "java.util.RandomAccess" |
| 364 | + final Class<?> boundClass = (Class<?>) bound; |
| 365 | + buf.append(boundClass.getName()); |
| 366 | + } |
| 367 | + else if (bound instanceof TypeVariable) { |
| 368 | + // e.g., "T" |
| 369 | + final TypeVariable<?> typeVariableBound = (TypeVariable<?>) bound; |
| 370 | + buf.append(typeVariableBound.getName()); |
| 371 | + } |
| 372 | + else if (bound instanceof ParameterizedType) { |
| 373 | + // e.g., "java.util.Collection<T>" |
| 374 | + final ParameterizedType pType = (ParameterizedType) bound; |
| 375 | + buf.append(pType.toString()); |
| 376 | + } |
| 377 | + else { |
| 378 | + throw new IllegalStateException("Unknown bound type: " + |
| 379 | + bound.getClass().getName()); |
| 380 | + } |
| 381 | + } |
| 382 | + return buf.toString(); |
| 383 | + } |
| 384 | + |
228 | 385 | } |
0 commit comments