AnnotationsTest.java revision 17000:fa3c4a60a616
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 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 } 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} 155