/** @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; } }
for (FieldMetadata metadata : new OrderedMapValues<FieldMetadata>(fields)) { Field field = metadata.field; if (readDeprecated && ignoreDeprecated && field.isAnnotationPresent(Deprecated.class)) continue; try { Object value = field.get(object); if (defaultValues != null) { Object defaultValue = defaultValues[i++]; if (debug) System.out.println("Writing field: " + field.getName() + " (" + type.getName() + ")"); writer.name(field.getName()); writeValue(value, field.getType(), metadata.elementType); } catch (ReflectionException ex) { throw new SerializationException("Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex); } catch (SerializationException ex) { ex.addTrace(field + " (" + type.getName() + ")");
Field field = allFields.get(i); if (field.isTransient()) continue; if (field.isStatic()) continue; if (field.isSynthetic()) continue; if (!field.isAccessible()) { try { field.setAccessible(true); } catch (AccessControlException ex) { continue; if (ignoreDeprecated && !readDeprecated && field.isAnnotationPresent(Deprecated.class)) continue; nameToField.put(field.getName(), new FieldMetadata(field));
public FieldMetadata (Field field) { this.field = field; int index = (ClassReflection.isAssignableFrom(ObjectMap.class, field.getType()) || ClassReflection.isAssignableFrom(Map.class, field.getType())) ? 1 : 0; this.elementType = field.getElementType(index); } }
/** Returns the value of the field on the supplied object. */ public Object get (Object obj) throws ReflectionException { try { return field.get(obj); } catch (IllegalArgumentException e) { throw new ReflectionException("Object is not an instance of " + getDeclaringClass(), e); } catch (IllegalAccessException e) { throw new ReflectionException("Illegal access to field: " + getName(), 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); } } }
field.set(object, readValue(field.getType(), metadata.elementType, child)); } catch (ReflectionException ex) { throw new SerializationException("Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex); } catch (SerializationException ex) { ex.addTrace(field.getName() + " (" + type.getName() + ")"); throw ex; } catch (RuntimeException runtimeEx) { SerializationException ex = new SerializationException(runtimeEx); ex.addTrace(child.trace()); ex.addTrace(field.getName() + " (" + type.getName() + ")"); throw ex;
/** Writes the specified field to the current JSON object. * @param elementType May be null if the type is unknown. */ public void writeField (Object object, String fieldName, String jsonName, Class elementType) { Class type = object.getClass(); ObjectMap<String, FieldMetadata> fields = getFields(type); FieldMetadata metadata = fields.get(fieldName); if (metadata == null) throw new SerializationException("Field not found: " + fieldName + " (" + type.getName() + ")"); Field field = metadata.field; if (elementType == null) elementType = metadata.elementType; try { if (debug) System.out.println("Writing field: " + field.getName() + " (" + type.getName() + ")"); writer.name(jsonName); writeValue(field.get(object), field.getType(), elementType); } catch (ReflectionException ex) { throw new SerializationException("Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex); } catch (SerializationException ex) { ex.addTrace(field + " (" + type.getName() + ")"); throw ex; } catch (Exception runtimeEx) { SerializationException ex = new SerializationException(runtimeEx); ex.addTrace(field + " (" + type.getName() + ")"); throw ex; } }
field.setAccessible(true); Annotation annotation = field .getDeclaredAnnotation(org.mini2Dx.core.serialization.annotation.Field.class); .getAnnotation(org.mini2Dx.core.serialization.annotation.Field.class); Object value = field.get(object); if (!fieldAnnotation.optional() && value == null) { throw new RequiredFieldException(currentClass, field.getName()); writeObject(field, value, field.getName(), xmlWriter);
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); } }
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); } }
/** 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(); } }
for (FieldMetadata metadata : fields.values()) { Field field = metadata.field; if (readDeprecated && ignoreDeprecated && field.isAnnotationPresent(Deprecated.class)) continue; try { values[i++] = field.get(object); } catch (ReflectionException ex) { throw new SerializationException("Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex); } catch (SerializationException ex) { ex.addTrace(field + " (" + type.getName() + ")");
if (field.isPublic()) { if (field.getName().equals(key)) { if (field.getType() == Integer.class || field.getType() == Integer.TYPE) { field.set(instance, ((NSNumber) dictValue).intValue()); break; if (field.getType() == Long.class || field.getType() == Long.TYPE) { field.set(instance, ((NSNumber) dictValue).longValue()); break; if (field.getType() == Float.class || field.getType() == Float.TYPE) { field.set(instance, ((NSNumber) dictValue).floatValue()); break; if (field.getType() == Double.class || field.getType() == Double.TYPE) { field.set(instance, ((NSNumber) dictValue).doubleValue()); break; if (field.getType() == Boolean.class || field.getType() == Boolean.TYPE) { field.set(instance, ((NSNumber) dictValue).boolValue()); break; if (field.getType() == String.class) { field.set(instance, dictValue.toString()); break;
/** Sets the value of the field on the supplied object. */ public void set (Object obj, Object value) throws ReflectionException { try { field.set(obj, value); } catch (IllegalArgumentException e) { throw new ReflectionException("Argument not valid for field: " + getName(), e); } catch (IllegalAccessException e) { throw new ReflectionException("Illegal access to field: " + getName(), e); } }
currentField.setAccessible(true); Class<?> fieldClass = currentField.getType(); if (fieldClass.isArray()) { int arraySize = Integer.parseInt(xmlReader.getAttributeValue("", "length")); xmlReader.next(); setSerializedCollectionField(xmlReader, deserializedCollection, currentField, fieldClass, result); } else if(currentField.isFinal()) { throw new SerializationException("Cannot use @Field on final " + fieldClass.getName() + " fields."); } else { currentField.set(result, deserializeObject(xmlReader, currentFieldName, fieldClass));
boolean fieldValueBoolean = false; try { fieldValueBoolean = (Boolean)field.get(field); } catch (Exception e) { Gdx.app.debug(TAG, "Cannot parse value for boolean " + field.getName()); final CheckBox checkBox = new CheckBox(field.getName(), skin); checkBox.setChecked(fieldValueBoolean); innerTable.add(checkBox).pad(1).align(Align.left);
while (currentClass != null && !currentClass.equals(Object.class)) { for (Field field : ClassReflection.getDeclaredFields(currentClass)) { field.setAccessible(true); Annotation annotation = field .getDeclaredAnnotation(org.mini2Dx.core.serialization.annotation.Field.class); .getAnnotation(org.mini2Dx.core.serialization.annotation.Field.class); JsonValue value = objectRoot.get(field.getName()); if (value == null || value.isNull()) { if (!fieldAnnotation.optional()) { throw new RequiredFieldException(currentClass, field.getName());
/** @return the {@link #selectedInterpolation selected} interpolation */ private Interpolation getInterpolation (String name) { try { return (Interpolation)ClassReflection.getField(Interpolation.class, name).get(null); } catch (Exception e) { throw new RuntimeException(e); } }