jeudi 17 septembre 2015

Invoking method on all ArrayList

Instead of using foreach externally each time I want to invoke methods on a arraylist I want to be able to call

ArrayList<String>.setText(); //Example.

I attempted this using reflection but I'm not sure how to implement it;

public class Array extends ArrayList
{
    public Array(Collection collection) {
        super(collection);
    }

    public void invokeMethod(String nameOfMethod, Object object)
    {
        for(int index = 0; index < size(); index++){
            get(index).getClass().getMethod(nameOfMethod, object.getClass());
        }

        //Example of a single object invocation.
        try {
            method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..);
        } catch (SecurityException e) {
            // ...
        } catch (NoSuchMethodException e) {
            // ...
        }
    }
}

Does anyone know how to implement this?





Aucun commentaire:

Enregistrer un commentaire