List of usage examples for java.lang Class getGenericInterfaces
public Type[] getGenericInterfaces()
From source file:Main.java
public static void main(String... args) { try {/* ww w. j a v a 2s . com*/ Class<?> c = Class.forName(args[0]); out.format("Class:%n %s%n%n", c.getCanonicalName()); out.format("Modifiers:%n %s%n%n", Modifier.toString(c.getModifiers())); out.format("Type Parameters:%n"); TypeVariable[] tv = c.getTypeParameters(); if (tv.length != 0) { out.format(" "); for (TypeVariable t : tv) out.format("%s ", t.getName()); out.format("%n%n"); } else { out.format(" -- No Type Parameters --%n%n"); } out.format("Implemented Interfaces:%n"); Type[] intfs = c.getGenericInterfaces(); if (intfs.length != 0) { for (Type intf : intfs) out.format(" %s%n", intf.toString()); out.format("%n"); } else { out.format(" -- No Implemented Interfaces --%n%n"); } out.format("Inheritance Path:%n"); List<Class> l = new ArrayList<Class>(); printAncestor(c, l); if (l.size() != 0) { for (Class<?> cl : l) out.format(" %s%n", cl.getCanonicalName()); out.format("%n"); } else { out.format(" -- No Super Classes --%n%n"); } out.format("Annotations:%n"); Annotation[] ann = c.getAnnotations(); if (ann.length != 0) { for (Annotation a : ann) out.format(" %s%n", a.toString()); out.format("%n"); } else { out.format(" -- No Annotations --%n%n"); } // production code should handle this exception more gracefully } catch (ClassNotFoundException x) { x.printStackTrace(); } }
From source file:Main.java
@SuppressWarnings("rawtypes") public static Class[] getGenericInterfaces(Class<?> clazz) { try {//from w w w. j a va2 s.c o m Type typeGeneric = clazz.getGenericInterfaces()[0]; if (typeGeneric != null) { if (typeGeneric instanceof ParameterizedType) { return getGeneric((ParameterizedType) typeGeneric); } } } catch (Exception e) { } return null; }
From source file:ClassUtils.java
static public Type[] getGenericType(Class<?> target) { if (target == null) return new Type[0]; Type[] types = target.getGenericInterfaces(); if (types.length > 0) { return types; }/* w ww .j a v a2 s . c o m*/ Type type = target.getGenericSuperclass(); if (type != null) { if (type instanceof ParameterizedType) { return new Type[] { type }; } } return new Type[0]; }
From source file:com.sf.ddao.crud.param.CRUDParameterService.java
public static Class<?> getCRUDDaoBean(Context ctx, int idx) { final MethodCallCtx callCtx = CtxHelper.get(ctx, MethodCallCtx.class); final Method method = callCtx.getMethod(); if (idx != USE_GENERICS) { Type beanClass;/*from w ww . ja v a 2 s . c o m*/ if (idx == DefaultParameter.RETURN_ARG_IDX) { beanClass = method.getGenericReturnType(); } else { beanClass = method.getGenericParameterTypes()[idx]; } if (beanClass instanceof Class) { return (Class) beanClass; } } Class<?> iFace = callCtx.getSubjClass(); for (Type type : iFace.getGenericInterfaces()) { final ParameterizedType parameterizedType = (ParameterizedType) type; if (parameterizedType.getRawType().equals(method.getDeclaringClass())) { final Type[] typeArguments = parameterizedType.getActualTypeArguments(); return (Class<?>) typeArguments[GENERICS_ARG_NUM]; } } throw new RuntimeException(iFace + " expected to extend " + CRUDDao.class); }
From source file:GenericUtils.java
/** * Get the Generic definitions from a class for given class without looking * super classes.//ww w. j a v a 2 s. c o m * @param classFrom Implementing class * @param interfaceClz class with generic definition * @return null if not found */ @SuppressWarnings("unchecked") public static Type[] getGenericDefinitonsThis(Class classFrom, Class interfaceClz) { Type[] genericInterfaces = classFrom.getGenericInterfaces(); for (Type type : genericInterfaces) { if (type instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) type; if (interfaceClz.isAssignableFrom((Class) pt.getRawType())) { return pt.getActualTypeArguments(); } } } // check if it if available on generic super class Type genericSuperclass = classFrom.getGenericSuperclass(); if (genericSuperclass instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) genericSuperclass; if (interfaceClz.equals(pt.getRawType())) { return pt.getActualTypeArguments(); } } return null; }
From source file:org.openmrs.module.webservices.rest.util.ReflectionUtil.java
/** * If clazz implements genericInterface<T, U, ...>, this method returns the parameterized type * with the given index from that interface. This method will recursively look at superclasses * until it finds one implementing the requested interface * /*www . j av a 2s.com*/ * @should find genericInterface on a superclass if clazz does not directly implement it * @should ignore type variables on the declaring interface * @should not inspect superclasses of the specified genericInterface */ @SuppressWarnings("rawtypes") public static Class getParameterizedTypeFromInterface(Class<?> clazz, Class<?> genericInterface, int index) { for (Type t : clazz.getGenericInterfaces()) { if (t instanceof ParameterizedType && ((Class) ((ParameterizedType) t).getRawType()).equals(genericInterface)) { //if we have reached the base interface that declares the type variable T, ignore it Type pType = ((ParameterizedType) t).getActualTypeArguments()[index]; if (!(pType instanceof TypeVariable)) { return (Class) pType; } } } if (clazz.getSuperclass() != null && genericInterface.isAssignableFrom(clazz.getSuperclass())) { return getParameterizedTypeFromInterface(clazz.getSuperclass(), genericInterface, index); } return null; }
From source file:com.google.code.guice.repository.spi.TypeUtil.java
public static void getGenericInterfacesActualTypes(Collection<Type> types, Class aClass) { if (aClass != null && types != null) { Type[] interfaces = aClass.getGenericInterfaces(); for (Type anInterface : interfaces) { if (anInterface instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) anInterface; Type[] actualTypes = parameterizedType.getActualTypeArguments(); types.addAll(Arrays.asList(actualTypes)); } else if (anInterface instanceof Class) { Class typeClass = (Class) anInterface; getGenericInterfacesActualTypes(types, typeClass); }//from ww w.j a va 2 s. c o m } } }
From source file:Main.java
public static Type[] getGenericType(Class<?> clazz, Class<?> interfaceClazz) { Type st = clazz.getGenericSuperclass(); Type[] ret = getActualTypeArguments(interfaceClazz, st); if (ret != null) return ret; for (Type t : clazz.getGenericInterfaces()) { ret = getActualTypeArguments(interfaceClazz, t); if (ret != null) return ret; }/*from ww w . j a v a 2 s .c om*/ Class<?> s = clazz.getSuperclass(); if (s == null || clazz.equals(s.getClass())) return new Type[0]; return getGenericType(s, interfaceClazz); }
From source file:JDBCPool.dbcp.demo.sourcecode.PoolImplUtils.java
/** * Obtain the concrete type used by an implementation of an interface that * uses a generic type./*from w ww .j av a 2 s. c o m*/ * * @param type The interface that defines a generic type * @param clazz The class that implements the interface with a concrete type * @param <T> The interface type * * @return concrete type used by the implementation */ private static <T> Object getGenericType(Class<T> type, Class<? extends T> clazz) { // Look to see if this class implements the generic interface // Get all the interfaces Type[] interfaces = clazz.getGenericInterfaces(); for (Type iface : interfaces) { // Only need to check interfaces that use generics if (iface instanceof ParameterizedType) { ParameterizedType pi = (ParameterizedType) iface; // Look for the generic interface if (pi.getRawType() instanceof Class) { if (type.isAssignableFrom((Class<?>) pi.getRawType())) { return getTypeParameter(clazz, pi.getActualTypeArguments()[0]); } } } } // Interface not found on this class. Look at the superclass. @SuppressWarnings("unchecked") Class<? extends T> superClazz = (Class<? extends T>) clazz.getSuperclass(); Object result = getGenericType(type, superClazz); if (result instanceof Class<?>) { // Superclass implements interface and defines explicit type for // generic return result; } else if (result instanceof Integer) { // Superclass implements interface and defines unknown type for // generic // Map that unknown type to the generic types defined in this class ParameterizedType superClassType = (ParameterizedType) clazz.getGenericSuperclass(); return getTypeParameter(clazz, superClassType.getActualTypeArguments()[((Integer) result).intValue()]); } else { // Error will be logged further up the call stack return null; } }
From source file:com.jeroensteenbeeke.hyperion.events.DefaultEventDispatcher.java
@SuppressWarnings("unchecked") static Class<? extends Event<?>> getEventClass(Class<?> handlerClass) { for (Class<?> i : handlerClass.getInterfaces()) { if (EventHandler.class.equals(i)) { for (Type t : handlerClass.getGenericInterfaces()) { if (t instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) t; if (EventHandler.class.equals(pt.getRawType())) { return (Class<? extends Event<?>>) pt.getActualTypeArguments()[0]; }/*from ww w . j a v a 2s .com*/ } } } else if (EventHandler.class.isAssignableFrom(i)) { return getEventClass((Class<? extends EventHandler<?>>) i); } } if (EventHandler.class.isAssignableFrom(handlerClass.getSuperclass())) { return getEventClass((Class<?>) handlerClass.getSuperclass()); } return null; }