sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl Java Examples

The following examples show how to use sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestForGenericType.java    From TarsJava with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testGenerictype() throws InvocationTargetException, IllegalAccessException, InstantiationException {
    CompletableFuture<Integer> completableFuture = new CompletableFuture<>();
    Method[] methods = TestsMethod.class.getMethods();
    Method method = methods[0];
    Type type = method.getGenericReturnType();
    Type type2 = method.getReturnType();
    Assert.assertEquals(type2, CompletableFuture.class);
    ParameterizedTypeImpl parameterizedType = (ParameterizedTypeImpl) method.getGenericReturnType();
    Assert.assertEquals(parameterizedType.getActualTypeArguments()[0].getTypeName(), "java.util.List<java.lang.Integer>");
    System.out.println(parameterizedType.getActualTypeArguments()[0].getTypeName());
    System.out.println("get inner type is " + parameterizedType.getActualTypeArguments()[0]);
    System.out.println("return type is " + method.getGenericReturnType());
    System.out.println(methods[0].invoke(TestsMethod.class.newInstance()));
    System.out.println(completableFuture.getClass().getName());
}
 
Example #2
Source File: SwaggerReader.java    From netty-rest with Apache License 2.0 6 votes vote down vote up
public static Type getActualType(Class readClass, Type parameterizedType)
{
    // if the parameter has a generic type, it will be read as Object
    // so we need to find the actual implementation and return that type.

    // the generic type may not come from the HttpService class, if it's not keep track of it:
    // ((TypeVariableImpl)parameters[2].getParameterizedType()).getGenericDeclaration().getTypeParameters()[0].getBounds()[0]
    if (parameterizedType instanceof TypeVariableImpl) {
        TypeVariable[] genericParameters = readClass.getSuperclass().getTypeParameters();
        Type[] implementations = ((ParameterizedTypeImpl) readClass.getGenericSuperclass()).getActualTypeArguments();
        for (int i = 0; i < genericParameters.length; i++) {
            if (genericParameters[i].getName().equals(((TypeVariableImpl) parameterizedType).getName())) {
                return implementations[i];
            }
        }
    }
    return parameterizedType;
}
 
Example #3
Source File: ClassUtils.java    From we-cmdb with Apache License 2.0 6 votes vote down vote up
public static <T extends ResourceDto<T, ?>> Map<String, Class<?>> getAllDtoRefResources(Class<T> dtoClazz) {
    Map<String, Class<?>> refResourceMap = new HashMap<>();

    Field[] fields = dtoClazz.getDeclaredFields();
    for (Field field : fields) {
        Class<?> type = field.getType();
        String dtoFieldName = field.getName();
        if (!type.getPackage().equals(java.lang.Integer.class.getPackage())) {
            if (type.equals(java.util.List.class)) {
                ParameterizedTypeImpl t = (ParameterizedTypeImpl) field.getGenericType();
                Class argType = null;
                try {
                    argType = Class.forName(t.getActualTypeArguments()[0].getTypeName());
                } catch (ClassNotFoundException e) {
                }
                refResourceMap.put(dtoFieldName, argType);
            } else {
                refResourceMap.put(dtoFieldName, type);
            }
        }
    }

    return refResourceMap;
}
 
Example #4
Source File: ParamsFlattener.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
private Object unflattedComplexType(Class<?> clazz, Props props, ObjectRefsManager manager) {

    Constructor<?> constructor = clazz.getConstructors()[0];
    
    List<Type> constClasses = new ArrayList<>();
    Class<?>[] parameterTypes = constructor.getParameterTypes();
    Type[] parameterGenericTypes = constructor.getGenericParameterTypes();
    for (int i = 0; i < parameterGenericTypes.length; i++) {
    	if (parameterGenericTypes[i] instanceof ParameterizedTypeImpl) {
    		// This is a class with a generic type
    		// (for example, a java.util.List<java.lang.String>)
    		constClasses.add(parameterGenericTypes[i]);
    	} else {
    		constClasses.add(parameterTypes[i]);
    	}
    }
    
    Object[] constParams = new Object[parameterTypes.length];
    List<String> paramNames = ParamAnnotationUtils.getParamNames(constructor);

    for (int i = 0; i < constParams.length; i++) {
      String paramName = paramNames.get(i);
      constParams[i] = unflattenValue(paramName, constClasses.get(i), props.getProp(paramName),
          manager);
    }

    try {
      return constructor.newInstance(constParams);
    } catch (Exception e) {
      throw new ProtocolException(
          "Exception while creating an object for the class '" + clazz.getSimpleName() + "'", e);
    }
  }
 
