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

The following examples show how to use sun.reflect.generics.reflectiveObjects.TypeVariableImpl. 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: 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 2
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 3
Source Project: sofa-acts   Source File: CSVApisUtilTest.java    License: Apache License 2.0 5 votes vote down vote up
/***
 * Test method for {@link CSVApisUtil#HandMutiParameType(Type, int)}
 */
@Test
public void testHandMutiParameType() {
    //class
    Type res = CSVApisUtil.HandMutiParameType(int.class, 0);
    Assert.assertEquals(res, int.class);

    res = CSVApisUtil.HandMutiParameType(int.class, 1);
    Assert.assertNotEquals(res, int.class);

    //ParameterizedType
    Map<String, String> map = new HashMap<String, String>(1);

    res = CSVApisUtil.HandMutiParameType(map.getClass().getGenericSuperclass(), 0);
    Assert.assertEquals(((TypeVariableImpl) res).getName(), "K");

    res = CSVApisUtil.HandMutiParameType(map.getClass().getGenericSuperclass(), 1);
    Assert.assertEquals(((TypeVariableImpl) res).getName(), "V");

    List<String> list = new ArrayList<String>(1);

    res = CSVApisUtil.HandMutiParameType(list.getClass().getGenericSuperclass(), 0);
    Assert.assertEquals(((TypeVariableImpl) res).getName(), "E");

    res = CSVApisUtil.HandMutiParameType(list.getClass().getGenericSuperclass(), 1);
    Assert.assertEquals(res, null);

}