use of com.badlogic.gdx.utils.reflect.Field in project libgdx by libgdx.
the class AnnotationTest method create.
@Override
public void create() {
font = new BitmapFont();
batch = new SpriteBatch();
try {
Annotation annotation = ClassReflection.getDeclaredAnnotation(AnnotatedClass.class, TestAnnotation.class);
if (annotation != null) {
TestAnnotation annotationInstance = annotation.getAnnotation(TestAnnotation.class);
println("Class annotation:\n name=" + annotationInstance.name() + ",\n values=" + Arrays.toString(annotationInstance.values()) + ",\n enum=" + annotationInstance.someEnum().toString());
} else {
println("ERROR: Class annotation not found.");
}
Field field = ClassReflection.getDeclaredField(AnnotatedClass.class, "annotatedValue");
if (field != null) {
Annotation[] annotations = field.getDeclaredAnnotations();
for (Annotation a : annotations) {
if (a.getAnnotationType().equals(TestAnnotation.class)) {
TestAnnotation annotationInstance = a.getAnnotation(TestAnnotation.class);
println("Field annotation:\n name=" + annotationInstance.name() + ",\n values=" + Arrays.toString(annotationInstance.values()) + ",\n enum=" + annotationInstance.someEnum().toString());
break;
}
}
} else {
println("ERROR: Field 'annotatedValue' not found.");
}
Method method = ClassReflection.getDeclaredMethod(AnnotatedClass.class, "annotatedMethod");
if (method != null) {
Annotation[] annotations = method.getDeclaredAnnotations();
for (Annotation a : annotations) {
if (a.getAnnotationType().equals(TestAnnotation.class)) {
TestAnnotation annotationInstance = a.getAnnotation(TestAnnotation.class);
println("Method annotation:\n name=" + annotationInstance.name() + ",\n values=" + Arrays.toString(annotationInstance.values()) + ",\n enum=" + annotationInstance.someEnum().toString());
break;
}
}
} else {
println("ERROR: Method 'annotatedMethod' not found.");
}
println("Class annotations w/@Inherit:");
Annotation[] annotations = ClassReflection.getAnnotations(InheritClassB.class);
for (Annotation a : annotations) {
println(" name=" + a.getAnnotationType().getSimpleName());
}
if (!ClassReflection.isAnnotationPresent(InheritClassB.class, TestInheritAnnotation.class)) {
println("ERROR: Inherited class annotation not found.");
}
} catch (Exception e) {
println("FAILED: " + e.getMessage());
message += e.getClass();
}
}
use of com.badlogic.gdx.utils.reflect.Field in project libgdx by libgdx.
the class Json method getDefaultValues.
private Object[] getDefaultValues(Class type) {
if (!usePrototypes)
return null;
if (classToDefaultValues.containsKey(type))
return classToDefaultValues.get(type);
Object object;
try {
object = newInstance(type);
} catch (Exception ex) {
classToDefaultValues.put(type, null);
return null;
}
ObjectMap<String, FieldMetadata> fields = getFields(type);
Object[] values = new Object[fields.size];
classToDefaultValues.put(type, values);
int i = 0;
for (FieldMetadata metadata : fields.values()) {
Field field = metadata.field;
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() + ")");
throw ex;
} catch (RuntimeException runtimeEx) {
SerializationException ex = new SerializationException(runtimeEx);
ex.addTrace(field + " (" + type.getName() + ")");
throw ex;
}
}
return values;
}
use of com.badlogic.gdx.utils.reflect.Field in project libgdx by libgdx.
the class Json method writeFields.
/** Writes all fields of the specified object to the current JSON object. */
public void writeFields(Object object) {
Class type = object.getClass();
Object[] defaultValues = getDefaultValues(type);
OrderedMap<String, FieldMetadata> fields = getFields(type);
int i = 0;
for (FieldMetadata metadata : new OrderedMapValues<FieldMetadata>(fields)) {
Field field = metadata.field;
try {
Object value = field.get(object);
if (defaultValues != null) {
Object defaultValue = defaultValues[i++];
if (value == null && defaultValue == null)
continue;
if (value != null && defaultValue != null) {
if (value.equals(defaultValue))
continue;
if (value.getClass().isArray() && defaultValue.getClass().isArray()) {
equals1[0] = value;
equals2[0] = defaultValue;
if (Arrays.deepEquals(equals1, equals2))
continue;
}
}
}
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() + ")");
throw ex;
} catch (Exception runtimeEx) {
SerializationException ex = new SerializationException(runtimeEx);
ex.addTrace(field + " (" + type.getName() + ")");
throw ex;
}
}
}
use of com.badlogic.gdx.utils.reflect.Field in project libgdx by libgdx.
the class Json method readFields.
public void readFields(Object object, JsonValue jsonMap) {
Class type = object.getClass();
ObjectMap<String, FieldMetadata> fields = getFields(type);
for (JsonValue child = jsonMap.child; child != null; child = child.next) {
FieldMetadata metadata = fields.get(child.name);
if (metadata == null) {
if (child.name.equals(typeName))
continue;
if (ignoreUnknownFields) {
if (debug)
System.out.println("Ignoring unknown field: " + child.name + " (" + type.getName() + ")");
continue;
} else {
SerializationException ex = new SerializationException("Field not found: " + child.name + " (" + type.getName() + ")");
ex.addTrace(child.trace());
throw ex;
}
}
Field field = metadata.field;
try {
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;
}
}
}
use of com.badlogic.gdx.utils.reflect.Field in project libgdx by libgdx.
the class Json method writeField.
/** 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;
}
}
Aggregations