author | rriggs |
Sun, 29 Oct 2017 18:20:53 -0400 | |
changeset 47471 | 304ef03403b1 |
parent 47216 | 71c04702a3d5 |
child 47489 | 6d0e943bcd24 |
permissions | -rw-r--r-- |
36511 | 1 |
/* |
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
2 |
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. |
36511 | 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. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.internal.module; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.io.InputStream; |
|
30 |
import java.io.OutputStream; |
|
31 |
import java.lang.module.ModuleDescriptor.Version; |
|
32 |
import java.util.ArrayList; |
|
33 |
import java.util.Collections; |
|
34 |
import java.util.HashMap; |
|
35 |
import java.util.List; |
|
36 |
import java.util.Map; |
|
37 |
import java.util.Set; |
|
38 |
||
39 |
import jdk.internal.org.objectweb.asm.Attribute; |
|
40 |
import jdk.internal.org.objectweb.asm.ClassReader; |
|
41 |
import jdk.internal.org.objectweb.asm.ClassVisitor; |
|
42 |
import jdk.internal.org.objectweb.asm.ClassWriter; |
|
43 |
import jdk.internal.org.objectweb.asm.Opcodes; |
|
44 |
||
45 |
import static jdk.internal.module.ClassFileAttributes.*; |
|
46 |
||
47 |
/** |
|
48 |
* Utility class to extend a module-info.class with additional attributes. |
|
49 |
*/ |
|
50 |
||
51 |
public final class ModuleInfoExtender { |
|
52 |
||
53 |
// the input stream to read the original module-info.class |
|
54 |
private final InputStream in; |
|
55 |
||
43336
be9fca030f91
8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents:
42703
diff
changeset
|
56 |
// the packages in the ModulePackages attribute |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41352
diff
changeset
|
57 |
private Set<String> packages; |
36511 | 58 |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
43336
diff
changeset
|
59 |
// the value for the module version in the Module attribute |
36511 | 60 |
private Version version; |
61 |
||
43336
be9fca030f91
8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents:
42703
diff
changeset
|
62 |
// the value of the ModuleMainClass attribute |
36511 | 63 |
private String mainClass; |
64 |
||
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44359
diff
changeset
|
65 |
// the value for the ModuleTarget attribute |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44359
diff
changeset
|
66 |
private String targetPlatform; |
36511 | 67 |
|
43336
be9fca030f91
8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents:
42703
diff
changeset
|
68 |
// the hashes for the ModuleHashes attribute |
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
69 |
private ModuleHashes hashes; |
36511 | 70 |
|
42703
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
71 |
// the value of the ModuleResolution attribute |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
72 |
private ModuleResolution moduleResolution; |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
73 |
|
36511 | 74 |
private ModuleInfoExtender(InputStream in) { |
75 |
this.in = in; |
|
76 |
} |
|
77 |
||
78 |
/** |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
43336
diff
changeset
|
79 |
* Sets the packages for the ModulePackages attribute |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
43336
diff
changeset
|
80 |
* |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
43336
diff
changeset
|
81 |
* @apiNote This method does not check that the package names are legal |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
43336
diff
changeset
|
82 |
* package names or that the set of packages is a super set of the |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
43336
diff
changeset
|
83 |
* packages in the module. |
36511 | 84 |
*/ |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41352
diff
changeset
|
85 |
public ModuleInfoExtender packages(Set<String> packages) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41352
diff
changeset
|
86 |
this.packages = Collections.unmodifiableSet(packages); |
36511 | 87 |
return this; |
88 |
} |
|
89 |
||
90 |
/** |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
43336
diff
changeset
|
91 |
* Sets the value for the module version in the Module attribute |
36511 | 92 |
*/ |
93 |
public ModuleInfoExtender version(Version version) { |
|
94 |
this.version = version; |
|
95 |
return this; |
|
96 |
} |
|
97 |
||
98 |
/** |
|
43336
be9fca030f91
8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents:
42703
diff
changeset
|
99 |
* Sets the value of the ModuleMainClass attribute. |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
43336
diff
changeset
|
100 |
* |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
43336
diff
changeset
|
101 |
* @apiNote This method does not check that the main class is a legal |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
43336
diff
changeset
|
102 |
* class name in a named package. |
36511 | 103 |
*/ |
104 |
public ModuleInfoExtender mainClass(String mainClass) { |
|
105 |
this.mainClass = mainClass; |
|
106 |
return this; |
|
107 |
} |
|
108 |
||
109 |
/** |
|
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44359
diff
changeset
|
110 |
* Sets the value for the ModuleTarget attribute. |
36511 | 111 |
*/ |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44359
diff
changeset
|
112 |
public ModuleInfoExtender targetPlatform(String targetPlatform) { |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44359
diff
changeset
|
113 |
this.targetPlatform = targetPlatform; |
36511 | 114 |
return this; |
115 |
} |
|
116 |
||
117 |
/** |
|
43336
be9fca030f91
8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents:
42703
diff
changeset
|
118 |
* The ModuleHashes attribute will be emitted to the module-info with |
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
119 |
* the hashes encapsulated in the given {@code ModuleHashes} |
36511 | 120 |
* object. |
121 |
*/ |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
122 |
public ModuleInfoExtender hashes(ModuleHashes hashes) { |
36511 | 123 |
this.hashes = hashes; |
124 |
return this; |
|
125 |
} |
|
126 |
||
127 |
/** |
|
42703
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
128 |
* Sets the value for the ModuleResolution attribute. |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
129 |
*/ |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
130 |
public ModuleInfoExtender moduleResolution(ModuleResolution mres) { |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
131 |
this.moduleResolution = mres; |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
132 |
return this; |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
133 |
} |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
134 |
|
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
135 |
/** |
36511 | 136 |
* A ClassVisitor that supports adding class file attributes. If an |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
43336
diff
changeset
|
137 |
* attribute already exists then the first occurrence of the attribute |
36511 | 138 |
* is replaced. |
139 |
*/ |
|
140 |
private static class AttributeAddingClassVisitor extends ClassVisitor { |
|
141 |
private Map<String, Attribute> attrs = new HashMap<>(); |
|
142 |
||
143 |
AttributeAddingClassVisitor(int api, ClassVisitor cv) { |
|
144 |
super(api, cv); |
|
145 |
} |
|
146 |
||
147 |
void addAttribute(Attribute attr) { |
|
148 |
attrs.put(attr.type, attr); |
|
149 |
} |
|
150 |
||
151 |
@Override |
|
152 |
public void visitAttribute(Attribute attr) { |
|
153 |
String name = attr.type; |
|
154 |
Attribute replacement = attrs.get(name); |
|
155 |
if (replacement != null) { |
|
156 |
attr = replacement; |
|
157 |
attrs.remove(name); |
|
158 |
} |
|
159 |
super.visitAttribute(attr); |
|
160 |
} |
|
161 |
||
162 |
/** |
|
163 |
* Adds any remaining attributes that weren't replaced to the |
|
164 |
* class file. |
|
165 |
*/ |
|
166 |
void finish() { |
|
167 |
attrs.values().forEach(a -> super.visitAttribute(a)); |
|
168 |
attrs.clear(); |
|
169 |
} |
|
170 |
} |
|
171 |
||
172 |
/** |
|
173 |
* Outputs the modified module-info.class to the given output stream. |
|
174 |
* Once this method has been called then the Extender object should |
|
175 |
* be discarded. |
|
176 |
*/ |
|
177 |
public void write(OutputStream out) throws IOException { |
|
41352 | 178 |
// emit to the output stream |
179 |
out.write(toByteArray()); |
|
180 |
} |
|
181 |
||
182 |
/** |
|
183 |
* Returns the bytes of the modified module-info.class. |
|
184 |
* Once this method has been called then the Extender object should |
|
185 |
* be discarded. |
|
186 |
*/ |
|
187 |
public byte[] toByteArray() throws IOException { |
|
36511 | 188 |
ClassWriter cw |
189 |
= new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES); |
|
190 |
||
191 |
AttributeAddingClassVisitor cv |
|
192 |
= new AttributeAddingClassVisitor(Opcodes.ASM5, cw); |
|
193 |
||
194 |
ClassReader cr = new ClassReader(in); |
|
195 |
||
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41352
diff
changeset
|
196 |
if (packages != null) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41352
diff
changeset
|
197 |
cv.addAttribute(new ModulePackagesAttribute(packages)); |
36511 | 198 |
if (mainClass != null) |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41352
diff
changeset
|
199 |
cv.addAttribute(new ModuleMainClassAttribute(mainClass)); |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44359
diff
changeset
|
200 |
if (targetPlatform != null) |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44359
diff
changeset
|
201 |
cv.addAttribute(new ModuleTargetAttribute(targetPlatform)); |
36511 | 202 |
if (hashes != null) |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41352
diff
changeset
|
203 |
cv.addAttribute(new ModuleHashesAttribute(hashes)); |
42703
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
204 |
if (moduleResolution != null) |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
205 |
cv.addAttribute(new ModuleResolutionAttribute(moduleResolution.value())); |
36511 | 206 |
|
207 |
List<Attribute> attrs = new ArrayList<>(); |
|
208 |
||
209 |
// prototypes of attributes that should be parsed |
|
42703
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
210 |
attrs.add(new ModuleAttribute(version)); |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41352
diff
changeset
|
211 |
attrs.add(new ModulePackagesAttribute()); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41352
diff
changeset
|
212 |
attrs.add(new ModuleMainClassAttribute()); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41352
diff
changeset
|
213 |
attrs.add(new ModuleTargetAttribute()); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41352
diff
changeset
|
214 |
attrs.add(new ModuleHashesAttribute()); |
36511 | 215 |
|
216 |
cr.accept(cv, attrs.toArray(new Attribute[0]), 0); |
|
217 |
||
218 |
// add any attributes that didn't replace previous attributes |
|
219 |
cv.finish(); |
|
220 |
||
41352 | 221 |
return cw.toByteArray(); |
36511 | 222 |
} |
223 |
||
224 |
/** |
|
225 |
* Returns an {@code Extender} that may be used to add additional |
|
226 |
* attributes to the module-info.class read from the given input |
|
227 |
* stream. |
|
228 |
*/ |
|
229 |
public static ModuleInfoExtender newExtender(InputStream in) { |
|
230 |
return new ModuleInfoExtender(in); |
|
231 |
} |
|
232 |
||
233 |
} |