jdk/test/tools/jlink/asmplugin/IdentityPluginTest.java
author sundar
Mon, 16 May 2016 14:47:27 +0530
changeset 38320 e24c7029e8ba
parent 36511 9d0388c6b336
permissions -rw-r--r--
8156914: jlink API minor cleanups Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
 * Asm plugin testing.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
 * @test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 * @summary Test basic functionality.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
 * @author Jean-Francois Denise
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
 * @modules java.base/jdk.internal.org.objectweb.asm
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
 *          jdk.jlink/jdk.tools.jlink.internal
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 *          jdk.jlink/jdk.tools.jlink.internal.plugins.asm
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
 * @build AsmPluginTestBase
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
 * @run main IdentityPluginTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.io.ByteArrayInputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.io.IOException;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    38
import java.io.UncheckedIOException;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import jdk.internal.org.objectweb.asm.ClassReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import jdk.internal.org.objectweb.asm.ClassVisitor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import jdk.internal.org.objectweb.asm.ClassWriter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import jdk.internal.org.objectweb.asm.Opcodes;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import jdk.tools.jlink.internal.plugins.asm.AsmPool.WritableClassPool;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    45
import jdk.tools.jlink.plugin.ModuleEntry;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    46
import jdk.tools.jlink.plugin.ModulePool;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
public class IdentityPluginTest extends AsmPluginTestBase {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
    public static void main(String[] args) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
        if (!isImageBuild()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
            System.err.println("Test not run. Not image build.");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
            return;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
        new IdentityPluginTest().test();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
    public void test() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
        IdentityPlugin asm = new IdentityPlugin();
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    60
        ModulePool resourcePool = asm.visit(getPool());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
        asm.test(getPool(), resourcePool);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
    private class IdentityPlugin extends TestPlugin {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
        public void visit() {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    68
            for (ModuleEntry res : getPools().getGlobalPool().getClasses()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
                if (res.getPath().endsWith("module-info.class")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
                ClassReader reader = getPools().getGlobalPool().getClassReader(res);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
                ClassWriter writer = new ClassWriter(reader, ClassWriter.COMPUTE_FRAMES);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
                IdentityClassVisitor visitor = new IdentityClassVisitor(writer);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
                reader.accept(visitor, ClassReader.EXPAND_FRAMES);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
                getPools().getGlobalPool().getTransformedClasses().addClass(writer);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
        @Override
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    81
        public void test(ModulePool inResources, ModulePool outResources) throws IOException {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
            if (outResources.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
                throw new AssertionError("Empty result");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
            if (!isVisitCalled()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
                throw new AssertionError("Resources not visited");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
            WritableClassPool transformedClasses = getPools().getGlobalPool().getTransformedClasses();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
            if (transformedClasses.getClasses().size() != getClasses().size()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
                throw new AssertionError("Number of transformed classes not equal to expected");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
            for (String className : getClasses()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
                if (transformedClasses.getClassReader(className) == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
                    throw new AssertionError("Class not transformed " + className);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
            }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    97
            outResources.entries().forEach(r -> {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
                if (r.getPath().endsWith(".class") && !r.getPath().endsWith("module-info.class")) {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    99
                    try {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   100
                        ClassReader reader = new ClassReader(new ByteArrayInputStream(r.getBytes()));
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   101
                        ClassWriter w = new ClassWriter(reader, ClassWriter.COMPUTE_FRAMES);
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   102
                        reader.accept(w, ClassReader.EXPAND_FRAMES);
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   103
                    } catch (IOException exp) {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   104
                        throw new UncheckedIOException(exp);
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   105
                    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
                }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   107
            });
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
        public String getName() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
            return "identity-plugin";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
    private static class IdentityClassVisitor extends ClassVisitor {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
        public IdentityClassVisitor(ClassWriter cv) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
            super(Opcodes.ASM5, cv);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
}