1 /*
2 * Copyright (c) 2013, 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 * @summary Test consistent parsing of ex-RUNTIME annotations that
27 * were changed and separately compiled to have CLASS retention
28 * @library /lib/testlibrary
29 * @build jdk.testlibrary.IOUtils
30 * @run main AnnotationTypeRuntimeAssumptionTest
31 */
32
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.lang.annotation.Retention;
36 import java.lang.annotation.RetentionPolicy;
37
38 import jdk.testlibrary.IOUtils;
39
40 import static java.lang.annotation.RetentionPolicy.CLASS;
41 import static java.lang.annotation.RetentionPolicy.RUNTIME;
42
43 /**
44 * This test simulates a situation where there are two mutually recursive
45 * {@link RetentionPolicy#RUNTIME RUNTIME} annotations {@link AnnA_v1 AnnA_v1}
46 * and {@link AnnB AnnB} and then the first is changed to have
47 * {@link RetentionPolicy#CLASS CLASS} retention and separately compiled.
48 * When {@link AnnA_v1 AnnA_v1} annotation is looked-up on {@link AnnB AnnB}
49 * it still appears to have {@link RetentionPolicy#RUNTIME RUNTIME} retention.
50 */
51 public class AnnotationTypeRuntimeAssumptionTest {
52
53 @Retention(RUNTIME)
54 @AnnB
55 public @interface AnnA_v1 {
56 }
57
58 // An alternative version of AnnA_v1 with CLASS retention instead.
59 // Used to simulate separate compilation (see AltClassLoader below).
123 if (resolve) {
124 resolveClass(c);
125 }
126 return c;
127 }
128 }
129 else { // not our class
130 return super.loadClass(name, resolve);
131 }
132 }
133
134 @Override
135 protected Class<?> findClass(String name)
136 throws ClassNotFoundException {
137 // special class name -> replace it with alternative name
138 if (name.endsWith("_v1")) {
139 String altName = name.substring(0, name.length() - 3) + "_v2";
140 String altPath = altName.replace('.', '/').concat(".class");
141 try (InputStream is = getResourceAsStream(altPath)) {
142 if (is != null) {
143 byte[] bytes = IOUtils.readFully(is);
144 // patch class bytes to contain original name
145 for (int i = 0; i < bytes.length - 2; i++) {
146 if (bytes[i] == '_' &&
147 bytes[i + 1] == 'v' &&
148 bytes[i + 2] == '2') {
149 bytes[i + 2] = '1';
150 }
151 }
152 return defineClass(name, bytes, 0, bytes.length);
153 }
154 else {
155 throw new ClassNotFoundException(name);
156 }
157 }
158 catch (IOException e) {
159 throw new ClassNotFoundException(name, e);
160 }
161 }
162 else { // not special class name -> just load the class
163 String path = name.replace('.', '/').concat(".class");
164 try (InputStream is = getResourceAsStream(path)) {
165 if (is != null) {
166 byte[] bytes = IOUtils.readFully(is);
167 return defineClass(name, bytes, 0, bytes.length);
168 }
169 else {
170 throw new ClassNotFoundException(name);
171 }
172 }
173 catch (IOException e) {
174 throw new ClassNotFoundException(name, e);
175 }
176 }
177 }
178 }
179 }
|
1 /*
2 * Copyright (c) 2013, 2017, 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 * @summary Test consistent parsing of ex-RUNTIME annotations that
27 * were changed and separately compiled to have CLASS retention
28 * @library /lib/testlibrary
29 * @build jdk.testlibrary.IOUtils
30 * @run main AnnotationTypeRuntimeAssumptionTest
31 */
32
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.lang.annotation.Retention;
36 import java.lang.annotation.RetentionPolicy;
37
38 import static java.lang.annotation.RetentionPolicy.CLASS;
39 import static java.lang.annotation.RetentionPolicy.RUNTIME;
40
41 /**
42 * This test simulates a situation where there are two mutually recursive
43 * {@link RetentionPolicy#RUNTIME RUNTIME} annotations {@link AnnA_v1 AnnA_v1}
44 * and {@link AnnB AnnB} and then the first is changed to have
45 * {@link RetentionPolicy#CLASS CLASS} retention and separately compiled.
46 * When {@link AnnA_v1 AnnA_v1} annotation is looked-up on {@link AnnB AnnB}
47 * it still appears to have {@link RetentionPolicy#RUNTIME RUNTIME} retention.
48 */
49 public class AnnotationTypeRuntimeAssumptionTest {
50
51 @Retention(RUNTIME)
52 @AnnB
53 public @interface AnnA_v1 {
54 }
55
56 // An alternative version of AnnA_v1 with CLASS retention instead.
57 // Used to simulate separate compilation (see AltClassLoader below).
121 if (resolve) {
122 resolveClass(c);
123 }
124 return c;
125 }
126 }
127 else { // not our class
128 return super.loadClass(name, resolve);
129 }
130 }
131
132 @Override
133 protected Class<?> findClass(String name)
134 throws ClassNotFoundException {
135 // special class name -> replace it with alternative name
136 if (name.endsWith("_v1")) {
137 String altName = name.substring(0, name.length() - 3) + "_v2";
138 String altPath = altName.replace('.', '/').concat(".class");
139 try (InputStream is = getResourceAsStream(altPath)) {
140 if (is != null) {
141 byte[] bytes = is.readAllBytes();
142 // patch class bytes to contain original name
143 for (int i = 0; i < bytes.length - 2; i++) {
144 if (bytes[i] == '_' &&
145 bytes[i + 1] == 'v' &&
146 bytes[i + 2] == '2') {
147 bytes[i + 2] = '1';
148 }
149 }
150 return defineClass(name, bytes, 0, bytes.length);
151 }
152 else {
153 throw new ClassNotFoundException(name);
154 }
155 }
156 catch (IOException e) {
157 throw new ClassNotFoundException(name, e);
158 }
159 }
160 else { // not special class name -> just load the class
161 String path = name.replace('.', '/').concat(".class");
162 try (InputStream is = getResourceAsStream(path)) {
163 if (is != null) {
164 byte[] bytes = is.readAllBytes();
165 return defineClass(name, bytes, 0, bytes.length);
166 }
167 else {
168 throw new ClassNotFoundException(name);
169 }
170 }
171 catch (IOException e) {
172 throw new ClassNotFoundException(name, e);
173 }
174 }
175 }
176 }
177 }
|