public static OpenType of(TypeInfo ti) { if(ti.getType() == Void.class || ti.getType() == Void.TYPE) { return SimpleType.VOID; } if(ti.isSimpleType()) { return forSimpleType(ti.getType()); } if(ti.isCollectionType()) { try { return new ArrayType(1, of(ti.getElementTypeInfo())); } catch (OpenDataException e) { throw new MException("Invalid array type", e); } } return forCompositeType(ti.getType()); }
public boolean isComplexType() { return typeInfo.isComplexType(); }
public Class<?> getElementType() { return typeInfo.getElementType(); }
protected boolean validateArray(String name, Object[] array, Validation validation, int maxErrors) { TypeInfo typeInfo = null; for(int i=0;i<array.length;i++) { Object value = array[i]; if(value == null) { continue; } if(typeInfo == null) { typeInfo = Types.getTypeInfo(value.getClass()); } if(typeInfo.isComplexType() || typeInfo.isCollectionType()) { String fullName = Strings.nullToEmpty(name) + "[" + String.valueOf(i) + "]"; if(!validate(fullName, value, validation, maxErrors) ){ return false; } } else { return true; } } return true; }
public boolean isSimpleElementType() { return null != elementTypeInfo && elementTypeInfo.isSimpleType(); }
/** * Returns true if the type is a collection type. */ default boolean isCollectionType() { return getTypeInfo().isCollectionType(); }
public static TypeInfo getTypeInfo(Class<?> type,Type genericType) { MTypeKind kind = null; Class<?> elementType = null; TypeInfo elementTypeInfo = null; if(isSimpleType(type, genericType)){ kind = MTypeKind.SIMPLE; }else if(isCollectionType(type, genericType)){ kind = MTypeKind.COLLECTION; elementType = getElementType(type, genericType); elementTypeInfo = getTypeInfo(elementType, null); }else{ kind = MTypeKind.COMPLEX; } return new TypeInfo(type, genericType, kind,elementType,elementTypeInfo); }
protected boolean validateMap(String name, Map map, Validation validation, int maxErrors) { TypeInfo typeInfo = null; for(Object item : map.entrySet()) { Map.Entry entry = (Map.Entry)item; Object value = entry.getValue(); if(null == value) { continue; } if(null == typeInfo) { typeInfo = Types.getTypeInfo(value.getClass()); } if(typeInfo.isComplexType() || typeInfo.isCollectionType()) { String fullName = Strings.nullToEmpty(name) + "['" + entry.getKey().toString() + "']"; if(!validate(fullName, value, validation, maxErrors) ){ return false; } } else { return true; } } return true; }
public boolean isSimpleType() { return typeInfo.isSimpleType(); }
protected boolean validateCollection(String name, Iterable iterable, Validation validation, int maxErrors) { int i=0; TypeInfo typeInfo = null; for(Object value : iterable) { if(value == null) { continue; } if(typeInfo == null) { typeInfo = Types.getTypeInfo(value.getClass()); } if(typeInfo.isComplexType() || typeInfo.isCollectionType()) { String fullName = Strings.nullToEmpty(name) + "[" + String.valueOf(i) + "]"; if(!validate(fullName, value, validation, maxErrors) ){ return false; } } else { return true; } i++; } return true; }
/** * Returns true if the type is a simple type. */ default boolean isSimpleType() { return getTypeInfo().isSimpleType(); }
public boolean isComplexElementType() { return null != elementTypeInfo && elementTypeInfo.isComplexType(); }
/** * Optional. Returns the type of element if the type is a collection type. */ default Class<?> getElementType() { return getTypeInfo().getElementType(); }
@Override public boolean isConventionalField(MetadataContext context,BeanProperty beanProperty) { if (!beanProperty.isReadable() || beanProperty.isTransient() || !beanProperty.isField()) { return false; } Class<?> type = beanProperty.getType(); if(Iterable.class.isAssignableFrom(type)){ return false; } if(Map.class.isAssignableFrom(type)){ return false; } //TODO : support complex type and related entity type return beanProperty.getTypeInfo().isSimpleType(); }
/** * Returns true if the type is a complex type. */ default boolean isComplexType() { return getTypeInfo().isComplexType(); }
public static <T> void applySimpleExtension(T ex, T target) { BeanType bt = BeanType.of(ex.getClass()); for(BeanProperty bp : bt.getProperties()) { if(!bp.isField() || !bp.isReadable() || !bp.isWritable()) { continue; } if(bp.isAnnotationPresent(JsonIgnore.class)) { continue; } if(bp.isAnnotationPresent(ManualExtend.class)) { continue; } if(!bp.getTypeInfo().isSimpleType()) { continue; } Object fromVal = bp.getValue(ex); if(null != fromVal) { bp.setValue(target, fromVal); } } }
public SimpleProperty(Class<T> type, T value) { Args.notNull(type); this.type = type; this.complex = Types.getTypeInfo(type, null).isComplexType();; this.value = value; }
@Override public <T> Property<T> getDynaProperty(String name, Class<T> type) { if(null != propertyProvider) { try { Property<T> p = propertyProvider.getDynaProperty(name, type); return p; } catch (UnsupportedDynaPropertyException e) { log.info("property {0} unsupported by {1}, use local config",name,propertyProvider.getClass()); } } String v = properties.get(name); if(null == v || v.isEmpty()) { return new NullProperty<>(); } TypeInfo ti = Types.getTypeInfo(type,null); if(ti.isComplexType()) { return new SimpleProperty<>(type, JSON.decode(v, type)); }else{ return new SimpleProperty<>(type, Converts.convert(v, type)); } }