Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reflection and nested classes

 
Dennis Noren
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to write code that uses reflection to get information for nested classes without knowing their names? I'm thinking of nested classes that extend the known superclass. In general, my understanding is that a superclass doesn't "know of" its subclasses, but is this an exception to that? I don't see any of the reflection methods that lead me to believe there is a standard way to get at any nested classes.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html getDeclaredClasses()
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html getClasses()

You're right, a superclass never knows about its zero or many subclasses (one of thousands of reasons why a non-final class is an implicit design flaw). You could write an agent using java.lang.instrumentation to maintain a registry of those classes that you're interested in.
[ August 04, 2005: Message edited by: Tony Morris ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic