jdk/test/tools/jlink/plugins/PrevisitorTest.java
author alanb
Fri, 07 Apr 2017 08:05:54 +0000
changeset 44545 83b611b88ac8
parent 43185 d75d9ff8d4e7
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
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
 * @test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
 * @summary Test previsitor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 * @author Andrei Eremeev
43185
d75d9ff8d4e7 8171380: Remove all exports from jdk.jlink
chegar
parents: 41559
diff changeset
    28
 * @modules jdk.jlink/jdk.tools.jlink
d75d9ff8d4e7 8171380: Remove all exports from jdk.jlink
chegar
parents: 41559
diff changeset
    29
 *          jdk.jlink/jdk.tools.jlink.internal
d75d9ff8d4e7 8171380: Remove all exports from jdk.jlink
chegar
parents: 41559
diff changeset
    30
 *          jdk.jlink/jdk.tools.jlink.plugin
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 * @run main/othervm PrevisitorTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.nio.ByteOrder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.ArrayList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.Collection;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.Map;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    40
import java.util.Optional;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.stream.Collectors;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import jdk.tools.jlink.internal.ImagePluginConfiguration;
41559
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents: 39834
diff changeset
    44
import jdk.tools.jlink.internal.Jlink;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import jdk.tools.jlink.internal.PluginRepository;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import jdk.tools.jlink.internal.ImagePluginStack;
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    47
import jdk.tools.jlink.internal.ResourcePoolManager;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    48
import jdk.tools.jlink.internal.ResourcePoolManager.ResourcePoolImpl;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import jdk.tools.jlink.internal.ResourcePrevisitor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import jdk.tools.jlink.internal.StringTable;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import jdk.tools.jlink.plugin.Plugin;
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    52
import jdk.tools.jlink.plugin.ResourcePool;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    53
import jdk.tools.jlink.plugin.ResourcePoolBuilder;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    54
import jdk.tools.jlink.plugin.ResourcePoolEntry;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
public class PrevisitorTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
    public static void main(String[] args) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
        new PrevisitorTest().test();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
    private static Plugin createPlugin(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
        return Jlink.newPlugin(name, Collections.emptyMap(), null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
    public void test() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
        CustomPlugin plugin = new CustomPlugin();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
        PluginRepository.registerPlugin(plugin);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        List<Plugin> plugins = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
        plugins.add(createPlugin(CustomPlugin.NAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(new Jlink.PluginsConfiguration(plugins,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
                null, null));
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    73
        ResourcePoolManager inResources = new ResourcePoolManager(ByteOrder.nativeOrder(), new CustomStringTable());
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    74
        inResources.add(ResourcePoolEntry.create("/aaa/bbb/res1.class", new byte[90]));
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    75
        inResources.add(ResourcePoolEntry.create("/aaa/bbb/res2.class", new byte[90]));
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    76
        inResources.add(ResourcePoolEntry.create("/aaa/bbb/res3.class", new byte[90]));
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    77
        inResources.add(ResourcePoolEntry.create("/aaa/ddd/res1.class", new byte[90]));
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    78
        inResources.add(ResourcePoolEntry.create("/aaa/res1.class", new byte[90]));
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    79
        ResourcePool outResources = stack.visitResources(inResources);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    80
        Collection<String> input = inResources.entries()
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
                .map(Object::toString)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
                .collect(Collectors.toList());
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    83
        Collection<String> output = outResources.entries()
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
                .map(Object::toString)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
                .collect(Collectors.toList());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
        if (!input.equals(output)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
            throw new AssertionError("Input and output resources differ: input: "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
                    + input + ", output: " + output);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
    private static class CustomStringTable implements StringTable {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
        private final List<String> strings = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
        public int addString(String str) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
            strings.add(str);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
            return strings.size() - 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
        public String getString(int id) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
            return strings.get(id);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
        public int size() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
            return strings.size();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39129
diff changeset
   112
    private static class CustomPlugin implements Plugin, ResourcePrevisitor {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
        private static String NAME = "plugin";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
        private boolean isPrevisitCalled = false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
        @Override
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   119
        public ResourcePool transform(ResourcePool inResources, ResourcePoolBuilder outResources) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
            if (!isPrevisitCalled) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
                throw new AssertionError("Previsit was not called");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
            }
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   123
            CustomStringTable table = (CustomStringTable)((ResourcePoolImpl)inResources).getStringTable();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
            if (table.size() == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
                throw new AssertionError("Table is empty");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
            Map<String, Integer> count = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
            for (int i = 0; i < table.size(); ++i) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
                String s = table.getString(i);
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   130
                Optional<ResourcePoolEntry> e = inResources.findEntry(s);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   131
                if (e.isPresent()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
                    throw new AssertionError();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
                count.compute(s, (k, c) -> 1 + (c == null ? 0 : c));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
            count.forEach((k, v) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
                if (v != 1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
                    throw new AssertionError("Expected one entry in the table, got: " + v + " for " + k);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
            });
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   141
            inResources.entries().forEach(r -> {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
                outResources.add(r);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   143
            });
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   144
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   145
            return outResources.build();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
        public String getName() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
            return NAME;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
        @Override
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   154
        public void previsit(ResourcePool resources, StringTable strings) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
            isPrevisitCalled = true;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   156
            resources.entries().forEach(r -> {
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   157
                String s = r.path();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
                int lastIndexOf = s.lastIndexOf('/');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
                if (lastIndexOf >= 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
                    strings.addString(s.substring(0, lastIndexOf));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
                }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   162
            });
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
}