Ronnie Marksch
Ronnie Marksch

Reputation: 118

how to get classes of generics parameters for fields

I am using the reflections library [1] to obtain a Field. For a field that is declared as public Map<Integer, Boolean> nameF; I want to get its string representation: "Map nameF". While "Map" and "nameF" are easy to obtain, I fail at getting the types "Integer" and "Boolean".

[1] https://github.com/ronmamo/reflections

package main;

import org.reflections.Reflections;
import org.reflections.scanners.FieldAnnotationsScanner;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.scanners.TypeAnnotationsScanner;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Map;
import java.util.Set;

public class Test {

    @Target({ElementType.FIELD})
    @Retention(RetentionPolicy.CLASS)
    public @interface MyAnnotation {
    }

    public class TestClass {

        @MyAnnotation
        public Map<Integer, Boolean> fieldFoo;

    }

    public static void main(String[] args) {
        Reflections reflections = new Reflections(Test.class.getCanonicalName(),
                new SubTypesScanner(false),
                new TypeAnnotationsScanner(),
                new FieldAnnotationsScanner());
        {
            Set<Field> annotated = reflections.getFieldsAnnotatedWith(MyAnnotation.class);
            for (Field controller : annotated) {
                System.out.println("#1:" + controller.getDeclaringClass().getCanonicalName() + " " + controller.getName() + " " + controller.getType().getCanonicalName() + " ");
                for (TypeVariable<? extends Class<?>> elem : controller.getType().getTypeParameters()) {
                    System.out.println("#2:" + elem);
                    for (Type bound : elem.getBounds()) {
                        System.out.println("#3:" + bound);
                        System.out.println("#4:" + bound.getTypeName());
                    }
                    for (AnnotatedType bound : elem.getAnnotatedBounds()) {
                        System.out.println("#5:" + bound);
                        System.out.println("#6:" + bound.getType());
                    }
                }
                System.out.println("#7:" + controller.getClass().getGenericSuperclass());

            }

        }


    }
}

the above code results in the following

#1:main.Test.TestClass fieldFoo java.util.Map 
#2:K
#3:class java.lang.Object
#4:java.lang.Object
#5:sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@ba4d54
#6:class java.lang.Object
#2:V
#3:class java.lang.Object
#4:java.lang.Object
#5:sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@12bc6874
#6:class java.lang.Object
#7:class java.lang.reflect.AccessibleObject

Upvotes: 1

Views: 104

Answers (1)

John Stringer
John Stringer

Reputation: 587

Use Field.getGenericType() then cast the Type returned to ParameterizedType and then call ParameterizedType.getActualTypeArguments() for your example this will return an array with 2; Integer and Boolean. Example based on your code:

public static void main(String[] args) {
    Reflections reflections = new Reflections(Test.class.getCanonicalName(),
            new SubTypesScanner(false),
            new TypeAnnotationsScanner(),
            new FieldAnnotationsScanner());
    {
        Set<Field> annotated = reflections.getFieldsAnnotatedWith(MyAnnotation.class);
        for (Field controller : annotated) {
            Type genericType = controller.getGenericType();
            if(genericType instanceof ParameterizedType){
                for(Type genericTypeArg: ((ParameterizedType)genericType).getActualTypeArguments()) {
                    System.out.println("Generic Type Arg: "+genericTypeArg.getTypeName());
                }
            } else {
                System.out.println("Can't determine generic type");
            }
        }
    }
}

Upvotes: 2

Related Questions