List of usage examples for java.lang.reflect ParameterizedType getActualTypeArguments
Type[] getActualTypeArguments();
From source file:com.laxser.blitz.web.paramresolver.ResolverFactoryImpl.java
private static Class<?>[] compileGenericParameterTypesDetail(Method method, int index) { Type genericParameterType = method.getGenericParameterTypes()[index]; ArrayList<Class<?>> typeDetailList = new ArrayList<Class<?>>(); if (genericParameterType instanceof ParameterizedType) { ParameterizedType aType = (ParameterizedType) genericParameterType; Type[] parameterArgTypes = aType.getActualTypeArguments(); for (Type parameterArgType : parameterArgTypes) { if (parameterArgType instanceof Class) { typeDetailList.add((Class<?>) parameterArgType); } else { typeDetailList.add(String.class); }/*from ww w . j a v a2 s. c o m*/ } Class<?>[] types = new Class[typeDetailList.size()]; typeDetailList.toArray(types); return types; } return null; }
From source file:com.bstek.dorado.data.method.MethodAutoMatchingUtils.java
private static Class<?> getElementType(Type type) { if (type instanceof ParameterizedType) { ParameterizedType paramedType = (ParameterizedType) type; if (Collection.class.isAssignableFrom(toClass(paramedType.getRawType()))) { return toClass(paramedType.getActualTypeArguments()[0]); }/*from www. j a v a 2s.c om*/ } else if (type instanceof ParameterizedCollectionType) { return ((ParameterizedCollectionType) type).getElementType(); } return null; }
From source file:ca.oson.json.util.ObjectUtil.java
public static <E> Class getTypeComponentClass(java.lang.reflect.Type type) { Class cl = type.getClass();// w ww .j ava2 s . c o m if (ComponentType.class.isAssignableFrom(cl)) { ComponentType componentType = (ComponentType) type; return componentType.getMainComponentType(); } //java.util.List<ca.oson.json.test.Dataset> String className = type.getTypeName(); Class ctype = getComponentType(className); if (ctype != null) { return ctype; } // Collection<String>, return String.class if (ParameterizedType.class.isAssignableFrom(cl)) { java.lang.reflect.ParameterizedType pt = (java.lang.reflect.ParameterizedType) type; if (pt.getActualTypeArguments().length > 0) { //return pt.getActualTypeArguments()[0].getClass(); className = pt.getActualTypeArguments()[0].getTypeName(); try { return Class.forName(className); } catch (ClassNotFoundException e) { // e.printStackTrace(); } } // GenericArrayType represents an array type whose component // type is either a parameterized type or a type variable. } else if (java.lang.reflect.GenericArrayType.class.isAssignableFrom(cl)) { java.lang.reflect.GenericArrayType pt = (java.lang.reflect.GenericArrayType) type; return getTypeClass(pt.getGenericComponentType()); } else if (java.lang.reflect.TypeVariable.class.isAssignableFrom(cl)) { java.lang.reflect.TypeVariable pt = (java.lang.reflect.TypeVariable) type; java.lang.reflect.Type[] types = pt.getBounds(); if (types != null && types.length > 0) { return getTypeClass(types[0]); } } return null; }
From source file:org.mule.cloudconnect.ibeans.IBeanFactoryBean.java
@SuppressWarnings("unchecked") public Class<T> getObjectType() { Type type = getClass().getGenericSuperclass(); ParameterizedType paramType = (ParameterizedType) type; return (Class<T>) paramType.getActualTypeArguments()[0]; }
From source file:org.everrest.core.impl.provider.multipart.ListMultipartFormDataMessageBodyReader.java
@Override public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { if (type == List.class && genericType instanceof ParameterizedType) { ParameterizedType t = (ParameterizedType) genericType; Type[] ta = t.getActualTypeArguments(); return ta.length == 1 && ta[0] == InputItem.class; }/*from w w w. j a va 2 s . com*/ return false; }
From source file:org.everrest.core.impl.provider.multipart.MapMultipartFormDataMessageBodyReader.java
@Override public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { if (type == Map.class && genericType instanceof ParameterizedType) { ParameterizedType t = (ParameterizedType) genericType; Type[] ta = t.getActualTypeArguments(); return ta.length == 2 && ta[0] == String.class && ta[1] == InputItem.class; }/*ww w. j a va 2 s. c o m*/ return false; }
From source file:com.crm.earnify.daos.GenericEarnifyDaoImpl.java
/** * By defining this class as abstract, we prevent Spring from creating * instance of this class If not defined as abstract, * getClass().getGenericSuperClass() would return Object. There would be * exception because Object class does not have constructor with parameters. */// w ww. j a v a 2s . c o m public GenericEarnifyDaoImpl() { Type t = getClass().getGenericSuperclass(); ParameterizedType pt = (ParameterizedType) t; daoType = (Class) pt.getActualTypeArguments()[0]; }
From source file:com.plan.proyecto.repositorios.DaoGenericoImpl.java
@SuppressWarnings("unchecked") public DaoGenericoImpl() { // Type t = getClass().getGenericSuperclass(); // ParameterizedType pt = (ParameterizedType) t; // persistentClass = (Class) pt.getActualTypeArguments()[0]; Type type = this.getClass().getGenericSuperclass(); if (type instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) type; Type[] fieldArgTypes = pt.getActualTypeArguments(); persistentClass = (Class<T>) fieldArgTypes[0]; }/*from w ww. j a v a 2 s. co m*/ }
From source file:org.openflexo.antar.binding.TypeUtils.java
/** * Determines if the class or interface represented by supplied <code>aType</code> object is either the same as, or is a superclass or * superinterface of, the class or interface represented by the specified <code>anOtherType</code> parameter. It returns * <code>true</code> if so; otherwise false<br> * This method also tried to resolve generics before to perform the assignability test * /*w w w . j a va 2 s . c o m*/ * @param aType * @param anOtherType * @param permissive * is a flag indicating if basic conversion between primitive types is allowed: for example, an int may be assign to a float * value after required conversion. * @return */ public static boolean isTypeAssignableFrom(Type aType, Type anOtherType, boolean permissive) { // Test if anOtherType instanceof aType /*if (aType instanceof CustomType || anOtherType instanceof CustomType) { logger.info("Called " + aType + " isAssignableFrom(" + anOtherType + ")"); logger.info("En gros je me demande si " + anOtherType + " est bien une instance de " + aType + " anOtherType est un " + anOtherType.getClass().getSimpleName()); }*/ // If supplied type is null return false if (aType == null || anOtherType == null) { return false; } // Everything could be assigned to Object if (isObject(aType)) { return true; } // Special case for Custom types if (aType instanceof CustomType) { return ((CustomType) aType).isTypeAssignableFrom(anOtherType, permissive); } if (anOtherType instanceof CustomType && isTypeAssignableFrom(aType, ((CustomType) anOtherType).getBaseClass())) { return true; } if (isVoid(aType)) { return isVoid(anOtherType); } if (isBoolean(aType)) { return isBoolean(anOtherType); } if (isDouble(aType)) { return isDouble(anOtherType) || isFloat(anOtherType) || isLong(anOtherType) || isInteger(anOtherType) || isShort(anOtherType) || isChar(anOtherType) || isByte(anOtherType); } if (isFloat(aType)) { return isDouble(anOtherType) && permissive || isFloat(anOtherType) || isLong(anOtherType) || isInteger(anOtherType) || isShort(anOtherType) || isChar(anOtherType) || isByte(anOtherType); } if (isLong(aType)) { return isLong(anOtherType) || isInteger(anOtherType) || isShort(anOtherType) || isChar(anOtherType) || isByte(anOtherType); } if (isInteger(aType)) { return isLong(anOtherType) && permissive || isInteger(anOtherType) || isShort(anOtherType) || isChar(anOtherType) || isByte(anOtherType); } if (isShort(aType)) { return isLong(anOtherType) && permissive || isInteger(anOtherType) && permissive || isShort(anOtherType) || isChar(anOtherType) || isByte(anOtherType); } if (isChar(aType)) { return isLong(anOtherType) && permissive || isInteger(anOtherType) && permissive || isShort(anOtherType) && permissive || isChar(anOtherType) || isByte(anOtherType); } if (isByte(aType)) { return isLong(anOtherType) && permissive || isInteger(anOtherType) && permissive || isShort(anOtherType) && permissive || isChar(anOtherType) || isByte(anOtherType); } if (aType instanceof WildcardType) { if (anOtherType instanceof WildcardType) { // If two wildcards, perform check on both upper bounds return isTypeAssignableFrom(((WildcardType) aType).getUpperBounds()[0], ((WildcardType) anOtherType).getUpperBounds()[0], permissive); } // Perform check on first upper bound only return isTypeAssignableFrom(((WildcardType) aType).getUpperBounds()[0], anOtherType, permissive); } if (aType instanceof GenericArrayType) { // logger.info("Called "+aType+" isAssignableFrom("+anOtherType+")"); // logger.info("anOtherType is a "+anOtherType.getClass()); if (anOtherType instanceof GenericArrayType) { return isTypeAssignableFrom(((GenericArrayType) aType).getGenericComponentType(), ((GenericArrayType) anOtherType).getGenericComponentType(), permissive); } else if (anOtherType instanceof Class && ((Class) anOtherType).isArray()) { return isTypeAssignableFrom(((GenericArrayType) aType).getGenericComponentType(), ((Class) anOtherType).getComponentType(), permissive); } return false; } // Look if we are on same class if (aType instanceof Class && anOtherType instanceof Class) { return isClassAncestorOf((Class) aType, (Class) anOtherType); } if (!isClassAncestorOf(getBaseClass(aType), getBaseClass(anOtherType))) { return false; } // logger.info(""+getBaseClass(aType)+" is ancestor of "+getBaseClass(anOtherType)); if (aType instanceof Class || anOtherType instanceof Class) { // One of two types is not parameterized, we cannot check, return true return true; } if (aType instanceof ParameterizedType && anOtherType instanceof ParameterizedType) { ParameterizedType t1 = (ParameterizedType) aType; ParameterizedType t2 = (ParameterizedType) anOtherType; // Now check that parameters size are the same if (t1.getActualTypeArguments().length != t2.getActualTypeArguments().length) { return false; } // Now, we have to compare parameter per parameter for (int i = 0; i < t1.getActualTypeArguments().length; i++) { Type st1 = t1.getActualTypeArguments()[i]; Type st2 = t2.getActualTypeArguments()[i]; if (!isTypeAssignableFrom(st1, st2, true)) { return false; } } return true; } return org.apache.commons.lang3.reflect.TypeUtils.isAssignable(anOtherType, aType); /*if (getBaseEntity() == type.getBaseEntity()) { // Base entities are the same, let's analyse parameters // If one of both paramters def is empty (parameters are not defined, as before java5) // accept it without performing a test which is impossible to perform if ((getParameters().size() == 0) || (type.getParameters().size() == 0)) return true; // Now check that parameters size are the same if (getParameters().size() != type.getParameters().size()) return false; // Now, we have to compare parameter per parameter for (int i=0; i<getParameters().size(); i++) { DMType localParam = getParameters().elementAt(i); DMType sourceParam = type.getParameters().elementAt(i); if (localParam.getKindOfType() == KindOfType.WILDCARD && localParam.getUpperBounds().size()==1) { DMType resultingSourceParamType; if (sourceParam.getKindOfType() == KindOfType.WILDCARD && sourceParam.getUpperBounds().size()==1) { resultingSourceParamType = sourceParam.getUpperBounds().firstElement().bound; } else { resultingSourceParamType = sourceParam; } if (!localParam.getUpperBounds().firstElement().bound.isAssignableFrom(resultingSourceParamType,permissive)) { return false; } } else if (!localParam.equals(sourceParam)) { return false; } } return true; } // Else it's a true ancestor else { //DMType parentType = makeInstantiatedDMType(type.getBaseEntity().getParentType(),type); DMType parentType = makeInstantiatedDMType(getBaseEntity().getClosestAncestorOf(type.getBaseEntity()),type); return isAssignableFrom(parentType,permissive); }*/ // return false; }
From source file:io.swagger.jaxrs.Reader.java
private static Class<?> getClassArgument(Type cls) { if (cls instanceof ParameterizedType) { final ParameterizedType parameterized = (ParameterizedType) cls; final Type[] args = parameterized.getActualTypeArguments(); if (args.length != 1) { LOGGER.error(String.format("Unexpected class definition: %s", cls)); return null; }//from w w w . j a v a 2 s . c o m final Type first = args[0]; if (first instanceof Class) { return (Class<?>) first; } else { return null; } } else { LOGGER.error(String.format("Unknown class definition: %s", cls)); return null; } }