Bug in valueOf
I tried this simple test case:
package symjava.test;
public class TestValueOf {
public static class Car {
public int id;
public Car(int id) {
this.id = id;
}
public Car add(Car c) {
return new Car(this.id+c.id);
}
}
public static class Jeep extends Car {
public Jeep(int id) {
super(id);
}
public static Jeep valueOf(Car c) {
return new Jeep(c.id);
}
}
public static void main(String[] args) {
Car c1 = new Car(1);
Car c2 = new Car(10);
Car c12 = c1 + c2;
//Jeep j = c12; //works
//Jeep j = Jeep.valueOf(c1 + c2); //works
Jeep j = c1 + c2; //NOT work!!!
System.out.println(j.id);
}
}
An exception is thrown out:
Exception in thread "main" java.lang.VerifyError: Operand stack underflow Exception Details: Location: symjava/test/TestValueOf.main([Ljava/lang/String;)V @25: invokestatic Reason: Attempt to pop empty stack. Current Frame: bci: @25 flags: { } locals: { '[Ljava/lang/String;', 'symjava/test/TestValueOf$Car', 'symjava/test/TestValueOf$Car', 'symjava/test/TestValueOf$Car' } stack: { } Bytecode: 0000000: bb00 1059 04b7 0012 4cbb 0010 5910 0ab7 0000010: 0012 4d2b 2cb6 0015 4eb8 0019 3a04 b200 0000020: 1f19 04b4 0025 b600 29b1
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2615)
at java.lang.Class.getMethod0(Class.java:2856)
at java.lang.Class.getMethod(Class.java:1668)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
I can compile and run your example with both, IntelliJ and javac. Are you using Eclipse? Is this bug already fixed?
Another Workaround for this problem is, to simply add a cast. (Jeep j = (Car)(c1 + c2)). This works for some reason.