在上一篇文章《学习HandlerThread》我们提到虽然HandlerThread类里有getThreadHandler()方法得到Handler,但是我们不可能调用到它。因为这个方法用@hide
注释了
/**
* @return a shared {@link Handler} associated with this thread
* @hide
*/
@NonNull
public Handler getThreadHandler() {
if (mHandler == null) {
mHandler = new Handler(getLooper());
}
return mHandler;
}
那么我们来看看@hide
究竟是何方神圣。
如果我们的android项目的build.gradle配置了compileSdk 32
,那么在我们编译我们android应用时,$ANDROID_SDK_HOME/platforms/android-32/android.jar
就会加入我们编译时的类路径中,参与app的编译。@hide
会控制我们编译时用到的这个android.jar包里的东西。
android.jar作为编译android本身的一部分被创建。Android framework的类会被分析,并创建它们的副本。这个副本有以下特点:
- 去除被标注了
@hide
的类、方法、字段等 - 所有剩下的方法都有stub实现,即
throw new RuntimeException("Stub!")
- 保留所有没有被去除的类、方法、字段的JavaDoc注解
我们使用javap命令查看一下android.os.HandlerThread.class里的东西:
% javap -verbose android/os/HandlerThread.class
从下面我们可以看到HandlerThread.class确实没有了getThreadHandler方法。
Classfile
***/adddd/android/android/os/HandlerThread.class
Last modified Jan 1, 2008; size 1043 bytes
SHA-256 checksum 8161f60748df5cf7489dbf189c3f4668d81a17595dbd55e52baa0f8263bb96d4
Compiled from "HandlerThread.java"
public class android.os.HandlerThread extends java.lang.Thread
minor version: 0
major version: 52
flags: (0x0021) ACC_PUBLIC, ACC_SUPER
this_class: #5 // android/os/HandlerThread
super_class: #6 // java/lang/Thread
interfaces: 0, fields: 0, methods: 8, attributes: 1
Constant pool:
#1 = Methodref #6.#31 // java/lang/Thread."<init>":()V
#2 = Class #32 // java/lang/RuntimeException
#3 = String #33 // Stub!
#4 = Methodref #2.#34 // java/lang/RuntimeException."<init>":(Ljava/lang/String;)V
#5 = Class #35 // android/os/HandlerThread
#6 = Class #36 // java/lang/Thread
#7 = Utf8 <init>
#8 = Utf8 (Ljava/lang/String;)V
#9 = Utf8 Code
#10 = Utf8 LineNumberTable
#11 = Utf8 LocalVariableTable
#12 = Utf8 this
#13 = Utf8 Landroid/os/HandlerThread;
#14 = Utf8 name
#15 = Utf8 Ljava/lang/String;
#16 = Utf8 (Ljava/lang/String;I)V
#17 = Utf8 priority
#18 = Utf8 I
#19 = Utf8 onLooperPrepared
#20 = Utf8 ()V
#21 = Utf8 run
#22 = Utf8 getLooper
#23 = Utf8 ()Landroid/os/Looper;
#24 = Utf8 quit
#25 = Utf8 ()Z
#26 = Utf8 quitSafely
#27 = Utf8 getThreadId
#28 = Utf8 ()I
#29 = Utf8 SourceFile
#30 = Utf8 HandlerThread.java
#31 = NameAndType #7:#20 // "<init>":()V
#32 = Utf8 java/lang/RuntimeException
#33 = Utf8 Stub!
#34 = NameAndType #7:#8 // "<init>":(Ljava/lang/String;)V
#35 = Utf8 android/os/HandlerThread
#36 = Utf8 java/lang/Thread
{
public android.os.HandlerThread(java.lang.String);
descriptor: (Ljava/lang/String;)V
flags: (0x0001) ACC_PUBLIC
Code:
stack=3, locals=2, args_size=2
0: aload_0
1: invokespecial #1 // Method java/lang/Thread."<init>":()V
4: new #2 // class java/lang/RuntimeException
7: dup
8: ldc #3 // String Stub!
10: invokespecial #4 // Method java/lang/RuntimeException."<init>":(Ljava/lang/String;)V
13: athrow
LineNumberTable:
line 23: 0
LocalVariableTable:
Start Length Slot Name Signature
0 14 0 this Landroid/os/HandlerThread;
0 14 1 name Ljava/lang/String;
public android.os.HandlerThread(java.lang.String, int);
descriptor: (Ljava/lang/String;I)V
flags: (0x0001) ACC_PUBLIC
Code:
stack=3, locals=3, args_size=3
0: aload_0
1: invokespecial #1 // Method java/lang/Thread."<init>":()V
4: new #2 // class java/lang/RuntimeException
7: dup
8: ldc #3 // String Stub!
10: invokespecial #4 // Method java/lang/RuntimeException."<init>":(Ljava/lang/String;)V
13: athrow
LineNumberTable:
line 25: 0
LocalVariableTable:
Start Length Slot Name Signature
0 14 0 this Landroid/os/HandlerThread;
0 14 1 name Ljava/lang/String;
0 14 2 priority I
protected void onLooperPrepared();
descriptor: ()V
flags: (0x0004) ACC_PROTECTED
Code:
stack=3, locals=1, args_size=1
0: new #2 // class java/lang/RuntimeException
3: dup
4: ldc #3 // String Stub!
6: invokespecial #4 // Method java/lang/RuntimeException."<init>":(Ljava/lang/String;)V
9: athrow
LineNumberTable:
line 27: 0
LocalVariableTable:
Start Length Slot Name Signature
0 10 0 this Landroid/os/HandlerThread;
public void run();
descriptor: ()V
flags: (0x0001) ACC_PUBLIC
Code:
stack=3, locals=1, args_size=1
0: new #2 // class java/lang/RuntimeException
3: dup
4: ldc #3 // String Stub!
6: invokespecial #4 // Method java/lang/RuntimeException."<init>":(Ljava/lang/String;)V
9: athrow
LineNumberTable:
line 29: 0
LocalVariableTable:
Start Length Slot Name Signature
0 10 0 this Landroid/os/HandlerThread;
public android.os.Looper getLooper();
descriptor: ()Landroid/os/Looper;
flags: (0x0001) ACC_PUBLIC
Code:
stack=3, locals=1, args_size=1
0: new #2 // class java/lang/RuntimeException
3: dup
4: ldc #3 // String Stub!
6: invokespecial #4 // Method java/lang/RuntimeException."<init>":(Ljava/lang/String;)V
9: athrow
LineNumberTable:
line 31: 0
LocalVariableTable:
Start Length Slot Name Signature
0 10 0 this Landroid/os/HandlerThread;
public boolean quit();
descriptor: ()Z
flags: (0x0001) ACC_PUBLIC
Code:
stack=3, locals=1, args_size=1
0: new #2 // class java/lang/RuntimeException
3: dup
4: ldc #3 // String Stub!
6: invokespecial #4 // Method java/lang/RuntimeException."<init>":(Ljava/lang/String;)V
9: athrow
LineNumberTable:
line 33: 0
LocalVariableTable:
Start Length Slot Name Signature
0 10 0 this Landroid/os/HandlerThread;
public boolean quitSafely();
descriptor: ()Z
flags: (0x0001) ACC_PUBLIC
Code:
stack=3, locals=1, args_size=1
0: new #2 // class java/lang/RuntimeException
3: dup
4: ldc #3 // String Stub!
6: invokespecial #4 // Method java/lang/RuntimeException."<init>":(Ljava/lang/String;)V
9: athrow
LineNumberTable:
line 35: 0
LocalVariableTable:
Start Length Slot Name Signature
0 10 0 this Landroid/os/HandlerThread;
public int getThreadId();
descriptor: ()I
flags: (0x0001) ACC_PUBLIC
Code:
stack=3, locals=1, args_size=1
0: new #2 // class java/lang/RuntimeException
3: dup
4: ldc #3 // String Stub!
6: invokespecial #4 // Method java/lang/RuntimeException."<init>":(Ljava/lang/String;)V
9: athrow
LineNumberTable:
line 37: 0
LocalVariableTable:
Start Length Slot Name Signature
0 10 0 this Landroid/os/HandlerThread;
}
SourceFile: "HandlerThread.java"
这里再介绍一下从这个HandlerThread.class里面看到的一些信息:
- minor version: 0 major version: 52:表示当前这个.class文件是用哪个编译器生产的
JDK Version | (in Decimal) Major Version |
Java SE 8.0 | 52 |
Java SE 11.0 | 55 |
- Compiled from “HandlerThread.java”:说明.class文件是由这"HandlerThread.java"编译而来的