1/*
2 * Copyright (c) 2016, 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
24import java.io.IOException;
25import java.io.InputStream;
26import java.lang.annotation.Annotation;
27import java.lang.module.Configuration;
28import java.lang.module.ModuleFinder;
29import java.nio.file.Files;
30import java.nio.file.Path;
31
32import java.util.ArrayList;
33import java.util.List;
34import java.util.Set;
35
36import jdk.internal.module.ClassFileAttributes;
37import jdk.internal.org.objectweb.asm.AnnotationVisitor;
38import jdk.internal.org.objectweb.asm.Attribute;
39import jdk.internal.org.objectweb.asm.ClassReader;
40import jdk.internal.org.objectweb.asm.ClassVisitor;
41import jdk.internal.org.objectweb.asm.ClassWriter;
42import jdk.internal.org.objectweb.asm.Opcodes;
43
44import org.testng.annotations.Test;
45import static org.testng.Assert.*;
46
47/**
48 * @test
49 * @modules java.base/jdk.internal.org.objectweb.asm
50 *          java.base/jdk.internal.module
51 *          java.xml
52 * @run testng AnnotationsTest
53 * @summary Basic test of annotations on modules
54 */
55
56public class AnnotationsTest {
57
58    /**
59     * Test that there are no annotations on an unnamed module.
60     */
61    @Test
62    public void testUnnamedModule() {
63        Module module = this.getClass().getModule();
64        assertTrue(module.getAnnotations().length == 0);
65        assertTrue(module.getDeclaredAnnotations().length == 0);
66    }
67
68    /**
69     * Test loading a module with a RuntimeVisibleAnnotation attribute.
70     * The test copies the module-info.class for java.xml, adds the attribute,
71     * and then loads the updated module.
72     */
73    @Test
74    public void testNamedModule() throws IOException {
75
76        // "deprecate" java.xml
77        Path dir = Files.createTempDirectory("mods");
78        deprecateModule("java.xml", true, "9", dir);
79
80        // "load" the cloned java.xml
81        Module module = loadModule(dir, "java.xml");
82
83        // check the annotation is present
84        assertTrue(module.isAnnotationPresent(Deprecated.class));
85        Deprecated d = module.getAnnotation(Deprecated.class);
86        assertNotNull(d, "@Deprecated not found");
87        assertTrue(d.forRemoval());
88        assertEquals(d.since(), "9");
89        Annotation[] a = module.getAnnotations();
90        assertTrue(a.length == 1);
91        assertTrue(a[0] instanceof Deprecated);
92        assertEquals(module.getDeclaredAnnotations(), a);
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 = ModuleLayer.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        ModuleLayer bootLayer = ModuleLayer.boot();
145
146        Configuration cf = bootLayer.configuration()
147                .resolve(finder, ModuleFinder.of(), Set.of(name));
148
149        ClassLoader scl = ClassLoader.getSystemClassLoader();
150        ModuleLayer 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}
157