File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reflection and nested classes

 
Dennis Noren
Greenhorn
Posts: 15
  • 0
  • 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
  • 0
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic