Java Code Examples for sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl

The following examples show how to use sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl. These examples are extracted from open source projects. 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 Project: we-cmdb   Source File: ClassUtils.java    License: 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 2
@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 3
Source Project: rest.vertx   Source File: ClassFactory.java    License: Apache License 2.0 6 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 4
Source Project: netty-rest   Source File: SwaggerReader.java    License: 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 5
Source Project: sylph   Source File: SinkTest.java    License: 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 6
Source Project: Voovan   Source File: TReflect.java    License: 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 Project: Voovan   Source File: TReflect.java    License: 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 8
Source Project: kurento-java   Source File: ParamsFlattener.java    License: 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 9
Source Project: presto-connectors   Source File: HbaseRowSerializerUtil.java    License: 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
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    ParameterizedTypeImpl.make(List.class, new Type[]{}, null);
}
 
Example 11
Source Project: dragonwell8_jdk   Source File: TypeResolver.java    License: 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 Project: TencentKona-8   Source File: TypeResolver.java    License: 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 Project: jdk8u60   Source File: TypeResolver.java    License: 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 Project: openjdk-jdk8u   Source File: TypeResolver.java    License: 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
/**
 * 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 Project: Bytecoder   Source File: TypeResolver.java    License: 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 17
Source Project: openjdk-jdk9   Source File: TypeResolver.java    License: 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 Project: jdk8u-jdk   Source File: TypeResolver.java    License: 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 Project: hottub   Source File: TypeResolver.java    License: 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 20
/**
 * 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 Project: openjdk-8   Source File: TypeResolver.java    License: 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 Project: jdk8u_jdk   Source File: TypeResolver.java    License: 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 Project: jdk8u-jdk   Source File: TypeResolver.java    License: 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 Project: jdk8u-dev-jdk   Source File: TypeResolver.java    License: 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());
}