Java Class.getTypeParameters Examples

Java Class.getTypeParameters - 30 examples found. These are the top rated real world Java examples of Class.getTypeParameters extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
 public Map<TypeVariable<?>, Type> getGenericArgumentMap(Type... arguments) {
   if (arguments != null
       && representingClass.getTypeParameters().length != arguments.length
       && arguments.length != 0) {
     throw new IllegalArgumentException(
         "arguments length != " + representingClass.getTypeParameters().length);
   }
   GenericInfo<T> genericInfo = applyArguments0(arguments);
   return Collections.unmodifiableMap(genericInfo.genericArgumentMap);
 }
Example #2
0
  /**
   * A simplistic implementation, it may not handle all cases but it should handle enough.
   *
   * @param implementation the type for which the parameter requires a resolution
   * @param type the type that owns the parameter
   * @param parameterIndex the parameter index
   * @return the resolved type
   */
  public static Type resolve(Type implementation, Class<?> type, int parameterIndex) {
    if (implementation == null) {
      throw new NullPointerException();
    }

    //
    if (implementation == type) {
      TypeVariable<? extends Class<?>>[] tp = type.getTypeParameters();
      if (parameterIndex < tp.length) {
        return tp[parameterIndex];
      } else {
        throw new IllegalArgumentException();
      }
    } else if (implementation instanceof Class<?>) {
      Class<?> c = (Class<?>) implementation;
      Type gsc = c.getGenericSuperclass();
      Type resolved = null;
      if (gsc != null) {
        resolved = resolve(gsc, type, parameterIndex);
        if (resolved == null) {
          // Try with interface
        }
      }
      return resolved;
    } else if (implementation instanceof ParameterizedType) {
      ParameterizedType pt = (ParameterizedType) implementation;
      Type[] typeArgs = pt.getActualTypeArguments();
      Type rawType = pt.getRawType();
      if (rawType == type) {
        return typeArgs[parameterIndex];
      } else if (rawType instanceof Class<?>) {
        Class<?> classRawType = (Class<?>) rawType;
        Type resolved = resolve(classRawType, type, parameterIndex);
        if (resolved == null) {
          return null;
        } else if (resolved instanceof TypeVariable) {
          TypeVariable resolvedTV = (TypeVariable) resolved;
          TypeVariable[] a = classRawType.getTypeParameters();
          for (int i = 0; i < a.length; i++) {
            if (a[i].equals(resolvedTV)) {
              return resolve(implementation, classRawType, i);
            }
          }
          throw new AssertionError();
        } else {
          throw new UnsupportedOperationException("Cannot support resolution of " + resolved);
        }
      } else {
        throw new UnsupportedOperationException();
      }
    } else {
      throw new UnsupportedOperationException(
          "todo " + implementation + " " + implementation.getClass());
    }
  }
  /** Checks for validity for classpath scanning. */
  public static boolean isValid(Class<?> type) {
    if (type.isInterface()) return false;

    if (type.getTypeParameters() != null && type.getTypeParameters().length > 0) {
      return false;
    }

    if (!isValidConstructor(type)) return false;

    return true;
  }
Example #4
0
  /** Resolves <code>TypeVariable</code> with given implementation class. */
  public static Type resolveVariable(TypeVariable variable, final Class implClass) {
    final Class rawType = getRawType(implClass, null);

    int index = ArraysUtil.indexOf(rawType.getTypeParameters(), variable);
    if (index >= 0) {
      return variable;
    }

    final Class[] interfaces = rawType.getInterfaces();
    final Type[] genericInterfaces = rawType.getGenericInterfaces();

    for (int i = 0; i <= interfaces.length; i++) {
      Class rawInterface;

      if (i < interfaces.length) {
        rawInterface = interfaces[i];
      } else {
        rawInterface = rawType.getSuperclass();
        if (rawInterface == null) {
          continue;
        }
      }

      final Type resolved = resolveVariable(variable, rawInterface);
      if (resolved instanceof Class || resolved instanceof ParameterizedType) {
        return resolved;
      }

      if (resolved instanceof TypeVariable) {
        final TypeVariable typeVariable = (TypeVariable) resolved;
        index = ArraysUtil.indexOf(rawInterface.getTypeParameters(), typeVariable);

        if (index < 0) {
          throw new IllegalArgumentException("Invalid type variable:" + typeVariable);
        }

        final Type type =
            i < genericInterfaces.length ? genericInterfaces[i] : rawType.getGenericSuperclass();

        if (type instanceof Class) {
          return Object.class;
        }

        if (type instanceof ParameterizedType) {
          return ((ParameterizedType) type).getActualTypeArguments()[index];
        }

        throw new IllegalArgumentException("Unsupported type: " + type);
      }
    }
    return null;
  }
 private ReferenceTypeUsage toReferenceTypeUsage(Class<?> clazz, Type type) {
   TypeDefinition typeDefinition = new ReflectionBasedTypeDefinition(clazz);
   ReferenceTypeUsage referenceTypeUsage = new ReferenceTypeUsage(typeDefinition);
   if (type instanceof ParameterizedType) {
     ParameterizedType parameterizedType = (ParameterizedType) type;
     for (int tp = 0; tp < clazz.getTypeParameters().length; tp++) {
       TypeVariable<? extends Class<?>> typeVariable = clazz.getTypeParameters()[tp];
       Type parameterType = parameterizedType.getActualTypeArguments()[tp];
       referenceTypeUsage
           .getTypeParameterValues()
           .add(typeVariable.getName(), toTypeUsage(parameterType));
     }
   }
   return referenceTypeUsage;
 }
Example #6
0
  /**
   * @param cls
   * @param toClass
   * @param subtypeVarAssigns
   * @return
   */
  private static Map<TypeVariable<?>, Type> getTypeArguments(
      Class<?> cls, Class<?> toClass, Map<TypeVariable<?>, Type> subtypeVarAssigns) {
    // make sure they're assignable
    if (!isAssignable(cls, toClass)) {
      return null;
    }

    // can't work with primitives
    if (cls.isPrimitive()) {
      // both classes are primitives?
      if (toClass.isPrimitive()) {
        // dealing with widening here. No type arguments to be
        // harvested with these two types.
        return new HashMap<TypeVariable<?>, Type>();
      }

      // work with wrapper the wrapper class instead of the primitive
      cls = ClassUtils.primitiveToWrapper(cls);
    }

    // create a copy of the incoming map, or an empty one if it's null
    HashMap<TypeVariable<?>, Type> typeVarAssigns =
        subtypeVarAssigns == null
            ? new HashMap<TypeVariable<?>, Type>()
            : new HashMap<TypeVariable<?>, Type>(subtypeVarAssigns);

    // no arguments for the parameters, or target class has been reached
    if (cls.getTypeParameters().length > 0 || toClass.equals(cls)) {
      return typeVarAssigns;
    }

    // walk the inheritance hierarchy until the target class is reached
    return getTypeArguments(getClosestParentType(cls, toClass), toClass, typeVarAssigns);
  }