1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 8054987 27 * @summary Test sharing of annotations between Executable/Field instances. 28 * Sharing should not be noticeable when performing mutating 29 * operations. 30 * @run testng AnnotationSharing 31 */ 32 33 import java.lang.annotation.*; 34 import java.lang.reflect.*; 35 36 import org.testng.annotations.Test; 37 38 public class AnnotationSharing { 39 public static void main(String ... args) throws Exception { 40 } 41 42 @Test 43 public void testMethodSharing() throws Exception { 44 Method[] m1 = AnnotationSharing.class.getMethods(); 45 Method[] m2 = AnnotationSharing.class.getMethods(); 46 validateSharingSafelyObservable(m1, m2); 47 } 48 49 @Test 50 public void testDeclaredMethodSharing() throws Exception { 51 Method[] m3 = AnnotationSharing.class.getDeclaredMethods(); 52 Method[] m4 = AnnotationSharing.class.getDeclaredMethods(); 53 validateSharingSafelyObservable(m3, m4); 54 } 55 56 @Test 57 public void testFieldSharing() throws Exception { 58 Field[] f1 = AnnotationSharing.class.getFields(); 59 Field[] f2 = AnnotationSharing.class.getFields(); 60 validateSharingSafelyObservable(f1, f2); 61 } 62 63 @Test 64 public void testDeclaredFieldsSharing() throws Exception { 65 Field[] f3 = AnnotationSharing.class.getDeclaredFields(); 66 Field[] f4 = AnnotationSharing.class.getDeclaredFields(); 67 validateSharingSafelyObservable(f3, f4); 68 } 69 70 @Test 71 public void testMethodSharingOccurs() throws Exception { 72 Method mm1 = AnnotationSharing.class.getDeclaredMethod("m", (Class<?>[])null); 73 Method mm2 = AnnotationSharing.class.getDeclaredMethod("m", (Class<?>[])null); 74 validateAnnotationSharing(mm1, mm2); 75 } 76 77 @Test 78 public void testMethodSharingIsSafe() throws Exception { 79 Method mm1 = AnnotationSharing.class.getDeclaredMethod("m", (Class<?>[])null); 80 Method mm2 = AnnotationSharing.class.getDeclaredMethod("m", (Class<?>[])null); 81 validateAnnotationSharingIsSafe(mm1, mm2); 82 validateArrayValues(mm1.getAnnotation(Baz.class), mm2.getAnnotation(Baz.class)); 83 } 84 85 @Test 86 public void testFieldSharingOccurs() throws Exception { 87 Field ff1 = AnnotationSharing.class.getDeclaredField("f"); 88 Field ff2 = AnnotationSharing.class.getDeclaredField("f"); 89 validateAnnotationSharing(ff1, ff2); 90 } 91 92 @Test 93 public void testFieldSharingIsSafe() throws Exception { 94 Field ff1 = AnnotationSharing.class.getDeclaredField("f"); 95 Field ff2 = AnnotationSharing.class.getDeclaredField("f"); 96 validateAnnotationSharingIsSafe(ff1, ff2); 97 validateArrayValues(ff1.getAnnotation(Baz.class), ff2.getAnnotation(Baz.class)); 98 } 99 100 // Validate that AccessibleObject instances are not shared 101 private static void validateSharingSafelyObservable(AccessibleObject[] m1, AccessibleObject[] m2) 102 throws Exception { 103 104 // Validate that setAccessible works 105 for (AccessibleObject m : m1) 106 m.setAccessible(false); 107 108 for (AccessibleObject m : m2) 109 m.setAccessible(true); 110 111 for (AccessibleObject m : m1) 112 if (m.isAccessible()) 113 throw new RuntimeException(m + " should not be accessible"); 114 115 for (AccessibleObject m : m2) 116 if (!m.isAccessible()) 117 throw new RuntimeException(m + " should be accessible"); 118 119 // Validate that methods are still equal() 120 for (int i = 0; i < m1.length; i++) 121 if (!m1[i].equals(m2[i])) 122 throw new RuntimeException(m1[i] + " and " + m2[i] + " should be equal()"); 123 124 // Validate that the arrays aren't shared 125 for (int i = 0; i < m1.length; i++) 126 m1[i] = null; 127 128 for (int i = 0; i < m2.length; i++) 129 if (m2[i] == null) 130 throw new RuntimeException("Detected sharing of AccessibleObject arrays"); 131 } 132 133 // Validate that annotations are shared 134 private static void validateAnnotationSharing(AccessibleObject m1, AccessibleObject m2) { 135 Bar b1 = m1.getAnnotation(Bar.class); 136 Bar b2 = m2.getAnnotation(Bar.class); 137 138 if (b1 != b2) 139 throw new RuntimeException(b1 + " and " + b2 + " should be =="); 140 141 } 142 143 // Validate that Method instances representing the annotation elements 144 // behave as intended 145 private static void validateAnnotationSharingIsSafe(AccessibleObject m1, AccessibleObject m2) 146 throws Exception { 147 Bar b1 = m1.getAnnotation(Bar.class); 148 Bar b2 = m2.getAnnotation(Bar.class); 149 150 Method mm1 = b1.annotationType().getMethod("value", (Class<?>[]) null); 151 Method mm2 = b2.annotationType().getMethod("value", (Class<?>[]) null); 152 inner(mm1, mm2); 153 154 mm1 = b1.getClass().getMethod("value", (Class<?>[]) null); 155 mm2 = b2.getClass().getMethod("value", (Class<?>[]) null); 156 inner(mm1, mm2); 157 158 } 159 private static void inner(Method mm1, Method mm2) 160 throws Exception { 161 if (!mm1.equals(mm2)) 162 throw new RuntimeException(mm1 + " and " + mm2 + " should be equal()"); 163 164 mm1.setAccessible(false); 165 mm2.setAccessible(true); 166 167 if (mm1.isAccessible()) 168 throw new RuntimeException(mm1 + " should not be accessible"); 169 170 if (!mm2.isAccessible()) 171 throw new RuntimeException(mm2 + " should be accessible"); 172 } 173 174 // Validate that array element values are not shared 175 private static void validateArrayValues(Baz a, Baz b) { 176 String[] s1 = a.value(); 177 String[] s2 = b.value(); 178 179 s1[0] = "22"; 180 181 if (!s2[0].equals("1")) 182 throw new RuntimeException("Mutation of array elements should not be detectable"); 183 } 184 185 @Foo @Bar("val") @Baz({"1", "2"}) 186 public void m() { 187 return ; 188 } 189 190 @Foo @Bar("someValue") @Baz({"1", "22", "33"}) 191 public Object f = new Object(); 192 } 193 194 @Retention(RetentionPolicy.RUNTIME) 195 @interface Foo {} 196 197 @Retention(RetentionPolicy.RUNTIME) 198 @interface Bar { 199 String value(); 200 } 201 202 @Retention(RetentionPolicy.RUNTIME) 203 @interface Baz { 204 String [] value(); 205 }