mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-20 19:14:38 +02:00
8138612: Do not retain declaration annotations on lambda formal parameters
Reviewed-by: jlahoda
This commit is contained in:
parent
ce4153b48f
commit
48e3b3f0df
3 changed files with 76 additions and 7 deletions
|
@ -1112,7 +1112,8 @@ public class ClassWriter extends ClassFile {
|
||||||
acount += writeMethodParametersAttr(m);
|
acount += writeMethodParametersAttr(m);
|
||||||
}
|
}
|
||||||
acount += writeMemberAttrs(m);
|
acount += writeMemberAttrs(m);
|
||||||
acount += writeParameterAttrs(m);
|
if (!m.isLambdaMethod())
|
||||||
|
acount += writeParameterAttrs(m);
|
||||||
endAttrs(acountIdx, acount);
|
endAttrs(acountIdx, acount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,10 +23,9 @@
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @test
|
* @test
|
||||||
* @bug 8044411 8079060
|
* @bug 8044411 8079060 8138612
|
||||||
* @summary Tests the RuntimeParameterVisibleAnnotations/RuntimeParameterInvisibleAnnotations attribute.
|
* @summary Tests the RuntimeParameterVisibleAnnotations/RuntimeParameterInvisibleAnnotations attribute.
|
||||||
* @library /tools/lib /tools/javac/lib ../lib
|
* @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 WorkAnnotations TestBase TestResult InMemoryFileManager ToolBox
|
||||||
* @build TestCase ClassType TestAnnotationInfo
|
* @build TestCase ClassType TestAnnotationInfo
|
||||||
* @build RuntimeParameterAnnotationsForLambdaTest AnnotationsTestBase RuntimeParameterAnnotationsTestBase
|
* @build RuntimeParameterAnnotationsForLambdaTest AnnotationsTestBase RuntimeParameterAnnotationsTestBase
|
||||||
|
@ -36,12 +35,11 @@
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import com.sun.tools.classfile.ClassFile;
|
import com.sun.tools.classfile.*;
|
||||||
import com.sun.tools.classfile.Method;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* RuntimeParameterAnnotationsForLambdaTest is a test which checks that RuntimeVisibleParameterAnnotationsAttribute
|
* 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.
|
* The test checks both single and repeatable annotations.
|
||||||
* All possible combinations of retention policies are tested.
|
* All possible combinations of retention policies are tested.
|
||||||
*
|
*
|
||||||
|
@ -74,8 +72,8 @@ public class RuntimeParameterAnnotationsForLambdaTest extends RuntimeParameterAn
|
||||||
TestCase.TestParameterInfo p3 = testMethodInfo.addParameter("String", "c");
|
TestCase.TestParameterInfo p3 = testMethodInfo.addParameter("String", "c");
|
||||||
annotations.annotate(p3);
|
annotations.annotate(p3);
|
||||||
String source = SOURCE_TEMPLATE.replace("%SOURCE%", generateLambdaSource(testMethodInfo));
|
String source = SOURCE_TEMPLATE.replace("%SOURCE%", generateLambdaSource(testMethodInfo));
|
||||||
echo("Testing:\n" + source);
|
|
||||||
addTestCase(source);
|
addTestCase(source);
|
||||||
|
echo("Testing:\n" + source);
|
||||||
ClassFile classFile = readClassFile(compile(source).getClasses().get(CLASS_NAME));
|
ClassFile classFile = readClassFile(compile(source).getClasses().get(CLASS_NAME));
|
||||||
boolean isFoundLambda = false;
|
boolean isFoundLambda = false;
|
||||||
for (Method method : classFile.methods) {
|
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) {
|
public String generateLambdaSource(TestCase.TestMethodInfo method) {
|
||||||
return method.parameters.stream()
|
return method.parameters.stream()
|
||||||
.map(TestCase.TestParameterInfo::generateSource)
|
.map(TestCase.TestParameterInfo::generateSource)
|
||||||
|
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue