Java cooperating generic classes : I have two abstract generic classes. They cooperate and hence depend on each other. Occasionally one needs to pass `this` to the other. I am trying to find a type safe way to do this.

    public abstract class AbstractA<T extends AbstractB<? extends AbstractA<T>>> {
    	
    	void foo(T aB) {
    		aB.bar(this);
    	}
    	
    }
    
    public abstract class AbstractB<T extends AbstractA<? extends AbstractB<T>>> {
    	
    	public void bar(T theA) {
    		// ... theA ...
    	}
    	
    }

I get a compiler error message at the call to `aB.bar()`: The method bar(capture#1-of ? extends AbstractA&lt;T&gt;) in the type AbstractB&lt;capture#1-of ? extends AbstractA&lt;T&gt;&gt; is not applicable for the arguments (AbstractA&lt;T&gt;).

I was thinking my problem may be related to the one discussed in http://stackoverflow.com/questions/8021900/java-generics-compilation-error-the-method-methodclasscapture1-of-extends. Inspired from that thread I coded a workaround in `AbstractB`:

    @SuppressWarnings("unchecked")
    // Parameter inexactly declared just AbstractA<? extends AbstractB<?>>
    // because the compiler didn’t accept my call to setA(T theA)
    public void setA(AbstractA<? extends AbstractB<?>> theA) {
    	// Unchecked cast from AbstractA<capture#1-of ? extends AbstractB<?>> to T
    	// ... (T) theA ...
    }

It works, but it’s not so nice with the inexact parameter type and the unchecked cast. In that other thread I liked the reply by Tom Hawtin - tackling, but I have not found a way to apply it to my situation. Can anyone think of clean solution?

(`class AbstractA<T extends AbstractB<? extends AbstractA<T>>>` looks a bit complicated; I think I need it for concrete subclasses to know each other’s exact types.)