author | alanb |
Fri, 16 Dec 2016 06:19:16 +0000 | |
changeset 42703 | 20c39ea4a507 |
parent 41911 | b3bb62588635 |
child 42704 | 42d42181a1aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
37313
b0c043d9ad18
8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
25859
diff
changeset
|
2 |
* Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.lang; |
|
27 |
||
41911 | 28 |
import jdk.internal.loader.BuiltinClassLoader; |
29 |
import jdk.internal.misc.VM; |
|
30 |
import jdk.internal.module.ModuleHashes; |
|
42703
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41911
diff
changeset
|
31 |
import jdk.internal.module.ModuleReferenceImpl; |
41911 | 32 |
|
33 |
import java.lang.module.ModuleDescriptor.Version; |
|
42703
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41911
diff
changeset
|
34 |
import java.lang.module.ModuleReference; |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41911
diff
changeset
|
35 |
import java.lang.module.ResolvedModule; |
41911 | 36 |
import java.lang.reflect.Layer; |
37 |
import java.lang.reflect.Module; |
|
38 |
import java.util.HashSet; |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
39 |
import java.util.Objects; |
41911 | 40 |
import java.util.Optional; |
41 |
import java.util.Set; |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
42 |
|
2 | 43 |
/** |
44 |
* An element in a stack trace, as returned by {@link |
|
45 |
* Throwable#getStackTrace()}. Each element represents a single stack frame. |
|
46 |
* All stack frames except for the one at the top of the stack represent |
|
47 |
* a method invocation. The frame at the top of the stack represents the |
|
48 |
* execution point at which the stack trace was generated. Typically, |
|
49 |
* this is the point at which the throwable corresponding to the stack trace |
|
50 |
* was created. |
|
51 |
* |
|
52 |
* @since 1.4 |
|
53 |
* @author Josh Bloch |
|
54 |
*/ |
|
55 |
public final class StackTraceElement implements java.io.Serializable { |
|
41911 | 56 |
// This field is set to the compacted String representation used |
57 |
// by StackTraceElement::toString and stored in serial form. |
|
58 |
// |
|
59 |
// This field is of Object type. VM initially sets this field to |
|
60 |
// the Class object of the declaring class to build the compacted string. |
|
61 |
private Object classOrLoaderModuleClassName; |
|
62 |
||
63 |
// Normally initialized by VM |
|
64 |
private String classLoaderName; |
|
36511 | 65 |
private String moduleName; |
66 |
private String moduleVersion; |
|
2 | 67 |
private String declaringClass; |
68 |
private String methodName; |
|
69 |
private String fileName; |
|
70 |
private int lineNumber; |
|
71 |
||
72 |
/** |
|
73 |
* Creates a stack trace element representing the specified execution |
|
36511 | 74 |
* point. The {@link #getModuleName module name} and {@link |
75 |
* #getModuleVersion module version} of the stack trace element will |
|
76 |
* be {@code null}. |
|
2 | 77 |
* |
78 |
* @param declaringClass the fully qualified name of the class containing |
|
79 |
* the execution point represented by the stack trace element |
|
80 |
* @param methodName the name of the method containing the execution point |
|
81 |
* represented by the stack trace element |
|
82 |
* @param fileName the name of the file containing the execution point |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
83 |
* represented by the stack trace element, or {@code null} if |
2 | 84 |
* this information is unavailable |
85 |
* @param lineNumber the line number of the source line containing the |
|
86 |
* execution point represented by this stack trace element, or |
|
87 |
* a negative number if this information is unavailable. A value |
|
88 |
* of -2 indicates that the method containing the execution point |
|
89 |
* is a native method |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
90 |
* @throws NullPointerException if {@code declaringClass} or |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
91 |
* {@code methodName} is null |
2 | 92 |
* @since 1.5 |
93 |
*/ |
|
94 |
public StackTraceElement(String declaringClass, String methodName, |
|
95 |
String fileName, int lineNumber) { |
|
41911 | 96 |
this(null, null, null, declaringClass, methodName, fileName, lineNumber); |
36511 | 97 |
} |
98 |
||
99 |
/** |
|
100 |
* Creates a stack trace element representing the specified execution |
|
101 |
* point. |
|
102 |
* |
|
41911 | 103 |
* @param classLoaderName the class loader name if the class loader of |
104 |
* the class containing the execution point represented by |
|
105 |
* the stack trace is named; otherwise {@code null} |
|
36511 | 106 |
* @param moduleName the module name if the class containing the |
107 |
* execution point represented by the stack trace is in a named |
|
41911 | 108 |
* module; otherwise {@code null} |
36511 | 109 |
* @param moduleVersion the module version if the class containing the |
110 |
* execution point represented by the stack trace is in a named |
|
41911 | 111 |
* module that has a version; otherwise {@code null} |
36511 | 112 |
* @param declaringClass the fully qualified name of the class containing |
113 |
* the execution point represented by the stack trace element |
|
114 |
* @param methodName the name of the method containing the execution point |
|
115 |
* represented by the stack trace element |
|
116 |
* @param fileName the name of the file containing the execution point |
|
117 |
* represented by the stack trace element, or {@code null} if |
|
118 |
* this information is unavailable |
|
119 |
* @param lineNumber the line number of the source line containing the |
|
120 |
* execution point represented by this stack trace element, or |
|
121 |
* a negative number if this information is unavailable. A value |
|
122 |
* of -2 indicates that the method containing the execution point |
|
123 |
* is a native method |
|
41911 | 124 |
* |
36511 | 125 |
* @throws NullPointerException if {@code declaringClass} is {@code null} |
126 |
* or {@code methodName} is {@code null} |
|
41911 | 127 |
* |
36511 | 128 |
* @since 9 |
129 |
*/ |
|
41911 | 130 |
public StackTraceElement(String classLoaderName, |
131 |
String moduleName, String moduleVersion, |
|
36511 | 132 |
String declaringClass, String methodName, |
133 |
String fileName, int lineNumber) { |
|
41911 | 134 |
this.classLoaderName = classLoaderName; |
135 |
this.moduleName = moduleName; |
|
136 |
this.moduleVersion = moduleVersion; |
|
137 |
this.declaringClass = Objects.requireNonNull(declaringClass, "Declaring class is null"); |
|
138 |
this.methodName = Objects.requireNonNull(methodName, "Method name is null"); |
|
139 |
this.fileName = fileName; |
|
140 |
this.lineNumber = lineNumber; |
|
2 | 141 |
} |
142 |
||
41911 | 143 |
/* |
144 |
* Private constructor for the factory methods to create StackTraceElement |
|
145 |
* for Throwable and StackFrameInfo |
|
37313
b0c043d9ad18
8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
25859
diff
changeset
|
146 |
*/ |
41911 | 147 |
private StackTraceElement() {} |
37313
b0c043d9ad18
8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
25859
diff
changeset
|
148 |
|
2 | 149 |
/** |
150 |
* Returns the name of the source file containing the execution point |
|
151 |
* represented by this stack trace element. Generally, this corresponds |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
152 |
* to the {@code SourceFile} attribute of the relevant {@code class} |
2 | 153 |
* file (as per <i>The Java Virtual Machine Specification</i>, Section |
154 |
* 4.7.7). In some systems, the name may refer to some source code unit |
|
155 |
* other than a file, such as an entry in source repository. |
|
156 |
* |
|
157 |
* @return the name of the file containing the execution point |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
158 |
* represented by this stack trace element, or {@code null} if |
2 | 159 |
* this information is unavailable. |
160 |
*/ |
|
161 |
public String getFileName() { |
|
162 |
return fileName; |
|
163 |
} |
|
164 |
||
165 |
/** |
|
166 |
* Returns the line number of the source line containing the execution |
|
167 |
* point represented by this stack trace element. Generally, this is |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
168 |
* derived from the {@code LineNumberTable} attribute of the relevant |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
169 |
* {@code class} file (as per <i>The Java Virtual Machine |
2 | 170 |
* Specification</i>, Section 4.7.8). |
171 |
* |
|
172 |
* @return the line number of the source line containing the execution |
|
173 |
* point represented by this stack trace element, or a negative |
|
174 |
* number if this information is unavailable. |
|
175 |
*/ |
|
176 |
public int getLineNumber() { |
|
177 |
return lineNumber; |
|
178 |
} |
|
179 |
||
180 |
/** |
|
36511 | 181 |
* Returns the module name of the module containing the execution point |
182 |
* represented by this stack trace element. |
|
183 |
* |
|
184 |
* @return the module name of the {@code Module} containing the execution |
|
185 |
* point represented by this stack trace element; {@code null} |
|
186 |
* if the module name is not available. |
|
187 |
* @since 9 |
|
188 |
* @see java.lang.reflect.Module#getName() |
|
189 |
*/ |
|
190 |
public String getModuleName() { |
|
191 |
return moduleName; |
|
192 |
} |
|
193 |
||
194 |
/** |
|
195 |
* Returns the module version of the module containing the execution point |
|
196 |
* represented by this stack trace element. |
|
197 |
* |
|
198 |
* @return the module version of the {@code Module} containing the execution |
|
199 |
* point represented by this stack trace element; {@code null} |
|
200 |
* if the module version is not available. |
|
201 |
* @since 9 |
|
202 |
* @see java.lang.module.ModuleDescriptor.Version |
|
203 |
*/ |
|
204 |
public String getModuleVersion() { |
|
205 |
return moduleVersion; |
|
206 |
} |
|
207 |
||
208 |
/** |
|
41911 | 209 |
* Returns the name of the class loader of the class containing the |
210 |
* execution point represented by this stack trace element. |
|
211 |
* |
|
212 |
* @return the name of the class loader of the class containing the execution |
|
213 |
* point represented by this stack trace element; {@code null} |
|
214 |
* if the class loader is not named. |
|
215 |
* |
|
216 |
* @since 9 |
|
217 |
* @see java.lang.ClassLoader#getName() |
|
218 |
*/ |
|
219 |
public String getClassLoaderName() { |
|
220 |
return classLoaderName; |
|
221 |
} |
|
222 |
||
223 |
/** |
|
2 | 224 |
* Returns the fully qualified name of the class containing the |
225 |
* execution point represented by this stack trace element. |
|
226 |
* |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
227 |
* @return the fully qualified name of the {@code Class} containing |
2 | 228 |
* the execution point represented by this stack trace element. |
229 |
*/ |
|
230 |
public String getClassName() { |
|
231 |
return declaringClass; |
|
232 |
} |
|
233 |
||
234 |
/** |
|
235 |
* Returns the name of the method containing the execution point |
|
236 |
* represented by this stack trace element. If the execution point is |
|
237 |
* contained in an instance or class initializer, this method will return |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
238 |
* the appropriate <i>special method name</i>, {@code <init>} or |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
239 |
* {@code <clinit>}, as per Section 3.9 of <i>The Java Virtual |
2 | 240 |
* Machine Specification</i>. |
241 |
* |
|
242 |
* @return the name of the method containing the execution point |
|
243 |
* represented by this stack trace element. |
|
244 |
*/ |
|
245 |
public String getMethodName() { |
|
246 |
return methodName; |
|
247 |
} |
|
248 |
||
249 |
/** |
|
250 |
* Returns true if the method containing the execution point |
|
251 |
* represented by this stack trace element is a native method. |
|
252 |
* |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
253 |
* @return {@code true} if the method containing the execution point |
2 | 254 |
* represented by this stack trace element is a native method. |
255 |
*/ |
|
256 |
public boolean isNativeMethod() { |
|
257 |
return lineNumber == -2; |
|
258 |
} |
|
259 |
||
260 |
/** |
|
261 |
* Returns a string representation of this stack trace element. The |
|
262 |
* format of this string depends on the implementation, but the following |
|
263 |
* examples may be regarded as typical: |
|
264 |
* <ul> |
|
265 |
* <li> |
|
41911 | 266 |
* "{@code com.foo.loader/foo@9.0/com.foo.Main.run(Main.java:101)}" |
267 |
* - See the description below. |
|
268 |
* </li> |
|
269 |
* <li> |
|
270 |
* "{@code com.foo.loader/foo@9.0/com.foo.Main.run(Main.java)}" |
|
271 |
* - The line number is unavailable. |
|
272 |
* </li> |
|
273 |
* <li> |
|
274 |
* "{@code com.foo.loader/foo@9.0/com.foo.Main.run(Unknown Source)}" |
|
275 |
* - Neither the file name nor the line number is available. |
|
276 |
* </li> |
|
277 |
* <li> |
|
278 |
* "{@code com.foo.loader/foo@9.0/com.foo.Main.run(Native Method)}" |
|
279 |
* - The method containing the execution point is a native method. |
|
280 |
* </li> |
|
281 |
* <li> |
|
282 |
* "{@code com.foo.loader//com.foo.bar.App.run(App.java:12)}" |
|
283 |
* - The class of the execution point is defined in the unnamed module of |
|
284 |
* the class loader named {@code com.foo.loader}. |
|
285 |
* </li> |
|
286 |
* <li> |
|
287 |
* "{@code acme@2.1/org.acme.Lib.test(Lib.java:80)}" |
|
288 |
* - The class of the execution point is defined in {@code acme} module |
|
289 |
* loaded by by a built-in class loader such as the application class loader. |
|
290 |
* </li> |
|
2 | 291 |
* <li> |
41911 | 292 |
* "{@code MyClass.mash(MyClass.java:9)}" |
293 |
* - {@code MyClass} class is on the application class path. |
|
294 |
* </li> |
|
2 | 295 |
* </ul> |
41911 | 296 |
* |
297 |
* <p> The first example shows a stack trace element consisting of |
|
298 |
* three elements, each separated by {@code "/"} followed with |
|
299 |
* the source file name and the line number of the source line |
|
300 |
* containing the execution point. |
|
301 |
* |
|
302 |
* The first element "{@code com.foo.loader}" is |
|
303 |
* the name of the class loader. The second element "{@code foo@9.0}" |
|
304 |
* is the module name and version. The third element is the method |
|
305 |
* containing the execution point; "{@code com.foo.Main"}" is the |
|
306 |
* fully-qualified class name and "{@code run}" is the name of the method. |
|
307 |
* "{@code Main.java}" is the source file name and "{@code 101}" is |
|
308 |
* the line number. |
|
309 |
* |
|
310 |
* <p> If a class is defined in an <em>unnamed module</em> |
|
311 |
* then the second element is omitted as shown in |
|
312 |
* "{@code com.foo.loader//com.foo.bar.App.run(App.java:12)}". |
|
313 |
* |
|
314 |
* If the class loader is a <a href="ClassLoader.html#builtinLoaders"> |
|
315 |
* built-in class loader</a> or is not named then the first element |
|
316 |
* and its following {@code "/"} are omitted as shown in |
|
317 |
* "{@code acme@2.1/org.acme.Lib.test(Lib.java:80)}". |
|
318 |
* If the first element is omitted and the module is an unnamed module, |
|
319 |
* the second element and its following {@code "/"} are also omitted |
|
320 |
* as shown in "{@code MyClass.mash(MyClass.java:9)}". |
|
36511 | 321 |
* |
2 | 322 |
* @see Throwable#printStackTrace() |
323 |
*/ |
|
324 |
public String toString() { |
|
41911 | 325 |
String s = buildLoaderModuleClassName(); |
326 |
if (s == null) { |
|
327 |
// all elements will be included |
|
328 |
s = ""; |
|
329 |
if (classLoaderName != null && !classLoaderName.isEmpty()) { |
|
330 |
s += classLoaderName + "/"; |
|
331 |
} |
|
332 |
if (moduleName != null && !moduleName.isEmpty()) { |
|
333 |
s += moduleName; |
|
334 |
||
335 |
if (moduleVersion != null && !moduleVersion.isEmpty()) { |
|
336 |
s += "@" + moduleVersion; |
|
337 |
} |
|
338 |
} |
|
339 |
s = s.isEmpty() ? declaringClass : s + "/" + declaringClass; |
|
36511 | 340 |
} |
41911 | 341 |
|
342 |
return s + "." + methodName + "(" + |
|
36511 | 343 |
(isNativeMethod() ? "Native Method)" : |
344 |
(fileName != null && lineNumber >= 0 ? |
|
345 |
fileName + ":" + lineNumber + ")" : |
|
346 |
(fileName != null ? ""+fileName+")" : "Unknown Source)"))); |
|
2 | 347 |
} |
348 |
||
349 |
/** |
|
350 |
* Returns true if the specified object is another |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
351 |
* {@code StackTraceElement} instance representing the same execution |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
352 |
* point as this instance. Two stack trace elements {@code a} and |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
353 |
* {@code b} are equal if and only if: |
18156 | 354 |
* <pre>{@code |
41911 | 355 |
* equals(a.getClassLoaderName(), b.getClassLoaderName()) && |
36511 | 356 |
* equals(a.getModuleName(), b.getModuleName()) && |
357 |
* equals(a.getModuleVersion(), b.getModuleVersion()) && |
|
2 | 358 |
* equals(a.getClassName(), b.getClassName()) && |
359 |
* equals(a.getMethodName(), b.getMethodName()) |
|
41911 | 360 |
* equals(a.getFileName(), b.getFileName()) && |
361 |
* a.getLineNumber() == b.getLineNumber() |
|
362 |
* |
|
18156 | 363 |
* }</pre> |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
364 |
* where {@code equals} has the semantics of {@link |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
365 |
* java.util.Objects#equals(Object, Object) Objects.equals}. |
2 | 366 |
* |
367 |
* @param obj the object to be compared with this stack trace element. |
|
368 |
* @return true if the specified object is another |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
369 |
* {@code StackTraceElement} instance representing the same |
2 | 370 |
* execution point as this instance. |
371 |
*/ |
|
372 |
public boolean equals(Object obj) { |
|
373 |
if (obj==this) |
|
374 |
return true; |
|
375 |
if (!(obj instanceof StackTraceElement)) |
|
376 |
return false; |
|
377 |
StackTraceElement e = (StackTraceElement)obj; |
|
41911 | 378 |
return Objects.equals(classLoaderName, e.classLoaderName) && |
36511 | 379 |
Objects.equals(moduleName, e.moduleName) && |
380 |
Objects.equals(moduleVersion, e.moduleVersion) && |
|
41911 | 381 |
e.declaringClass.equals(declaringClass) && |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
382 |
e.lineNumber == lineNumber && |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
383 |
Objects.equals(methodName, e.methodName) && |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
384 |
Objects.equals(fileName, e.fileName); |
2 | 385 |
} |
386 |
||
387 |
/** |
|
388 |
* Returns a hash code value for this stack trace element. |
|
389 |
*/ |
|
390 |
public int hashCode() { |
|
391 |
int result = 31*declaringClass.hashCode() + methodName.hashCode(); |
|
41911 | 392 |
result = 31*result + Objects.hashCode(classLoaderName); |
36511 | 393 |
result = 31*result + Objects.hashCode(moduleName); |
394 |
result = 31*result + Objects.hashCode(moduleVersion); |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
395 |
result = 31*result + Objects.hashCode(fileName); |
2 | 396 |
result = 31*result + lineNumber; |
397 |
return result; |
|
398 |
} |
|
399 |
||
41911 | 400 |
|
401 |
/** |
|
402 |
* Build the compacted String representation to be returned by |
|
403 |
* toString method from the declaring Class object. |
|
404 |
*/ |
|
405 |
synchronized String buildLoaderModuleClassName() { |
|
406 |
if (classOrLoaderModuleClassName == null) |
|
407 |
return null; |
|
408 |
||
409 |
if (classOrLoaderModuleClassName instanceof Class) { |
|
410 |
Class<?> cls = (Class<?>)classOrLoaderModuleClassName; |
|
411 |
classOrLoaderModuleClassName = toLoaderModuleClassName(cls); |
|
412 |
} |
|
413 |
return (String)classOrLoaderModuleClassName; |
|
414 |
} |
|
415 |
||
416 |
/** |
|
417 |
* Returns <loader>/<module>/<fully-qualified-classname> string |
|
418 |
* representation of the given class. |
|
419 |
* <p> |
|
420 |
* If the module is a non-upgradeable JDK module then omit |
|
421 |
* its version string. |
|
422 |
* <p> |
|
423 |
* If the loader has no name, or if the loader is one of the built-in |
|
424 |
* loaders (`boot`, `platform`, or `app`) then drop the first element |
|
425 |
* (`<loader>/`). |
|
426 |
* <p> |
|
427 |
* If the first element has been dropped and the module is unnamed |
|
428 |
* then drop the second element (`<module>/`). |
|
429 |
* <p> |
|
430 |
* If the first element is not dropped and the module is unnamed |
|
431 |
* then drop `<module>`. |
|
432 |
*/ |
|
433 |
private static String toLoaderModuleClassName(Class<?> cls) { |
|
434 |
ClassLoader loader = cls.getClassLoader0(); |
|
435 |
Module m = cls.getModule(); |
|
436 |
||
437 |
// First element - class loader name |
|
438 |
// Call package-private ClassLoader::name method |
|
439 |
String s = ""; |
|
440 |
if (loader != null && loader.name() != null && |
|
441 |
!(loader instanceof BuiltinClassLoader)) { |
|
442 |
s = loader.name() + "/"; |
|
443 |
} |
|
444 |
||
445 |
// Second element - module name and version |
|
446 |
if (m != null && m.isNamed()) { |
|
447 |
s += m.getName(); |
|
448 |
// Include version if it is a user module or upgradeable module |
|
449 |
// |
|
450 |
// If it is JDK non-upgradeable module which is recorded |
|
451 |
// in the hashes in java.base, omit the version. |
|
452 |
if (!isHashedInJavaBase(m)) { |
|
453 |
Optional<Version> ov = m.getDescriptor().version(); |
|
454 |
if (ov.isPresent()) { |
|
455 |
String version = "@" + ov.get().toString(); |
|
456 |
s += version; |
|
457 |
} |
|
458 |
} |
|
459 |
} |
|
460 |
||
461 |
// fully-qualified class name |
|
462 |
return s.isEmpty() ? cls.getName() : s + "/" + cls.getName(); |
|
463 |
} |
|
464 |
||
465 |
/** |
|
466 |
* Returns true if the module is hashed with java.base. |
|
467 |
* <p> |
|
468 |
* This method returns false when running on the exploded image |
|
469 |
* since JDK modules are not hashed. They have no Version attribute |
|
470 |
* and so "@<version>" part will be omitted anyway. |
|
471 |
*/ |
|
472 |
private static boolean isHashedInJavaBase(Module m) { |
|
473 |
// return true if module system is not initialized as the code |
|
474 |
// must be in java.base |
|
475 |
if (!VM.isModuleSystemInited()) |
|
476 |
return true; |
|
477 |
||
478 |
return Layer.boot() == m.getLayer() && HashedModules.contains(m); |
|
479 |
} |
|
480 |
||
481 |
/* |
|
482 |
* Finds JDK non-upgradeable modules, i.e. the modules that are |
|
483 |
* included in the hashes in java.base. |
|
484 |
*/ |
|
485 |
private static class HashedModules { |
|
486 |
static Set<String> HASHED_MODULES = hashedModules(); |
|
487 |
||
488 |
static Set<String> hashedModules() { |
|
489 |
||
42703
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41911
diff
changeset
|
490 |
Optional<ResolvedModule> resolvedModule = Layer.boot() |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41911
diff
changeset
|
491 |
.configuration() |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41911
diff
changeset
|
492 |
.findModule("java.base"); |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41911
diff
changeset
|
493 |
assert resolvedModule.isPresent(); |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41911
diff
changeset
|
494 |
ModuleReference mref = resolvedModule.get().reference(); |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41911
diff
changeset
|
495 |
assert mref instanceof ModuleReferenceImpl; |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41911
diff
changeset
|
496 |
ModuleHashes hashes = ((ModuleReferenceImpl)mref).recordedHashes(); |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41911
diff
changeset
|
497 |
if (hashes != null) { |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41911
diff
changeset
|
498 |
Set<String> names = new HashSet<>(hashes.names()); |
41911 | 499 |
names.add("java.base"); |
500 |
return names; |
|
501 |
} |
|
502 |
||
503 |
return Set.of(); |
|
504 |
} |
|
505 |
||
506 |
static boolean contains(Module m) { |
|
507 |
return HASHED_MODULES.contains(m.getName()); |
|
508 |
} |
|
509 |
} |
|
510 |
||
511 |
||
512 |
/* |
|
513 |
* Returns an array of StackTraceElements of the given depth |
|
514 |
* filled from the backtrace of a given Throwable. |
|
515 |
*/ |
|
516 |
static StackTraceElement[] of(Throwable x, int depth) { |
|
517 |
StackTraceElement[] stackTrace = new StackTraceElement[depth]; |
|
518 |
for (int i = 0; i < depth; i++) { |
|
519 |
stackTrace[i] = new StackTraceElement(); |
|
520 |
} |
|
521 |
||
522 |
// VM to fill in StackTraceElement |
|
523 |
initStackTraceElements(stackTrace, x); |
|
524 |
||
525 |
// ensure the proper StackTraceElement initialization |
|
526 |
for (StackTraceElement ste : stackTrace) { |
|
527 |
ste.buildLoaderModuleClassName(); |
|
528 |
} |
|
529 |
return stackTrace; |
|
530 |
} |
|
531 |
||
532 |
/* |
|
533 |
* Returns a StackTraceElement from a given StackFrameInfo. |
|
534 |
*/ |
|
535 |
static StackTraceElement of(StackFrameInfo sfi) { |
|
536 |
StackTraceElement ste = new StackTraceElement(); |
|
537 |
initStackTraceElement(ste, sfi); |
|
538 |
||
539 |
ste.buildLoaderModuleClassName(); |
|
540 |
return ste; |
|
541 |
} |
|
542 |
||
543 |
/* |
|
544 |
* Sets the given stack trace elements with the backtrace |
|
545 |
* of the given Throwable. |
|
546 |
*/ |
|
547 |
private static native void initStackTraceElements(StackTraceElement[] elements, |
|
548 |
Throwable x); |
|
549 |
/* |
|
550 |
* Sets the given stack trace element with the given StackFrameInfo |
|
551 |
*/ |
|
552 |
private static native void initStackTraceElement(StackTraceElement element, |
|
553 |
StackFrameInfo sfi); |
|
554 |
||
2 | 555 |
private static final long serialVersionUID = 6992337162326171013L; |
556 |
} |