/** * Deserializes a Collection of name-values onto an object * * @param target The object to deserialize onto * @param values The collection of values to apply to the object * @param check A check to filter which fields to deserialize */ public void deserializeOnto(Object target, PersistedDataMap values, DeserializeFieldCheck check, DeserializationContext context) { for (Map.Entry<String, PersistedData> field : values.entrySet()) { FieldMetadata<?, ?> fieldInfo = classMetadata.getField(field.getKey()); if (fieldInfo != null && check.shouldDeserialize(classMetadata, fieldInfo)) { deserializeOnto(target, fieldInfo, field.getValue(), context); } else if (fieldInfo == null) { logger.warn("Cannot deserialize unknown field '{}' onto '{}'", field.getKey(), classMetadata.getUri()); } } }
FieldMetadata<?, ?> fieldMetadata = elementMetadata.getField(ID_FIELD); if (fieldMetadata == null) { logger.warn("UIWidget type {} lacks id field", elementMetadata.getUri());
public List<Property<?, ?>> createProperties(Object target) { List<Property<?, ?>> properties = Lists.newArrayList(); try { Class<?> type = target.getClass(); ReflectFactory reflectFactory = CoreRegistry.get(ReflectFactory.class); CopyStrategyLibrary copyStrategies = new CopyStrategyLibrary(reflectFactory); ClassMetadata<?, ?> classMetadata = new DefaultClassMetadata<>(new SimpleUri(), type, reflectFactory, copyStrategies); for (Field field : getAllFields(type)) { Annotation annotation = getFactory(field); if (annotation != null) { FieldMetadata<Object, ?> fieldMetadata = (FieldMetadata<Object, ?>) classMetadata.getField(field.getName()); PropertyFactory factory = factories.get(annotation.annotationType()); Property property = factory.create(target, fieldMetadata, field.getName(), annotation); properties.add(property); } } } catch (NoSuchMethodException e) { throw new RuntimeException(e); } return properties; }
public ReflectionProvider(T target, Context context) { try { ReflectFactory reflectFactory = context.get(ReflectFactory.class); CopyStrategyLibrary copyStrategies = context.get(CopyStrategyLibrary.class); ClassMetadata<T, ?> classMetadata = new DefaultClassMetadata<>(new SimpleUri(), (Class<T>) target.getClass(), reflectFactory, copyStrategies); for (Field field : getAllFields(target.getClass(), and(withAnnotation(Range.class), or(withType(Float.TYPE), withType(Float.class))))) { Range range = field.getAnnotation(Range.class); FieldMetadata<T, Float> fieldMetadata = (FieldMetadata<T, Float>) classMetadata.getField(field.getName()); Property property = new FloatProperty(target, fieldMetadata, range.min(), range.max()); properties.add(property); } } catch (NoSuchMethodException e) { logger.error("Cannot provide provide inspection for {}, does not have a default constructor", target.getClass()); } }
private <T> Map<Class<? extends T>, Integer> applySerializationInfo(List<NetData.SerializationInfo> infoList, ClassLibrary<T> classLibrary) { Map<Class<? extends T>, Integer> idTable = Maps.newHashMap(); for (NetData.SerializationInfo info : infoList) { ClassMetadata<? extends T, ?> metadata = classLibrary.getMetadata(new SimpleUri(info.getName())); if (metadata != null) { idTable.put(metadata.getType(), info.getId()); for (int i = 0; i < info.getFieldIds().size(); ++i) { FieldMetadata<?, ?> field = metadata.getField(info.getFieldName(i)); if (field != null) { field.setId(info.getFieldIds().byteAt(i)); } else { logger.error("Server has unknown field '{}' on '{}'", info.getFieldName(i), info.getName()); } } } else { logger.error("Server has unknown class '{}'", info.getName()); } } return idTable; }