public Converter<Object> create(Type type, Genson genson) { if (TypeUtil.match(type, Object.class, true)) { return UntypedConverter.instance; } return null; } }
public Converter<Object> create(Type type, Genson genson) { if (TypeUtil.match(type, Object.class, true)) { return UntypedConverter.instance; } return null; } }
public Trilean isAccessor(Method method, Class<?> baseClass) { Type expectedType = TypeUtil.lookupGenericType(BeanView.class, baseClass); expectedType = TypeUtil.expandType(expectedType, baseClass); expectedType = TypeUtil.typeOf(0, expectedType); int modifiers = method.getModifiers(); return Trilean.valueOf((method.getName().startsWith("get") || (method.getName() .startsWith("is") && (TypeUtil.match(method.getGenericReturnType(), Boolean.class, false) || boolean.class.equals(method.getReturnType())))) && TypeUtil.match(expectedType, method.getGenericParameterTypes()[0], false) && Modifier.isPublic(modifiers) && !Modifier.isAbstract(modifiers) && !Modifier.isNative(modifiers)); }
public Trilean isAccessor(Method method, Class<?> baseClass) { Type expectedType = TypeUtil.lookupGenericType(BeanView.class, baseClass); expectedType = TypeUtil.expandType(expectedType, baseClass); expectedType = TypeUtil.typeOf(0, expectedType); int modifiers = method.getModifiers(); return Trilean.valueOf((method.getName().startsWith("get") || (method.getName() .startsWith("is") && (TypeUtil.match(method.getGenericReturnType(), Boolean.class, false) || boolean.class.equals(method.getReturnType())))) && TypeUtil.match(expectedType, method.getGenericParameterTypes()[0], false) && Modifier.isPublic(modifiers) && !Modifier.isAbstract(modifiers) && !Modifier.isNative(modifiers)); }
/** * 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; }
/** * 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; }
public Trilean isMutator(Method method, Class<?> baseClass) { Type expectedType = TypeUtil.lookupGenericType(BeanView.class, baseClass); expectedType = TypeUtil.expandType(expectedType, baseClass); expectedType = TypeUtil.typeOf(0, expectedType); int modifiers = method.getModifiers(); return Trilean.valueOf(method.getName().startsWith("set") && void.class.equals(method.getReturnType()) && method.getGenericParameterTypes().length == 2 && TypeUtil.match(expectedType, method.getGenericParameterTypes()[1], false) && Modifier.isPublic(modifiers) && !Modifier.isAbstract(modifiers) && !Modifier.isNative(modifiers)); }
public Trilean isMutator(Method method, Class<?> baseClass) { Type expectedType = TypeUtil.lookupGenericType(BeanView.class, baseClass); expectedType = TypeUtil.expandType(expectedType, baseClass); expectedType = TypeUtil.typeOf(0, expectedType); int modifiers = method.getModifiers(); return Trilean.valueOf(method.getName().startsWith("set") && void.class.equals(method.getReturnType()) && method.getGenericParameterTypes().length == 2 && TypeUtil.match(expectedType, method.getGenericParameterTypes()[1], false) && Modifier.isPublic(modifiers) && !Modifier.isAbstract(modifiers) && !Modifier.isNative(modifiers)); }
/** * @return true if this object or its wrapped object (if the object extends Wrapper) is of type clazz. */ public static boolean isOfType(Object object, Class<?> clazz) { return TypeUtil.match(object.getClass(), clazz, false) || (isWrapped(object) && Wrapper.isOfType(((Wrapper<?>) object).unwrap(), clazz)); } }
/** * @return true if this object or its wrapped object (if the object extends Wrapper) is of type clazz. */ public static boolean isOfType(Object object, Class<?> clazz) { return TypeUtil.match(object.getClass(), clazz, false) || (isWrapped(object) && Wrapper.isOfType(((Wrapper<?>) object).unwrap(), clazz)); } }
/** * 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; }
/** * 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); // This means we did expand the best we could and end up with Object, in which case match will be true if oType // is Object too and false otherwise if (Object.class.equals(clazz)) return match; 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; }
Converter<?> provide(BeanProperty property, Genson genson) { Type type = property.getType(); for (Iterator<? extends ContextualFactory<?>> it = contextualFactories.iterator(); it .hasNext(); ) { ContextualFactory<?> factory = it.next(); Converter<?> object = null; Type factoryType = lookupGenericType(ContextualFactory.class, factory.getClass()); factoryType = expandType(factoryType, factory.getClass()); Type factoryParameter = typeOf(0, factoryType); if (type instanceof Class<?> && ((Class<?>) type).isPrimitive()) type = wrap((Class<?>) type); if (match(type, factoryParameter, false) && (object = factory.create(property, genson)) != null) { return object; } } return null; } }
@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; }
Converter<?> provide(BeanProperty property, Genson genson) { Type type = property.getType(); for (Iterator<? extends ContextualFactory<?>> it = contextualFactories.iterator(); it .hasNext(); ) { ContextualFactory<?> factory = it.next(); Converter<?> object = null; Type factoryType = lookupGenericType(ContextualFactory.class, factory.getClass()); factoryType = expandType(factoryType, factory.getClass()); Type factoryParameter = typeOf(0, factoryType); if (type instanceof Class<?> && ((Class<?>) type).isPrimitive()) type = wrap((Class<?>) type); if (match(type, factoryParameter, false) && (object = factory.create(property, genson)) != null) { return object; } } return null; } }
@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; }
@SuppressWarnings("unchecked") protected <T> T provide(Class<T> forClass, Type withParameterType, Map<Type, ? extends T> fromTypeMap, Genson genson) { if (fromTypeMap.containsKey(withParameterType)) return fromTypeMap.get(withParameterType); Type wrappedParameterType = withParameterType; if (withParameterType instanceof Class<?> && ((Class<?>) withParameterType).isPrimitive()) wrappedParameterType = TypeUtil.wrap((Class<?>) withParameterType); for (Iterator<Factory<?>> it = factories.iterator(); it.hasNext(); ) { Factory<?> factory = it.next(); Object object; Type factoryType = TypeUtil.lookupGenericType(Factory.class, factory.getClass()); factoryType = TypeUtil.expandType(factoryType, factory.getClass()); // it is a parameterized type and we want the parameter corresponding to Serializer from // Factory<Serializer<?>> factoryType = TypeUtil.typeOf(0, factoryType); Type factoryParameter = TypeUtil.typeOf(0, factoryType); if (forClass.isAssignableFrom(TypeUtil.getRawClass(factoryType)) && TypeUtil.match(wrappedParameterType, factoryParameter, false) && (object = factory.create(withParameterType, genson)) != null) { return forClass.cast(object); } } return (T) beanDescriptorProvider.provide(TypeUtil.getRawClass(withParameterType), withParameterType, genson); }
@SuppressWarnings("unchecked") protected <T> T provide(Class<T> forClass, Type withParameterType, Map<Type, ? extends T> fromTypeMap, Genson genson) { if (fromTypeMap.containsKey(withParameterType)) return fromTypeMap.get(withParameterType); Type wrappedParameterType = withParameterType; if (withParameterType instanceof Class<?> && ((Class<?>) withParameterType).isPrimitive()) wrappedParameterType = TypeUtil.wrap((Class<?>) withParameterType); for (Iterator<Factory<?>> it = factories.iterator(); it.hasNext(); ) { Factory<?> factory = it.next(); Object object; Type factoryType = TypeUtil.lookupGenericType(Factory.class, factory.getClass()); factoryType = TypeUtil.expandType(factoryType, factory.getClass()); // it is a parameterized type and we want the parameter corresponding to Serializer from // Factory<Serializer<?>> factoryType = TypeUtil.typeOf(0, factoryType); Type factoryParameter = TypeUtil.typeOf(0, factoryType); if (forClass.isAssignableFrom(TypeUtil.getRawClass(factoryType)) && TypeUtil.match(wrappedParameterType, factoryParameter, false) && (object = factory.create(withParameterType, genson)) != null) { return forClass.cast(object); } } return (T) beanDescriptorProvider.provide(TypeUtil.getRawClass(withParameterType), withParameterType, genson); }
if (!match(propertyClass, converterPropertyType, false)) throw new ClassCastException("The type defined in " + ann.value().getName() + " is not assignale from property " + property.getName()
if (!match(propertyClass, converterPropertyType, false)) throw new ClassCastException("The type defined in " + ann.value().getName() + " is not assignale from property " + property.getName()