Can transient keywords mark a method?
Asked Answered
L

6

26

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?

Lyric answered 26/4, 2013 at 10:9 Comment(6)
@user85121 can you provide the link where you see that?Cheiron
I checked jdk 1.6 source and it doesn't have transient keywordCora
Related: #4937303Ewold
@user85121, what version of jdk do you have? This is an important question, in order to check what is going on in the specific source. If this code is the result from a decompiler the author should have mentioned this!Stony
It isn't in JDK5/6, might have come in between releases or never at all. See javasourcecode.org/html/open-source/jdk/jdk-5.0/java/util/… and javasourcecode.org/html/open-source/jdk/jdk-6u23/java/util/…Ewold
Seems the author disappeared... :(Stony
C
51

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;

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

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.

Clarindaclarine answered 26/4, 2013 at 10:20 Comment(0)
S
6

If this code has been decompiled it is most likely a result of this: Why Java methods with varargs identified as transient?

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.

Stony answered 26/4, 2013 at 10:22 Comment(0)
M
3

transient can only be applied to member variables and not to methods so there is a problem here.

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.

I think there is a bug in whichever decompiler you're using which is erroneously adding transisent to the method declaration.

Myself answered 26/4, 2013 at 10:20 Comment(0)
C
1

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?

Cheiron answered 26/4, 2013 at 10:16 Comment(0)
E
1

Java documentation states that transient keyword is only applied to instance variables so this doesn´t make any sense

Embolden answered 26/4, 2013 at 10:18 Comment(0)
R
0
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private Boolean  succ;
    private String code;
    private String msg;
    protected T data;

    public Result() {
    }

    public Result(Boolean succ) {
        this.succ = succ;
    }

    
    


    //Can use this here
    @Transient
    public boolean isFail(){
        return !isSucc();
    }
    public boolean isSucc() {
        return succ != null && succ;
    }
}
Rationalize answered 12/10, 2021 at 7:17 Comment(2)
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.Whirlybird
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Dexedrine

© 2022 - 2024 — McMap. All rights reserved.