为什么带有varargs的Java方法被识别为瞬态? [英] Why Java methods with varargs identified as transient?

查看:170
本文介绍了为什么带有varargs的Java方法被识别为瞬态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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:


Java编程语言中的关键字,指示字段不是对象的序列化形式的一部分。当一个对象被序列化时,其瞬态字段的值不包含在串行表示中,而其非瞬态字段的值包括在内。

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){}
}

输出:

main --> public static
foo --> public static transient


推荐答案

答案的排序可以在 javassist AccessFlag的代码

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方法被识别为瞬态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