// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class GenFun {
    public static interface MyInterface<T> {
        public T giveMe();
    }
    public static class MyClass<T> implements MyInterface<T> {
        public T giveMe() {
            return null; // not easy to create a T but that is another problem
        }
    }
    public static class MyFactory<T>
    {
         private final Constructor<? extends MyClass<T>> ctor;
         public MyFactory(Class<? extends MyClass<T>> clz) throws NoSuchMethodException, SecurityException
         {
                ctor = clz.getConstructor();
         }
         public MyInterface<T> create() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
             return ctor.newInstance(new Object[0]);
         }
    }
    public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        @SuppressWarnings("unchecked")
        MyFactory<String> mf = new MyFactory<String>((Class<? extends MyClass<String>>) MyClass.class);
        MyInterface<String> mi = mf.create();
        String s = mi.giveMe();
        System.out.println(s == null);
    }
}
