Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/ModuleTests/AnnotationsTest.java
41149 views
1
/*
2
* Copyright (c) 2016, 2020, 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
import java.io.IOException;
25
import java.io.InputStream;
26
import java.io.OutputStream;
27
import java.lang.annotation.Annotation;
28
import java.lang.module.Configuration;
29
import java.lang.module.ModuleDescriptor;
30
import java.lang.module.ModuleFinder;
31
import java.net.URL;
32
import java.net.URLClassLoader;
33
import java.nio.file.Files;
34
import java.nio.file.Path;
35
import java.util.ArrayList;
36
import java.util.List;
37
import java.util.Set;
38
39
import jdk.internal.module.ModuleInfoWriter;
40
import jdk.internal.org.objectweb.asm.AnnotationVisitor;
41
import jdk.internal.org.objectweb.asm.Attribute;
42
import jdk.internal.org.objectweb.asm.ClassReader;
43
import jdk.internal.org.objectweb.asm.ClassVisitor;
44
import jdk.internal.org.objectweb.asm.ClassWriter;
45
import jdk.internal.org.objectweb.asm.Opcodes;
46
import jdk.internal.org.objectweb.asm.commons.ModuleTargetAttribute;
47
48
import org.testng.annotations.Test;
49
import static org.testng.Assert.*;
50
51
/**
52
* @test
53
* @modules java.base/jdk.internal.org.objectweb.asm
54
* java.base/jdk.internal.org.objectweb.asm.commons
55
* java.base/jdk.internal.module
56
* @run testng AnnotationsTest
57
* @summary Basic test of annotations on modules
58
*/
59
60
public class AnnotationsTest {
61
62
/**
63
* Test that there are no annotations on an unnamed module.
64
*/
65
@Test
66
public void testUnnamedModule() {
67
Module module = this.getClass().getModule();
68
assertTrue(module.getAnnotations().length == 0);
69
assertTrue(module.getDeclaredAnnotations().length == 0);
70
}
71
72
/**
73
* Test reflectively reading the annotations on a named module.
74
*/
75
@Test
76
public void testNamedModule() throws IOException {
77
Path mods = Files.createTempDirectory(Path.of(""), "mods");
78
79
// @Deprecated(since="9", forRemoval=true) module foo { }
80
ModuleDescriptor descriptor = ModuleDescriptor.newModule("foo").build();
81
byte[] classBytes = ModuleInfoWriter.toBytes(descriptor);
82
classBytes = addDeprecated(classBytes, true, "9");
83
Files.write(mods.resolve("module-info.class"), classBytes);
84
85
// create module layer with module foo
86
Module module = loadModule(mods, "foo");
87
88
// check the annotation is present
89
assertTrue(module.isAnnotationPresent(Deprecated.class));
90
Deprecated d = module.getAnnotation(Deprecated.class);
91
assertNotNull(d, "@Deprecated not found");
92
assertTrue(d.forRemoval());
93
assertEquals(d.since(), "9");
94
Annotation[] a = module.getAnnotations();
95
assertTrue(a.length == 1);
96
assertTrue(a[0] instanceof Deprecated);
97
assertEquals(module.getDeclaredAnnotations(), a);
98
}
99
100
/**
101
* Test reflectively reading annotations on a named module where the module
102
* is mapped to a class loader that can locate a module-info.class.
103
*/
104
@Test
105
public void testWithModuleInfoResourceXXXX() throws IOException {
106
Path mods = Files.createTempDirectory(Path.of(""), "mods");
107
108
// classes directory with module-info.class
109
Path classes = Files.createTempDirectory(Path.of("."), "classes");
110
Path mi = classes.resolve("module-info.class");
111
try (OutputStream out = Files.newOutputStream(mi)) {
112
ModuleDescriptor descriptor = ModuleDescriptor.newModule("lurker").build();
113
ModuleInfoWriter.write(descriptor, out);
114
}
115
116
// URLClassLoader that can locate a module-info.class resource
117
URL url = classes.toUri().toURL();
118
URLClassLoader loader = new URLClassLoader(new URL[] { url });
119
assertTrue(loader.findResource("module-info.class") != null);
120
121
// module foo { }
122
ModuleDescriptor descriptor = ModuleDescriptor.newModule("foo").build();
123
byte[] classBytes = ModuleInfoWriter.toBytes(descriptor);
124
Files.write(mods.resolve("module-info.class"), classBytes);
125
126
// create module layer with module foo
127
Module foo = loadModule(mods, "foo", loader);
128
129
// check the annotation is not present
130
assertFalse(foo.isAnnotationPresent(Deprecated.class));
131
132
// @Deprecated(since="11", forRemoval=true) module bar { }
133
descriptor = ModuleDescriptor.newModule("bar").build();
134
classBytes = ModuleInfoWriter.toBytes(descriptor);
135
classBytes = addDeprecated(classBytes, true, "11");
136
Files.write(mods.resolve("module-info.class"), classBytes);
137
138
// create module layer with module bar
139
Module bar = loadModule(mods, "bar", loader);
140
141
// check the annotation is present
142
assertTrue(bar.isAnnotationPresent(Deprecated.class));
143
}
144
145
/**
146
* Adds the Deprecated annotation to the given module-info class file.
147
*/
148
static byte[] addDeprecated(byte[] bytes, boolean forRemoval, String since) {
149
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
150
+ ClassWriter.COMPUTE_FRAMES);
151
152
ClassVisitor cv = new ClassVisitor(Opcodes.ASM6, cw) { };
153
154
ClassReader cr = new ClassReader(bytes);
155
List<Attribute> attrs = new ArrayList<>();
156
attrs.add(new ModuleTargetAttribute());
157
cr.accept(cv, attrs.toArray(new Attribute[0]), 0);
158
159
AnnotationVisitor annotationVisitor
160
= cv.visitAnnotation("Ljava/lang/Deprecated;", true);
161
annotationVisitor.visit("forRemoval", forRemoval);
162
annotationVisitor.visit("since", since);
163
annotationVisitor.visitEnd();
164
165
return cw.toByteArray();
166
}
167
168
/**
169
* Load the module of the given name in the given directory into a
170
* child layer with the given class loader as the parent class loader.
171
*/
172
static Module loadModule(Path dir, String name, ClassLoader parent)
173
throws IOException
174
{
175
ModuleFinder finder = ModuleFinder.of(dir);
176
177
ModuleLayer bootLayer = ModuleLayer.boot();
178
179
Configuration cf = bootLayer.configuration()
180
.resolve(finder, ModuleFinder.of(), Set.of(name));
181
182
ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, parent);
183
184
Module module = layer.findModule(name).orElse(null);
185
assertNotNull(module, name + " not loaded");
186
return module;
187
}
188
189
static Module loadModule(Path dir, String name) throws IOException {
190
return loadModule(dir, name, ClassLoader.getSystemClassLoader());
191
}
192
}
193
194