Generic type information for classes within closures not emitted by javac 11.0.15
Dawid Weiss
dawid.weiss at gmail.com
Wed Jun 8 20:08:14 UTC 2022
Good evening,
I've spent an interesting day debugging a very awkward error in Google
Guice: a binding would work in JDK17 but would result in an odd (and
misleading) error information under JDK11. This turns out to be a problem
with generic signatures emitted by the corresponding compilers.
I narrowed it down to essentially something like this [full source at 1]:
Callable<Void> r =
() -> {
class Foo1 {
public Map<String, Integer> field;
}
System.out.println("Foo1.field type: " +
Foo1.class.getField("field").getGenericType());
return null;
};
r.call();
When I compile and run this snippet under java 11.0.15, I get only the
following signatures for generic types:
Foo1.field type: interface java.util.Map
if you move the inner class declaration outside of the closure, proper
generic info is stored and then loaded/ printed:
Foo2.field type: java.util.Map<java.lang.String, java.lang.Integer>
In Java 17.0.3, this compiles and works fine and I can't find any issue
where this was fixed. Is this a class bytecode constraint somewhere? My
reading of the JLS [2] would suggest the signature is mandatory, even if
emitted from within that nested class Foo1?
Any pointers would be appreciated!
Dawid
[1] https://gist.github.com/dweiss/d77452c52daa178e041adce145c2e86a
[2]
https://docs.oracle.com/javase/specs/jvms/se11/html/jvms-4.html#jvms-4.7.9.1
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20220608/5bc6e637/attachment.htm>
More information about the compiler-dev
mailing list