java 瞬态关键字可以标记方法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16233910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can transient keywords mark a method?
提问by Chuanshi Liu
In a java class java.util.Locale, I find that the keyword transient marked a method.
在java 类java.util.Locale 中,我发现关键字transient 标记了一个方法。
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: // 'public static final int TRANSIENT = 0x00000080;
...
static final int VARARGS = 0x00000080;
'
return localenameprovider.getDisplayLanguage(s1, locale);
case 1: // 'public String getObject(LocaleNameProvider localeNameProvider,
Locale locale,
String key,
Object... params) { <-- varargs
1'
return localenameprovider.getDisplayCountry(s1, locale);
case 2: // '##代码##2'
return localenameprovider.getDisplayVariant(s1, locale);
}
if(!$assertionsDisabled)
throw new AssertionError();
else
return null;
}
Can someone tell me why can this be?
有人能告诉我为什么会这样吗?
回答by Evgeniy Dorofeev
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
不,它不能,它只对字段有效。您似乎通过反编译从 .class 获取源代码。这是反编译器的错误,如果您查看java.lang.reflect.Modifier
src,您会看到它transient
并varargs
具有相同的值
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
对于字段0x00000080
意味着transient
,对于方法(您的情况)意味着varargs
。这是getObject
在 java.util.Locale src 中的样子
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
.
在 .class(字节码)中,可变参数由 Object[] 表示为最后一个参数 + 修饰符位 7 = 1 (0x80)。我猜反编译器很旧,根本不知道varargs
从 Java 1.5 开始是哪个版本,所以它打印为transient
.
回答by Menios
If this code has been decompiled it is most likely a result of this: Why Java methods with varargs identified as transient?
如果此代码已被反编译,则很可能是由于以下原因:为什么带有可变参数的 Java 方法被标识为瞬态?
I am quoting from there:
我从那里引用:
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; 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.
可以在 javassist AccessFlag 的代码中找到某种答案
公共静态最终 int TRANSIENT = 0x0080; 公共静态最终 int VARARGS = 0x0080;看起来两者具有相同的值。由于瞬态对于方法没有任何意义,而可变参数对于字段没有任何意义,因此它们可以相同。
回答by Dave Webb
transient
can only be applied to member variables and not to methods so there is a problem here.
transient
只能应用于成员变量,不能应用于方法,所以这里有问题。
Looking at the variable names in your code - things like String s
and Object[] aboj
- it looks like this source has been generated by decompiling the relevant .class
file.
查看代码中的变量名称——比如String s
和Object[] aboj
——看起来这个源代码是通过反编译相关.class
文件生成的。
I think there is a bug in whichever decompiler you're using which is erroneously adding transisent
to the method declaration.
我认为您使用的任何反编译器都存在错误,该错误会错误地添加transisent
到方法声明中。
回答by Eugene
This has to be a bug. Or some buggy revision? transient is only applied on variables. Can you provide a link where you see that?
这一定是个bug。或者一些错误的修订?瞬态仅适用于变量。你能提供一个你看到的链接吗?
回答by Rubén
Java documentation states that transient keyword is only applied to instance variables so this doesn′t make any sense
Java 文档指出,transient 关键字仅适用于实例变量,因此这没有任何意义