samedi 16 mai 2015

Why does Class.getMethods not return inherited methods when used in generic method?

I am writing a generic method to determine the changes between two versions of my JPA entities. All of my entity classes such as User extend CommonEntity. In order to test reflection as a possible solution to this problem, I wrote the following non generic code.

Method[] methods = oldUser.getClass().getMethods();

for (Method method : methods) {

    logger.debug("Method Name: {}", method.getName());
    logger.debug("Method Return Type: {}", method.getReturnType());

}

This code showed all the methods from the User class and CommonEntity class. I then wrote the following generic code and passed two versions of my User entity to the method:

public static <T> String compare(T oldVersion, T newVersion) throws IllegalArgumentException,
        IllegalAccessException, InvocationTargetException {

    Method[] methods = oldVersion.getClass().getMethods();

    for (Method method : methods) {

        logger.debug("Method Name: {}", method.getName());
        logger.debug("Method Return Type: {}", method.getReturnType());

    }

    return changes;

}

For some reason, this code only shows the methods from the User class and does not show the methods from the CommonEntity class.

What is it about my generic method that breaks the getMethods() ability to find the inherited methods?





Aucun commentaire:

Enregistrer un commentaire