RFR: JDK-8062359: javac Attr crashes with NPE in TypeAnnotationsValidator visitNewClass

Jan Lahoda jan.lahoda at oracle.com
Thu Nov 20 17:16:07 UTC 2014


Thanks Michael for the reproducible case.

The tree.clazz.type null check seems reasonable as a short(er) term fix. 
I wonder if we need the tree.clazz null check? I am not sure under which 
circumstances tree.clazz could be null.

Thanks,
     Jan

On 19.11.2014 17:13, Joel Borggrén-Franck wrote:
> Hi,
>
> Thanks for the reproducer Michael. Jan minimized it and we now have both a test and a fix.
>
> Problem is when an anonymous inner class extends an unresolvable class an anonymous inner class inside it won’t get attributed. Long term we might fix this by trying harder to attribute the innermost class but a short term fix is to properly check that tree.clazz and tree.clazz.type aren’t null.
>
> Patch with test below and attached.
>
> cheers
> /Joel
>
> diff -r f62d01419621 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	Wed Nov 19 13:46:04 2014 +0100
> +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Wed Nov 19 17:01:57 2014 +0100
> @@ -4486,14 +4486,15 @@
>               super.visitTypeTest(tree);
>           }
>           public void visitNewClass(JCNewClass tree) {
> -            if (tree.clazz.hasTag(ANNOTATED_TYPE)) {
> -                checkForDeclarationAnnotations(((JCAnnotatedType) tree.clazz).annotations,
> -                        tree.clazz.type.tsym);
> -            }
> -            if (tree.def != null) {
> -                checkForDeclarationAnnotations(tree.def.mods.annotations, tree.clazz.type.tsym);
> -            }
> -            if (tree.clazz.type != null) {
> +            if (tree.clazz != null && tree.clazz.type != null) {
> +                if (tree.clazz.hasTag(ANNOTATED_TYPE)) {
> +                    checkForDeclarationAnnotations(((JCAnnotatedType) tree.clazz).annotations,
> +                            tree.clazz.type.tsym);
> +                }
> +                if (tree.def != null) {
> +                    checkForDeclarationAnnotations(tree.def.mods.annotations, tree.clazz.type.tsym);
> +                }
> +
>                   validateAnnotatedType(tree.clazz, tree.clazz.type);
>               }
>               super.visitNewClass(tree);
> diff -r f62d01419621 test/tools/javac/8062359/UnresolvableClassNPEInAttrTest.java
> --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
> +++ b/test/tools/javac/8062359/UnresolvableClassNPEInAttrTest.java	Wed Nov 19 17:01:57 2014 +0100
> @@ -0,0 +1,17 @@
> +/*
> + * @test /nodynamiccopyright/
> + * @bug 8062359
> + * @summary NullPointerException in Attr when type-annotating an anonymous
> + *          inner class in an unresolvable class
> + * @compile/fail/ref=UnresolvableClassNPEInAttrTest.out -XDrawDiagnostics UnresolvableClassNPEInAttrTest.java
> + */
> +
> +public class UnresolvableClassNPEInAttrTest {
> +    public static void main(String[] args) {
> +        new Undefined() {
> +            void test() {
> +                new Object() {};
> +            }
> +        };
> +    }
> +}
> diff -r f62d01419621 test/tools/javac/8062359/UnresolvableClassNPEInAttrTest.out
> --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
> +++ b/test/tools/javac/8062359/UnresolvableClassNPEInAttrTest.out	Wed Nov 19 17:01:57 2014 +0100
> @@ -0,0 +1,2 @@
> +UnresolvableClassNPEInAttrTest.java:11:13: compiler.err.cant.resolve.location: kindname.class, Undefined, , , (compiler.misc.location: kindname.class, UnresolvableClassNPEInAttrTest, null)
> +1 error
>
>
>
>
>
>> On 18 Nov 2014, at 11:41, Joel Borggrén-Franck <joel.franck at oracle.com> wrote:
>>
>> Hi,
>>
>> Here is a proposed fix for [1]: javac Attr crashes with NPE in TypeAnnotationsValidator visitNewClass
>>
>> This is for 9 but I will propose an identical backport to 8u as well. Patch is very small:
>>
>> [1] https://bugs.openjdk.java.net/browse/JDK-8062359
>
>
>


More information about the compiler-dev mailing list