author | alanb |
Fri, 10 Feb 2017 09:04:39 +0000 | |
changeset 43712 | 5dfd0950317c |
parent 42703 | 20c39ea4a507 |
permissions | -rw-r--r-- |
42338 | 1 |
/* |
2 |
* Copyright (c) 2016, 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.lang.annotation.Annotation; |
|
27 |
import java.lang.module.Configuration; |
|
28 |
import java.lang.module.ModuleFinder; |
|
29 |
import java.lang.reflect.Layer; |
|
30 |
import java.lang.reflect.Module; |
|
31 |
import java.nio.file.Files; |
|
32 |
import java.nio.file.Path; |
|
33 |
||
34 |
import java.util.ArrayList; |
|
35 |
import java.util.List; |
|
36 |
import java.util.Set; |
|
37 |
||
38 |
import jdk.internal.module.ClassFileAttributes; |
|
39 |
import jdk.internal.org.objectweb.asm.AnnotationVisitor; |
|
40 |
import jdk.internal.org.objectweb.asm.Attribute; |
|
41 |
import jdk.internal.org.objectweb.asm.ClassReader; |
|
42 |
import jdk.internal.org.objectweb.asm.ClassVisitor; |
|
43 |
import jdk.internal.org.objectweb.asm.ClassWriter; |
|
44 |
import jdk.internal.org.objectweb.asm.Opcodes; |
|
45 |
||
46 |
import org.testng.annotations.Test; |
|
47 |
import static org.testng.Assert.*; |
|
48 |
||
49 |
/** |
|
50 |
* @test |
|
51 |
* @modules java.base/jdk.internal.org.objectweb.asm |
|
52 |
* java.base/jdk.internal.module |
|
53 |
* java.xml |
|
54 |
* @run testng AnnotationsTest |
|
55 |
* @summary Basic test of annotations on modules |
|
56 |
*/ |
|
57 |
||
58 |
public class AnnotationsTest { |
|
59 |
||
60 |
/** |
|
61 |
* Test that there are no annotations on an unnamed module. |
|
62 |
*/ |
|
63 |
@Test |
|
64 |
public void testUnnamedModule() { |
|
65 |
Module module = this.getClass().getModule(); |
|
66 |
assertTrue(module.getAnnotations().length == 0); |
|
67 |
} |
|
68 |
||
69 |
/** |
|
70 |
* Test loading a module with a RuntimeVisibleAnnotation attribute. |
|
71 |
* The test copies the module-info.class for java.xml, adds the attribute, |
|
72 |
* and then loads the updated module. |
|
73 |
*/ |
|
74 |
@Test |
|
75 |
public void testNamedModule() throws IOException { |
|
76 |
||
77 |
// "deprecate" java.xml |
|
78 |
Path dir = Files.createTempDirectory("mods"); |
|
79 |
deprecateModule("java.xml", true, "9", dir); |
|
80 |
||
81 |
// "load" the cloned java.xml |
|
82 |
Module module = loadModule(dir, "java.xml"); |
|
83 |
||
84 |
// check the annotation is present |
|
85 |
assertTrue(module.isAnnotationPresent(Deprecated.class)); |
|
86 |
Deprecated d = module.getAnnotation(Deprecated.class); |
|
87 |
assertNotNull(d, "@Deprecated not found"); |
|
88 |
assertTrue(d.forRemoval()); |
|
89 |
assertEquals(d.since(), "9"); |
|
90 |
Annotation[] a = module.getAnnotations(); |
|
91 |
assertTrue(a.length == 1); |
|
92 |
assertTrue(a[0] instanceof Deprecated); |
|
93 |
} |
|
94 |
||
95 |
||
96 |
/** |
|
97 |
* Copy the module-info.class for the given module, add the |
|
98 |
* Deprecated annotation, and write the updated module-info.class |
|
99 |
* to a directory. |
|
100 |
*/ |
|
101 |
static void deprecateModule(String name, |
|
102 |
boolean forRemoval, |
|
103 |
String since, |
|
104 |
Path output) throws IOException { |
|
105 |
Module module = Layer.boot().findModule(name).orElse(null); |
|
106 |
assertNotNull(module, name + " not found"); |
|
107 |
||
108 |
InputStream in = module.getResourceAsStream("module-info.class"); |
|
109 |
assertNotNull(in, "No module-info.class for " + name); |
|
110 |
||
111 |
try (in) { |
|
112 |
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS |
|
113 |
+ ClassWriter.COMPUTE_FRAMES); |
|
114 |
||
115 |
ClassVisitor cv = new ClassVisitor(Opcodes.ASM5, cw) { }; |
|
116 |
||
117 |
ClassReader cr = new ClassReader(in); |
|
118 |
||
119 |
List<Attribute> attrs = new ArrayList<>(); |
|
120 |
attrs.add(new ClassFileAttributes.ModuleAttribute()); |
|
121 |
attrs.add(new ClassFileAttributes.ModulePackagesAttribute()); |
|
122 |
attrs.add(new ClassFileAttributes.ModuleTargetAttribute()); |
|
123 |
cr.accept(cv, attrs.toArray(new Attribute[0]), 0); |
|
124 |
||
125 |
AnnotationVisitor annotationVisitor |
|
126 |
= cv.visitAnnotation("Ljava/lang/Deprecated;", true); |
|
127 |
annotationVisitor.visit("forRemoval", forRemoval); |
|
128 |
annotationVisitor.visit("since", since); |
|
129 |
annotationVisitor.visitEnd(); |
|
130 |
||
131 |
byte[] bytes = cw.toByteArray(); |
|
132 |
Path mi = output.resolve("module-info.class"); |
|
133 |
Files.write(mi, bytes); |
|
134 |
} |
|
135 |
} |
|
136 |
||
137 |
/** |
|
138 |
* Load the module of the given name in the given directory into a |
|
139 |
* child layer. |
|
140 |
*/ |
|
141 |
static Module loadModule(Path dir, String name) throws IOException { |
|
142 |
ModuleFinder finder = ModuleFinder.of(dir); |
|
143 |
||
144 |
Layer bootLayer = Layer.boot(); |
|
145 |
||
146 |
Configuration cf = bootLayer.configuration() |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
147 |
.resolve(finder, ModuleFinder.of(), Set.of(name)); |
42338 | 148 |
|
149 |
ClassLoader scl = ClassLoader.getSystemClassLoader(); |
|
150 |
Layer layer = bootLayer.defineModulesWithOneLoader(cf, scl); |
|
151 |
||
152 |
Module module = layer.findModule(name).orElse(null); |
|
153 |
assertNotNull(module, name + " not loaded"); |
|
154 |
return module; |
|
155 |
} |
|
156 |
} |