Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

how discover subclasses of a class?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can you enumerate the subclasses of a known class or Class object?
 
author & internet detective
Posts: 40551
825
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Randy,
Brute force. You would have to loop through all classes using reflection and see if they were subclasses of the one you were interested in.
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or use the dreaded "instanceof"

if (a instanceof b)

Mark
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Jeanne]: You would have to loop through all classes using reflection

...which in turn raises the question, how do you get all classes? The only way I know is to get the classpath (System.getProperty("java.class.path")), separate it using File.pathSeparator, and then search each part of the classpath for valid class files. (Which may be single .class files, or .class files inside a .jar file.) Even then, at best you can check all classes available in the classpath - but it's always possible to define a new class dynamically with a ClassLoader. So you're never guaranteed of finding all subclasses, but you can find all subclasses which are in the classpath.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm curious about the motivation here.

I might do this as part of a static analysis tool. Eclipse can display the hierarchy tree up and down given any class. But I can't imagine doing it as a function of a running application. A superclass that needs to know its subclasses sounds suspect.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess Eclipse goes down from subclasses and just stores the catalogue somewhere.
reply
    Bookmark Topic Watch Topic
  • New Topic