jdk/test/java/lang/reflect/Module/AnnotationsTest.java
author alanb
Fri, 10 Feb 2017 09:04:39 +0000
changeset 43712 5dfd0950317c
parent 42703 20c39ea4a507
permissions -rw-r--r--
8173393: Module system implementation refresh (2/2017) Reviewed-by: dfuchs, psandoz, mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, claes.redestad@oracle.com, alex.buckley@oracle.com, mark.reinhold@oracle.com, john.r.rose@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     1
/*
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     4
 *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     8
 *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    13
 * accompanied this code).
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    14
 *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    18
 *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    21
 * questions.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    22
 */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    23
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    24
import java.io.IOException;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    25
import java.io.InputStream;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    26
import java.lang.annotation.Annotation;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    27
import java.lang.module.Configuration;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    28
import java.lang.module.ModuleFinder;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    29
import java.lang.reflect.Layer;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    30
import java.lang.reflect.Module;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    31
import java.nio.file.Files;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    32
import java.nio.file.Path;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    33
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    34
import java.util.ArrayList;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    35
import java.util.List;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    36
import java.util.Set;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    37
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    38
import jdk.internal.module.ClassFileAttributes;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    39
import jdk.internal.org.objectweb.asm.AnnotationVisitor;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    40
import jdk.internal.org.objectweb.asm.Attribute;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    41
import jdk.internal.org.objectweb.asm.ClassReader;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    42
import jdk.internal.org.objectweb.asm.ClassVisitor;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    43
import jdk.internal.org.objectweb.asm.ClassWriter;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    44
import jdk.internal.org.objectweb.asm.Opcodes;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    45
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    46
import org.testng.annotations.Test;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    47
import static org.testng.Assert.*;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    48
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    49
/**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    50
 * @test
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    51
 * @modules java.base/jdk.internal.org.objectweb.asm
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    52
 *          java.base/jdk.internal.module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    53
 *          java.xml
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    54
 * @run testng AnnotationsTest
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    55
 * @summary Basic test of annotations on modules
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    56
 */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    57
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    58
public class AnnotationsTest {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    59
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    60
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    61
     * Test that there are no annotations on an unnamed module.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    62
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    63
    @Test
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    64
    public void testUnnamedModule() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    65
        Module module = this.getClass().getModule();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    66
        assertTrue(module.getAnnotations().length == 0);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    67
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    68
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    69
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    70
     * Test loading a module with a RuntimeVisibleAnnotation attribute.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    71
     * The test copies the module-info.class for java.xml, adds the attribute,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    72
     * and then loads the updated module.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    73
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    74
    @Test
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    75
    public void testNamedModule() throws IOException {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    76
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    77
        // "deprecate" java.xml
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    78
        Path dir = Files.createTempDirectory("mods");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    79
        deprecateModule("java.xml", true, "9", dir);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    80
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    81
        // "load" the cloned java.xml
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    82
        Module module = loadModule(dir, "java.xml");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    83
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    84
        // check the annotation is present
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    85
        assertTrue(module.isAnnotationPresent(Deprecated.class));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    86
        Deprecated d = module.getAnnotation(Deprecated.class);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    87
        assertNotNull(d, "@Deprecated not found");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    88
        assertTrue(d.forRemoval());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    89
        assertEquals(d.since(), "9");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    90
        Annotation[] a = module.getAnnotations();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    91
        assertTrue(a.length == 1);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    92
        assertTrue(a[0] instanceof Deprecated);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    93
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    94
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    95
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    96
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    97
     * Copy the module-info.class for the given module, add the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    98
     * Deprecated annotation, and write the updated module-info.class
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    99
     * to a directory.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   100
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   101
    static void deprecateModule(String name,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   102
                                boolean forRemoval,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   103
                                String since,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   104
                                Path output) throws IOException {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   105
        Module module = Layer.boot().findModule(name).orElse(null);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   106
        assertNotNull(module, name + " not found");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   107
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   108
        InputStream in = module.getResourceAsStream("module-info.class");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   109
        assertNotNull(in, "No module-info.class for " + name);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   110
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   111
        try (in) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   112
            ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   113
                                             + ClassWriter.COMPUTE_FRAMES);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   114
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   115
            ClassVisitor cv = new ClassVisitor(Opcodes.ASM5, cw) { };
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   116
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   117
            ClassReader cr = new ClassReader(in);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   118
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   119
            List<Attribute> attrs = new ArrayList<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   120
            attrs.add(new ClassFileAttributes.ModuleAttribute());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   121
            attrs.add(new ClassFileAttributes.ModulePackagesAttribute());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   122
            attrs.add(new ClassFileAttributes.ModuleTargetAttribute());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   123
            cr.accept(cv, attrs.toArray(new Attribute[0]), 0);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   124
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   125
            AnnotationVisitor annotationVisitor
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   126
                = cv.visitAnnotation("Ljava/lang/Deprecated;", true);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   127
            annotationVisitor.visit("forRemoval", forRemoval);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   128
            annotationVisitor.visit("since", since);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   129
            annotationVisitor.visitEnd();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   130
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   131
            byte[] bytes = cw.toByteArray();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   132
            Path mi = output.resolve("module-info.class");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   133
            Files.write(mi, bytes);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   134
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   135
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   136
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   137
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   138
     * Load the module of the given name in the given directory into a
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   139
     * child layer.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   140
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   141
    static Module loadModule(Path dir, String name) throws IOException {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   142
        ModuleFinder finder = ModuleFinder.of(dir);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   143
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   144
        Layer bootLayer = Layer.boot();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   145
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   146
        Configuration cf = bootLayer.configuration()
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   147
                .resolve(finder, ModuleFinder.of(), Set.of(name));
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   148
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   149
        ClassLoader scl = ClassLoader.getSystemClassLoader();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   150
        Layer layer = bootLayer.defineModulesWithOneLoader(cf, scl);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   151
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   152
        Module module = layer.findModule(name).orElse(null);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   153
        assertNotNull(module, name + " not loaded");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   154
        return module;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   155
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   156
}