changeset 3094:3705299024dc

Quick fix for a NPE created for lambda expressions within fields. Added two tests, one of which fails.
author wmdietl
date Thu, 05 Dec 2013 20:46:38 -0500
parents 4d6bcc50a30a
children de9c3cdd5999
files src/share/classes/com/sun/tools/javac/code/TypeAnnotations.java test/tools/javac/annotations/typeAnnotations/newlocations/Lambda.java test/tools/javac/annotations/typeAnnotations/referenceinfos/Lambda.java
diffstat 3 files changed, 37 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/code/TypeAnnotations.java	Thu Dec 05 16:51:09 2013 -0500
+++ b/src/share/classes/com/sun/tools/javac/code/TypeAnnotations.java	Thu Dec 05 20:46:38 2013 -0500
@@ -346,20 +346,28 @@
                     sym.owner.type.asMethodType().recvtype = type;
                     // note that the typeAnnotations will also be added to the owner below.
                 } else {
-                    MethodType methType = sym.owner.type.asMethodType();
-                    List<VarSymbol> params = ((MethodSymbol)sym.owner).params;
-                    List<Type> oldArgs = methType.argtypes;
-                    ListBuffer<Type> newArgs = new ListBuffer<Type>();
-                    while (params.nonEmpty()) {
-                        if (params.head == sym) {
-                            newArgs.add(type);
-                        } else {
-                            newArgs.add(oldArgs.head);
+                    // TODO: don't do anything if sym is a lambda parameter.
+                    // How can we check for that?
+                    if (sym.owner.type != null) {
+                        MethodType methType = sym.owner.type.asMethodType();
+                        List<VarSymbol> params = ((MethodSymbol)sym.owner).params;
+                        List<Type> oldArgs = methType.argtypes;
+                        ListBuffer<Type> newArgs = new ListBuffer<Type>();
+                        boolean found = false;
+                        while (params.nonEmpty()) {
+                            if (params.head == sym) {
+                                newArgs.add(type);
+                                found = true;
+                            } else {
+                                newArgs.add(oldArgs.head);
+                            }
+                            oldArgs = oldArgs.tail;
+                            params = params.tail;
                         }
-                        oldArgs = oldArgs.tail;
-                        params = params.tail;
+                        if (found) {
+                            methType.argtypes = newArgs.toList();
+                        }
                     }
-                    methType.argtypes = newArgs.toList();
                 }
             } else {
                 sym.type = type;
--- a/test/tools/javac/annotations/typeAnnotations/newlocations/Lambda.java	Thu Dec 05 16:51:09 2013 -0500
+++ b/test/tools/javac/annotations/typeAnnotations/newlocations/Lambda.java	Thu Dec 05 20:46:38 2013 -0500
@@ -57,6 +57,12 @@
     LambdaInt2 getLambda() {
         return (@TA Object x, @TB Object y) -> { @TA Object l = null; System.out.println("We have: " + (@TB Object) x); };
     }
+
+    java.util.function.IntUnaryOperator x = (@TA int y) -> 1; 
+
+    java.util.function.IntUnaryOperator foo() {
+        return (@TA int y) -> 2;
+    }
 }
 
 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
--- a/test/tools/javac/annotations/typeAnnotations/referenceinfos/Lambda.java	Thu Dec 05 16:51:09 2013 -0500
+++ b/test/tools/javac/annotations/typeAnnotations/referenceinfos/Lambda.java	Thu Dec 05 20:46:38 2013 -0500
@@ -285,4 +285,15 @@
                 "  }" +
                 "}";
     }
+
+    @TADescriptions({
+        @TADescription(annotation = "TA", type = METHOD_FORMAL_PARAMETER,
+                paramIndex = 0)
+    })
+    public String lambdaField1() {
+        return
+                "class Test {" +
+                "  java.util.function.IntUnaryOperator field = (@TA int y) -> 1;" +
+                "}";
+    }
 }