Search in sources :

Example 1 with Annotation

use of com.badlogic.gdx.utils.reflect.Annotation 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)

Aggregations

BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)1 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)1 Annotation (com.badlogic.gdx.utils.reflect.Annotation)1 Field (com.badlogic.gdx.utils.reflect.Field)1 Method (com.badlogic.gdx.utils.reflect.Method)1