I want to get the mMap
collection held in Bundle
.
In the bundle
class, there is getMap()
The method can be obtained, but it is marked as hide
, so I use reflection to get it.
ArrayMap<String, Object> metaMap= null;
try {
Class ownerClass = Class.forName(Bundle.class.getName());
Class superclass = ownerClass.getSuperclass();
Method[] declaredMethods = superclass.getDeclaredMethods();
// getMapMethod.setAccessible(true);
// metaMap= (ArrayMap<String, Object>) getMapMethod.invoke(metaData);
Log.e("test","superclass: "+superclass.getName());
declaredMethods.toString();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
This is the code I tested. I used to call the method directly, and then an exception was thrown. So I printed all the methods:
0 = {Method@4216} "public void android.os.BaseBundle.clear()"
1 = {Method@4217} "public boolean android.os.BaseBundle.containsKey(java.lang.String)"
2 = {Method@4218} "public java.lang.Object android.os.BaseBundle.get(java.lang.String)"
3 = {Method@4219} "public boolean android.os.BaseBundle.getBoolean(java.lang.String)"
4 = {Method@4220} "public boolean android.os.BaseBundle.getBoolean(java.lang.String,boolean)"
5 = {Method@4221} "public [Z android.os.BaseBundle.getBooleanArray(java.lang.String)"
6 = {Method@4222} "byte android.os.BaseBundle.getByte(java.lang.String)"
7 = {Method@4223} "java.lang.Byte android.os.BaseBundle.getByte(java.lang.String,byte)"
8 = {Method@4224} "[B android.os.BaseBundle.getByteArray(java.lang.String)"
9 = {Method@4225} "char android.os.BaseBundle.getChar(java.lang.String)"
10 = {Method@4226} "char android.os.BaseBundle.getChar(java.lang.String,char)"
11 = {Method@4227} "[C android.os.BaseBundle.getCharArray(java.lang.String)"
12 = {Method@4228} "java.lang.CharSequence android.os.BaseBundle.getCharSequence(java.lang.String)"
13 = {Method@4229} "java.lang.CharSequence android.os.BaseBundle.getCharSequence(java.lang.String,java.lang.CharSequence)"
14 = {Method@4230} "[Ljava.lang.CharSequence; android.os.BaseBundle.getCharSequenceArray(java.lang.String)"
15 = {Method@4231} "java.util.ArrayList android.os.BaseBundle.getCharSequenceArrayList(java.lang.String)"
16 = {Method@4232} "java.lang.ClassLoader android.os.BaseBundle.getClassLoader()"
17 = {Method@4233} "public double android.os.BaseBundle.getDouble(java.lang.String)"
18 = {Method@4234} "public double android.os.BaseBundle.getDouble(java.lang.String,double)"
19 = {Method@4235} "public [D android.os.BaseBundle.getDoubleArray(java.lang.String)"
20 = {Method@4236} "float android.os.BaseBundle.getFloat(java.lang.String)"
21 = {Method@4237} "float android.os.BaseBundle.getFloat(java.lang.String,float)"
22 = {Method@4238} "[F android.os.BaseBundle.getFloatArray(java.lang.String)"
23 = {Method@4239} "public int android.os.BaseBundle.getInt(java.lang.String)"
24 = {Method@4240} "public int android.os.BaseBundle.getInt(java.lang.String,int)"
25 = {Method@4241} "public [I android.os.BaseBundle.getIntArray(java.lang.String)"
26 = {Method@4242} "java.util.ArrayList android.os.BaseBundle.getIntegerArrayList(java.lang.String)"
27 = {Method@4243} "public long android.os.BaseBundle.getLong(java.lang.String)"
28 = {Method@4244} "public long android.os.BaseBundle.getLong(java.lang.String,long)"
29 = {Method@4245} "public [J android.os.BaseBundle.getLongArray(java.lang.String)"
30 = {Method@4246} "public java.lang.String android.os.BaseBundle.getPairValue()"
31 = {Method@4247} "java.io.Serializable android.os.BaseBundle.getSerializable(java.lang.String)"
32 = {Method@4248} "short android.os.BaseBundle.getShort(java.lang.String)"
33 = {Method@4249} "short android.os.BaseBundle.getShort(java.lang.String,short)"
34 = {Method@4250} "[S android.os.BaseBundle.getShortArray(java.lang.String)"
35 = {Method@4251} "public java.lang.String android.os.BaseBundle.getString(java.lang.String)"
36 = {Method@4252} "public java.lang.String android.os.BaseBundle.getString(java.lang.String,java.lang.String)"
37 = {Method@4253} "public [Ljava.lang.String; android.os.BaseBundle.getStringArray(java.lang.String)"
38 = {Method@4254} "java.util.ArrayList android.os.BaseBundle.getStringArrayList(java.lang.String)"
39 = {Method@4255} "public boolean android.os.BaseBundle.isEmpty()"
40 = {Method@4256} "public boolean android.os.BaseBundle.isParcelled()"
41 = {Method@4257} "public java.util.Set android.os.BaseBundle.keySet()"
42 = {Method@4258} "public void android.os.BaseBundle.putAll(android.os.PersistableBundle)"
43 = {Method@4259} "void android.os.BaseBundle.putAll(android.util.ArrayMap)"
44 = {Method@4260} "public void android.os.BaseBundle.putBoolean(java.lang.String,boolean)"
45 = {Method@4261} "public void android.os.BaseBundle.putBooleanArray(java.lang.String,boolean[])"
46 = {Method@4262} "void android.os.BaseBundle.putByte(java.lang.String,byte)"
47 = {Method@4263} "void android.os.BaseBundle.putByteArray(java.lang.String,byte[])"
48 = {Method@4264} "void android.os.BaseBundle.putChar(java.lang.String,char)"
49 = {Method@4265} "void android.os.BaseBundle.putCharArray(java.lang.String,char[])"
50 = {Method@4266} "void android.os.BaseBundle.putCharSequence(java.lang.String,java.lang.CharSequence)"
51 = {Method@4267} "void android.os.BaseBundle.putCharSequenceArray(java.lang.String,java.lang.CharSequence[])"
52 = {Method@4268} "void android.os.BaseBundle.putCharSequenceArrayList(java.lang.String,java.util.ArrayList)"
53 = {Method@4269} "public void android.os.BaseBundle.putDouble(java.lang.String,double)"
54 = {Method@4270} "public void android.os.BaseBundle.putDoubleArray(java.lang.String,double[])"
55 = {Method@4271} "void android.os.BaseBundle.putFloat(java.lang.String,float)"
56 = {Method@4272} "void android.os.BaseBundle.putFloatArray(java.lang.String,float[])"
57 = {Method@4273} "public void android.os.BaseBundle.putInt(java.lang.String,int)"
58 = {Method@4274} "public void android.os.BaseBundle.putIntArray(java.lang.String,int[])"
59 = {Method@4275} "void android.os.BaseBundle.putIntegerArrayList(java.lang.String,java.util.ArrayList)"
60 = {Method@4276} "public void android.os.BaseBundle.putLong(java.lang.String,long)"
61 = {Method@4277} "public void android.os.BaseBundle.putLongArray(java.lang.String,long[])"
62 = {Method@4278} "void android.os.BaseBundle.putSerializable(java.lang.String,java.io.Serializable)"
63 = {Method@4279} "void android.os.BaseBundle.putShort(java.lang.String,short)"
64 = {Method@4280} "void android.os.BaseBundle.putShortArray(java.lang.String,short[])"
65 = {Method@4281} "public void android.os.BaseBundle.putString(java.lang.String,java.lang.String)"
66 = {Method@4282} "public void android.os.BaseBundle.putStringArray(java.lang.String,java.lang.String[])"
67 = {Method@4283} "void android.os.BaseBundle.putStringArrayList(java.lang.String,java.util.ArrayList)"
68 = {Method@4284} "void android.os.BaseBundle.readFromParcelInner(android.os.Parcel)"
69 = {Method@4285} "public void android.os.BaseBundle.remove(java.lang.String)"
70 = {Method@4286} "void android.os.BaseBundle.setClassLoader(java.lang.ClassLoader)"
71 = {Method@4287} "public int android.os.BaseBundle.size()"
72 = {Method@4288} "void android.os.BaseBundle.typeWarning(java.lang.String,java.lang.Object,java.lang.String,java.lang.ClassCastException)"
73 = {Method@4289} "void android.os.BaseBundle.typeWarning(java.lang.String,java.lang.Object,java.lang.String,java.lang.Object,java.lang.ClassCastException)"
74 = {Method@4290} "synchronized void android.os.BaseBundle.unparcel()"
75 = {Method@4291} "void android.os.BaseBundle.writeToParcelInner(android.os.Parcel,int)"
76 = {Method@4292} "private void android.os.BaseBundle.readFromParcelInner(android.os.Parcel,int)"
The methods I saw in the source code are as follows: (Only the method name is copied.)
BaseBundle
BaseBundle
BaseBundle
BaseBundle
BaseBundle
BaseBundle
BaseBundle
clear
containsKey
get
getBoolean
getBoolean
getBooleanArray
getByte
getByte
getByteArray
getChar
getChar
getCharArray
getCharSequence
getCharSequence
getCharSequenceArray
getCharSequenceArrayList
getClassLoader
getDouble
getDouble
getDoubleArray
getFloat
getFloat
getFloatArray
getInt
getInt
getIntArray
getIntegerArrayList
getLong
getLong
getLongArray
getMap
getPairValue
getSerializable
getShort
getShort
getShortArray
getString
getString
getStringArray
getStringArrayList
isEmpty
isEmptyParcel
isParcelled
keySet
putAll
putAll
putBoolean
putBooleanArray
putByte
putByteArray
putChar
putCharArray
putCharSequence
putCharSequenceArray
putCharSequenceArrayList
putDouble
putDoubleArray
putFloat
putFloatArray
putInt
putIntArray
putIntegerArrayList
putLong
putLongArray
putSerializable
putShort
putShortArray
putString
putStringArray
putStringArrayList
readFromParcelInner
readFromParcelInner
remove
setClassLoader
setShouldDefuse
size
typeWarning
typeWarning
unparcel
writeToParcelInner
Among them, isParcelled()
and getPairValue()
are also marked as hide
, but they can be obtained. I want to ask why?
I guess it’s the sdk version. My phone is sdk = 23
, and the version I compiled is 25
. I guess the methods that haven’t been obtained are after 23. Added, I wonder if there is any way to check the SDK version difference test, or there are other reasons.
巴扎黑2017-05-16 13:26:23
I compared the Bundle source code of Api23 and Api25, and there was no obvious information about getMap.
Then I compared the Bundle source code of Api16 and Api25, and found clues, as shown in the yellow block in the picture below:
We can see that on Api25, Bundle extends BaseBundle, but not on Api16, and the data structures used on both sides have also changed.
Let’s take a look at the BaseBundle on Api25, and there is indeed getMap:
/** @hide */
ArrayMap<String, Object> getMap() {
unparcel();
return mMap;
}
Then let’s look at searching for getMap in the BaseBundle of Api23. The results are as follows:
Obviously, On Api23, there is no getMap method in either Bundle or its parent class BaseBundle, so The reflection cannot be adjusted either.
为情所困2017-05-16 13:26:23
@hide belongs to a javadoc tag. When the Bundle source code is compiled into a jar package, the methods marked with @hide will be excluded. The generated jar package is for developers, so we cannot directly pass it in the IDE. The reference to the bundle object directly calls the hide method of the bundle class. This is no different from the SDK compiled version.
After you become familiar with Android as a whole, you can try to compile the Android source code to generate your own android.jar package. For example, there are more camera developments here, and many camera methods have been hidden by us. We don’t want to call the hide method through reflection every time, so we compiled a complete set of android.jar packages ourselves. The methods that are hidden in the camera can be directly called in the IDE.