@SuppressWarnings("unchecked") protected Class<? extends BeanView<T>> findViewFor(Type type, List<Class<? extends BeanView<?>>> views) { for (Class<? extends BeanView<?>> v : views) { Type searchedType = TypeUtil.lookupGenericType(BeanView.class, v); searchedType = TypeUtil.expandType(searchedType, v); searchedType = TypeUtil.typeOf(0, searchedType); if (TypeUtil.match(type, searchedType, false)) { return (Class<? extends BeanView<T>>) v; } } return null; }
public PropertyMutator createMutator(String name, Method method, Type ofType, Genson genson) { // the target bean must be second parameter for beanview mutators BeanView<?> beanview = views.get(getRawClass(ofType)); if (beanview != null) { Type superTypeWithParameter = TypeUtil.lookupGenericType(BeanView.class, beanview.getClass()); Class<?> tClass = getRawClass(typeOf(0, expandType(superTypeWithParameter, beanview.getClass()))); Type type = expandType(method.getGenericParameterTypes()[0], ofType); return new BeanViewPropertyMutator(name, method, type, beanview, tClass); } else return null; }
public final static Class<?> getRawClass(Type type) { if (type instanceof Class<?>) return (Class<?>) type; else if (type instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) type; return (Class<?>) pType.getRawType(); } else if (type instanceof GenericArrayType) { Type componentType = ((GenericArrayType) type).getGenericComponentType(); return Array.newInstance(getRawClass(componentType), 0).getClass(); } else return getRawClass(expand(type, null)); }
/** * Searches for the typevariable definition in the inClass hierarchy. * * @param type * @param inClass * @return the resolved type or type if unable to resolve it. */ public final static Type resolveTypeVariable(TypeVariable<? extends GenericDeclaration> type, Class<?> inClass) { return resolveTypeVariable(type, genericDeclarationToClass(type.getGenericDeclaration()), inClass); }
private void addDefaultSerializers(List<? extends Serializer<?>> serializers) { if (serializers != null) { for (Serializer<?> serializer : serializers) { Type typeOfConverter = TypeUtil.typeOf(0, TypeUtil.lookupGenericType(Serializer.class, serializer.getClass())); typeOfConverter = TypeUtil.expandType(typeOfConverter, serializer.getClass()); if (!serializersMap.containsKey(typeOfConverter)) serializersMap.put(typeOfConverter, serializer); } } }
public PropertyMutator createMutator(String name, Field field, Type ofType, Genson genson) { Class<?> ofClass = getRawClass(ofType); Type expandedType = TypeUtil.expandType(field.getGenericType(), ofType); return new PropertyMutator.FieldMutator(name, field, expandedType, ofClass); }
@SuppressWarnings("unchecked") private GenericType(Type type) { this.type = type; this.rawClass = (Class<T>) TypeUtil.getRawClass(type); }
} else { try { circularTypes.put(type, getRawClass(type)); TypeAndRootClassKey key = new TypeAndRootClassKey(type, rootType); Type expandedType = _cache.get(key); Type[] expandedArgs = new Type[len]; for (int i = 0; i < len; i++) { expandedArgs[i] = expandType(args[i], rootType); expandedType = new ExpandedParameterizedType(pType, getRawClass(rootType), expandedArgs); } else if (type instanceof TypeVariable) { @SuppressWarnings("unchecked") String typeName = tvType.getName(); int idx = 0; for (TypeVariable<?> parameter : genericDeclarationToClass(tvType.getGenericDeclaration()) .getTypeParameters()) { if (typeName.equals(parameter.getName())) { expandedType = resolveTypeVariable(tvType, getRawClass(rootType)); expandedType = expandType(tvType.getBounds()[0], rootType); } else if (type instanceof GenericArrayType) { GenericArrayType genArrType = (GenericArrayType) type; Type cType = expandType(genArrType.getGenericComponentType(), rootType); if (genArrType.getGenericComponentType() == cType) cType = Object.class; expandedType = new ExpandedGenericArrayType(genArrType, cType, getRawClass(rootType));
@SuppressWarnings({"rawtypes", "unchecked"}) public Converter<Object> create(Type forType, Genson genson) { if (forType instanceof GenericArrayType || (forType instanceof Class<?> && ((Class<?>) forType).isArray())) { if (byte.class.equals(getCollectionType(forType))) { return (Converter) ByteArrayConverter.instance; } else { Converter<?> elementConverter = genson.provideConverter(TypeUtil .getCollectionType(forType)); return new ArrayConverter(TypeUtil.getRawClass(TypeUtil .getCollectionType(forType)), elementConverter); } } return null; } }
/** * Deep comparison between type and oType. If parameter strictMatch is true, then type and oType will be strictly * compared otherwise this method checks whether oType is assignable from type. */ public final static boolean match(Type type, Type oType, boolean strictMatch) { if (type == null || oType == null) return type == null && oType == null; Class<?> clazz = getRawClass(type); Class<?> oClazz = getRawClass(oType); boolean match = strictMatch ? oClazz.equals(clazz) : oClazz.isAssignableFrom(clazz); if (Object.class.equals(oClazz) && !strictMatch) return match; if (clazz.isArray() && !oClazz.isArray()) return match; Type[] types = getTypes(type); Type[] oTypes = getTypes(oType); match = match && (types.length == oTypes.length || types.length == 0); for (int i = 0; i < types.length && match; i++) match = match(types[i], oTypes[i], strictMatch); return match; }
/** * Returns the type of this Collection or Array. * * @throws IllegalArgumentException if type is not a Collection, not a generic array and not a primitive array. */ public final static Type getCollectionType(Type type) { if (type instanceof GenericArrayType) { return ((GenericArrayType) type).getGenericComponentType(); } else if (type instanceof Class<?>) { Class<?> clazz = (Class<?>) type; if (clazz.isArray()) return clazz.getComponentType(); else if (Collection.class.isAssignableFrom(clazz)) { return Object.class; } } else if (type instanceof ParameterizedType && Collection.class.isAssignableFrom(getRawClass(type))) { return typeOf(0, type); } throw new IllegalArgumentException( "Could not extract parametrized type, are you sure it is a Collection or an Array?"); }
/** * Resolves all public methods starting with get/is (boolean) and parameter less as * accessors. */ public Trilean isAccessor(Method method, Class<?> fromClass) { if (!method.isBridge()) { String name = method.getName(); int len = name.length(); if (methodVisibilityFilter.isVisible(method) && ((len > 3 && name.startsWith("get")) || (len > 2 && name.startsWith("is") && (TypeUtil .match(TypeUtil.expandType(method.getGenericReturnType(), fromClass), Boolean.class, false) || TypeUtil.match( method.getGenericReturnType(), boolean.class, false)))) && method.getParameterTypes().length == 0) return TRUE; } return FALSE; }
final static Type expand(Type type, Class<?> inClass) { Type expandedType = null; if (type instanceof TypeVariable) { @SuppressWarnings("unchecked") // for the moment we assume it is a class, we can later handle ctr and methods TypeVariable<GenericDeclaration> tvType = (TypeVariable<GenericDeclaration>) type; if (inClass == null) inClass = genericDeclarationToClass(tvType.getGenericDeclaration()); expandedType = resolveTypeVariable(tvType, inClass); if (type.equals(expandedType)) expandedType = tvType.getBounds()[0]; } else if (type instanceof WildcardType) { WildcardType wType = (WildcardType) type; expandedType = wType.getUpperBounds().length > 0 ? expand(wType.getUpperBounds()[0], inClass) : Object.class; } else return type; return expandedType == null || type.equals(expandedType) ? Object.class : expandedType; }
public Type[] expandTypes(Type[] typesToExpand, Type inContext) { Type[] expandedTypes = new Type[typesToExpand.length]; for (int i = 0; i < typesToExpand.length; i++) { expandedTypes[i] = TypeUtil.expandType(typesToExpand[i], inContext); } return expandedTypes; } }
@Override public Converter<Optional<Object>> create(Type type, Genson genson) { Type typeOfValue = TypeUtil.typeOf(0, type); return new OptionalConverter<Object>(genson.provideConverter(typeOfValue)); } }
return inClass.getGenericInterfaces()[i]; } else { Type superType = lookupGenericType(ofClass, interfaces[i]); if (superType != null) return superType; if (ofClass.equals(superClass)) return inClass.getGenericSuperclass(); return lookupGenericType(ofClass, inClass.getSuperclass());
public Converter<Object> create(Type type, Genson genson) { if (TypeUtil.match(type, Object.class, true)) { return UntypedConverter.instance; } return null; } }
for (int i = 0; i < interfaces.length && resolvedType == null; i++) { superClass = interfaces[i]; resolvedType = resolveTypeVariable(type, declaringClass, superClass); genericSuperClass = inClass.getGenericInterfaces()[i]; resolvedType = resolveTypeVariable(type, declaringClass, superClass); genericSuperClass = inClass.getGenericSuperclass();
private void addDefaultDeserializers(List<? extends Deserializer<?>> deserializers) { if (deserializers != null) { for (Deserializer<?> deserializer : deserializers) { Type typeOfConverter = TypeUtil.typeOf(0, TypeUtil.lookupGenericType(Deserializer.class, deserializer.getClass())); typeOfConverter = TypeUtil.expandType(typeOfConverter, deserializer.getClass()); if (!deserializersMap.containsKey(typeOfConverter)) deserializersMap.put(typeOfConverter, deserializer); } } }
public PropertyMutator createMutator(String name, Field field, Type ofType, Genson genson) { Class<?> ofClass = getRawClass(ofType); Type expandedType = TypeUtil.expandType(field.getGenericType(), ofType); return new PropertyMutator.FieldMutator(name, field, expandedType, ofClass); }