Is it possible to dynamically grab all the classes in a particular package (within the current project, not in a JAR file or elsewhere), attempt to create new instances of them (assume they have a no-argument constructor), and store all these instances in an array for later processing? Assume that they are all derived from a common base class other than Object.
For example, here's a possible class tree:
package main:
* Main.java
package data:
* DataOne.java
* DataTwo.java
* DataThree.java
package list:
* DataBaseClass.java
* DataList.java
The DataList class needs to contain an array of new instances of all the classes in the data package, and should update itself if classes are added/removed. Right now, I handle this the old-fashioned way, in DataList:
What I'd like to do is make that arr variable have automatically generated contents. In this particular case, DataBaseClass serves as the common base class for DataOne, DataTwo, and DataThree.
For example, here's a possible class tree:
package main:
* Main.java
package data:
* DataOne.java
* DataTwo.java
* DataThree.java
package list:
* DataBaseClass.java
* DataList.java
The DataList class needs to contain an array of new instances of all the classes in the data package, and should update itself if classes are added/removed. Right now, I handle this the old-fashioned way, in DataList:
Code:
DataBaseClass[] arr = { new DataOne(), new DataTwo(), new DataThree() };