Example #5
Source File: TReflect.java    From Voovan with Apache License 2.0 5 votes vote down vote up
/**
 * 获取类型的范型类型
 * @param type 类型对象
 * @return Class[] 对象
 */
public static Class[] getGenericClass(Type type) {
    ParameterizedType parameterizedType = null;
    if(type instanceof ParameterizedType) {
        parameterizedType = (ParameterizedType) type;
    }

    if(parameterizedType==null){
        return null;
    }

    Type[] actualType = parameterizedType.getActualTypeArguments();
    Class[] result = new Class[actualType.length];

    for(int i=0;i<actualType.length;i++){
        Type oneType = actualType[i];

        if(oneType instanceof Class){
            result[i] = (Class)oneType;
        } else if(type instanceof ParameterizedType){
            result[i] = ((ParameterizedTypeImpl)oneType).getRawType();
        } else {
            result[i] = null;
        }
    }

    return result;
}
 
Example #6
Source File: TReflect.java    From Voovan with Apache License 2.0 5 votes vote down vote up
/**
 * 获取类型的范型类型信息
 * @param type 类型对象
 * @return GenericInfo[] 范型类型信息
 */
public static GenericInfo[] getGenericInfo(Type type) {
    if(type == null) {
        return null;
    }

    ParameterizedType parameterizedType = null;
    if(type instanceof ParameterizedType) {
        parameterizedType = (ParameterizedType) type;
    }

    if(parameterizedType==null){
        return null;
    }
    Type[] actualType = parameterizedType.getActualTypeArguments();
    GenericInfo[] result = new GenericInfo[actualType.length];

    for(int i=0;i<actualType.length;i++){
        Type oneType = actualType[i];

        if(oneType instanceof Class){
            result[i] = new GenericInfo((Class)oneType, null);
        } else if(type instanceof ParameterizedType){
            result[i] = new GenericInfo(((ParameterizedTypeImpl)oneType).getRawType(), oneType);
        } else {
            result[i] = new GenericInfo(null, oneType);
        }
    }

    return result;
}
 
Example #7
Source File: SinkTest.java    From sylph with Apache License 2.0 5 votes vote down vote up
@Test
public void genericTest()
{
    Type[] type = TestSink.class.getGenericInterfaces();

    Type checkType = ParameterizedTypeImpl.make(Sink.class, new Type[] {ParameterizedTypeImpl.make(List.class, new Type[] {String.class}, null)}, null);
    Assert.assertArrayEquals(type, new Type[] {checkType});
}
 
Example #8
Source File: ClassFactory.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
public static boolean checkIfCompatibleType(Class<?> expected, Type actual) {

        if (expected == null) {
            return false;
        }

        if (actual == null) {
            return true;
        }

        if (expected.isPrimitive()) {
            actual = convertToPrimitiveType(actual);
        }

        if (actual instanceof ParameterizedType) {
            return expected.isAssignableFrom(((ParameterizedTypeImpl) actual).getRawType());
        }

        if (actual instanceof TypeVariableImpl) { // we don't know at this point ... generic type
            return true;
        }

        return expected.equals(actual) || expected.isInstance(actual) || ((Class<?>) actual).isAssignableFrom(expected);
    }
 
Example #9
Source File: HbaseRowSerializerUtil.java    From presto-connectors with Apache License 2.0 4 votes vote down vote up
public MyTypeReference(Class<?> keyType, Class<?> valueType)
{
    this.type = ParameterizedTypeImpl.make(Map.class, new java.lang.reflect.Type[] {keyType, valueType}, null);
}
 
Example #10
Source File: MalformedParameterizedTypeExceptionServlet.java    From easybuggy with Apache License 2.0 4 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    ParameterizedTypeImpl.make(List.class, new Type[]{}, null);
}
 
