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

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

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

问题

private native Throwable fillInStackTrace(int dummy);

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

更新:

public class MyEx extends RuntimeException{
   
    @Override
    public synchronized Throwable fillInStackTrace() {
        Method method = null;
        try {
            Class<?>[] classArray = new Class<?>[1];
            classArray[0] = int.class;
            method = Throwable.class.getDeclaredMethod("fillInStackTrace", classArray);
            method.setAccessible(true);
            Object obj = method.invoke(this, 6);

            StackTraceElement[] trace = ((MyEx) obj).getStackTrace();
            System.out.println();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return this;
    }
}

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

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

英文:
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:

public class MyEx extends RuntimeException{
   
    @Override
    public synchronized Throwable fillInStackTrace() {
        Method method = null;
        try {
            Class&lt;?&gt;[] classArray = new Class&lt;?&gt;[1];
            classArray[0] = int.class;
            method =Throwable.class.getDeclaredMethod(&quot;fillInStackTrace&quot;, classArray);
            method.setAccessible(true);
            Object obg = method.invoke(this, 6);

            StackTraceElement[] trace = ((MyEx) obg).getStackTrace();
            System.out.println();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return this;
    }
}

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中,桥接方法的实现如下:

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

如您所见,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:

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

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:

确定