reflection

  • Thread starter marcin.strugala
  • Start date
M

marcin.strugala

Hello,
could you please give me some hint how to retreive information about
inheritance of class? Is it possible to use reflection to this
purpose? I would like to collect all information about classes i.e.
all class' fields and its base class (if the one exists). I know how
to discover information about class' fields using reflection but I
have no idea how to use this mechanism to collect information about
class' inheritance.

Thanks in advance
 
A

Arne Vajhøj

could you please give me some hint how to retreive information about
inheritance of class? Is it possible to use reflection to this
purpose? I would like to collect all information about classes i.e.
all class' fields and its base class (if the one exists). I know how
to discover information about class' fields using reflection but I
have no idea how to use this mechanism to collect information about
class' inheritance.

I have never used it myself, but Class has a getSuperclass method - it
sound as if it will do it.

Arne
 
M

Mike Schilling

Hello,
could you please give me some hint how to retreive information about
inheritance of class? Is it possible to use reflection to this
purpose? I would like to collect all information about classes i.e.
all class' fields and its base class (if the one exists). I know how
to discover information about class' fields using reflection but I
have no idea how to use this mechanism to collect information about
class' inheritance.

Start with a java.lang.Class object, obtained either from an instance

int.getClass()

or a name

Class.forName(name);

On the Class you'll find methods to get the superclass, the interfaces
implemented, the fields, the methods, etc. The classes that represent the
other objects used in reflection (Method, Field, etc.) are found in
java.lang.reflect. Read the documentation carefully to see exactly what
each method returns, for instance

Class.getFields()
-- all *public* methods, including those inherited from superclasses
Class.getDeclaredFields()
-- all methods declared *by this class*

The facility is quite powerful, and, except for method bytecode, gives you
almost everything you'd get from opening up the classfile and reading it
yourself. (The one thing I've wanted but couldn't get is whether the field
is a compile-time constant or not. This information is in the classfile,
but AFAICT not made available via reflection.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top