Path: blob/master/test/jdk/java/lang/ModuleTests/AnnotationsTest.java
41149 views
/*1* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.IOException;24import java.io.InputStream;25import java.io.OutputStream;26import java.lang.annotation.Annotation;27import java.lang.module.Configuration;28import java.lang.module.ModuleDescriptor;29import java.lang.module.ModuleFinder;30import java.net.URL;31import java.net.URLClassLoader;32import java.nio.file.Files;33import java.nio.file.Path;34import java.util.ArrayList;35import java.util.List;36import java.util.Set;3738import jdk.internal.module.ModuleInfoWriter;39import jdk.internal.org.objectweb.asm.AnnotationVisitor;40import jdk.internal.org.objectweb.asm.Attribute;41import jdk.internal.org.objectweb.asm.ClassReader;42import jdk.internal.org.objectweb.asm.ClassVisitor;43import jdk.internal.org.objectweb.asm.ClassWriter;44import jdk.internal.org.objectweb.asm.Opcodes;45import jdk.internal.org.objectweb.asm.commons.ModuleTargetAttribute;4647import org.testng.annotations.Test;48import static org.testng.Assert.*;4950/**51* @test52* @modules java.base/jdk.internal.org.objectweb.asm53* java.base/jdk.internal.org.objectweb.asm.commons54* java.base/jdk.internal.module55* @run testng AnnotationsTest56* @summary Basic test of annotations on modules57*/5859public class AnnotationsTest {6061/**62* Test that there are no annotations on an unnamed module.63*/64@Test65public void testUnnamedModule() {66Module module = this.getClass().getModule();67assertTrue(module.getAnnotations().length == 0);68assertTrue(module.getDeclaredAnnotations().length == 0);69}7071/**72* Test reflectively reading the annotations on a named module.73*/74@Test75public void testNamedModule() throws IOException {76Path mods = Files.createTempDirectory(Path.of(""), "mods");7778// @Deprecated(since="9", forRemoval=true) module foo { }79ModuleDescriptor descriptor = ModuleDescriptor.newModule("foo").build();80byte[] classBytes = ModuleInfoWriter.toBytes(descriptor);81classBytes = addDeprecated(classBytes, true, "9");82Files.write(mods.resolve("module-info.class"), classBytes);8384// create module layer with module foo85Module module = loadModule(mods, "foo");8687// check the annotation is present88assertTrue(module.isAnnotationPresent(Deprecated.class));89Deprecated d = module.getAnnotation(Deprecated.class);90assertNotNull(d, "@Deprecated not found");91assertTrue(d.forRemoval());92assertEquals(d.since(), "9");93Annotation[] a = module.getAnnotations();94assertTrue(a.length == 1);95assertTrue(a[0] instanceof Deprecated);96assertEquals(module.getDeclaredAnnotations(), a);97}9899/**100* Test reflectively reading annotations on a named module where the module101* is mapped to a class loader that can locate a module-info.class.102*/103@Test104public void testWithModuleInfoResourceXXXX() throws IOException {105Path mods = Files.createTempDirectory(Path.of(""), "mods");106107// classes directory with module-info.class108Path classes = Files.createTempDirectory(Path.of("."), "classes");109Path mi = classes.resolve("module-info.class");110try (OutputStream out = Files.newOutputStream(mi)) {111ModuleDescriptor descriptor = ModuleDescriptor.newModule("lurker").build();112ModuleInfoWriter.write(descriptor, out);113}114115// URLClassLoader that can locate a module-info.class resource116URL url = classes.toUri().toURL();117URLClassLoader loader = new URLClassLoader(new URL[] { url });118assertTrue(loader.findResource("module-info.class") != null);119120// module foo { }121ModuleDescriptor descriptor = ModuleDescriptor.newModule("foo").build();122byte[] classBytes = ModuleInfoWriter.toBytes(descriptor);123Files.write(mods.resolve("module-info.class"), classBytes);124125// create module layer with module foo126Module foo = loadModule(mods, "foo", loader);127128// check the annotation is not present129assertFalse(foo.isAnnotationPresent(Deprecated.class));130131// @Deprecated(since="11", forRemoval=true) module bar { }132descriptor = ModuleDescriptor.newModule("bar").build();133classBytes = ModuleInfoWriter.toBytes(descriptor);134classBytes = addDeprecated(classBytes, true, "11");135Files.write(mods.resolve("module-info.class"), classBytes);136137// create module layer with module bar138Module bar = loadModule(mods, "bar", loader);139140// check the annotation is present141assertTrue(bar.isAnnotationPresent(Deprecated.class));142}143144/**145* Adds the Deprecated annotation to the given module-info class file.146*/147static byte[] addDeprecated(byte[] bytes, boolean forRemoval, String since) {148ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS149+ ClassWriter.COMPUTE_FRAMES);150151ClassVisitor cv = new ClassVisitor(Opcodes.ASM6, cw) { };152153ClassReader cr = new ClassReader(bytes);154List<Attribute> attrs = new ArrayList<>();155attrs.add(new ModuleTargetAttribute());156cr.accept(cv, attrs.toArray(new Attribute[0]), 0);157158AnnotationVisitor annotationVisitor159= cv.visitAnnotation("Ljava/lang/Deprecated;", true);160annotationVisitor.visit("forRemoval", forRemoval);161annotationVisitor.visit("since", since);162annotationVisitor.visitEnd();163164return cw.toByteArray();165}166167/**168* Load the module of the given name in the given directory into a169* child layer with the given class loader as the parent class loader.170*/171static Module loadModule(Path dir, String name, ClassLoader parent)172throws IOException173{174ModuleFinder finder = ModuleFinder.of(dir);175176ModuleLayer bootLayer = ModuleLayer.boot();177178Configuration cf = bootLayer.configuration()179.resolve(finder, ModuleFinder.of(), Set.of(name));180181ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, parent);182183Module module = layer.findModule(name).orElse(null);184assertNotNull(module, name + " not loaded");185return module;186}187188static Module loadModule(Path dir, String name) throws IOException {189return loadModule(dir, name, ClassLoader.getSystemClassLoader());190}191}192193194