jeudi 15 septembre 2016

Can I create a parameterized annonymous class using a Class object as parameter in Java?

I know the answer is no, but how can I achieve something similar?

public class MyClass
{
    HashMap<Object, Delegate> map = new HashMap<>();

    public <T extends ArgClass> void register(Object key, T args) throws IllegalAccessException
    {
        IFunctionalInterface<T> aux = new IFunctionalInterface<T>()
        {
            @Override
            public void run(Object obj, T args)
            {
                // TODO Auto-generated method stub
            }
        };

        Method m = aux.getClass().getDeclaredMethods()[0];

        //The Delegate class is defined somewhere else
        Delegate d = new Delegate(m);

        map.put(key, d);
    }
}

How can I change register's signature to void register(Object obj, Class cls) and still mantain the restriction of T extends ArgClass and how would I get the IFunctionalInterface.run appropriate for an object of type cls?





Aucun commentaire:

Enregistrer un commentaire