protected Binding<Float> createFloatBinding(Object target, final FieldMetadata<Object, ?> fieldMetadata) { Class<?> type = fieldMetadata.getType(); if (type == Integer.class || type == Integer.TYPE) { return new Binding<Float>() {
@Override @SuppressWarnings("unchecked") public void set(Float value) { Class<? extends Number> type = binding.fieldMetadata.getType(); if (type == Integer.TYPE || type == Integer.class) { ((Binding<Integer>) binding).set(value.intValue()); } else if (type == Float.TYPE || type == Float.class) { ((Binding<Float>) binding).set(value); } } }
@Override @SuppressWarnings("unchecked") public void set(Float value) { Class<? extends Number> type = binding.fieldMetadata.getType(); if (type == Integer.TYPE || type == Integer.class) { ((Binding<Integer>) binding).set(value.intValue()); } else if (type == Float.TYPE || type == Float.class) { ((Binding<Float>) binding).set(value); } } }
@SuppressWarnings("unchecked") protected WorldConfigNumberBinding(WorldConfigurator config, String label, ComponentLibrary compLib, FieldMetadata<Object, ?> field) { Class<?> type = field.getType(); if (type == Integer.TYPE || type == Integer.class) { this.binding = new WorldSetupScreen.WorldConfigBinding<>(config, label, compLib, (FieldMetadata<Object, Integer>) field); } else if (type == Float.TYPE || type == Float.class) { this.binding = new WorldSetupScreen.WorldConfigBinding<>(config, label, compLib, (FieldMetadata<Object, Float>) field); } }
@SuppressWarnings("unchecked") protected WorldConfigNumberBinding(WorldConfigurator config, String label, ComponentLibrary compLib, FieldMetadata<Object, ?> field) { Class<?> type = field.getType(); if (type == Integer.TYPE || type == Integer.class) { this.binding = new WorldConfigBinding<>(config, label, compLib, (FieldMetadata<Object, Integer>) field); } else if (type == Float.TYPE || type == Float.class) { this.binding = new WorldConfigBinding<>(config, label, compLib, (FieldMetadata<Object, Float>) field); } }
/** * Deserializes a value onto an object * * @param target The object to deserialize the field onto * @param fieldMetadata The metadata of the field * @param data The serialized value of the field * @param context The deserialization context */ public void deserializeOnto(Object target, FieldMetadata<?, ?> fieldMetadata, PersistedData data, DeserializationContext context) { TypeHandler<?> handler = getHandlerFor(fieldMetadata); if (handler == null) { logger.error("No type handler for type {} used by {}::{}", fieldMetadata.getType(), target.getClass(), fieldMetadata); } else { try { Object deserializedValue = handler.deserialize(data, context); fieldMetadata.setValue(target, deserializedValue); } catch (DeserializationException e) { logger.error("Unable to deserialize field '{}' from '{}'", fieldMetadata.getName(), data.toString(), e); } } }
private List<ComponentFieldUri> determineAnimationPoolUris() { final List<ComponentFieldUri> animationSetUris = Lists.newArrayList(); for (ComponentMetadata<?> componentMetadata : componentLibrary.iterateComponentMetadata()) { SimpleUri uri = componentMetadata.getUri(); for (FieldMetadata<?, ?> fieldMetadata : componentMetadata.getFields()) { if (fieldMetadata.getType().isAssignableFrom(List.class)) { Type fieldType = fieldMetadata.getField().getGenericType(); if (fieldType instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) fieldType; Type[] typeArguments = parameterizedType.getActualTypeArguments(); if (typeArguments.length == 1) { Class<?> typeClass = ReflectionUtil.getClassOfType(typeArguments[0]); if (typeClass.isAssignableFrom(MeshAnimation.class)) { animationSetUris.add(new ComponentFieldUri(uri, fieldMetadata.getName())); } } } } } } return animationSetUris; }
@Override public Property create(Object target, final FieldMetadata<Object, ?> fieldMetadata, String id, OneOf.Enum info) { Class<?> cls = fieldMetadata.getType(); Object[] items = cls.getEnumConstants(); UIDropdown dropdown = new UIDropdown(); dropdown.bindOptions(new DefaultBinding(Arrays.asList(items))); Binding binding = createTextBinding(target, fieldMetadata); dropdown.bindSelection(binding); String label = fromLabelOrId(info.label(), id); return new Property<>(label, binding, dropdown, info.description()); } }
@Override public Property create(Object target, FieldMetadata<Object, ?> fieldMetadata, String id, TextField info) { UITextEntry<T> text = new UITextEntry<>(); Binding<T> textBinding = createTextBinding(target, (FieldMetadata<Object, T>) fieldMetadata); TextMapper<T> textMapper = createTextMapping(fieldMetadata.getType()); text.setFormatter(textMapper); text.setParser(textMapper); text.bindValue(textBinding); String label = fromLabelOrId(info.label(), id); return new Property<>(label, textBinding, text, info.description()); } }
@Test public void testPrivateField() throws Exception { DefaultClassMetadata<PrivateField> metadata = new DefaultClassMetadata<>(new SimpleUri(), PrivateField.class, factory, copyStrategyLibrary); assertNotNull(metadata); assertEquals(1, metadata.getFieldCount()); FieldMetadata fieldMetadata = metadata.getField("name"); assertNotNull(fieldMetadata); assertEquals(String.class, fieldMetadata.getType()); assertEquals("name", fieldMetadata.getName()); assertNotNull(metadata.newInstance()); }