Skip to content Skip to sidebar Skip to footer

A Way Of Saying "for All Subclasses In My Project That Extend This Superclass"?

Here's something that's got me a bit stumped but intrigued all the same. In my Android game I have various Levels that extend the superclass Level. What I am trying to do is build

Solution 1:

  1. The question itself is wrong. You cannot loop over List ("Every Level Subclass In My Project") and get instances of Level. l should be Class.

  2. From the context, I think you meant "every instance of every Level subclass". No, it is not possible - a virtual machine is not and should not be a database. You cannot just query for objects, you have to manage references in your code (but that you already knew that - your constructor solution will work).

Solution 2:

Option 1:

Lately I had to solve a similar problem within JavaSE. I'm using the Google Reflections Library for that:

http://code.google.com/p/reflections/

However I'm not sure if it can run with Android. I think it's worth to give it a try, since it's quite easy to use. In your case you would do something like:

Reflections reflections = new Reflections("my.project.prefix");
Set<Class<? extends Level>> subTypes = reflections.getSubTypesOf(Level.class);

That would give you a Set (subTypes) to iterate on and put it in the HashMap.

Option 2:

You could maybe use custom annotations to annotate your Level classes, for example:

@LevelpublicclassMyCustomLevel {}

Then use a custom annotation processor which implements AbstractProcessor to process the annotation at compile time. Implement the process method to find all classes annotated with your @Level annotation. Now you can write the full names of the found classes to a property file in your META-INF dir. From your application you can read this property file and instantiate the classes using reflection.

Solution 3:

If you're trying to dynamically fetch the list of all classes that extend Level at runtime, that's not really possible, I'm afraid. Have a look at this thread: How do you find all subclasses of a given class in Java?

Solution 4:

I think you might want to make the level an interface and then check if it's an interface.

In its most common form, an interface is a group of related methods with empty bodies. A bicycle's behavior, if specified as an interface, might appear as follows:

interfaceBicycle {

       voidchangeCadence(int newValue);   // wheel revolutions per minutevoidchangeGear(int newValue);

       voidspeedUp(int increment);

       voidapplyBrakes(int decrement);
}

To implement this interface, the name of your class would change (to a particular brand of bicycle, for example, such as ACMEBicycle), and you'd use the implements keyword in the class declaration:

classACMEBicycleimplementsBicycle {

   // remainder of this class implemented as before

}

Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

Solution 5:

I think standard way in the "spirit" of java is the service provider pattern.

Add a declaration file in the META-INF/services of the "plugin" jar and use java.util.ServiceLoader (http://developer.android.com/reference/java/util/ServiceLoader.html) to enumerate your providers.

Post a Comment for "A Way Of Saying "for All Subclasses In My Project That Extend This Superclass"?"