Can a method in an abstract class use reflection to get information about the instantiation class?

advertisements

I was wondering if a method within an abstract class could get information about the instantiating class, for example, the number of constructor methods and the types of arguments they take.

Further, I was wondering if it'd be possible to know which one of the above was called for the particular instantiation.

Thanks


I think you can.

package test;

public abstract class AbstractClass {

    public abstract String something();

    public void printConstructorsOfInstantiatingClass() {
        System.out.println(getClass().getDeclaredConstructors());
    }
}

package test;

public class ConcreteClass extends AbstractClass {
    @Override
    public String something() {
        return "concreteSomething";
    }
}

package test;

public class TestMain {
    public static void main(String[] args) {
        ConcreteClass cc = new ConcreteClass();
        cc.printConstructorsOfInstantiatingClass();
    }
}

same works with getDeclaredMethods and other methods concerning reflection.