jeudi 10 juin 2021

How to instantiate objects and passing it to other constructors in a loop

That's a question about logic, to be honest, but i can't think in a answer to it. Basically, i have this code that loads decorators and store the Class in decoratorConstructors:

                Collection<String> decorators = (Collection<String>) (((javax.swing.JButton)e.getSource()).getClientProperty( "selectedDecoratorsNames"));
                ArrayList<Constructor> decoratorConstructors = new ArrayList();
                String interfaceClassName = "interfaces.PizzaComponent";
                Class interfaceClass = null;
                Method interfaceMethodPrepare = null;
                
                for(String decorator: decorators) {
                    Constructor decoratorConstructor;
                    Class metaFactory = null;
                    String decoratorName = decoratorNames.get(decorator);
                    String className = decoratorName + "." + decorator;
                    
                    interfaceClass = Class.forName(interfaceClassName, true, sysLoader);
                    metaFactory = Class.forName(className, true, sysLoader);
                    interfaceMethodPrepare = interfaceClass.getDeclaredMethod("preparar");
                    
                    decoratorConstructors.add(metaFactory.getConstructor(interfaceClass));
                }
                
                Object interfaceObject = interfaceClass;
                
                System.out.println(decoratorConstructors);
                
                Object pizza = interfaceClass.cast(decoratorConstructors.get(2).newInstance(
                        decoratorConstructors.get(1).newInstance(
                                decoratorConstructors.get(0).newInstance(
                                        new PizzaSimples()
                                )       
                        )
                ));

                
                interfaceMethodPrepare.invoke(pizza);

How could i run this piece of code on a loop:

                Object pizza = interfaceClass.cast(decoratorConstructors.get(2).newInstance(
                        decoratorConstructors.get(1).newInstance(
                                decoratorConstructors.get(0).newInstance(
                                        new PizzaSimples()
                                )       
                        )
                ));




Aucun commentaire:

Enregistrer un commentaire