Consider the following two Java classes:
a.) class Test { void foo(Object foobar) { } }
b.) class Test { void foo(pkg.not.in.classpath.FooBar foobar) { } }
Can you give any example for which it would be technically impossible to generate a valid Java class file without performing compile time dependency checking?
Consider this code:
public class GotDeps {
public static void main(String[] args) {
int i = 1;
Dep.foo(i);
}
}
If the target method has the signature public static void foo(int n), then these instructions will be generated:
public static void main(java.lang.String[]);
Code:
0: iconst_1
1: istore_1
2: iload_1
3: invokestatic #16; //Method Dep.foo:(I)V
6: return
If the target method has the signature public static void foo(long n), then the int will be promoted to a long prior to the method invocation:
public static void main(java.lang.String[]);
Code:
0: iconst_1
1: istore_1
2: iload_1
3: i2l
4: invokestatic #16; //Method Dep.foo:(J)V
7: return
In this case, it would not be possible to generate the invocation instructions or how to populate the CONSTANT_Methodref_info structure referred to in the class constant pool by the number 16. See the class file format in the VM spec for more details.