-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
8266766: Arrays of types that cannot be an annotation member do not y…
…ield exceptions Reviewed-by: darcy, jfranck
- jdk-25+6
- jdk-25+5
- jdk-25+4
- jdk-25+3
- jdk-25+2
- jdk-25+1
- jdk-25+0
- jdk-24+32
- jdk-24+31
- jdk-24+30
- jdk-24+29
- jdk-24+28
- jdk-24+27
- jdk-24+26
- jdk-24+25
- jdk-24+24
- jdk-24+23
- jdk-24+22
- jdk-24+21
- jdk-24+20
- jdk-24+19
- jdk-24+18
- jdk-24+17
- jdk-24+16
- jdk-24+15
- jdk-24+14
- jdk-24+13
- jdk-24+12
- jdk-24+11
- jdk-24+10
- jdk-24+9
- jdk-24+8
- jdk-24+7
- jdk-24+6
- jdk-24+5
- jdk-24+4
- jdk-24+3
- jdk-24+2
- jdk-24+1
- jdk-24+0
- jdk-23+37
- jdk-23+36
- jdk-23+35
- jdk-23+34
- jdk-23+33
- jdk-23+32
- jdk-23+31
- jdk-23+30
- jdk-23+29
- jdk-23+28
- jdk-23+27
- jdk-23+26
- jdk-23+25
- jdk-23+24
- jdk-23+23
- jdk-23+22
- jdk-23+21
- jdk-23+20
- jdk-23+19
- jdk-23+18
- jdk-23+17
- jdk-23+16
- jdk-23+15
- jdk-23+14
- jdk-23+13
- jdk-23+12
- jdk-23+11
- jdk-23+10
- jdk-23+9
- jdk-23+8
- jdk-23+7
- jdk-23+6
- jdk-23+5
- jdk-23+4
- jdk-23+3
- jdk-23+2
- jdk-23+1
- jdk-23+0
- jdk-23-ga
- jdk-22+36
- jdk-22+35
- jdk-22+34
- jdk-22+33
- jdk-22+32
- jdk-22+31
- jdk-22+30
- jdk-22+29
- jdk-22+28
- jdk-22+27
- jdk-22+26
- jdk-22+25
- jdk-22+24
- jdk-22+23
- jdk-22+22
- jdk-22+21
- jdk-22+20
- jdk-22+19
- jdk-22+18
- jdk-22+17
- jdk-22+16
- jdk-22+15
- jdk-22+14
- jdk-22+13
- jdk-22+12
- jdk-22+11
- jdk-22+10
- jdk-22+9
- jdk-22+8
- jdk-22+7
- jdk-22+6
- jdk-22+5
- jdk-22+4
- jdk-22+3
- jdk-22+2
- jdk-22+1
- jdk-22+0
- jdk-22-ga
- jdk-21+35
- jdk-21+34
- jdk-21+33
- jdk-21+32
- jdk-21+31
- jdk-21+30
- jdk-21+29
- jdk-21+28
- jdk-21+27
- jdk-21+26
- jdk-21+25
- jdk-21+24
- jdk-21+23
- jdk-21+22
- jdk-21+21
- jdk-21+20
- jdk-21+19
- jdk-21+18
- jdk-21+17
- jdk-21+16
- jdk-21+15
- jdk-21+14
- jdk-21+13
- jdk-21+12
- jdk-21+11
- jdk-21+10
- jdk-21+9
- jdk-21+8
- jdk-21+7
- jdk-21+6
- jdk-21+5
- jdk-21+4
- jdk-21+3
- jdk-21+2
- jdk-21+1
- jdk-21+0
- jdk-21-ga
- jdk-20+36
- jdk-20+35
- jdk-20+34
- jdk-20+33
- jdk-20+32
- jdk-20+31
- jdk-20+30
- jdk-20+29
- jdk-20+28
- jdk-20+27
- jdk-20+26
- jdk-20+25
- jdk-20+24
- jdk-20+23
- jdk-20+22
- jdk-20+21
- jdk-20+20
- jdk-20+19
- jdk-20+18
- jdk-20+17
- jdk-20+16
- jdk-20+15
- jdk-20+14
- jdk-20+13
- jdk-20+12
- jdk-20+11
- jdk-20+10
- jdk-20+9
- jdk-20+8
- jdk-20+7
- jdk-20+6
- jdk-20+5
- jdk-20+4
- jdk-20+3
- jdk-20+2
- jdk-20+1
- jdk-20+0
- jdk-20-ga
- jdk-19+36
- jdk-19+35
- jdk-19+34
- jdk-19+33
- jdk-19+32
- jdk-19+31
- jdk-19+30
- jdk-19+29
- jdk-19+28
- jdk-19+27
- jdk-19+26
- jdk-19+25
- jdk-19+24
- jdk-19+23
- jdk-19+22
- jdk-19+21
- jdk-19+20
- jdk-19+19
- jdk-19+18
- jdk-19+17
- jdk-19+16
- jdk-19+15
- jdk-19+14
- jdk-19+13
- jdk-19+12
- jdk-19+11
- jdk-19+10
- jdk-19+9
- jdk-19+8
- jdk-19+7
- jdk-19+6
- jdk-19+5
- jdk-19+4
- jdk-19+3
- jdk-19+2
- jdk-19+1
- jdk-19+0
- jdk-19-ga
- jdk-18+37
- jdk-18+36
- jdk-18+35
- jdk-18+34
- jdk-18+33
- jdk-18+32
- jdk-18+31
- jdk-18+30
- jdk-18+29
- jdk-18+28
- jdk-18+27
- jdk-18+26
- jdk-18+25
- jdk-18+24
- jdk-18+23
- jdk-18+22
- jdk-18+21
- jdk-18+20
- jdk-18+19
- jdk-18+18
- jdk-18+17
- jdk-18+16
- jdk-18+15
- jdk-18+14
- jdk-18+13
- jdk-18+12
- jdk-18+11
- jdk-18+10
- jdk-18+9
- jdk-18+8
- jdk-18+7
- jdk-18+6
- jdk-18+5
- jdk-18+4
- jdk-18+3
- jdk-18+2
- jdk-18+1
- jdk-18+0
- jdk-18-ga
- jdk-17+35
- jdk-17+34
- jdk-17+33
- jdk-17+32
- jdk-17+31
- jdk-17+30
- jdk-17+29
- jdk-17+28
- jdk-17+27
- jdk-17+26
- jdk-17-ga
Showing
2 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
test/jdk/java/lang/annotation/AnnotationTypeMismatchException/ArrayTypeMismatchTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright (c) 2021, 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 8266766 | ||
* @summary An array property of a type that is no longer of a type that is a legal member of an | ||
* annotation should throw an AnnotationTypeMismatchException. | ||
* @modules java.base/jdk.internal.org.objectweb.asm | ||
* @run main ArrayTypeMismatchTest | ||
*/ | ||
|
||
import jdk.internal.org.objectweb.asm.AnnotationVisitor; | ||
import jdk.internal.org.objectweb.asm.ClassWriter; | ||
import jdk.internal.org.objectweb.asm.Opcodes; | ||
import jdk.internal.org.objectweb.asm.Type; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.annotation.AnnotationTypeMismatchException; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.reflect.InvocationTargetException; | ||
|
||
public class ArrayTypeMismatchTest { | ||
|
||
public static void main(String[] args) throws Exception { | ||
/* | ||
* This test creates an annotation where the annotation member's type is an array with | ||
* a component type that cannot be legally used for an annotation member. This can happen | ||
* if a class is recompiled independencly of the annotation type and linked at runtime | ||
* in this new version. For a test, a class is created as: | ||
* | ||
* package sample; | ||
* @Carrier(value = { @NoAnnotation }) | ||
* class Host { } | ||
* | ||
* where NoAnnotation is defined as a regular interface and not as an annotation type. | ||
* The classes are created by using ASM to emulate this state. | ||
*/ | ||
ByteArrayClassLoader cl = new ByteArrayClassLoader(NoAnnotation.class.getClassLoader()); | ||
cl.init(annotationType(), carrierType()); | ||
@SuppressWarnings("unchecked") | ||
Class<? extends Annotation> host = (Class<? extends Annotation>) cl.loadClass("sample.Host"); | ||
Annotation sample = cl.loadClass("sample.Carrier").getAnnotation(host); | ||
try { | ||
Object value = host.getMethod("value").invoke(sample); | ||
throw new IllegalStateException("Found value: " + value); | ||
} catch (InvocationTargetException ite) { | ||
Throwable cause = ite.getCause(); | ||
if (cause instanceof AnnotationTypeMismatchException) { | ||
AnnotationTypeMismatchException e = ((AnnotationTypeMismatchException) cause); | ||
if (!e.element().getName().equals("value")) { | ||
throw new IllegalStateException("Unexpected element: " + e.element()); | ||
} else if (!e.foundType().equals("Array with component tag: @")) { | ||
throw new IllegalStateException("Unexpected type: " + e.foundType()); | ||
} | ||
} else { | ||
throw new IllegalStateException(cause); | ||
} | ||
} | ||
} | ||
|
||
private static byte[] carrierType() { | ||
ClassWriter writer = new ClassWriter(0); | ||
writer.visit(Opcodes.V1_8, 0, "sample/Carrier", null, Type.getInternalName(Object.class), null); | ||
AnnotationVisitor v = writer.visitAnnotation("Lsample/Host;", true); | ||
AnnotationVisitor a = v.visitArray("value"); | ||
a.visitAnnotation(null, Type.getDescriptor(NoAnnotation.class)).visitEnd(); | ||
a.visitEnd(); | ||
v.visitEnd(); | ||
writer.visitEnd(); | ||
return writer.toByteArray(); | ||
} | ||
|
||
private static byte[] annotationType() { | ||
ClassWriter writer = new ClassWriter(0); | ||
writer.visit(Opcodes.V1_8, | ||
Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE | Opcodes.ACC_ANNOTATION, | ||
"sample/Host", | ||
null, | ||
Type.getInternalName(Object.class), | ||
new String[]{Type.getInternalName(Annotation.class)}); | ||
AnnotationVisitor a = writer.visitAnnotation(Type.getDescriptor(Retention.class), true); | ||
a.visitEnum("value", Type.getDescriptor(RetentionPolicy.class), RetentionPolicy.RUNTIME.name()); | ||
writer.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT, | ||
"value", | ||
Type.getMethodDescriptor(Type.getType(NoAnnotation[].class)), | ||
null, | ||
null).visitEnd(); | ||
writer.visitEnd(); | ||
return writer.toByteArray(); | ||
} | ||
|
||
public interface NoAnnotation { } | ||
|
||
public static class ByteArrayClassLoader extends ClassLoader { | ||
|
||
public ByteArrayClassLoader(ClassLoader parent) { | ||
super(parent); | ||
} | ||
|
||
void init(byte[] annotationType, byte[] carrierType) { | ||
defineClass("sample.Host", annotationType, 0, annotationType.length); | ||
defineClass("sample.Carrier", carrierType, 0, carrierType.length); | ||
} | ||
} | ||
} |
0924382
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review
Issues