Search in sources :

Example 6 with Field

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();
    }
}
Also used : Field(com.badlogic.gdx.utils.reflect.Field) Method(com.badlogic.gdx.utils.reflect.Method) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) Annotation(com.badlogic.gdx.utils.reflect.Annotation)

Example 7 with Field

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;
}
Also used : Field(com.badlogic.gdx.utils.reflect.Field) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) IOException(java.io.IOException) AccessControlException(java.security.AccessControlException) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException)

Example 8 with Field

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;
        }
    }
}
Also used : Field(com.badlogic.gdx.utils.reflect.Field) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) OrderedMapValues(com.badlogic.gdx.utils.OrderedMap.OrderedMapValues) IOException(java.io.IOException) AccessControlException(java.security.AccessControlException) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException)

Example 9 with Field

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;
        }
    }
}
Also used : Field(com.badlogic.gdx.utils.reflect.Field) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException)

Example 10 with Field

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;
    }
}
Also used : Field(com.badlogic.gdx.utils.reflect.Field) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) IOException(java.io.IOException) AccessControlException(java.security.AccessControlException) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException)

Aggregations

Field (com.badlogic.gdx.utils.reflect.Field)13 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)5 ReflectionException (com.badlogic.gdx.utils.reflect.ReflectionException)5 AccessControlException (java.security.AccessControlException)4 Color (com.badlogic.gdx.graphics.Color)3 IOException (java.io.IOException)3 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)2 Actor (com.badlogic.gdx.scenes.scene2d.Actor)2 ListStyle (com.badlogic.gdx.scenes.scene2d.ui.List.ListStyle)2 ScrollPaneStyle (com.badlogic.gdx.scenes.scene2d.ui.ScrollPane.ScrollPaneStyle)2 ChangeListener (com.badlogic.gdx.scenes.scene2d.utils.ChangeListener)2 Drawable (com.badlogic.gdx.scenes.scene2d.utils.Drawable)2 SpriteDrawable (com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable)2 Array (com.badlogic.gdx.utils.Array)2 Json (com.badlogic.gdx.utils.Json)2 Method (com.badlogic.gdx.utils.reflect.Method)2 Iterator (java.util.Iterator)2 InputAdapter (com.badlogic.gdx.InputAdapter)1 InputMultiplexer (com.badlogic.gdx.InputMultiplexer)1 Pixmap (com.badlogic.gdx.graphics.Pixmap)1