Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/reflect/annotationSharing/AnnotationSharing.java
38821 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 805498726* @summary Test sharing of annotations between Executable/Field instances.27* Sharing should not be noticeable when performing mutating28* operations.29* @run testng AnnotationSharing30*/3132import java.lang.annotation.*;33import java.lang.reflect.*;3435import org.testng.annotations.Test;3637public class AnnotationSharing {38public static void main(String ... args) throws Exception {39}4041@Test42public void testMethodSharing() throws Exception {43Method[] m1 = AnnotationSharing.class.getMethods();44Method[] m2 = AnnotationSharing.class.getMethods();45validateSharingSafelyObservable(m1, m2);46}4748@Test49public void testDeclaredMethodSharing() throws Exception {50Method[] m3 = AnnotationSharing.class.getDeclaredMethods();51Method[] m4 = AnnotationSharing.class.getDeclaredMethods();52validateSharingSafelyObservable(m3, m4);53}5455@Test56public void testFieldSharing() throws Exception {57Field[] f1 = AnnotationSharing.class.getFields();58Field[] f2 = AnnotationSharing.class.getFields();59validateSharingSafelyObservable(f1, f2);60}6162@Test63public void testDeclaredFieldsSharing() throws Exception {64Field[] f3 = AnnotationSharing.class.getDeclaredFields();65Field[] f4 = AnnotationSharing.class.getDeclaredFields();66validateSharingSafelyObservable(f3, f4);67}6869@Test70public void testMethodSharingOccurs() throws Exception {71Method mm1 = AnnotationSharing.class.getDeclaredMethod("m", (Class<?>[])null);72Method mm2 = AnnotationSharing.class.getDeclaredMethod("m", (Class<?>[])null);73validateAnnotationSharing(mm1, mm2);74}7576@Test77public void testMethodSharingIsSafe() throws Exception {78Method mm1 = AnnotationSharing.class.getDeclaredMethod("m", (Class<?>[])null);79Method mm2 = AnnotationSharing.class.getDeclaredMethod("m", (Class<?>[])null);80validateAnnotationSharingIsSafe(mm1, mm2);81validateArrayValues(mm1.getAnnotation(Baz.class), mm2.getAnnotation(Baz.class));82}8384@Test85public void testFieldSharingOccurs() throws Exception {86Field ff1 = AnnotationSharing.class.getDeclaredField("f");87Field ff2 = AnnotationSharing.class.getDeclaredField("f");88validateAnnotationSharing(ff1, ff2);89}9091@Test92public void testFieldSharingIsSafe() throws Exception {93Field ff1 = AnnotationSharing.class.getDeclaredField("f");94Field ff2 = AnnotationSharing.class.getDeclaredField("f");95validateAnnotationSharingIsSafe(ff1, ff2);96validateArrayValues(ff1.getAnnotation(Baz.class), ff2.getAnnotation(Baz.class));97}9899// Validate that AccessibleObject instances are not shared100private static void validateSharingSafelyObservable(AccessibleObject[] m1, AccessibleObject[] m2)101throws Exception {102103// Validate that setAccessible works104for (AccessibleObject m : m1)105m.setAccessible(false);106107for (AccessibleObject m : m2)108m.setAccessible(true);109110for (AccessibleObject m : m1)111if (m.isAccessible())112throw new RuntimeException(m + " should not be accessible");113114for (AccessibleObject m : m2)115if (!m.isAccessible())116throw new RuntimeException(m + " should be accessible");117118// Validate that methods are still equal()119for (int i = 0; i < m1.length; i++)120if (!m1[i].equals(m2[i]))121throw new RuntimeException(m1[i] + " and " + m2[i] + " should be equal()");122123// Validate that the arrays aren't shared124for (int i = 0; i < m1.length; i++)125m1[i] = null;126127for (int i = 0; i < m2.length; i++)128if (m2[i] == null)129throw new RuntimeException("Detected sharing of AccessibleObject arrays");130}131132// Validate that annotations are shared133private static void validateAnnotationSharing(AccessibleObject m1, AccessibleObject m2) {134Bar b1 = m1.getAnnotation(Bar.class);135Bar b2 = m2.getAnnotation(Bar.class);136137if (b1 != b2)138throw new RuntimeException(b1 + " and " + b2 + " should be ==");139140}141142// Validate that Method instances representing the annotation elements143// behave as intended144private static void validateAnnotationSharingIsSafe(AccessibleObject m1, AccessibleObject m2)145throws Exception {146Bar b1 = m1.getAnnotation(Bar.class);147Bar b2 = m2.getAnnotation(Bar.class);148149Method mm1 = b1.annotationType().getMethod("value", (Class<?>[]) null);150Method mm2 = b2.annotationType().getMethod("value", (Class<?>[]) null);151inner(mm1, mm2);152153mm1 = b1.getClass().getMethod("value", (Class<?>[]) null);154mm2 = b2.getClass().getMethod("value", (Class<?>[]) null);155inner(mm1, mm2);156157}158private static void inner(Method mm1, Method mm2)159throws Exception {160if (!mm1.equals(mm2))161throw new RuntimeException(mm1 + " and " + mm2 + " should be equal()");162163mm1.setAccessible(false);164mm2.setAccessible(true);165166if (mm1.isAccessible())167throw new RuntimeException(mm1 + " should not be accessible");168169if (!mm2.isAccessible())170throw new RuntimeException(mm2 + " should be accessible");171}172173// Validate that array element values are not shared174private static void validateArrayValues(Baz a, Baz b) {175String[] s1 = a.value();176String[] s2 = b.value();177178s1[0] = "22";179180if (!s2[0].equals("1"))181throw new RuntimeException("Mutation of array elements should not be detectable");182}183184@Foo @Bar("val") @Baz({"1", "2"})185public void m() {186return ;187}188189@Foo @Bar("someValue") @Baz({"1", "22", "33"})190public Object f = new Object();191}192193@Retention(RetentionPolicy.RUNTIME)194@interface Foo {}195196@Retention(RetentionPolicy.RUNTIME)197@interface Bar {198String value();199}200201@Retention(RetentionPolicy.RUNTIME)202@interface Baz {203String [] value();204}205206207