[PATCH] 8133135: NPE on anonymous class defined by qualified instance creation expression with diamond

bsrbnd bsrbnd at gmail.com
Mon Aug 31 13:40:55 UTC 2015


Hi,

As explained in issue 8133135, the following code produces a null
pointer exception because the symbol of the anonymous class is not set
during the attribute phase:

class List<T> {}

class ClassOut {
    class ClassIn<T> { public ClassIn() {}}
}

public class TestBug {
    public static <T> List<T> m(List<T> list, T item) {return list;}


    public static void main(String[] args) {
        m(new List<ClassOut.ClassIn<String>>(), new ClassOut().new
ClassIn<>(){});
    }
}

Method Attr.visitAnonymousClassDefinition() doesn't perform well type
inference with diamond. Recurcivity condition on tree.clazz.type seems
to be wrong because it never changes... instead, it should be made on
clazz.type (or clazztype) to go deeper on the type resolution.

The following patch shows a possible solution, I think (to be checked
by someone who knows the code better than me...):

diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
@@ -2173,7 +2173,7 @@
             final boolean isDiamond = TreeInfo.isDiamond(tree);
             if (isDiamond
                     && ((tree.constructorType != null &&
inferenceContext.free(tree.constructorType))
-                    || (tree.clazz.type != null &&
inferenceContext.free(tree.clazz.type)))) {
+                    || (clazz.type != null &&
inferenceContext.free(clazz.type)))) {
                 final ResultInfo resultInfoForClassDefinition =
this.resultInfo;

inferenceContext.addFreeTypeListener(List.of(tree.constructorType,
tree.clazz.type),
                         instantiatedContext -> {

Regards,

bsrbnd


More information about the compiler-dev mailing list