author | alanb |
Fri, 07 Apr 2017 08:05:54 +0000 | |
changeset 44545 | 83b611b88ac8 |
parent 43185 | d75d9ff8d4e7 |
permissions | -rw-r--r-- |
36511 | 1 |
/* |
2 |
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @summary Test previsitor |
|
27 |
* @author Andrei Eremeev |
|
43185 | 28 |
* @modules jdk.jlink/jdk.tools.jlink |
29 |
* jdk.jlink/jdk.tools.jlink.internal |
|
30 |
* jdk.jlink/jdk.tools.jlink.plugin |
|
36511 | 31 |
* @run main/othervm PrevisitorTest |
32 |
*/ |
|
33 |
import java.nio.ByteOrder; |
|
34 |
import java.util.ArrayList; |
|
35 |
import java.util.Collection; |
|
36 |
import java.util.Collections; |
|
37 |
import java.util.HashMap; |
|
38 |
import java.util.List; |
|
39 |
import java.util.Map; |
|
38320 | 40 |
import java.util.Optional; |
36511 | 41 |
import java.util.stream.Collectors; |
42 |
||
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 | 45 |
import jdk.tools.jlink.internal.PluginRepository; |
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 | 49 |
import jdk.tools.jlink.internal.ResourcePrevisitor; |
50 |
import jdk.tools.jlink.internal.StringTable; |
|
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 | 55 |
|
56 |
public class PrevisitorTest { |
|
57 |
||
58 |
public static void main(String[] args) throws Exception { |
|
59 |
new PrevisitorTest().test(); |
|
60 |
} |
|
61 |
||
62 |
private static Plugin createPlugin(String name) { |
|
63 |
return Jlink.newPlugin(name, Collections.emptyMap(), null); |
|
64 |
} |
|
65 |
||
66 |
public void test() throws Exception { |
|
67 |
CustomPlugin plugin = new CustomPlugin(); |
|
68 |
PluginRepository.registerPlugin(plugin); |
|
69 |
List<Plugin> plugins = new ArrayList<>(); |
|
70 |
plugins.add(createPlugin(CustomPlugin.NAME)); |
|
71 |
ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(new Jlink.PluginsConfiguration(plugins, |
|
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 | 80 |
Collection<String> input = inResources.entries() |
36511 | 81 |
.map(Object::toString) |
82 |
.collect(Collectors.toList()); |
|
38320 | 83 |
Collection<String> output = outResources.entries() |
36511 | 84 |
.map(Object::toString) |
85 |
.collect(Collectors.toList()); |
|
86 |
if (!input.equals(output)) { |
|
87 |
throw new AssertionError("Input and output resources differ: input: " |
|
88 |
+ input + ", output: " + output); |
|
89 |
} |
|
90 |
} |
|
91 |
||
92 |
private static class CustomStringTable implements StringTable { |
|
93 |
||
94 |
private final List<String> strings = new ArrayList<>(); |
|
95 |
||
96 |
@Override |
|
97 |
public int addString(String str) { |
|
98 |
strings.add(str); |
|
99 |
return strings.size() - 1; |
|
100 |
} |
|
101 |
||
102 |
@Override |
|
103 |
public String getString(int id) { |
|
104 |
return strings.get(id); |
|
105 |
} |
|
106 |
||
107 |
public int size() { |
|
108 |
return strings.size(); |
|
109 |
} |
|
110 |
} |
|
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 | 113 |
|
114 |
private static String NAME = "plugin"; |
|
115 |
||
116 |
private boolean isPrevisitCalled = false; |
|
117 |
||
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 | 120 |
if (!isPrevisitCalled) { |
121 |
throw new AssertionError("Previsit was not called"); |
|
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 | 124 |
if (table.size() == 0) { |
125 |
throw new AssertionError("Table is empty"); |
|
126 |
} |
|
127 |
Map<String, Integer> count = new HashMap<>(); |
|
128 |
for (int i = 0; i < table.size(); ++i) { |
|
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 | 131 |
if (e.isPresent()) { |
36511 | 132 |
throw new AssertionError(); |
133 |
} |
|
134 |
count.compute(s, (k, c) -> 1 + (c == null ? 0 : c)); |
|
135 |
} |
|
136 |
count.forEach((k, v) -> { |
|
137 |
if (v != 1) { |
|
138 |
throw new AssertionError("Expected one entry in the table, got: " + v + " for " + k); |
|
139 |
} |
|
140 |
}); |
|
38320 | 141 |
inResources.entries().forEach(r -> { |
36511 | 142 |
outResources.add(r); |
38320 | 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 | 146 |
} |
147 |
||
148 |
@Override |
|
149 |
public String getName() { |
|
150 |
return NAME; |
|
151 |
} |
|
152 |
||
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 | 155 |
isPrevisitCalled = true; |
38320 | 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 | 158 |
int lastIndexOf = s.lastIndexOf('/'); |
159 |
if (lastIndexOf >= 0) { |
|
160 |
strings.addString(s.substring(0, lastIndexOf)); |
|
161 |
} |
|
38320 | 162 |
}); |
36511 | 163 |
} |
164 |
} |
|
165 |
} |