8138612: Do not retain declaration annotations on lambda formal parameters
authorsadayapalam
Fri, 06 Nov 2015 14:45:44 +0530
changeset 33705 96aa1b93de52
parent 33559 f242d4332f56
child 33706 0d21ecb55e6a
child 33911 af2cff5bd523
8138612: Do not retain declaration annotations on lambda formal parameters Reviewed-by: jlahoda
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java
langtools/test/tools/javac/classfiles/attributes/annotations/RuntimeParameterAnnotationsForLambdaTest.java
langtools/test/tools/javac/lambda/SE5AnnotationsOnLambdaParameters.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java	Wed Jul 05 20:59:28 2017 +0200
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java	Fri Nov 06 14:45:44 2015 +0530
@@ -1112,7 +1112,8 @@
                 acount += writeMethodParametersAttr(m);
         }
         acount += writeMemberAttrs(m);
-        acount += writeParameterAttrs(m);
+        if (!m.isLambdaMethod())
+            acount += writeParameterAttrs(m);
         endAttrs(acountIdx, acount);
     }
 
--- a/langtools/test/tools/javac/classfiles/attributes/annotations/RuntimeParameterAnnotationsForLambdaTest.java	Wed Jul 05 20:59:28 2017 +0200
+++ b/langtools/test/tools/javac/classfiles/attributes/annotations/RuntimeParameterAnnotationsForLambdaTest.java	Fri Nov 06 14:45:44 2015 +0530
@@ -23,10 +23,9 @@
 
 /*
  * @test
- * @bug 8044411 8079060
+ * @bug 8044411 8079060 8138612
  * @summary Tests the RuntimeParameterVisibleAnnotations/RuntimeParameterInvisibleAnnotations attribute.
  * @library /tools/lib /tools/javac/lib ../lib
- * @ignore 8079060 javac does not generate RuntimeParameterAnnotation attributes for lambda expressions
  * @build WorkAnnotations TestBase TestResult InMemoryFileManager ToolBox
  * @build TestCase ClassType TestAnnotationInfo
  * @build RuntimeParameterAnnotationsForLambdaTest AnnotationsTestBase RuntimeParameterAnnotationsTestBase
@@ -36,12 +35,11 @@
 import java.util.List;
 import java.util.stream.Collectors;
 
-import com.sun.tools.classfile.ClassFile;
-import com.sun.tools.classfile.Method;
+import com.sun.tools.classfile.*;
 
 /**
  * RuntimeParameterAnnotationsForLambdaTest is a test which checks that RuntimeVisibleParameterAnnotationsAttribute
- * and RuntimeInvisibleParameterAnnotationsAttribute are generated properly for lambda expressions.
+ * and RuntimeInvisibleParameterAnnotationsAttribute are not generated at all for lambda expressions.
  * The test checks both single and repeatable annotations.
  * All possible combinations of retention policies are tested.
  *
@@ -74,8 +72,8 @@
                     TestCase.TestParameterInfo p3 = testMethodInfo.addParameter("String", "c");
                     annotations.annotate(p3);
                     String source = SOURCE_TEMPLATE.replace("%SOURCE%", generateLambdaSource(testMethodInfo));
+                    addTestCase(source);
                     echo("Testing:\n" + source);
-                    addTestCase(source);
                     ClassFile classFile = readClassFile(compile(source).getClasses().get(CLASS_NAME));
                     boolean isFoundLambda = false;
                     for (Method method : classFile.methods) {
@@ -94,6 +92,17 @@
         }
     }
 
+    protected void testAttributes(
+            TestCase.TestMethodInfo testMethod,
+            ClassFile classFile,
+            Method method) throws ConstantPoolException {
+        Attributes attributes = method.attributes;
+        RuntimeParameterAnnotations_attribute attr = (RuntimeParameterAnnotations_attribute) attributes.get(Attribute.RuntimeInvisibleParameterAnnotations);
+        checkNull(attr, String.format("%s should be null", Attribute.RuntimeInvisibleParameterAnnotations));
+        attr = (RuntimeParameterAnnotations_attribute) attributes.get(Attribute.RuntimeVisibleParameterAnnotations);
+        checkNull(attr, String.format("%s should be null", Attribute.RuntimeVisibleParameterAnnotations));
+    }
+
     public String generateLambdaSource(TestCase.TestMethodInfo method) {
         return method.parameters.stream()
                 .map(TestCase.TestParameterInfo::generateSource)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/lambda/SE5AnnotationsOnLambdaParameters.java	Fri Nov 06 14:45:44 2015 +0530
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8138612
+ * @summary Do not retain declaration annotations on lambda formal parameters
+ * @run main SE5AnnotationsOnLambdaParameters
+ */
+
+import java.lang.annotation.Annotation;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.reflect.Method;
+
+public class SE5AnnotationsOnLambdaParameters {
+    @Retention(RetentionPolicy.RUNTIME)
+    @interface Annot {}
+
+    interface Runnable {
+        void run(int x);
+    }
+
+    public void run(Runnable r) {}
+
+    public static void main(@Annot String [] args) throws ClassNotFoundException {
+        new SE5AnnotationsOnLambdaParameters().run((@Annot int x) -> { System.out.println(x + args.length); });
+        Class<?> clazz = Class.forName("SE5AnnotationsOnLambdaParameters");
+        for (Method m : clazz.getDeclaredMethods()) {
+            if (m.getName().startsWith("lambda$")) {
+                for (Annotation[] annots : m.getParameterAnnotations()) {
+                    if (annots.length > 0) {
+                        throw new AssertionError("Unexpected annotations on lambda parameters");
+                    }
+                }
+            }
+        }
+    }
+}
\ No newline at end of file