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