8138612: Do not retain declaration annotations on lambda formal parameters

Reviewed-by: jlahoda
This commit is contained in:
Srikanth Adayapalam 2015-11-06 14:45:44 +05:30
parent ce4153b48f
commit 48e3b3f0df
3 changed files with 76 additions and 7 deletions

View file

@ -1112,6 +1112,7 @@ public class ClassWriter extends ClassFile {
acount += writeMethodParametersAttr(m);
}
acount += writeMemberAttrs(m);
if (!m.isLambdaMethod())
acount += writeParameterAttrs(m);
endAttrs(acountIdx, acount);
}

View file

@ -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 @@ public class RuntimeParameterAnnotationsForLambdaTest extends RuntimeParameterAn
TestCase.TestParameterInfo p3 = testMethodInfo.addParameter("String", "c");
annotations.annotate(p3);
String source = SOURCE_TEMPLATE.replace("%SOURCE%", generateLambdaSource(testMethodInfo));
echo("Testing:\n" + source);
addTestCase(source);
echo("Testing:\n" + source);
ClassFile classFile = readClassFile(compile(source).getClasses().get(CLASS_NAME));
boolean isFoundLambda = false;
for (Method method : classFile.methods) {
@ -94,6 +92,17 @@ public class RuntimeParameterAnnotationsForLambdaTest extends RuntimeParameterAn
}
}
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)

View file

@ -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");
}
}
}
}
}
}