0

Let's assume following setup:

trait Fruit{
def name:String
}

trait Fruit2 extends Fruit{
def name:String
}

case class Apple(name:String) extends Fruit
case class Pear(name:String) extends Fruit
case class Dragon-fruit(name:String) extends Fruit2

trait Color
case class ColoredApple[C](name String) extends Fruit

case class Red extends Color
val RedApple = ColoredApple[Red]("red apple")
val ...

First, is there any way instead of reflection to get all Fruit classes? How can I get a list of these classes and variables (e.g. all ColoredApple with Red color) using/not-using reflections? something like this:

val allRedApples = ???
Omid
  • 1,959
  • 25
  • 42
  • Can you make `Fruit` sealed? – Daenyth Apr 20 '15 at 18:53
  • spoiler - if you can't - then it's [impossible](http://stackoverflow.com/questions/1087401/is-it-possible-to-get-all-the-subclasses-of-a-class); if you can, then it's possible to do with a macro http://stackoverflow.com/questions/13671734/iteration-over-a-sealed-trait-in-scala – dk14 Apr 20 '15 at 18:56
  • @Daenyth No, (but would be happy to know what will happen if it is sealed) – Omid Apr 20 '15 at 18:56
  • Are you trying to get this list at runtime or at code editing time? What problem are you trying to solve? – Daenyth Apr 20 '15 at 18:56
  • @dk14 Macro makes me sad:( – Omid Apr 20 '15 at 18:57
  • @Daenyth code editing time – Omid Apr 20 '15 at 18:58
  • @Omid unfortunatelly, if your subclasses can be placed in several modules it's not possible to do even for Java (at least in handy way) AFAIK – dk14 Apr 20 '15 at 18:58
  • 1
    If you need it at code edit, you can do ctrl+t in eclipse or ctrl+h in intellij. This should show the class hierarchy (including all extending classes). P.S. I don't have IDEA installed so that is only as far as I rememember. – Denis Rosca Apr 20 '15 at 19:06
  • Sorry guys, I confused you by saying in code editing time! I need it in run-time but I want to have access to type in editing time. I edited my question – Omid Apr 20 '15 at 19:08
  • Do you want a list of subtypes or a list of instances? From where? The current thread? All threads? Resolving when? It looks like you're going down a road to a terrible codebase, _what real problem are you trying to solve_? – Daenyth Apr 20 '15 at 19:13
  • I'm trying to write a type matcher. It's for my research project, I need to find all classes with different types to match which can run with which to come up with different execution paths – Omid Apr 20 '15 at 19:23
  • That sounds an awful lot like the halting problem. – Daenyth Apr 21 '15 at 14:55

1 Answers1

1

It's not possible to do it without reflection if you can't make your class sealed

It's also not possible to get complete and accurate results.

It looks like there's a java library called reflections which offers a way:

Reflections reflections = new Reflections("my.project");

Set<Class<? extends SomeType>> subTypes = reflections.getSubTypesOf(SomeType.class);
Community
  • 1
  • 1
Daenyth
  • 35,856
  • 13
  • 85
  • 124