List of usage examples for java.lang Class getGenericInterfaces
public Type[] getGenericInterfaces()
From source file:Main.java
/** * get generic class by actual type argument index. *//*from w w w . j ava2 s .co m*/ public static Class<?> getGenericClass(Class<?> cls, int actualTypeArgIndex) { try { ParameterizedType parameterizedType; if (cls.getGenericInterfaces().length > 0 && cls.getGenericInterfaces()[0] instanceof ParameterizedType) { parameterizedType = ((ParameterizedType) cls.getGenericInterfaces()[0]); } else if (cls.getGenericSuperclass() instanceof ParameterizedType) { parameterizedType = (ParameterizedType) cls.getGenericSuperclass(); } else { parameterizedType = null; } if (parameterizedType != null) { Object genericClass = parameterizedType.getActualTypeArguments()[actualTypeArgIndex]; if (genericClass instanceof ParameterizedType) { return (Class<?>) ((ParameterizedType) genericClass).getRawType(); } else if (genericClass instanceof GenericArrayType) { Class<?> componentType = (Class<?>) ((GenericArrayType) genericClass).getGenericComponentType(); if (componentType.isArray()) { return componentType; } else { return Array.newInstance(componentType, 0).getClass(); } } else if (genericClass instanceof Class) { return (Class<?>) genericClass; } } } catch (Exception e) { } if (cls.getSuperclass() != null && cls.getSuperclass() != Object.class) { return getGenericClass(cls.getSuperclass(), actualTypeArgIndex); } else { throw new IllegalArgumentException(cls.getName() + " generic type undefined!"); } }
From source file:GenericReflectionTest.java
public static void printClass(Class<?> cl) { System.out.print(cl);/* ww w. j a v a 2s . co m*/ printTypes(cl.getTypeParameters(), "<", ", ", ">", true); Type sc = cl.getGenericSuperclass(); if (sc != null) { System.out.print(" extends "); printType(sc, false); } printTypes(cl.getGenericInterfaces(), " implements ", ", ", "", false); System.out.println(); }
From source file:com.enation.eop.sdk.utils.ReflectionUtils.java
public static Class getInterfaceClassGenricType(final Class clazz, final int index) { Type[] genTypes = clazz.getGenericInterfaces(); if (genTypes == null || genTypes.length == 0) { logger.warn(clazz.getSimpleName() + "'s not impl interface "); return Object.class; }//from ww w. j av a2 s . c o m if (!(genTypes[0] instanceof ParameterizedType)) { logger.warn(clazz.getSimpleName() + "'s superclass not ParameterizedType"); return Object.class; } Type[] params = ((ParameterizedType) genTypes[0]).getActualTypeArguments(); if (index >= params.length || index < 0) { logger.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: " + params.length); return Object.class; } if (!(params[index] instanceof Class)) { logger.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter"); return Object.class; } return (Class) params[index]; }
From source file:com.jaspersoft.jasperserver.war.helper.GenericParametersHelper.java
private static ParameterizedType findParametrizedType(Class<?> classToParse, Class<?> genericClassToFind, Map<String, Class<?>> inputParameterValues) { ParameterizedType type = null; if (genericClassToFind.isInterface()) { final Type[] genericInterfaces = classToParse.getGenericInterfaces(); if (genericInterfaces != null && genericInterfaces.length > 0) { for (Type genericInterface : genericInterfaces) { if (genericInterface == genericClassToFind) { throw new IllegalArgumentException(classToParse.getName() + " is raw implementation of " + genericClassToFind.getName()); }// w w w .j a va 2 s .co m if (genericInterface instanceof ParameterizedType) { ParameterizedType currentParametrizedType = (ParameterizedType) genericInterface; Map<String, Class<?>> currentParameterValues = new HashMap<String, Class<?>>( inputParameterValues); if (currentParametrizedType.getRawType() == genericClassToFind) { type = (ParameterizedType) genericInterface; } else { currentParameterValues = getCurrentParameterValues( ((Class<?>) currentParametrizedType.getRawType()).getTypeParameters(), currentParametrizedType.getActualTypeArguments(), new HashMap<String, Class<?>>(inputParameterValues)); type = findParametrizedType((Class<?>) currentParametrizedType.getRawType(), genericClassToFind, currentParameterValues); } if (type != null) { inputParameterValues.clear(); inputParameterValues.putAll(currentParameterValues); break; } } } } } else { final Type genericSuperclass = classToParse.getGenericSuperclass(); if (genericSuperclass == genericClassToFind) { log.debug(classToParse.getName() + " is raw subclass of " + genericClassToFind.getName()); } else if (genericSuperclass instanceof ParameterizedType && ((ParameterizedType) genericSuperclass).getRawType() == genericClassToFind) { type = (ParameterizedType) genericSuperclass; } } return type; }
From source file:info.archinnov.achilles.internals.parser.CodecFactory.java
public static CodecContext buildCodecContext(AptUtils aptUtils, AnnotationMirror codecFromType) { Optional<Class<Codec>> codecClassO = getElementValueClass(codecFromType, "value", false); if (codecClassO.isPresent()) { Class<Codec> codecClass = codecClassO.get(); List<Type> genericTypes = Arrays.asList(codecClass.getGenericInterfaces()); final List<TypeName> codecTypes = genericTypes.stream().filter(x -> x instanceof ParameterizedType) .map(x -> (ParameterizedType) x) .filter(x -> x.getRawType().getTypeName() .equals(info.archinnov.achilles.type.codec.Codec.class.getCanonicalName())) .flatMap(x -> Arrays.asList(x.getActualTypeArguments()).stream()).map(TypeName::get) .collect(Collectors.toList()); aptUtils.validateTrue(codecTypes.size() == 2, "Codec class '%s' should have 2 parameters: Codec<FROM, TO>", codecClass); return new CodecContext(ClassName.get(codecClass), codecTypes.get(0), codecTypes.get(1)); } else {/*from w w w . jav a2s . co m*/ return buildCodecContext(aptUtils, getElementValueClassName(codecFromType, "value", false).toString()); } }
From source file:com.feilong.commons.core.lang.reflect.TypeUtil.java
/** * generic interfaces parameterized type. * * @param klass/* w ww. ja v a2s . c o m*/ * the klass * @param extractInterfaceClass * the extract interface class * @return the generic interfaces parameterized type * @see java.lang.Class#getGenericInterfaces() * @see java.lang.reflect.ParameterizedType#getRawType() * @since 1.1.1 */ private static ParameterizedType getGenericInterfacesParameterizedType(Class<?> klass, Class<?> extractInterfaceClass) { if (Validator.isNullOrEmpty(klass)) { throw new NullPointerException("klass can't be null/empty!"); } Type[] genericInterfaces = klass.getGenericInterfaces(); for (Type genericInterface : genericInterfaces) { if (genericInterface instanceof ParameterizedType) { ParameterizedType genericInterfacesType = (ParameterizedType) genericInterface; Type rawType = genericInterfacesType.getRawType(); if (extractInterfaceClass == rawType) { return genericInterfacesType; } } } return null; }
From source file:GenericsUtil.java
/** * Returns all of the {@link ParameterizedType}s implemented * by the given class. If none are implemented then an array * of zero length is returned./*w w w . j a v a2 s . c om*/ * @param clazz the class * @return an array of ParameterizedType */ public static ParameterizedType[] getGenericTypes(Class<?> clazz) { List<ParameterizedType> types = new ArrayList<ParameterizedType>(); // add superclass if (clazz.getGenericSuperclass() instanceof ParameterizedType) { types.add((ParameterizedType) clazz.getGenericSuperclass()); } // add interfaces for (Type type : clazz.getGenericInterfaces()) { if (type instanceof ParameterizedType) { types.add((ParameterizedType) type); } } // return list return types.toArray(new ParameterizedType[0]); }
From source file:com.feilong.core.lang.reflect.TypeUtil.java
/** * generic interfaces parameterized type. * * @param klass//from w w w.j av a 2s . c o m * the klass * @param extractInterfaceClass * the extract interface class * @return the generic interfaces parameterized type * @see java.lang.Class#getGenericInterfaces() * @see java.lang.reflect.ParameterizedType#getRawType() * @since 1.1.1 */ private static ParameterizedType getGenericInterfacesParameterizedType(Class<?> klass, Class<?> extractInterfaceClass) { Validate.notNull(klass, "klass can't be null/empty!"); Validate.notNull(extractInterfaceClass, "extractInterfaceClass can't be null/empty!"); Type[] genericInterfaces = klass.getGenericInterfaces(); for (Type genericInterface : genericInterfaces) { if (genericInterface instanceof ParameterizedType) { ParameterizedType genericInterfacesType = (ParameterizedType) genericInterface; Type rawType = genericInterfacesType.getRawType(); if (extractInterfaceClass == rawType) { return genericInterfacesType; } } } return null; }
From source file:io.github.benas.randombeans.util.ReflectionUtils.java
private static List<Type[]> getActualTypeArgumentsOfGenericInterfaces(final Class<?> type) { List<Type[]> actualTypeArguments = new ArrayList<>(); Type[] genericInterfaceTypes = type.getGenericInterfaces(); for (Type currentGenericInterfaceType : genericInterfaceTypes) { if (currentGenericInterfaceType instanceof ParameterizedType) { actualTypeArguments.add(((ParameterizedType) currentGenericInterfaceType).getActualTypeArguments()); }// ww w.ja va 2s. c o m } return actualTypeArguments; }
From source file:com.github.rvesse.airline.Accessor.java
private static Type[] getTypeParameters(Class<?> desiredType, Type type) { if (type instanceof Class) { Class<?> rawClass = (Class<?>) type; // if this is the collection class we're done if (desiredType.equals(type)) { return null; }/*from ww w. ja v a 2s.co m*/ for (Type iface : rawClass.getGenericInterfaces()) { Type[] collectionType = getTypeParameters(desiredType, iface); if (collectionType != null) { return collectionType; } } return getTypeParameters(desiredType, rawClass.getGenericSuperclass()); } if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; Type rawType = parameterizedType.getRawType(); if (desiredType.equals(rawType)) { return parameterizedType.getActualTypeArguments(); } Type[] collectionTypes = getTypeParameters(desiredType, rawType); if (collectionTypes != null) { for (int i = 0; i < collectionTypes.length; i++) { if (collectionTypes[i] instanceof TypeVariable) { TypeVariable<?> typeVariable = (TypeVariable<?>) collectionTypes[i]; TypeVariable<?>[] rawTypeParams = ((Class<?>) rawType).getTypeParameters(); for (int j = 0; j < rawTypeParams.length; j++) { if (typeVariable.getName().equals(rawTypeParams[j].getName())) { collectionTypes[i] = parameterizedType.getActualTypeArguments()[j]; } } } } } return collectionTypes; } return null; }