`int dummy`在Java中的`fillInStackTrace()`方法中是什么意思?

huangapple go评论147阅读模式
英文:

What is the meaning of `int dummy` in the fillInStackTrace() method in Java?

问题

  1. private native Throwable fillInStackTrace(int dummy);

这个方法在创建异常时使用dummy=0来调用。dummy的含义是什么?它是要构造的堆栈跟踪的深度吗?

更新:

  1. public class MyEx extends RuntimeException{
  2. @Override
  3. public synchronized Throwable fillInStackTrace() {
  4. Method method = null;
  5. try {
  6. Class<?>[] classArray = new Class<?>[1];
  7. classArray[0] = int.class;
  8. method = Throwable.class.getDeclaredMethod("fillInStackTrace", classArray);
  9. method.setAccessible(true);
  10. Object obj = method.invoke(this, 6);
  11. StackTraceElement[] trace = ((MyEx) obj).getStackTrace();
  12. System.out.println();
  13. } catch (NoSuchMethodException e) {
  14. e.printStackTrace();
  15. } catch (IllegalAccessException e) {
  16. e.printStackTrace();
  17. } catch (InvocationTargetException e) {
  18. e.printStackTrace();
  19. }
  20. return this;
  21. }
  22. }

看起来dummy真的是虚拟的,无论我放入什么值,结果都是一样的...

我想将堆栈跟踪的大小限制为3,以减少内存消耗,从执行的角度来看,创建异常也会更快。我有一个真实的用例,我需要很多浅层堆栈跟踪的异常。

英文:
  1. private native Throwable fillInStackTrace(int dummy);

This method is called with dummy=0 when an Exception is created. What is the meaning of dummy? Is it the depth of the stack trace to construct?

Update:

  1. public class MyEx extends RuntimeException{
  2. @Override
  3. public synchronized Throwable fillInStackTrace() {
  4. Method method = null;
  5. try {
  6. Class&lt;?&gt;[] classArray = new Class&lt;?&gt;[1];
  7. classArray[0] = int.class;
  8. method =Throwable.class.getDeclaredMethod(&quot;fillInStackTrace&quot;, classArray);
  9. method.setAccessible(true);
  10. Object obg = method.invoke(this, 6);
  11. StackTraceElement[] trace = ((MyEx) obg).getStackTrace();
  12. System.out.println();
  13. } catch (NoSuchMethodException e) {
  14. e.printStackTrace();
  15. } catch (IllegalAccessException e) {
  16. e.printStackTrace();
  17. } catch (InvocationTargetException e) {
  18. e.printStackTrace();
  19. }
  20. return this;
  21. }
  22. }

It seems that dummy is really dummy, it doesn't matter what value I put in, the result will be the same...

I wanted to limit the stack trace size to 3 to consume less memory and also creating exceptions would be faster from an execution point of view. I have a real use case where I need many exceptions but with shallow stack traces.

答案1

得分: 1

它没有任何意义。这是一个虚拟参数。

本地代码实现完全忽略了这个参数。例如,在OpenJDK 11中,桥接方法的实现如下:

  1. /*
  2. * 在这个异常中填入当前堆栈跟踪。通常在创建异常时自动调用,但用户也可以显式调用此方法。此例程返回`this',因此您可以编写`throw e.fillInStackTrace();'
  3. */
  4. JNIEXPORT jobject JNICALL
  5. Java_java_lang_Throwable_fillInStackTrace(JNIEnv *env,
  6. jobject throwable, jint dummy)
  7. {
  8. JVM_FillInStackTrace(env, throwable);
  9. return throwable;
  10. }

如您所见,dummy 参数被忽略。


如果您想要限制堆栈跟踪的深度,支持的方法是使用 -XX:MaxJavaStackTraceDepth=depth 选项。

英文:

It doesn't mean anything. It is a dummy argument.

The native code implementation completely ignores the argument. For example, in OpenJDK 11, the bridge method implementation is as follows:

  1. /*
  2. * Fill in the current stack trace in this exception. This is
  3. * usually called automatically when the exception is created but it
  4. * may also be called explicitly by the user. This routine returns
  5. * `this&#39; so you can write &#39;throw e.fillInStackTrace();&#39;
  6. */
  7. JNIEXPORT jobject JNICALL
  8. Java_java_lang_Throwable_fillInStackTrace(JNIEnv *env,
  9. jobject throwable, jint dummy)
  10. {
  11. JVM_FillInStackTrace(env, throwable);
  12. return throwable;
  13. }

As you can see, the dummy argument is ignored.


If you are looking for a way to limit the depth of a stacktrace, the supported way to do this is to use the -XX:MaxJavaStackTraceDepth=depth option.

huangapple
  • 本文由 发表于 2020年9月5日 18:33:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63752932.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定