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;
31import java.nio.file.Paths;
32
33import java.util.ArrayList;
34import java.util.List;
35import java.util.Set;
36
37import jdk.internal.module.ClassFileAttributes;
38import jdk.internal.org.objectweb.asm.AnnotationVisitor;
39import jdk.internal.org.objectweb.asm.Attribute;
40import jdk.internal.org.objectweb.asm.ClassReader;
41import jdk.internal.org.objectweb.asm.ClassVisitor;
42import jdk.internal.org.objectweb.asm.ClassWriter;
43import jdk.internal.org.objectweb.asm.Opcodes;
44
45import org.testng.annotations.Test;
46import 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
57public 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.
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(Paths.get(""), "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        assertEquals(module.getDeclaredAnnotations(), a);
94    }
95
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);
144
145        ModuleLayer bootLayer = ModuleLayer.boot();
146
147        Configuration cf = bootLayer.configuration()
148                .resolve(finder, ModuleFinder.of(), Set.of(name));
149
150        ClassLoader scl = ClassLoader.getSystemClassLoader();
151        ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, scl);
152
153        Module module = layer.findModule(name).orElse(null);
154        assertNotNull(module, name + " not loaded");
155        return module;
156    }
157}
158