author | alanb |
Thu, 01 Dec 2016 08:57:53 +0000 | |
changeset 42338 | a60f280f803c |
parent 40261 | 86a49ba76f52 |
child 42703 | 20c39ea4a507 |
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 java.lang.module; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.io.UncheckedIOException; |
|
30 |
import java.net.URI; |
|
31 |
import java.util.Objects; |
|
32 |
import java.util.Optional; |
|
33 |
import java.util.function.Supplier; |
|
34 |
||
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
35 |
import jdk.internal.module.ModuleHashes.HashSupplier; |
36511 | 36 |
|
37 |
||
38 |
/** |
|
39 |
* A reference to a module's content. |
|
40 |
* |
|
41 |
* <p> A module reference contains the module's descriptor and its location, if |
|
42 |
* known. It also has the ability to create a {@link ModuleReader} in order to |
|
43 |
* access the module's content, which may be inside the Java run-time system |
|
44 |
* itself or in an artifact such as a modular JAR file. |
|
45 |
* |
|
46 |
* @see ModuleFinder |
|
47 |
* @see ModuleReader |
|
48 |
* @since 9 |
|
49 |
*/ |
|
50 |
||
51 |
public final class ModuleReference { |
|
52 |
||
53 |
private final ModuleDescriptor descriptor; |
|
54 |
private final URI location; |
|
55 |
private final Supplier<ModuleReader> readerSupplier; |
|
56 |
||
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
57 |
// true if this is a reference to a patched module |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
58 |
private boolean patched; |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
59 |
|
36511 | 60 |
// the function that computes the hash of this module reference |
61 |
private final HashSupplier hasher; |
|
62 |
||
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
40261
diff
changeset
|
63 |
// cached hash to avoid needing to compute it many times |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
40261
diff
changeset
|
64 |
private byte[] cachedHash; |
36511 | 65 |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
66 |
|
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
67 |
/** |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
68 |
* Constructs a new instance of this class. |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
69 |
*/ |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
70 |
ModuleReference(ModuleDescriptor descriptor, |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
71 |
URI location, |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
72 |
Supplier<ModuleReader> readerSupplier, |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
73 |
boolean patched, |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
74 |
HashSupplier hasher) |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
75 |
|
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
76 |
{ |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
77 |
this.descriptor = Objects.requireNonNull(descriptor); |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
78 |
this.location = location; |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
79 |
this.readerSupplier = Objects.requireNonNull(readerSupplier); |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
80 |
this.patched = patched; |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
81 |
this.hasher = hasher; |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
82 |
} |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
83 |
|
36511 | 84 |
/** |
85 |
* Constructs a new instance of this class. |
|
86 |
*/ |
|
87 |
ModuleReference(ModuleDescriptor descriptor, |
|
88 |
URI location, |
|
89 |
Supplier<ModuleReader> readerSupplier, |
|
90 |
HashSupplier hasher) |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
91 |
|
36511 | 92 |
{ |
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
93 |
this(descriptor, location, readerSupplier, false, hasher); |
36511 | 94 |
} |
95 |
||
96 |
||
97 |
/** |
|
98 |
* Constructs a new instance of this class. |
|
99 |
* |
|
100 |
* <p> The {@code readSupplier} parameter is the supplier of the {@link |
|
101 |
* ModuleReader} that may be used to read the module content. Its {@link |
|
102 |
* Supplier#get() get()} method throws {@link UncheckedIOException} if an |
|
103 |
* I/O error occurs opening the module content. The {@code get()} method |
|
104 |
* throws {@link SecurityException} if opening the module is denied by the |
|
105 |
* security manager. |
|
106 |
* |
|
107 |
* @param descriptor |
|
108 |
* The module descriptor |
|
109 |
* @param location |
|
110 |
* The module location or {@code null} if not known |
|
111 |
* @param readerSupplier |
|
112 |
* The {@code Supplier} of the {@code ModuleReader} |
|
113 |
*/ |
|
114 |
public ModuleReference(ModuleDescriptor descriptor, |
|
115 |
URI location, |
|
116 |
Supplier<ModuleReader> readerSupplier) |
|
117 |
{ |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
118 |
this(descriptor, location, readerSupplier, false, null); |
36511 | 119 |
} |
120 |
||
121 |
/** |
|
122 |
* Returns the module descriptor. |
|
123 |
* |
|
124 |
* @return The module descriptor |
|
125 |
*/ |
|
126 |
public ModuleDescriptor descriptor() { |
|
127 |
return descriptor; |
|
128 |
} |
|
129 |
||
130 |
||
131 |
/** |
|
132 |
* Returns the location of this module's content, if known. |
|
133 |
* |
|
134 |
* <p> This URI, when present, is used as the {@linkplain |
|
135 |
* java.security.CodeSource#getLocation location} value of a {@link |
|
136 |
* java.security.CodeSource CodeSource} so that a module's classes can be |
|
137 |
* granted specific permissions when loaded by a {@link |
|
138 |
* java.security.SecureClassLoader SecureClassLoader}. |
|
139 |
* |
|
140 |
* @return The location or an empty {@code Optional} if not known |
|
141 |
*/ |
|
142 |
public Optional<URI> location() { |
|
143 |
return Optional.ofNullable(location); |
|
144 |
} |
|
145 |
||
146 |
||
147 |
/** |
|
148 |
* Opens the module content for reading. |
|
149 |
* |
|
150 |
* <p> This method opens the module content by invoking the {@link |
|
151 |
* Supplier#get() get()} method of the {@code readSupplier} specified at |
|
152 |
* construction time. </p> |
|
153 |
* |
|
154 |
* @return A {@code ModuleReader} to read the module |
|
155 |
* |
|
156 |
* @throws IOException |
|
157 |
* If an I/O error occurs |
|
158 |
* @throws SecurityException |
|
159 |
* If denied by the security manager |
|
160 |
*/ |
|
161 |
public ModuleReader open() throws IOException { |
|
162 |
try { |
|
163 |
return readerSupplier.get(); |
|
164 |
} catch (UncheckedIOException e) { |
|
165 |
throw e.getCause(); |
|
166 |
} |
|
167 |
||
168 |
} |
|
169 |
||
170 |
||
171 |
/** |
|
40261
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
37779
diff
changeset
|
172 |
* Returns {@code true} if this module has been patched via --patch-module. |
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
173 |
*/ |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
174 |
boolean isPatched() { |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
175 |
return patched; |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
176 |
} |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
177 |
|
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
178 |
/** |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
179 |
* Returns the hash supplier for this module. |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
180 |
*/ |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
181 |
HashSupplier hasher() { |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
182 |
return hasher; |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
183 |
} |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
184 |
|
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
185 |
/** |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
40261
diff
changeset
|
186 |
* Computes the hash of this module. Returns {@code null} if the hash |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
40261
diff
changeset
|
187 |
* cannot be computed. |
36511 | 188 |
* |
189 |
* @throws java.io.UncheckedIOException if an I/O error occurs |
|
190 |
*/ |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
40261
diff
changeset
|
191 |
byte[] computeHash(String algorithm) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
40261
diff
changeset
|
192 |
byte[] result = cachedHash; |
36511 | 193 |
if (result != null) |
194 |
return result; |
|
195 |
if (hasher == null) |
|
196 |
return null; |
|
197 |
cachedHash = result = hasher.generate(algorithm); |
|
198 |
return result; |
|
199 |
} |
|
200 |
||
201 |
/** |
|
202 |
* Computes a hash code for this module reference. |
|
203 |
* |
|
204 |
* <p> The hash code is based upon the components of the reference, and |
|
205 |
* satisfies the general contract of the {@link Object#hashCode |
|
206 |
* Object.hashCode} method. </p> |
|
207 |
* |
|
208 |
* @return The hash-code value for this module reference |
|
209 |
*/ |
|
210 |
@Override |
|
211 |
public int hashCode() { |
|
212 |
int hc = hash; |
|
213 |
if (hc == 0) { |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
40261
diff
changeset
|
214 |
hc = descriptor.hashCode(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
40261
diff
changeset
|
215 |
hc = 43 * hc + readerSupplier.hashCode(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
40261
diff
changeset
|
216 |
hc = 43 * hc + Objects.hashCode(location); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
40261
diff
changeset
|
217 |
hc = 43 * hc + Objects.hashCode(hasher); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
40261
diff
changeset
|
218 |
hc = 43 * hc + Boolean.hashCode(patched); |
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
219 |
if (hc == 0) |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
220 |
hc = -1; |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
221 |
hash = hc; |
36511 | 222 |
} |
223 |
return hc; |
|
224 |
} |
|
225 |
||
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
226 |
private int hash; |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
227 |
|
36511 | 228 |
/** |
229 |
* Tests this module reference for equality with the given object. |
|
230 |
* |
|
231 |
* <p> If the given object is not a {@code ModuleReference} then this |
|
232 |
* method returns {@code false}. Two module references are equal if their |
|
233 |
* module descriptors are equal, their locations are equal or both unknown, |
|
234 |
* and were created with equal supplier objects to access the module |
|
235 |
* content. </p> |
|
236 |
* |
|
237 |
* <p> This method satisfies the general contract of the {@link |
|
238 |
* java.lang.Object#equals(Object) Object.equals} method. </p> |
|
239 |
* |
|
240 |
* @param ob |
|
241 |
* the object to which this object is to be compared |
|
242 |
* |
|
243 |
* @return {@code true} if, and only if, the given object is a module |
|
244 |
* reference that is equal to this module reference |
|
245 |
*/ |
|
246 |
@Override |
|
247 |
public boolean equals(Object ob) { |
|
248 |
if (!(ob instanceof ModuleReference)) |
|
249 |
return false; |
|
250 |
ModuleReference that = (ModuleReference)ob; |
|
251 |
||
252 |
return Objects.equals(this.descriptor, that.descriptor) |
|
253 |
&& Objects.equals(this.location, that.location) |
|
254 |
&& Objects.equals(this.readerSupplier, that.readerSupplier) |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
255 |
&& Objects.equals(this.hasher, that.hasher) |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
256 |
&& this.patched == that.patched; |
36511 | 257 |
} |
258 |
||
259 |
/** |
|
260 |
* Returns a string describing this module reference. |
|
261 |
* |
|
262 |
* @return A string describing this module reference |
|
263 |
*/ |
|
264 |
@Override |
|
265 |
public String toString() { |
|
266 |
return ("[module " + descriptor().name() |
|
267 |
+ ", location=" + location + "]"); |
|
268 |
} |
|
269 |
||
270 |
} |