There is a trick to make inter-class circular reference within Java generics:
public static class GenA<A extends GenA<A, B>, B extends GenB<A, B>> {
public B objB;
}
public static class GenB<A extends GenA<A, B>, B extends GenB<A, B>> {
public A objA;
}
When I try to instantiate real object:
GenA<GenA, GenB> genA = new GenA<>();
I get compilation errors:
CircularRef.java:7: error: type argument GenA is not within bounds of type-variable A
GenA<GenA, GenB> genA = new GenA<>();
where A,B are type-variables:
A extends GenA<A,B> declared in class GenA
B extends GenB<A,B> declared in class GenA
CircularRef.java:7: warning: [rawtypes] found raw type: GenA
GenA<GenA, GenB> genA = new GenA<>();
missing type arguments for generic class GenA<A,B>
where A,B are type-variables:
A extends GenA<A,B> declared in class GenA
B extends GenB<A,B> declared in class GenA
CircularRef.java:7: warning: [rawtypes] found raw type: GenB
GenA<GenA, GenB> genA = new GenA<>();
missing type arguments for generic class GenB<A,B>
where A,B are type-variables:
A extends GenA<A,B> declared in class GenB
B extends GenB<A,B> declared in class GenB
CircularRef.java:7: warning: [unchecked] unchecked method invocation: constructor <init> in class GenA is applied to given types
GenA<GenA, GenB> genA = new GenA<>();
required: no arguments
found: no arguments
CircularRef.java:7: warning: [unchecked] unchecked conversion
GenA<GenA, GenB> genA = new GenA<>();
required: GenA<GenA,GenB>
found: GenA
I know workaround:
public static class AdapterA extends GenA<AdapterA, AdapterB> { }
public static class AdapterB extends GenB<AdapterA, AdapterB> { }
which works like:
AdapterA adA = new AdapterA();
AdapterB adB = new AdapterB();
adA.objB = adB;
adB.objA = adA;
How can I instantiate GenA
/ GenB
classes directly?
They are not abstract
. I like -Xlint:all
says no warning too.
EXTRA I wrote question Defining typed hierarchy with Java generics and type self-referencing which contains above problem hidden in a lot of code and bored users like to close it ))
Some hints about declaring generic circular referencing declaration are at:
- Creating circular generic references
- Cyclic generic dependenices
- How to get "circular" generics working in Java?
UPDATE With adding extra reference:
public static class GenA<A extends GenA<A, B, C>, B extends GenB<A, B, C>, C extends GenC<A, B, C>> {
public B objB;
public C objC;
}
public static class GenB<A extends GenA<A, B, C>, B extends GenB<A, B, C>, C extends GenC<A, B, C>> {
public A objA;
public C objC;
}
public static class GenC<A extends GenA<A, B, C>, B extends GenB<A, B, C>, C extends GenC<A, B, C>> {
public A objA;
public B objB;
}
problem the same for:
GenA<GenA, GenB, GenC> genA = new GenA<>();
and can be resolved as usual:
public static class AdapterA extends GenA<AdapterA, AdapterB, AdapterC> { }
public static class AdapterB extends GenB<AdapterA, AdapterB, AdapterC> { }
public static class AdapterC extends GenC<AdapterA, AdapterB, AdapterC> { }
UPDATE 2 The problem is not with instantiation but with declaration of type, following fails:
GenA<GenA, GenB> genA;