lundi 22 mai 2017

How to get the packagename of fields by reflection?

I'd like to detect fields in my class that either are custom fields (like Address or are native java fields.

Therefore I'm trying to read the package name of each field (to later check if they start with java.*.

public class ReflectionTest {
    public class MyDTO {
        private String name;
        private Address address;
    }

    public class Address {
        private String street;
        private String zip;
        private String town;
    }


    @Test
    public void testPackageName() {
        for (Field field : MyDTO.class.getDeclaredFields()) {
            //always: java.lang.Class
            System.out.println(field.getGenericType().getClass().getPackage().getName()); 
        }
    }
}

Problem: the package shown is always java.lang. Why?





Aucun commentaire:

Enregistrer un commentaire