jdk/test/java/lang/ModuleTests/AnnotationsTest.java
changeset 44545 83b611b88ac8
child 45652 33342314ce89
equal deleted inserted replaced
44480:2c33418a6d57 44545:83b611b88ac8
       
     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.nio.file.Files;
       
    30 import java.nio.file.Path;
       
    31 
       
    32 import java.util.ArrayList;
       
    33 import java.util.List;
       
    34 import java.util.Set;
       
    35 
       
    36 import jdk.internal.module.ClassFileAttributes;
       
    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 
       
    44 import org.testng.annotations.Test;
       
    45 import 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 
       
    56 public 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     }
       
    66 
       
    67     /**
       
    68      * Test loading a module with a RuntimeVisibleAnnotation attribute.
       
    69      * The test copies the module-info.class for java.xml, adds the attribute,
       
    70      * and then loads the updated module.
       
    71      */
       
    72     @Test
       
    73     public void testNamedModule() throws IOException {
       
    74 
       
    75         // "deprecate" java.xml
       
    76         Path dir = Files.createTempDirectory("mods");
       
    77         deprecateModule("java.xml", true, "9", dir);
       
    78 
       
    79         // "load" the cloned java.xml
       
    80         Module module = loadModule(dir, "java.xml");
       
    81 
       
    82         // check the annotation is present
       
    83         assertTrue(module.isAnnotationPresent(Deprecated.class));
       
    84         Deprecated d = module.getAnnotation(Deprecated.class);
       
    85         assertNotNull(d, "@Deprecated not found");
       
    86         assertTrue(d.forRemoval());
       
    87         assertEquals(d.since(), "9");
       
    88         Annotation[] a = module.getAnnotations();
       
    89         assertTrue(a.length == 1);
       
    90         assertTrue(a[0] instanceof Deprecated);
       
    91     }
       
    92 
       
    93 
       
    94     /**
       
    95      * Copy the module-info.class for the given module, add the
       
    96      * Deprecated annotation, and write the updated module-info.class
       
    97      * to a directory.
       
    98      */
       
    99     static void deprecateModule(String name,
       
   100                                 boolean forRemoval,
       
   101                                 String since,
       
   102                                 Path output) throws IOException {
       
   103         Module module = ModuleLayer.boot().findModule(name).orElse(null);
       
   104         assertNotNull(module, name + " not found");
       
   105 
       
   106         InputStream in = module.getResourceAsStream("module-info.class");
       
   107         assertNotNull(in, "No module-info.class for " + name);
       
   108 
       
   109         try (in) {
       
   110             ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
       
   111                                              + ClassWriter.COMPUTE_FRAMES);
       
   112 
       
   113             ClassVisitor cv = new ClassVisitor(Opcodes.ASM5, cw) { };
       
   114 
       
   115             ClassReader cr = new ClassReader(in);
       
   116 
       
   117             List<Attribute> attrs = new ArrayList<>();
       
   118             attrs.add(new ClassFileAttributes.ModuleAttribute());
       
   119             attrs.add(new ClassFileAttributes.ModulePackagesAttribute());
       
   120             attrs.add(new ClassFileAttributes.ModuleTargetAttribute());
       
   121             cr.accept(cv, attrs.toArray(new Attribute[0]), 0);
       
   122 
       
   123             AnnotationVisitor annotationVisitor
       
   124                 = cv.visitAnnotation("Ljava/lang/Deprecated;", true);
       
   125             annotationVisitor.visit("forRemoval", forRemoval);
       
   126             annotationVisitor.visit("since", since);
       
   127             annotationVisitor.visitEnd();
       
   128 
       
   129             byte[] bytes = cw.toByteArray();
       
   130             Path mi = output.resolve("module-info.class");
       
   131             Files.write(mi, bytes);
       
   132         }
       
   133     }
       
   134 
       
   135     /**
       
   136      * Load the module of the given name in the given directory into a
       
   137      * child layer.
       
   138      */
       
   139     static Module loadModule(Path dir, String name) throws IOException {
       
   140         ModuleFinder finder = ModuleFinder.of(dir);
       
   141 
       
   142         ModuleLayer bootLayer = ModuleLayer.boot();
       
   143 
       
   144         Configuration cf = bootLayer.configuration()
       
   145                 .resolve(finder, ModuleFinder.of(), Set.of(name));
       
   146 
       
   147         ClassLoader scl = ClassLoader.getSystemClassLoader();
       
   148         ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, scl);
       
   149 
       
   150         Module module = layer.findModule(name).orElse(null);
       
   151         assertNotNull(module, name + " not loaded");
       
   152         return module;
       
   153     }
       
   154 }