Proxy.getProxyClass
accepts a varargs of interfaces. Just build up an array of classes to pass to it. If you're not sure at compile time of which or how many interfaces to pass, then you might store them into a list and then invoke toArray(T[])
to convert the list to an array
List<Class> interfaces = new LinkedList<Class>();
interfaces.add(Animal.class);
if (cage.animal instanceof Run) {
interfaces.add(Run.class);
}
if (cage.animal instanceof Fly) {
interfaces.add(Fly.class);
}
Proxy.getProxyClass(cl, interfaces.toArray(new Class[interfaces.size()]));