Can transient keywords mark a method?

13,255

Solution 1

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.

Solution 2

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.

Solution 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.

Solution 4

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?

Solution 5

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

Share:
13,255

Related videos on Youtube

Chuanshi Liu
Author by

Chuanshi Liu

Updated on October 12, 2021

Comments

  • Chuanshi Liu
    Chuanshi Liu over 2 years

    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?

  • Suraj Rao
    Suraj Rao over 2 years
    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.
  • Admin
    Admin over 2 years
    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.