JDK-8056970 : Type annotation on Lambda in initializer block
  • Type: Sub-task
  • Component: tools
  • Sub-Component: javac
  • Priority: P3
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2014-08-31
  • Updated: 2016-07-28
Description
Take the following code:

import java.lang.annotation.*;

interface Consumer {
    void method(String s);
}

class LambdaInInitBlock {
    {
        Consumer fn1 = (@TA String i) -> {};
    }
}

@Target(ElementType.TYPE_USE)
@interface TA {}


This works correctly if the lambda is in a method/constructor. But with the lambda in the initializer block, javac 1.8 gives:

An exception has occurred in the compiler (1.8.0_05). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport)  after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report.  Thank you.
java.lang.NullPointerException
	at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.separateAnnotationsKinds(TypeAnnotations.java:351)
	at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.visitLambda(TypeAnnotations.java:1132)
	at com.sun.tools.javac.tree.JCTree$JCLambda.accept(JCTree.java:1618)
	at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
	at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.scan(TypeAnnotations.java:275)
	at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.visitVarDef(TypeAnnotations.java:1196)
	at com.sun.tools.javac.tree.JCTree$JCVariableDecl.accept(JCTree.java:846)
	at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
	at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.scan(TypeAnnotations.java:275)
	at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:57)
	at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.visitBlock(TypeAnnotations.java:1206)
	at com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:903)
	at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
	at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.scan(TypeAnnotations.java:275)
	at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:57)
	at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.visitClassDef(TypeAnnotations.java:1042)
	at com.sun.tools.javac.tree.JCTree$JCClassDecl.accept(JCTree.java:687)
	at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
	at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.scan(TypeAnnotations.java:275)
	at com.sun.tools.javac.code.TypeAnnotations.organizeTypeAnnotationsBodies(TypeAnnotations.java:155)
	at com.sun.tools.javac.comp.Attr.attribClassBody(Attr.java:4335)
	at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4215)
	at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4149)
	at com.sun.tools.javac.comp.Attr.attrib(Attr.java:4124)
	at com.sun.tools.javac.main.JavaCompiler.attribute(JavaCompiler.java:1251)
	at com.sun.tools.javac.main.JavaCompiler.compile2(JavaCompiler.java:904)
	at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:863)
	at com.sun.tools.javac.main.Main.compile(Main.java:523)
	at com.sun.tools.javac.main.Main.compile(Main.java:381)
	at com.sun.tools.javac.main.Main.compile(Main.java:370)
	at com.sun.tools.javac.main.Main.compile(Main.java:361)
	at com.sun.tools.javac.Main.compile(Main.java:56)
	at com.sun.tools.javac.Main.main(Main.java:42)

The error occurs in both 1.8.0_05 and 1.8.0_20.