瞬态关键字可以标记方法吗? [英] Can transient keywords mark a method?

查看:169
本文介绍了瞬态关键字可以标记方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在java类java.util.Locale中,我发现关键字transient标记了一个方法。

In a java class java.util.Locale, I find that the keyword transient marked a method.

 public final class Locale
    implements Cloneable, Serializable
{
    private static class LocaleNameGetter
        implements sun.util.LocaleServiceProviderPool.LocalizedObjectGetter
    {

        public transient String getObject(LocaleNameProvider localenameprovider, Locale locale, String s, Object aobj[])
        {
            if(!$assertionsDisabled && aobj.length != 2)
                throw new AssertionError();
            int i = ((Integer)aobj[0]).intValue();
            String s1 = (String)aobj[1];
            switch(i)
            {
            case 0: // '\0'
                return localenameprovider.getDisplayLanguage(s1, locale);

            case 1: // '\001'
                return localenameprovider.getDisplayCountry(s1, locale);

            case 2: // '\002'
                return localenameprovider.getDisplayVariant(s1, locale);
            }
            if(!$assertionsDisabled)
                throw new AssertionError();
            else
                return null;
        }

有人可以告诉我为什么会这样吗?

Can someone tell me why can this be?

推荐答案

不,它不能,它只对字段有效。您似乎通过反编译从.class获取源代码。这是反编译器的错误,如果你看一下 java.lang.reflect.Modifier src,你会看到 transient varargs 具有相同的值

No it can't, it's only valid for fields. You seem to get your source from .class by decompiling. This is the decompiler bug, if you take a look at java.lang.reflect.Modifier src you will see that transient and varargs have the same value

public static final int TRANSIENT        = 0x00000080;
...
static final int VARARGS   = 0x00000080;

字段 0x00000080 表示 transient ,对于方法(您的情况),它意味着 varargs 。这就是 getObject 在java.util.Locale src中的样子

for a field 0x00000080 means transient, for a method (your case) it means varargs. This is how getObject looks like in java.util.Locale src

public String getObject(LocaleNameProvider localeNameProvider,
                        Locale locale, 
                        String key,
                        Object... params) {   <-- varargs

在.class(字节码)中,varargs由Object []表示为最后一个参数+修饰符bit 7 = 1(0x80)。我想反编译器是旧的,根本不知道 varargs 这是自Java 1.5以来所以它打印为 transient

In .class (bytecode) varargs is represented by Object[] as the last parameter + modifier bit 7 = 1 (0x80). I guess the decompiler is old and simply does not know about varargs which is since Java 1.5 so it printed it as transient.

这篇关于瞬态关键字可以标记方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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