问题描述
我正在使用Java Reflection API,并观察到具有可变参数列表的方法变为瞬态。为什么这个以及 transient
关键字在这个上下文中意味着什么?
I was playing with Java Reflection API and observed that methods with variadic argument list become transient. Why is that and what does transient
keyword mean in this context?
来自Java词汇表,瞬态:
From Java Glossary, transient:
然而,这个定义没有说明方法。有什么想法吗?
However this definition does not say anything about methods. Any ideas?
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class Dummy {
public static void main(String[] args) {
for(Method m : Dummy.class.getDeclaredMethods()){
System.out.println(m.getName() + " --> "+Modifier.toString(m.getModifiers()));
}
}
public static void foo(int... args){}
}
输出:
main --> public static
foo --> public static transient
推荐答案
答案的排序可以在
Sort of an answer can be found in the code of javassist AccessFlag
public static final int TRANSIENT = 0x0080;
public static final int VARARGS = 0x0080;
它们似乎都具有相同的值。而且因为 transient
对于方法没有任何意义,而varargs对于字段没有任何意义,它们可以是相同的。
It appears both have the same values. And since transient
means nothing for methods, while varargs means nothing for fields, it is ok for them to be the same.
但是,修饰符
类不能将此考虑在内。我提出了一个问题。它需要一个新常量 - VARARGS
和一个新方法 - isVarargs(..)
。并且可以重写 toString()
方法以包含transient / varargs。
But it is not OK for the Modifier
class not to take this into account. I'd file an issue about it. It needs a new constant - VARARGS
and a new method - isVarargs(..)
. And the toString()
method can be rewritten to include "transient/varargs".
这篇关于为什么带有varargs的Java方法被识别为瞬态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!