Example #11
Source File: TypeResolver.java    From jdk8u-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Replaces a {@link Class Class} with type parameters
 * with a {@link ParameterizedType ParameterizedType}
 * where every parameter is bound to itself.
 * When calling {@link #resolveInClass} in the context of {@code inClass},
 * we can't just pass {@code inClass} as the {@code actual} parameter,
 * because if {@code inClass} has type parameters
 * that would be interpreted as accessing the raw type,
 * so we would get unwanted erasure.
 * This is why we bind each parameter to itself.
 * If {@code inClass} does have type parameters and has methods
 * where those parameters appear in the return type or argument types,
 * we will correctly leave those types alone.
 *
 * @param inClass  the base class used to resolve
 * @return a parameterized type for the class,
 *         or the same class as {@code inClass}
 */
private static Type getActualType(Class<?> inClass) {
    Type[] params = inClass.getTypeParameters();
    return (params.length == 0)
            ? inClass
            : ParameterizedTypeImpl.make(
                    inClass, params, inClass.getEnclosingClass());
}
 
Example #12
Source File: TypeResolver.java    From jdk8u-dev-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Replaces a {@link Class Class} with type parameters
 * with a {@link ParameterizedType ParameterizedType}
 * where every parameter is bound to itself.
 * When calling {@link #resolveInClass} in the context of {@code inClass},
 * we can't just pass {@code inClass} as the {@code actual} parameter,
 * because if {@code inClass} has type parameters
 * that would be interpreted as accessing the raw type,
 * so we would get unwanted erasure.
 * This is why we bind each parameter to itself.
 * If {@code inClass} does have type parameters and has methods
 * where those parameters appear in the return type or argument types,
 * we will correctly leave those types alone.
 *
 * @param inClass  the base class used to resolve
 * @return a parameterized type for the class,
 *         or the same class as {@code inClass}
 */
private static Type getActualType(Class<?> inClass) {
    Type[] params = inClass.getTypeParameters();
    return (params.length == 0)
            ? inClass
            : ParameterizedTypeImpl.make(
                    inClass, params, inClass.getEnclosingClass());
}
 
Example #13
Source File: TypeResolver.java    From jdk8u-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Replaces a {@link Class Class} with type parameters
 * with a {@link ParameterizedType ParameterizedType}
 * where every parameter is bound to itself.
 * When calling {@link #resolveInClass} in the context of {@code inClass},
 * we can't just pass {@code inClass} as the {@code actual} parameter,
 * because if {@code inClass} has type parameters
 * that would be interpreted as accessing the raw type,
 * so we would get unwanted erasure.
 * This is why we bind each parameter to itself.
 * If {@code inClass} does have type parameters and has methods
 * where those parameters appear in the return type or argument types,
 * we will correctly leave those types alone.
 *
 * @param inClass  the base class used to resolve
 * @return a parameterized type for the class,
 *         or the same class as {@code inClass}
 */
private static Type getActualType(Class<?> inClass) {
    Type[] params = inClass.getTypeParameters();
    return (params.length == 0)
            ? inClass
            : ParameterizedTypeImpl.make(
                    inClass, params, inClass.getEnclosingClass());
}
 
Example #14
Source File: TypeResolver.java    From jdk8u_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Replaces a {@link Class Class} with type parameters
 * with a {@link ParameterizedType ParameterizedType}
 * where every parameter is bound to itself.
 * When calling {@link #resolveInClass} in the context of {@code inClass},
 * we can't just pass {@code inClass} as the {@code actual} parameter,
 * because if {@code inClass} has type parameters
 * that would be interpreted as accessing the raw type,
 * so we would get unwanted erasure.
 * This is why we bind each parameter to itself.
 * If {@code inClass} does have type parameters and has methods
 * where those parameters appear in the return type or argument types,
 * we will correctly leave those types alone.
 *
 * @param inClass  the base class used to resolve
 * @return a parameterized type for the class,
 *         or the same class as {@code inClass}
 */
private static Type getActualType(Class<?> inClass) {
    Type[] params = inClass.getTypeParameters();
    return (params.length == 0)
            ? inClass
            : ParameterizedTypeImpl.make(
                    inClass, params, inClass.getEnclosingClass());
}
 
Example #15
Source File: TypeResolver.java    From openjdk-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Replaces a {@link Class Class} with type parameters
 * with a {@link ParameterizedType ParameterizedType}
 * where every parameter is bound to itself.
 * When calling {@link #resolveInClass} in the context of {@code inClass},
 * we can't just pass {@code inClass} as the {@code actual} parameter,
 * because if {@code inClass} has type parameters
 * that would be interpreted as accessing the raw type,
 * so we would get unwanted erasure.
 * This is why we bind each parameter to itself.
 * If {@code inClass} does have type parameters and has methods
 * where those parameters appear in the return type or argument types,
 * we will correctly leave those types alone.
 *
 * @param inClass  the base class used to resolve
 * @return a parameterized type for the class,
 *         or the same class as {@code inClass}
 */
private static Type getActualType(Class<?> inClass) {
    Type[] params = inClass.getTypeParameters();
    return (params.length == 0)
            ? inClass
            : ParameterizedTypeImpl.make(
                    inClass, params, inClass.getEnclosingClass());
}
 
Example #16
Source File: TypeResolver.java    From openjdk-8-source with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Replaces a {@link Class Class} with type parameters
 * with a {@link ParameterizedType ParameterizedType}
 * where every parameter is bound to itself.
 * When calling {@link #resolveInClass} in the context of {@code inClass},
 * we can't just pass {@code inClass} as the {@code actual} parameter,
 * because if {@code inClass} has type parameters
 * that would be interpreted as accessing the raw type,
 * so we would get unwanted erasure.
 * This is why we bind each parameter to itself.
 * If {@code inClass} does have type parameters and has methods
 * where those parameters appear in the return type or argument types,
 * we will correctly leave those types alone.
 *
 * @param inClass  the base class used to resolve
 * @return a parameterized type for the class,
 *         or the same class as {@code inClass}
 */
private static Type getActualType(Class<?> inClass) {
    Type[] params = inClass.getTypeParameters();
    return (params.length == 0)
            ? inClass
            : ParameterizedTypeImpl.make(
                    inClass, params, inClass.getEnclosingClass());
}
 
Example #17
Source File: TypeResolver.java    From hottub with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Replaces a {@link Class Class} with type parameters
 * with a {@link ParameterizedType ParameterizedType}
 * where every parameter is bound to itself.
 * When calling {@link #resolveInClass} in the context of {@code inClass},
 * we can't just pass {@code inClass} as the {@code actual} parameter,
 * because if {@code inClass} has type parameters
 * that would be interpreted as accessing the raw type,
 * so we would get unwanted erasure.
 * This is why we bind each parameter to itself.
 * If {@code inClass} does have type parameters and has methods
 * where those parameters appear in the return type or argument types,
 * we will correctly leave those types alone.
 *
 * @param inClass  the base class used to resolve
 * @return a parameterized type for the class,
 *         or the same class as {@code inClass}
 */
private static Type getActualType(Class<?> inClass) {
    Type[] params = inClass.getTypeParameters();
    return (params.length == 0)
            ? inClass
            : ParameterizedTypeImpl.make(
                    inClass, params, inClass.getEnclosingClass());
}
 
Example #18
Source File: TypeResolver.java    From openjdk-jdk9 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Replaces a {@link Class Class} with type parameters
 * with a {@link ParameterizedType ParameterizedType}
 * where every parameter is bound to itself.
 * When calling {@link #resolveInClass} in the context of {@code inClass},
 * we can't just pass {@code inClass} as the {@code actual} parameter,
 * because if {@code inClass} has type parameters
 * that would be interpreted as accessing the raw type,
 * so we would get unwanted erasure.
 * This is why we bind each parameter to itself.
 * If {@code inClass} does have type parameters and has methods
 * where those parameters appear in the return type or argument types,
 * we will correctly leave those types alone.
 *
 * @param inClass  the base class used to resolve
 * @return a parameterized type for the class,
 *         or the same class as {@code inClass}
 */
private static Type getActualType(Class<?> inClass) {
    Type[] params = inClass.getTypeParameters();
    return (params.length == 0)
            ? inClass
            : ParameterizedTypeImpl.make(
                    inClass, params, inClass.getEnclosingClass());
}
 
Example #19
Source File: TypeResolver.java    From Bytecoder with Apache License 2.0 3 votes vote down vote up
/**
 * Replaces a {@link Class Class} with type parameters
 * with a {@link ParameterizedType ParameterizedType}
 * where every parameter is bound to itself.
 * When calling {@link #resolveInClass} in the context of {@code inClass},
 * we can't just pass {@code inClass} as the {@code actual} parameter,
 * because if {@code inClass} has type parameters
 * that would be interpreted as accessing the raw type,
 * so we would get unwanted erasure.
 * This is why we bind each parameter to itself.
 * If {@code inClass} does have type parameters and has methods
 * where those parameters appear in the return type or argument types,
 * we will correctly leave those types alone.
 *
 * @param inClass  the base class used to resolve
 * @return a parameterized type for the class,
 *         or the same class as {@code inClass}
 */
private static Type getActualType(Class<?> inClass) {
    Type[] params = inClass.getTypeParameters();
    return (params.length == 0)
            ? inClass
            : ParameterizedTypeImpl.make(
                    inClass, params, inClass.getEnclosingClass());
}
 
Example #20
Source File: TypeResolver.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Replaces a {@link Class Class} with type parameters
 * with a {@link ParameterizedType ParameterizedType}
 * where every parameter is bound to itself.
 * When calling {@link #resolveInClass} in the context of {@code inClass},
 * we can't just pass {@code inClass} as the {@code actual} parameter,
 * because if {@code inClass} has type parameters
 * that would be interpreted as accessing the raw type,
 * so we would get unwanted erasure.
 * This is why we bind each parameter to itself.
 * If {@code inClass} does have type parameters and has methods
 * where those parameters appear in the return type or argument types,
 * we will correctly leave those types alone.
 *
 * @param inClass  the base class used to resolve
 * @return a parameterized type for the class,
 *         or the same class as {@code inClass}
 */
private static Type getActualType(Class<?> inClass) {
    Type[] params = inClass.getTypeParameters();
    return (params.length == 0)
            ? inClass
            : ParameterizedTypeImpl.make(
                    inClass, params, inClass.getEnclosingClass());
}
 
Example #21
Source File: TypeResolver.java    From openjdk-jdk8u with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Replaces a {@link Class Class} with type parameters
 * with a {@link ParameterizedType ParameterizedType}
 * where every parameter is bound to itself.
 * When calling {@link #resolveInClass} in the context of {@code inClass},
 * we can't just pass {@code inClass} as the {@code actual} parameter,
 * because if {@code inClass} has type parameters
 * that would be interpreted as accessing the raw type,
 * so we would get unwanted erasure.
 * This is why we bind each parameter to itself.
 * If {@code inClass} does have type parameters and has methods
 * where those parameters appear in the return type or argument types,
 * we will correctly leave those types alone.
 *
 * @param inClass  the base class used to resolve
 * @return a parameterized type for the class,
 *         or the same class as {@code inClass}
 */
private static Type getActualType(Class<?> inClass) {
    Type[] params = inClass.getTypeParameters();
    return (params.length == 0)
            ? inClass
            : ParameterizedTypeImpl.make(
                    inClass, params, inClass.getEnclosingClass());
}
 
Example #22
Source File: TypeResolver.java    From jdk8u60 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Replaces a {@link Class Class} with type parameters
 * with a {@link ParameterizedType ParameterizedType}
 * where every parameter is bound to itself.
 * When calling {@link #resolveInClass} in the context of {@code inClass},
 * we can't just pass {@code inClass} as the {@code actual} parameter,
 * because if {@code inClass} has type parameters
 * that would be interpreted as accessing the raw type,
 * so we would get unwanted erasure.
 * This is why we bind each parameter to itself.
 * If {@code inClass} does have type parameters and has methods
 * where those parameters appear in the return type or argument types,
 * we will correctly leave those types alone.
 *
 * @param inClass  the base class used to resolve
 * @return a parameterized type for the class,
 *         or the same class as {@code inClass}
 */
private static Type getActualType(Class<?> inClass) {
    Type[] params = inClass.getTypeParameters();
    return (params.length == 0)
            ? inClass
            : ParameterizedTypeImpl.make(
                    inClass, params, inClass.getEnclosingClass());
}
 
Example #23
Source File: TypeResolver.java    From TencentKona-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Replaces a {@link Class Class} with type parameters
 * with a {@link ParameterizedType ParameterizedType}
 * where every parameter is bound to itself.
 * When calling {@link #resolveInClass} in the context of {@code inClass},
 * we can't just pass {@code inClass} as the {@code actual} parameter,
 * because if {@code inClass} has type parameters
 * that would be interpreted as accessing the raw type,
 * so we would get unwanted erasure.
 * This is why we bind each parameter to itself.
 * If {@code inClass} does have type parameters and has methods
 * where those parameters appear in the return type or argument types,
 * we will correctly leave those types alone.
 *
 * @param inClass  the base class used to resolve
 * @return a parameterized type for the class,
 *         or the same class as {@code inClass}
 */
private static Type getActualType(Class<?> inClass) {
    Type[] params = inClass.getTypeParameters();
    return (params.length == 0)
            ? inClass
            : ParameterizedTypeImpl.make(
                    inClass, params, inClass.getEnclosingClass());
}
 
Example #24
Source File: TypeResolver.java    From dragonwell8_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Replaces a {@link Class Class} with type parameters
 * with a {@link ParameterizedType ParameterizedType}
 * where every parameter is bound to itself.
 * When calling {@link #resolveInClass} in the context of {@code inClass},
 * we can't just pass {@code inClass} as the {@code actual} parameter,
 * because if {@code inClass} has type parameters
 * that would be interpreted as accessing the raw type,
 * so we would get unwanted erasure.
 * This is why we bind each parameter to itself.
 * If {@code inClass} does have type parameters and has methods
 * where those parameters appear in the return type or argument types,
 * we will correctly leave those types alone.
 *
 * @param inClass  the base class used to resolve
 * @return a parameterized type for the class,
 *         or the same class as {@code inClass}
 */
private static Type getActualType(Class<?> inClass) {
    Type[] params = inClass.getTypeParameters();
    return (params.length == 0)
            ? inClass
            : ParameterizedTypeImpl.make(
                    inClass, params, inClass.getEnclosingClass());
}