Generics Problem - Cannot compile code, possibly due to type erasure
I have some code similar to this:
public class A<E> {
protected E value;
public E getValue() {
return value;
}
public void setValue(E value) {
this.value = value;
}
}
Then I have a class to extend this class, called B
public class B extends A<Boolean> {
// B has some other code, but doesn't edit the set/get methods
}
Then I compile these two classes to A.class, B.class and they're JARed.
Then I have the following code:
public class C {
// ... snip
B var = new B();
var.setValue(true);
if(var.getValue()) {
// etc
}
// ... snip
}
Attempting to compile this results in an error telling me that var.getValue returns as an Object. I KNOW it's a boolean, and when I include that C with the A/B java files and compile all at once, it works fine.
But when I compile A/B separately to C I get the issue that it believes var.getValue is an Object.
I've been reading about generics and Type Erasure, but I couldn't figure a way to solve it.
I'm trying to build A/B into a package that classes like C use, but if I have to cast all getValues then there is no point in generics.
Where am I going wrong?
(The actual code is in this github repo and the offending classes that are A and B are ModOption/ModBooleanOption. I don't have the C code in there, but it's obvious)
Edit: I'm using JavaSE6 compiling and obfuscating. The weird thing is that if I compile regularly and then try this example is also works fine. I'm beginning to suspect the obfuscator.
This problem is solved; I tested further and discovered that the obfuscator is breaking the generics, I have no idea how and don't care why. I'll be contacting the mai开发者_Python百科ntainers of it.
I was able to reproduce this only by setting my compiler to javac -source 1.4
while compiling C. If you're using an IDE, check the project that contains C to make sure your Java VM and source support is at 1.5 or above.
精彩评论