This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reflection and nested classes  RSS feed

 
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!