View Javadoc

    1   package org.acegisecurity.domain.util;
    2
    3   import java.lang.reflect.ParameterizedType;
    4   import java.lang.reflect.Type;
    5
    6
    7   /***
    8    * Provides a helper that locates the declarated generics type of a class.
    9    *
    10   * @author Ben Alex
    11   * @version $Id: GenericsUtils.java,v 1.2 2005/11/17 00:55:50 benalex Exp $
    12   */
    13  public class GenericsUtils {
    14      /***
    15       * Locates the first generic declaration on a class.
    16       *
    17       * @param clazz The class to introspect
    18       * @return the first generic declaration, or <code>null</code> if cannot be determined
    19       */
    20      public static Class getGeneric(Class clazz) {
    21          Type genType = clazz.getGenericSuperclass();
    22
    23          if (genType instanceof ParameterizedType) {
    24              Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
    25
    26              if ((params != null) && (params.length == 1)) {
    27                  return (Class) params[0];
    28              }
    29          }
    30
    31          return null;
    32      }
    33  }