author | rriggs |
Sun, 29 Oct 2017 18:20:53 -0400 | |
changeset 47471 | 304ef03403b1 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
42703 | 1 |
/* |
2 |
* Copyright (c) 2016, 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. 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.UncheckedIOException; |
|
30 |
import java.lang.module.ModuleDescriptor; |
|
31 |
import java.lang.module.ModuleReader; |
|
32 |
import java.lang.module.ModuleReference; |
|
33 |
import java.net.URI; |
|
34 |
import java.util.Objects; |
|
35 |
import java.util.function.Supplier; |
|
36 |
||
37 |
/** |
|
38 |
* A ModuleReference implementation that supports referencing a module that |
|
39 |
* is patched and/or can be tied to other modules by means of hashes. |
|
40 |
*/ |
|
41 |
||
42 |
public class ModuleReferenceImpl extends ModuleReference { |
|
43 |
||
45652
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
44359
diff
changeset
|
44 |
// location of module |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
44359
diff
changeset
|
45 |
private final URI location; |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
44359
diff
changeset
|
46 |
|
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
44359
diff
changeset
|
47 |
// the module reader |
42703 | 48 |
private final Supplier<ModuleReader> readerSupplier; |
49 |
||
50 |
// non-null if the module is patched |
|
51 |
private final ModulePatcher patcher; |
|
52 |
||
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
53 |
// ModuleTarget if the module is OS/architecture specific |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
54 |
private final ModuleTarget target; |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
55 |
|
42703 | 56 |
// the hashes of other modules recorded in this module |
57 |
private final ModuleHashes recordedHashes; |
|
58 |
||
59 |
// the function that computes the hash of this module |
|
60 |
private final ModuleHashes.HashSupplier hasher; |
|
61 |
||
62 |
// ModuleResolution flags |
|
63 |
private final ModuleResolution moduleResolution; |
|
64 |
||
65 |
// cached hash of this module to avoid needing to compute it many times |
|
66 |
private byte[] cachedHash; |
|
67 |
||
68 |
/** |
|
69 |
* Constructs a new instance of this class. |
|
70 |
*/ |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
71 |
public ModuleReferenceImpl(ModuleDescriptor descriptor, |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
72 |
URI location, |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
73 |
Supplier<ModuleReader> readerSupplier, |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
74 |
ModulePatcher patcher, |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
75 |
ModuleTarget target, |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
76 |
ModuleHashes recordedHashes, |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
77 |
ModuleHashes.HashSupplier hasher, |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
78 |
ModuleResolution moduleResolution) |
42703 | 79 |
{ |
80 |
super(descriptor, Objects.requireNonNull(location)); |
|
45652
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
44359
diff
changeset
|
81 |
this.location = location; |
42703 | 82 |
this.readerSupplier = readerSupplier; |
83 |
this.patcher = patcher; |
|
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
84 |
this.target = target; |
42703 | 85 |
this.recordedHashes = recordedHashes; |
86 |
this.hasher = hasher; |
|
87 |
this.moduleResolution = moduleResolution; |
|
88 |
} |
|
89 |
||
90 |
@Override |
|
91 |
public ModuleReader open() throws IOException { |
|
92 |
try { |
|
93 |
return readerSupplier.get(); |
|
94 |
} catch (UncheckedIOException e) { |
|
95 |
throw e.getCause(); |
|
96 |
} |
|
97 |
} |
|
98 |
||
99 |
/** |
|
100 |
* Returns {@code true} if this module has been patched via --patch-module. |
|
101 |
*/ |
|
102 |
public boolean isPatched() { |
|
103 |
return (patcher != null); |
|
104 |
} |
|
105 |
||
106 |
/** |
|
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
107 |
* Returns the ModuleTarget or {@code null} if the no target platform. |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
108 |
*/ |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
109 |
public ModuleTarget moduleTarget() { |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
110 |
return target; |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
111 |
} |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
112 |
|
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
113 |
/** |
42703 | 114 |
* Returns the hashes recorded in this module or {@code null} if there |
115 |
* are no hashes recorded. |
|
116 |
*/ |
|
117 |
public ModuleHashes recordedHashes() { |
|
118 |
return recordedHashes; |
|
119 |
} |
|
120 |
||
121 |
/** |
|
122 |
* Returns the supplier that computes the hash of this module. |
|
123 |
*/ |
|
124 |
ModuleHashes.HashSupplier hasher() { |
|
125 |
return hasher; |
|
126 |
} |
|
127 |
||
128 |
/** |
|
129 |
* Returns the ModuleResolution flags. |
|
130 |
*/ |
|
131 |
public ModuleResolution moduleResolution() { |
|
132 |
return moduleResolution; |
|
133 |
} |
|
134 |
||
135 |
/** |
|
136 |
* Computes the hash of this module. Returns {@code null} if the hash |
|
137 |
* cannot be computed. |
|
138 |
* |
|
139 |
* @throws java.io.UncheckedIOException if an I/O error occurs |
|
140 |
*/ |
|
141 |
public byte[] computeHash(String algorithm) { |
|
142 |
byte[] result = cachedHash; |
|
143 |
if (result != null) |
|
144 |
return result; |
|
145 |
if (hasher == null) |
|
146 |
return null; |
|
147 |
cachedHash = result = hasher.generate(algorithm); |
|
148 |
return result; |
|
149 |
} |
|
150 |
||
151 |
@Override |
|
152 |
public int hashCode() { |
|
153 |
int hc = hash; |
|
154 |
if (hc == 0) { |
|
155 |
hc = descriptor().hashCode(); |
|
45652
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
44359
diff
changeset
|
156 |
hc = 43 * hc + Objects.hashCode(location); |
42703 | 157 |
hc = 43 * hc + Objects.hashCode(patcher); |
158 |
if (hc == 0) |
|
159 |
hc = -1; |
|
160 |
hash = hc; |
|
161 |
} |
|
162 |
return hc; |
|
163 |
} |
|
164 |
||
165 |
private int hash; |
|
166 |
||
167 |
@Override |
|
168 |
public boolean equals(Object ob) { |
|
169 |
if (!(ob instanceof ModuleReferenceImpl)) |
|
170 |
return false; |
|
171 |
ModuleReferenceImpl that = (ModuleReferenceImpl)ob; |
|
172 |
||
173 |
// assume module content, recorded hashes, etc. are the same |
|
174 |
// when the modules have equal module descriptors, are at the |
|
175 |
// same location, and are patched by the same patcher. |
|
176 |
return Objects.equals(this.descriptor(), that.descriptor()) |
|
45652
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
44359
diff
changeset
|
177 |
&& Objects.equals(this.location, that.location) |
42703 | 178 |
&& Objects.equals(this.patcher, that.patcher); |
179 |
} |
|
180 |
||
181 |
@Override |
|
182 |
public String toString() { |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
183 |
StringBuilder sb = new StringBuilder(); |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
184 |
sb.append("[module "); |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
185 |
sb.append(descriptor().name()); |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
186 |
sb.append(", location="); |
45652
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
44359
diff
changeset
|
187 |
sb.append(location); |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
188 |
if (isPatched()) sb.append(" (patched)"); |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
189 |
sb.append("]"); |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
190 |
return sb.toString(); |
42703 | 191 |
} |
192 |
||
193 |
} |