Creating Objects with dynamic Subclasses using a String Array

Makozer :

im actually working on a Project for my University.

In a case, the user is able to produce new Objects and my problem is, that ALL needed Objects are from a SubClass of ASpaceShip.

The Question is now how to create dynamic objects with a array of String Inputs from the User.

One of the Problems is, that there will be more SubClasses in the Future and a switch for 50+ subclasses ... naaaa

Example:

class SpaceShipOne extends ASpaceShip
class SpaceShipTwo extends ASpaceShip
class SpaceShipSpecial extends ASpaceShip
class SpaceShipN extends ASpaceShip

Input from User, Stringarray

input[0] = "SpaceShipTwo";
input[1] = "SpaceShipSpecial";
input[n] = "AlreadyExistingClassName";

Needed:

ArrayList<ASpaceShip> shipList; // Containts all Objects from the User input.

I would love to just loop the userinput and

ASpaceShip ship = new (input[0])(); // casting the class with the String name 

But this sadly doesnt work ... Inet isnt giving much help on this topic or stuff that could work but is deprecated :(

Some ideas here?


Code of Servlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    // Required Objects
    HttpSession session = request.getSession(); 
    Player player = (Player)session.getAttribute("player");     
    TechTree techtree = player.getTechTree();                   // Ships need their TechTree
    ArrayList<ASpaceShip> ships = new ArrayList<ASpaceShip>();  // to save Ships which are build
    ArrayList<ASpaceShip> allResearchedShips = techtree.getAllResearchedShips();    // to save Ships which are build

    Enumeration<String> parameterNames = request.getParameterNames(); 
    while (parameterNames.hasMoreElements()) { 
        String shipName = parameterNames.nextElement();
        String[] paramSValue = request.getParameterValues(shipName);
        int pVal = Integer.parseInt(paramSValue[0]);
        if (hasShip(allResearchedShips, shipName)) {
            if (pVal > 0) {
                //ASpaceShip newShip = new Class<shipName>(techtree,1);
            }
        }

    }            

} // End doPost
nullPointer :

If I understand well your question, you would simply need to have a switch / case statement or an if etc, checking user input string and constructing the appropriate object using the proper subclass. Then all constructed object could be added to an ArrayList<ASpaceShip> , eg holding all objects that extend ASpaceShip

ASpaceShip a = null;
ArrayList<ASpaceShip> myList = new ArrayList<>();
for (String s : inputArray) {
     if ("SpaceShipOne".equals(s))  a = new SpaceShipOne();
     if ("SpaceShipTwo".equals(s))  a = new SpaceShipTwo();
     ...
     myList.add(a);
}

UPDATE:

Using reflection :

ASpaceShip a = null;
ArrayList<ASpaceShip> myList = new ArrayList<>();
String[] array = {"SpaceShipTwo","SpaceShipSpecial"};
for (String className : array) {
    Class aClass = Class.forName("yourPackagePath" + className); // need to pass full class name here
    a = (ASpaceShip)aClass.newInstance();
    myList.add(a);
}
for (ASpaceShip as : myList) System.out.println(as.getClass().getSimpleName());

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=172105&siteId=1