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?
From Java Glossary, transient:
A keyword in the Java programming language that indicates that a field is not part of the serialized form of an object. When an object is serialized, the values of its transient fields are not included in the serial representation, while the values of its non-transient fields are included.
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){}
}
Outputs:
main --> public static
foo --> public static transient
Sort of an answer can be found in the code of javassist
AccessFlag
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.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 thetoString()
method can be rewritten to include "transient/varargs".