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 
  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.
  72      * The test copies the module-info.class for java.xml, adds the attribute,
  73      * and then loads the updated module.
  74      */
  75     @Test
  76     public void testNamedModule() throws IOException {
  77 
  78         // "deprecate" java.xml
  79         Path dir = Files.createTempDirectory(Paths.get(""), "mods");
  80         deprecateModule("java.xml", true, "9", dir);
  81 
  82         // "load" the cloned java.xml
  83         Module module = loadModule(dir, "java.xml");
  84 
  85         // check the annotation is present
  86         assertTrue(module.isAnnotationPresent(Deprecated.class));
  87         Deprecated d = module.getAnnotation(Deprecated.class);
  88         assertNotNull(d, "@Deprecated not found");
  89         assertTrue(d.forRemoval());
  90         assertEquals(d.since(), "9");
  91         Annotation[] a = module.getAnnotations();
  92         assertTrue(a.length == 1);
  93         assertTrue(a[0] instanceof Deprecated);
  94         assertEquals(module.getDeclaredAnnotations(), a);
  95     }
  96 
  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);
 142 
 143         ModuleLayer bootLayer = ModuleLayer.boot();
 144 
 145         Configuration cf = bootLayer.configuration()
 146                 .resolve(finder, ModuleFinder.of(), Set.of(name));
 147 
 148         ClassLoader scl = ClassLoader.getSystemClassLoader();
 149         ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, scl);
 150 
 151         Module module = layer.findModule(name).orElse(null);
 152         assertNotNull(module, name + " not loaded");
 153         return module;
 154     }
 155 }