1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
/*
* Copyright (c) 2014, 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 8054987
* @summary Test sharing of annotations between Executable/Field instances.
* Sharing should not be noticeable when performing mutating
* operations.
* @run testng AnnotationSharing
*/
import java.lang.annotation.*;
import java.lang.reflect.*;
import org.testng.annotations.Test;
public class AnnotationSharing {
@Test
public void testMethodSharing() throws Exception {
Method[] m1 = AnnotationSharing.class.getMethods();
Method[] m2 = AnnotationSharing.class.getMethods();
validateSharingSafelyObservable(m1, m2);
}
@Test
public void testDeclaredMethodSharing() throws Exception {
Method[] m3 = AnnotationSharing.class.getDeclaredMethods();
Method[] m4 = AnnotationSharing.class.getDeclaredMethods();
validateSharingSafelyObservable(m3, m4);
}
@Test
public void testFieldSharing() throws Exception {
Field[] f1 = AnnotationSharing.class.getFields();
Field[] f2 = AnnotationSharing.class.getFields();
validateSharingSafelyObservable(f1, f2);
}
@Test
public void testDeclaredFieldsSharing() throws Exception {
Field[] f3 = AnnotationSharing.class.getDeclaredFields();
Field[] f4 = AnnotationSharing.class.getDeclaredFields();
validateSharingSafelyObservable(f3, f4);
}
@Test
public void testMethodSharingOccurs() throws Exception {
Method mm1 = AnnotationSharing.class.getDeclaredMethod("m", (Class<?>[])null);
Method mm2 = AnnotationSharing.class.getDeclaredMethod("m", (Class<?>[])null);
validateAnnotationSharing(mm1, mm2);
}
@Test
public void testMethodSharingIsSafe() throws Exception {
Method mm1 = AnnotationSharing.class.getDeclaredMethod("m", (Class<?>[])null);
Method mm2 = AnnotationSharing.class.getDeclaredMethod("m", (Class<?>[])null);
validateAnnotationSharingIsSafe(mm1, mm2);
validateArrayValues(mm1.getAnnotation(Baz.class), mm2.getAnnotation(Baz.class));
}
@Test
public void testFieldSharingOccurs() throws Exception {
Field ff1 = AnnotationSharing.class.getDeclaredField("f");
Field ff2 = AnnotationSharing.class.getDeclaredField("f");
validateAnnotationSharing(ff1, ff2);
}
@Test
public void testFieldSharingIsSafe() throws Exception {
Field ff1 = AnnotationSharing.class.getDeclaredField("f");
Field ff2 = AnnotationSharing.class.getDeclaredField("f");
validateAnnotationSharingIsSafe(ff1, ff2);
validateArrayValues(ff1.getAnnotation(Baz.class), ff2.getAnnotation(Baz.class));
}
// Validate that AccessibleObject instances are not shared
private static void validateSharingSafelyObservable(AccessibleObject[] m1, AccessibleObject[] m2)
throws Exception {
// Validate that setAccessible works
for (AccessibleObject m : m1)
m.setAccessible(false);
for (AccessibleObject m : m2)
m.setAccessible(true);
for (AccessibleObject m : m1)
if (m.isAccessible())
throw new RuntimeException(m + " should not be accessible");
for (AccessibleObject m : m2)
if (!m.isAccessible())
throw new RuntimeException(m + " should be accessible");
// Validate that methods are still equal()
for (int i = 0; i < m1.length; i++)
if (!m1[i].equals(m2[i]))
throw new RuntimeException(m1[i] + " and " + m2[i] + " should be equal()");
// Validate that the arrays aren't shared
for (int i = 0; i < m1.length; i++)
m1[i] = null;
for (int i = 0; i < m2.length; i++)
if (m2[i] == null)
throw new RuntimeException("Detected sharing of AccessibleObject arrays");
}
// Validate that annotations are shared
private static void validateAnnotationSharing(AccessibleObject m1, AccessibleObject m2) {
Bar b1 = m1.getAnnotation(Bar.class);
Bar b2 = m2.getAnnotation(Bar.class);
if (b1 != b2)
throw new RuntimeException(b1 + " and " + b2 + " should be ==");
}
// Validate that Method instances representing the annotation elements
// behave as intended
private static void validateAnnotationSharingIsSafe(AccessibleObject m1, AccessibleObject m2)
throws Exception {
Bar b1 = m1.getAnnotation(Bar.class);
Bar b2 = m2.getAnnotation(Bar.class);
Method mm1 = b1.annotationType().getMethod("value", (Class<?>[]) null);
Method mm2 = b2.annotationType().getMethod("value", (Class<?>[]) null);
inner(mm1, mm2);
mm1 = b1.getClass().getMethod("value", (Class<?>[]) null);
mm2 = b2.getClass().getMethod("value", (Class<?>[]) null);
inner(mm1, mm2);
}
private static void inner(Method mm1, Method mm2)
throws Exception {
if (!mm1.equals(mm2))
throw new RuntimeException(mm1 + " and " + mm2 + " should be equal()");
mm1.setAccessible(false);
mm2.setAccessible(true);
if (mm1.isAccessible())
throw new RuntimeException(mm1 + " should not be accessible");
if (!mm2.isAccessible())
throw new RuntimeException(mm2 + " should be accessible");
}
// Validate that array element values are not shared
private static void validateArrayValues(Baz a, Baz b) {
String[] s1 = a.value();
String[] s2 = b.value();
s1[0] = "22";
if (!s2[0].equals("1"))
throw new RuntimeException("Mutation of array elements should not be detectable");
}
@Foo @Bar("val") @Baz({"1", "2"})
public void m() {
return ;
}
@Foo @Bar("someValue") @Baz({"1", "22", "33"})
public Object f = new Object();
}
@Retention(RetentionPolicy.RUNTIME)
@interface Foo {}
@Retention(RetentionPolicy.RUNTIME)
@interface Bar {
String value();
}
@Retention(RetentionPolicy.RUNTIME)
@interface Baz {
String [] value();
}
|