75 * Creates a stack trace element representing the specified execution 76 * point. The {@link #getModuleName module name} and {@link 77 * #getModuleVersion module version} of the stack trace element will 78 * be {@code null}. 79 * 80 * @param declaringClass the fully qualified name of the class containing 81 * the execution point represented by the stack trace element 82 * @param methodName the name of the method containing the execution point 83 * represented by the stack trace element 84 * @param fileName the name of the file containing the execution point 85 * represented by the stack trace element, or {@code null} if 86 * this information is unavailable 87 * @param lineNumber the line number of the source line containing the 88 * execution point represented by this stack trace element, or 89 * a negative number if this information is unavailable. A value 90 * of -2 indicates that the method containing the execution point 91 * is a native method 92 * @throws NullPointerException if {@code declaringClass} or 93 * {@code methodName} is null 94 * @since 1.5 95 */ 96 public StackTraceElement(String declaringClass, String methodName, 97 String fileName, int lineNumber) { 98 this(null, null, null, declaringClass, methodName, fileName, lineNumber); 99 } 100 101 /** 102 * Creates a stack trace element representing the specified execution 103 * point. 104 * 105 * @param classLoaderName the class loader name if the class loader of 106 * the class containing the execution point represented by 107 * the stack trace is named; otherwise {@code null} 108 * @param moduleName the module name if the class containing the 109 * execution point represented by the stack trace is in a named 110 * module; otherwise {@code null} 111 * @param moduleVersion the module version if the class containing the 112 * execution point represented by the stack trace is in a named 113 * module that has a version; otherwise {@code null} 114 * @param declaringClass the fully qualified name of the class containing 115 * the execution point represented by the stack trace element 116 * @param methodName the name of the method containing the execution point 117 * represented by the stack trace element 118 * @param fileName the name of the file containing the execution point 119 * represented by the stack trace element, or {@code null} if 120 * this information is unavailable 121 * @param lineNumber the line number of the source line containing the 122 * execution point represented by this stack trace element, or 123 * a negative number if this information is unavailable. A value 124 * of -2 indicates that the method containing the execution point 125 * is a native method 126 * 127 * @throws NullPointerException if {@code declaringClass} is {@code null} 128 * or {@code methodName} is {@code null} 129 * 130 * @since 9 131 */ 132 public StackTraceElement(String classLoaderName, 133 String moduleName, String moduleVersion, 134 String declaringClass, String methodName, 135 String fileName, int lineNumber) { 136 this.classLoaderName = classLoaderName; 137 this.moduleName = moduleName; 138 this.moduleVersion = moduleVersion; 139 this.declaringClass = Objects.requireNonNull(declaringClass, "Declaring class is null"); 140 this.methodName = Objects.requireNonNull(methodName, "Method name is null"); 141 this.fileName = fileName; 142 this.lineNumber = lineNumber; 143 } 144 145 /* 146 * Private constructor for the factory methods to create StackTraceElement 147 * for Throwable and StackFrameInfo 148 */ 149 private StackTraceElement() {} 150 170 * derived from the {@code LineNumberTable} attribute of the relevant 171 * {@code class} file (as per <i>The Java Virtual Machine 172 * Specification</i>, Section 4.7.8). 173 * 174 * @return the line number of the source line containing the execution 175 * point represented by this stack trace element, or a negative 176 * number if this information is unavailable. 177 */ 178 public int getLineNumber() { 179 return lineNumber; 180 } 181 182 /** 183 * Returns the module name of the module containing the execution point 184 * represented by this stack trace element. 185 * 186 * @return the module name of the {@code Module} containing the execution 187 * point represented by this stack trace element; {@code null} 188 * if the module name is not available. 189 * @since 9 190 * @see java.lang.reflect.Module#getName() 191 */ 192 public String getModuleName() { 193 return moduleName; 194 } 195 196 /** 197 * Returns the module version of the module containing the execution point 198 * represented by this stack trace element. 199 * 200 * @return the module version of the {@code Module} containing the execution 201 * point represented by this stack trace element; {@code null} 202 * if the module version is not available. 203 * @since 9 204 * @see java.lang.module.ModuleDescriptor.Version 205 */ 206 public String getModuleVersion() { 207 return moduleVersion; 208 } 209 210 /** 211 * Returns the name of the class loader of the class containing the 212 * execution point represented by this stack trace element. 213 * 214 * @return the name of the class loader of the class containing the execution 215 * point represented by this stack trace element; {@code null} 216 * if the class loader is not named. 217 * 218 * @since 9 219 * @see java.lang.ClassLoader#getName() 220 */ 221 public String getClassLoaderName() { 222 return classLoaderName; 223 } 224 225 /** 226 * Returns the fully qualified name of the class containing the 227 * execution point represented by this stack trace element. 228 * 229 * @return the fully qualified name of the {@code Class} containing 230 * the execution point represented by this stack trace element. 231 */ 232 public String getClassName() { 233 return declaringClass; 234 } 235 236 /** 237 * Returns the name of the method containing the execution point 238 * represented by this stack trace element. If the execution point is 312 * 313 * <p> If a class is defined in an <em>unnamed module</em> 314 * then the second element is omitted as shown in 315 * "{@code com.foo.loader//com.foo.bar.App.run(App.java:12)}". 316 * 317 * <p> If the class loader is a <a href="ClassLoader.html#builtinLoaders"> 318 * built-in class loader</a> or is not named then the first element 319 * and its following {@code "/"} are omitted as shown in 320 * "{@code acme@2.1/org.acme.Lib.test(Lib.java:80)}". 321 * If the first element is omitted and the module is an unnamed module, 322 * the second element and its following {@code "/"} are also omitted 323 * as shown in "{@code MyClass.mash(MyClass.java:9)}". 324 * 325 * <p> The {@code toString} method may return two different values on two 326 * {@code StackTraceElement} instances that are 327 * {@linkplain #equals(Object) equal}, for example one created via the 328 * constructor, and one obtained from {@link java.lang.Throwable} or 329 * {@link java.lang.StackWalker.StackFrame}, where an implementation may 330 * choose to omit some element in the returned string. 331 * 332 * @see Throwable#printStackTrace() 333 */ 334 public String toString() { 335 String s = ""; 336 if (!dropClassLoaderName() && classLoaderName != null && 337 !classLoaderName.isEmpty()) { 338 s += classLoaderName + "/"; 339 } 340 if (moduleName != null && !moduleName.isEmpty()) { 341 s += moduleName; 342 343 if (!dropModuleVersion() && moduleVersion != null && 344 !moduleVersion.isEmpty()) { 345 s += "@" + moduleVersion; 346 } 347 } 348 s = s.isEmpty() ? declaringClass : s + "/" + declaringClass; 349 350 return s + "." + methodName + "(" + 351 (isNativeMethod() ? "Native Method)" : 359 * {@code StackTraceElement} instance representing the same execution 360 * point as this instance. Two stack trace elements {@code a} and 361 * {@code b} are equal if and only if: 362 * <pre>{@code 363 * equals(a.getClassLoaderName(), b.getClassLoaderName()) && 364 * equals(a.getModuleName(), b.getModuleName()) && 365 * equals(a.getModuleVersion(), b.getModuleVersion()) && 366 * equals(a.getClassName(), b.getClassName()) && 367 * equals(a.getMethodName(), b.getMethodName()) 368 * equals(a.getFileName(), b.getFileName()) && 369 * a.getLineNumber() == b.getLineNumber() 370 * 371 * }</pre> 372 * where {@code equals} has the semantics of {@link 373 * java.util.Objects#equals(Object, Object) Objects.equals}. 374 * 375 * @param obj the object to be compared with this stack trace element. 376 * @return true if the specified object is another 377 * {@code StackTraceElement} instance representing the same 378 * execution point as this instance. 379 */ 380 public boolean equals(Object obj) { 381 if (obj==this) 382 return true; 383 if (!(obj instanceof StackTraceElement)) 384 return false; 385 StackTraceElement e = (StackTraceElement)obj; 386 return Objects.equals(classLoaderName, e.classLoaderName) && 387 Objects.equals(moduleName, e.moduleName) && 388 Objects.equals(moduleVersion, e.moduleVersion) && 389 e.declaringClass.equals(declaringClass) && 390 e.lineNumber == lineNumber && 391 Objects.equals(methodName, e.methodName) && 392 Objects.equals(fileName, e.fileName); 393 } 394 395 /** 396 * Returns a hash code value for this stack trace element. 397 */ 398 public int hashCode() { | 75 * Creates a stack trace element representing the specified execution 76 * point. The {@link #getModuleName module name} and {@link 77 * #getModuleVersion module version} of the stack trace element will 78 * be {@code null}. 79 * 80 * @param declaringClass the fully qualified name of the class containing 81 * the execution point represented by the stack trace element 82 * @param methodName the name of the method containing the execution point 83 * represented by the stack trace element 84 * @param fileName the name of the file containing the execution point 85 * represented by the stack trace element, or {@code null} if 86 * this information is unavailable 87 * @param lineNumber the line number of the source line containing the 88 * execution point represented by this stack trace element, or 89 * a negative number if this information is unavailable. A value 90 * of -2 indicates that the method containing the execution point 91 * is a native method 92 * @throws NullPointerException if {@code declaringClass} or 93 * {@code methodName} is null 94 * @since 1.5 95 * @revised 9 96 * @spec JPMS 97 */ 98 public StackTraceElement(String declaringClass, String methodName, 99 String fileName, int lineNumber) { 100 this(null, null, null, declaringClass, methodName, fileName, lineNumber); 101 } 102 103 /** 104 * Creates a stack trace element representing the specified execution 105 * point. 106 * 107 * @param classLoaderName the class loader name if the class loader of 108 * the class containing the execution point represented by 109 * the stack trace is named; otherwise {@code null} 110 * @param moduleName the module name if the class containing the 111 * execution point represented by the stack trace is in a named 112 * module; otherwise {@code null} 113 * @param moduleVersion the module version if the class containing the 114 * execution point represented by the stack trace is in a named 115 * module that has a version; otherwise {@code null} 116 * @param declaringClass the fully qualified name of the class containing 117 * the execution point represented by the stack trace element 118 * @param methodName the name of the method containing the execution point 119 * represented by the stack trace element 120 * @param fileName the name of the file containing the execution point 121 * represented by the stack trace element, or {@code null} if 122 * this information is unavailable 123 * @param lineNumber the line number of the source line containing the 124 * execution point represented by this stack trace element, or 125 * a negative number if this information is unavailable. A value 126 * of -2 indicates that the method containing the execution point 127 * is a native method 128 * 129 * @throws NullPointerException if {@code declaringClass} is {@code null} 130 * or {@code methodName} is {@code null} 131 * 132 * @since 9 133 * @spec JPMS 134 */ 135 public StackTraceElement(String classLoaderName, 136 String moduleName, String moduleVersion, 137 String declaringClass, String methodName, 138 String fileName, int lineNumber) { 139 this.classLoaderName = classLoaderName; 140 this.moduleName = moduleName; 141 this.moduleVersion = moduleVersion; 142 this.declaringClass = Objects.requireNonNull(declaringClass, "Declaring class is null"); 143 this.methodName = Objects.requireNonNull(methodName, "Method name is null"); 144 this.fileName = fileName; 145 this.lineNumber = lineNumber; 146 } 147 148 /* 149 * Private constructor for the factory methods to create StackTraceElement 150 * for Throwable and StackFrameInfo 151 */ 152 private StackTraceElement() {} 153 173 * derived from the {@code LineNumberTable} attribute of the relevant 174 * {@code class} file (as per <i>The Java Virtual Machine 175 * Specification</i>, Section 4.7.8). 176 * 177 * @return the line number of the source line containing the execution 178 * point represented by this stack trace element, or a negative 179 * number if this information is unavailable. 180 */ 181 public int getLineNumber() { 182 return lineNumber; 183 } 184 185 /** 186 * Returns the module name of the module containing the execution point 187 * represented by this stack trace element. 188 * 189 * @return the module name of the {@code Module} containing the execution 190 * point represented by this stack trace element; {@code null} 191 * if the module name is not available. 192 * @since 9 193 * @spec JPMS 194 * @see java.lang.reflect.Module#getName() 195 */ 196 public String getModuleName() { 197 return moduleName; 198 } 199 200 /** 201 * Returns the module version of the module containing the execution point 202 * represented by this stack trace element. 203 * 204 * @return the module version of the {@code Module} containing the execution 205 * point represented by this stack trace element; {@code null} 206 * if the module version is not available. 207 * @since 9 208 * @spec JPMS 209 * @see java.lang.module.ModuleDescriptor.Version 210 */ 211 public String getModuleVersion() { 212 return moduleVersion; 213 } 214 215 /** 216 * Returns the name of the class loader of the class containing the 217 * execution point represented by this stack trace element. 218 * 219 * @return the name of the class loader of the class containing the execution 220 * point represented by this stack trace element; {@code null} 221 * if the class loader is not named. 222 * 223 * @since 9 224 * @spec JPMS 225 * @see java.lang.ClassLoader#getName() 226 */ 227 public String getClassLoaderName() { 228 return classLoaderName; 229 } 230 231 /** 232 * Returns the fully qualified name of the class containing the 233 * execution point represented by this stack trace element. 234 * 235 * @return the fully qualified name of the {@code Class} containing 236 * the execution point represented by this stack trace element. 237 */ 238 public String getClassName() { 239 return declaringClass; 240 } 241 242 /** 243 * Returns the name of the method containing the execution point 244 * represented by this stack trace element. If the execution point is 318 * 319 * <p> If a class is defined in an <em>unnamed module</em> 320 * then the second element is omitted as shown in 321 * "{@code com.foo.loader//com.foo.bar.App.run(App.java:12)}". 322 * 323 * <p> If the class loader is a <a href="ClassLoader.html#builtinLoaders"> 324 * built-in class loader</a> or is not named then the first element 325 * and its following {@code "/"} are omitted as shown in 326 * "{@code acme@2.1/org.acme.Lib.test(Lib.java:80)}". 327 * If the first element is omitted and the module is an unnamed module, 328 * the second element and its following {@code "/"} are also omitted 329 * as shown in "{@code MyClass.mash(MyClass.java:9)}". 330 * 331 * <p> The {@code toString} method may return two different values on two 332 * {@code StackTraceElement} instances that are 333 * {@linkplain #equals(Object) equal}, for example one created via the 334 * constructor, and one obtained from {@link java.lang.Throwable} or 335 * {@link java.lang.StackWalker.StackFrame}, where an implementation may 336 * choose to omit some element in the returned string. 337 * 338 * @revised 9 339 * @spec JPMS 340 * @see Throwable#printStackTrace() 341 */ 342 public String toString() { 343 String s = ""; 344 if (!dropClassLoaderName() && classLoaderName != null && 345 !classLoaderName.isEmpty()) { 346 s += classLoaderName + "/"; 347 } 348 if (moduleName != null && !moduleName.isEmpty()) { 349 s += moduleName; 350 351 if (!dropModuleVersion() && moduleVersion != null && 352 !moduleVersion.isEmpty()) { 353 s += "@" + moduleVersion; 354 } 355 } 356 s = s.isEmpty() ? declaringClass : s + "/" + declaringClass; 357 358 return s + "." + methodName + "(" + 359 (isNativeMethod() ? "Native Method)" : 367 * {@code StackTraceElement} instance representing the same execution 368 * point as this instance. Two stack trace elements {@code a} and 369 * {@code b} are equal if and only if: 370 * <pre>{@code 371 * equals(a.getClassLoaderName(), b.getClassLoaderName()) && 372 * equals(a.getModuleName(), b.getModuleName()) && 373 * equals(a.getModuleVersion(), b.getModuleVersion()) && 374 * equals(a.getClassName(), b.getClassName()) && 375 * equals(a.getMethodName(), b.getMethodName()) 376 * equals(a.getFileName(), b.getFileName()) && 377 * a.getLineNumber() == b.getLineNumber() 378 * 379 * }</pre> 380 * where {@code equals} has the semantics of {@link 381 * java.util.Objects#equals(Object, Object) Objects.equals}. 382 * 383 * @param obj the object to be compared with this stack trace element. 384 * @return true if the specified object is another 385 * {@code StackTraceElement} instance representing the same 386 * execution point as this instance. 387 * 388 * @revised 9 389 * @spec JPMS 390 */ 391 public boolean equals(Object obj) { 392 if (obj==this) 393 return true; 394 if (!(obj instanceof StackTraceElement)) 395 return false; 396 StackTraceElement e = (StackTraceElement)obj; 397 return Objects.equals(classLoaderName, e.classLoaderName) && 398 Objects.equals(moduleName, e.moduleName) && 399 Objects.equals(moduleVersion, e.moduleVersion) && 400 e.declaringClass.equals(declaringClass) && 401 e.lineNumber == lineNumber && 402 Objects.equals(methodName, e.methodName) && 403 Objects.equals(fileName, e.fileName); 404 } 405 406 /** 407 * Returns a hash code value for this stack trace element. 408 */ 409 public int hashCode() { |