I defined a Java interface to represent the ability to copy an object (no, I do not want to use Cloneable , but thanks for the suggestion ;-). It is common:
public interface Copiable<T> { T copy(); }
An object will only make copies of its type:
public class Foo implements Copiable<Foo> { Foo copy() { return new Foo(); } }
Now I am reading Class<?> Objects from a non-shared API (Hibernate metadata if anyone is interested), and if they are Copiable , I want to register them using one of my components. The register method has the following signature:
<T extends Copiable<T>> register(Class<T> clazz);
My problem is, how do I pass a class before passing it to a method? What I am doing now is:
Class<?> candidateClass = ... if (Copiable.class.isAssignableFrom(candidateClass)) { register(candidateClass.asSubclass(Copiable.class)); }
This does what I want, but with a compiler warning. And I donβt think I can express a recursive binding at runtime.
Is there any way to redo my code so that it is visible?
thanks
source share