private void set (String fieldName, Object object, Object value) { try { ClassReflection.getField(object.getClass(), fieldName).set(object, value); } catch (Throwable ex) { throw new GdxRuntimeException(ex); } }
private void setField (Field field, Task<E> task, Object value) { field.setAccessible(true); Object valueObject = castValue(field, value); if (valueObject == null) throwAttributeTypeException(getCurrentTask().name, field.getName(), field.getType().getSimpleName()); try { field.set(task, valueObject); } catch (ReflectionException e) { throw new GdxRuntimeException(e); } }
/** Each field on the <code>to</code> object is set to the value for the field with the same name on the <code>from</code> * object. The <code>to</code> object must have at least all the fields of the <code>from</code> object with the same name and * type. */ public void copyFields (Object from, Object to) { ObjectMap<String, FieldMetadata> toFields = getFields(from.getClass()); for (ObjectMap.Entry<String, FieldMetadata> entry: getFields(from.getClass())) { FieldMetadata toField = toFields.get(entry.key); Field fromField = entry.value.field; if (toField == null) throw new SerializationException("To object is missing field" + entry.key); try { toField.field.set(to, fromField.get(from)); } catch (ReflectionException ex) { throw new SerializationException("Error copying field: " + fromField.getName(), ex); } } }
private void setField (Field field, Task<E> task, Object value) { field.setAccessible(true); Object valueObject = castValue(field, value); try { field.set(task, valueObject); } catch (ReflectionException e) { throw new GdxRuntimeException(e); } }
/** Each field on the <code>to</code> object is set to the value for the field with the same name on the <code>from</code> * object. The <code>to</code> object must have at least all the fields of the <code>from</code> object with the same name and * type. */ public void copyFields (Object from, Object to) { ObjectMap<String, FieldMetadata> toFields = getFields(from.getClass()); for (ObjectMap.Entry<String, FieldMetadata> entry: getFields(from.getClass())) { FieldMetadata toField = toFields.get(entry.key); Field fromField = entry.value.field; if (toField == null) throw new SerializationException("To object is missing field" + entry.key); try { toField.field.set(to, fromField.get(from)); } catch (ReflectionException ex) { throw new SerializationException("Error copying field: " + fromField.getName(), ex); } } }
/** Injects actors from group into target's fields annotated with {@link InjectActor} using reflection. */ public static void injectActorFields(Object target, Group group) { Class<?> handledClass = target.getClass(); while (handledClass != null && !handledClass.equals(Object.class)) { for (final Field field : ClassReflection.getDeclaredFields(handledClass)) { if (field != null && field.isAnnotationPresent(InjectActor.class)) { try { InjectActor annotation = field.getDeclaredAnnotation(InjectActor.class).getAnnotation(InjectActor.class); String actorName = annotation.value(); if (actorName.length() == 0) { actorName = field.getName(); } Actor actor = group.findActor(actorName); if (actor == null && actorName.equals(group.getName())) { actor = group; } if (actor == null) { Gdx.app.error(TAG_INJECT_FIELDS, "Can't find actor with name: " + actorName + " in group: " + group + " to inject into: " + target); } else { field.setAccessible(true); field.set(target, actor); } } catch (final ReflectionException exception) { Gdx.app.error(TAG_INJECT_FIELDS, "Unable to set value into field: " + field + " of object: " + target, exception); } } } handledClass = handledClass.getSuperclass(); } }
field.set(object, readValue(field.getType(), metadata.elementType, child)); } catch (ReflectionException ex) { throw new SerializationException("Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex);
field.set(object, readValue(field.getType(), metadata.elementType, child)); } catch (ReflectionException ex) { throw new SerializationException("Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex);
/** @param object May be null if the field is static. * @param elementType May be null if the type is unknown. */ public void readField (Object object, Field field, String jsonName, Class elementType, JsonValue jsonMap) { JsonValue jsonValue = jsonMap.get(jsonName); if (jsonValue == null) return; try { field.set(object, readValue(field.getType(), elementType, jsonValue)); } catch (ReflectionException ex) { throw new SerializationException( "Error accessing field: " + field.getName() + " (" + field.getDeclaringClass().getName() + ")", ex); } catch (SerializationException ex) { ex.addTrace(field.getName() + " (" + field.getDeclaringClass().getName() + ")"); throw ex; } catch (RuntimeException runtimeEx) { SerializationException ex = new SerializationException(runtimeEx); ex.addTrace(jsonValue.trace()); ex.addTrace(field.getName() + " (" + field.getDeclaringClass().getName() + ")"); throw ex; } }
/** @param object May be null if the field is static. * @param elementType May be null if the type is unknown. */ public void readField (Object object, Field field, String jsonName, Class elementType, JsonValue jsonMap) { JsonValue jsonValue = jsonMap.get(jsonName); if (jsonValue == null) return; try { field.set(object, readValue(field.getType(), elementType, jsonValue)); } catch (ReflectionException ex) { throw new SerializationException( "Error accessing field: " + field.getName() + " (" + field.getDeclaringClass().getName() + ")", ex); } catch (SerializationException ex) { ex.addTrace(field.getName() + " (" + field.getDeclaringClass().getName() + ")"); throw ex; } catch (RuntimeException runtimeEx) { SerializationException ex = new SerializationException(runtimeEx); ex.addTrace(jsonValue.trace()); ex.addTrace(field.getName() + " (" + field.getDeclaringClass().getName() + ")"); throw ex; } }
private <T> void setPrimitiveField(Field field, Class<?> fieldClass, T object, String value) throws SerializationException { if(field.isFinal()) { throw new SerializationException("Cannot use @Field on final " + fieldClass.getName() + " fields."); } try { if (fieldClass.equals(Boolean.TYPE) || fieldClass.equals(Boolean.class)) { field.set(object, Boolean.parseBoolean(value)); } else if (fieldClass.equals(Byte.TYPE) || fieldClass.equals(Byte.class)) { field.set(object, Byte.parseByte(value)); } else if (fieldClass.equals(Character.TYPE) || fieldClass.equals(Character.class)) { field.set(object, value.charAt(0)); } else if (fieldClass.equals(Double.TYPE) || fieldClass.equals(Double.class)) { field.set(object, Double.parseDouble(value)); } else if (fieldClass.equals(Float.TYPE) || fieldClass.equals(Float.class)) { field.set(object, Float.parseFloat(value)); } else if (fieldClass.equals(Integer.TYPE) || fieldClass.equals(Integer.class)) { field.set(object, Integer.parseInt(value)); } else if (fieldClass.equals(Long.TYPE) || fieldClass.equals(Long.class)) { field.set(object, Long.parseLong(value)); } else if (fieldClass.equals(Short.TYPE) || fieldClass.equals(Short.class)) { field.set(object, Short.parseShort(value)); } else { field.set(object, value); } } catch (Exception e) { throw new SerializationException(e.getMessage(), e); } }
@Override public boolean keyTyped(InputEvent event, char character) { String userInput = fieldValue.getText(); float newFieldValue; try { newFieldValue = Float.parseFloat(userInput); } catch (NumberFormatException e) { return true; } try { field.set(field, (Float)newFieldValue); } catch (ReflectionException e) { Gdx.app.debug(TAG, "Cannot set value for " + field.getName()); } return true; } });
private <T> void setEnumField(Field field, Class<?> fieldClass, T object, String value) throws SerializationException { if(field.isFinal()) { throw new SerializationException("Cannot use @Field on final enum fields. Use the @ConstructorArg method instead."); } try { field.set(object, Enum.valueOf((Class<Enum>) fieldClass, value)); } catch (ReflectionException e) { throw new SerializationException(e.getMessage(), e); } }
@Override public void changed(ChangeEvent event, Actor actor) { try { field.set(field, (Boolean)checkBox.isChecked()); } catch (ReflectionException e) { Gdx.app.debug(TAG, "Cannot set value for " + field.getName()); } } });
public DeserializedCollection(Field field, Class<?> fieldClass, Object object) throws ReflectionException { super(); this.field = field; this.fieldClass = fieldClass; if(field.isFinal()) { collection = (T) field.get(object); } else { collection = (T) (fieldClass.isInterface() ? ClassReflection.newInstance(getFallbackImplementation()) : ClassReflection.newInstance(fieldClass)); field.set(object, collection); } }
public DeserializedMap(Field field, Class<?> fieldClass, Object object) throws ReflectionException { super(); this.field = field; this.fieldClass = fieldClass; if(field.isFinal()) { map = (T) field.get(object); } else { map = (T) (fieldClass.isInterface() ? ClassReflection.newInstance(getFallbackImplementation()) : ClassReflection.newInstance(fieldClass)); field.set(object, map); } }
/** Each field on the <code>to</code> object is set to the value for the field with the same name on the <code>from</code> * object. The <code>to</code> object must have at least all the fields of the <code>from</code> object with the same name and * type. */ public void copyFields (Object from, Object to) { ObjectMap<String, FieldMetadata> toFields = getFields(from.getClass()); for (ObjectMap.Entry<String, FieldMetadata> entry: getFields(from.getClass())) { FieldMetadata toField = toFields.get(entry.key); Field fromField = entry.value.field; if (toField == null) throw new SerializationException("To object is missing field" + entry.key); try { toField.field.set(to, fromField.get(from)); } catch (ReflectionException ex) { throw new SerializationException("Error copying field: " + fromField.getName(), ex); } } }
field.set(object, readValue(field.getType(), metadata.elementType, child)); } catch (ReflectionException ex) { throw new SerializationException("Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex);
/** @param object May be null if the field is static. * @param elementType May be null if the type is unknown. */ public void readField (Object object, Field field, String jsonName, Class elementType, JsonValue jsonMap) { JsonValue jsonValue = jsonMap.get(jsonName); if (jsonValue == null) return; try { field.set(object, readValue(field.getType(), elementType, jsonValue)); } catch (ReflectionException ex) { throw new SerializationException( "Error accessing field: " + field.getName() + " (" + field.getDeclaringClass().getName() + ")", ex); } catch (SerializationException ex) { ex.addTrace(field.getName() + " (" + field.getDeclaringClass().getName() + ")"); throw ex; } catch (RuntimeException runtimeEx) { SerializationException ex = new SerializationException(runtimeEx); ex.addTrace(jsonValue.trace()); ex.addTrace(field.getName() + " (" + field.getDeclaringClass().getName() + ")"); throw ex; } }