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.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Set;
36
37 import jdk.internal.module.ClassFileAttributes;
38 import jdk.internal.org.objectweb.asm.AnnotationVisitor;
39 import jdk.internal.org.objectweb.asm.Attribute;
40 import jdk.internal.org.objectweb.asm.ClassReader;
41 import jdk.internal.org.objectweb.asm.ClassVisitor;
42 import jdk.internal.org.objectweb.asm.ClassWriter;
43 import jdk.internal.org.objectweb.asm.Opcodes;
44
45 import org.testng.annotations.Test;
46 import static org.testng.Assert.*;
47
48 /**
49 * @test
50 * @modules java.base/jdk.internal.org.objectweb.asm
51 * java.base/jdk.internal.module
52 * java.xml
53 * @run testng AnnotationsTest
54 * @summary Basic test of annotations on modules
55 */
56
57 public class AnnotationsTest {
58
59 /**
60 * Test that there are no annotations on an unnamed module.
61 */
62 @Test
63 public void testUnnamedModule() {
64 Module module = this.getClass().getModule();
65 assertTrue(module.getAnnotations().length == 0);
66 assertTrue(module.getDeclaredAnnotations().length == 0);
67 }
68
69 /**
70 * Test loading a module with a RuntimeVisibleAnnotation attribute.
96
97 /**
98 * Copy the module-info.class for the given module, add the
99 * Deprecated annotation, and write the updated module-info.class
100 * to a directory.
101 */
102 static void deprecateModule(String name,
103 boolean forRemoval,
104 String since,
105 Path output) throws IOException {
106 Module module = ModuleLayer.boot().findModule(name).orElse(null);
107 assertNotNull(module, name + " not found");
108
109 InputStream in = module.getResourceAsStream("module-info.class");
110 assertNotNull(in, "No module-info.class for " + name);
111
112 try (in) {
113 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
114 + ClassWriter.COMPUTE_FRAMES);
115
116 ClassVisitor cv = new ClassVisitor(Opcodes.ASM5, cw) { };
117
118 ClassReader cr = new ClassReader(in);
119
120 List<Attribute> attrs = new ArrayList<>();
121 attrs.add(new ClassFileAttributes.ModuleAttribute());
122 attrs.add(new ClassFileAttributes.ModulePackagesAttribute());
123 attrs.add(new ClassFileAttributes.ModuleTargetAttribute());
124 cr.accept(cv, attrs.toArray(new Attribute[0]), 0);
125
126 AnnotationVisitor annotationVisitor
127 = cv.visitAnnotation("Ljava/lang/Deprecated;", true);
128 annotationVisitor.visit("forRemoval", forRemoval);
129 annotationVisitor.visit("since", since);
130 annotationVisitor.visitEnd();
131
132 byte[] bytes = cw.toByteArray();
133 Path mi = output.resolve("module-info.class");
134 Files.write(mi, bytes);
135 }
136 }
137
138 /**
139 * Load the module of the given name in the given directory into a
140 * child layer.
141 */
142 static Module loadModule(Path dir, String name) throws IOException {
143 ModuleFinder finder = ModuleFinder.of(dir);
|
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.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Set;
36
37 import jdk.internal.org.objectweb.asm.AnnotationVisitor;
38 import jdk.internal.org.objectweb.asm.Attribute;
39 import jdk.internal.org.objectweb.asm.ClassReader;
40 import jdk.internal.org.objectweb.asm.ClassVisitor;
41 import jdk.internal.org.objectweb.asm.ClassWriter;
42 import jdk.internal.org.objectweb.asm.Opcodes;
43 import jdk.internal.org.objectweb.asm.commons.ModuleTargetAttribute;
44
45 import org.testng.annotations.Test;
46 import static org.testng.Assert.*;
47
48 /**
49 * @test
50 * @modules java.base/jdk.internal.org.objectweb.asm
51 * java.base/jdk.internal.org.objectweb.asm.commons
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 assertTrue(module.getDeclaredAnnotations().length == 0);
68 }
69
70 /**
71 * Test loading a module with a RuntimeVisibleAnnotation attribute.
97
98 /**
99 * Copy the module-info.class for the given module, add the
100 * Deprecated annotation, and write the updated module-info.class
101 * to a directory.
102 */
103 static void deprecateModule(String name,
104 boolean forRemoval,
105 String since,
106 Path output) throws IOException {
107 Module module = ModuleLayer.boot().findModule(name).orElse(null);
108 assertNotNull(module, name + " not found");
109
110 InputStream in = module.getResourceAsStream("module-info.class");
111 assertNotNull(in, "No module-info.class for " + name);
112
113 try (in) {
114 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
115 + ClassWriter.COMPUTE_FRAMES);
116
117 ClassVisitor cv = new ClassVisitor(Opcodes.ASM6, cw) { };
118
119 ClassReader cr = new ClassReader(in);
120 List<Attribute> attrs = new ArrayList<>();
121 attrs.add(new ModuleTargetAttribute());
122 cr.accept(cv, attrs.toArray(new Attribute[0]), 0);
123
124 AnnotationVisitor annotationVisitor
125 = cv.visitAnnotation("Ljava/lang/Deprecated;", true);
126 annotationVisitor.visit("forRemoval", forRemoval);
127 annotationVisitor.visit("since", since);
128 annotationVisitor.visitEnd();
129
130 byte[] bytes = cw.toByteArray();
131 Path mi = output.resolve("module-info.class");
132 Files.write(mi, bytes);
133 }
134 }
135
136 /**
137 * Load the module of the given name in the given directory into a
138 * child layer.
139 */
140 static Module loadModule(Path dir, String name) throws IOException {
141 ModuleFinder finder = ModuleFinder.of(dir);
|