在日常开发中,会经常使用反射调用隐藏api或者其他方法,此时需要一个非常方便就能指定位置的反射库,此反射库采用接口式的写法,可快速的复制源码上的方法,打上注解,反射代码则会自动生成,不用额外的编写反射代码。
allprojects {
repositories {
...
// 加入仓库
maven { url 'https://jitpack.io' }
}
}
implementation 'com.github.CodingGay.BlackReflection:core:1.0.3'
annotationProcessor 'com.github.CodingGay.BlackReflection:compiler:1.0.3'
1. 如果你需要反射 android.app.ActivityThread 中的各种方法,参考:ActivityThread.java
@BClassName("android.app.ActivityThread")
public interface ActivityThread {
@BStaticMethod
Object currentPackageName();
@BStaticMethod
Object currentActivityThread();
@BMethod
String getProcessName();
@BMethod
void sendActivityResult(@BParamClass(IBinder.class) IBinder token, String id, int requestCode, int resultCode, Intent data);
@BClassName("android.app.ActivityThread$H")
interface H {
@BStaticField
int CREATE_SERVICE();
}
}
Object currentActivityThread = BRActivityThread.get().currentActivityThread();
String processName = BRActivityThread.get(currentActivityThread).getProcessName();
Log.d(TAG, "processName: " + processName);
BRActivityThread是程序自动生成的类,生成规则是BR + ClassName
- BRActivityThread.get() 用于调用静态方法
- BRActivityThread.get(caller) 用于调用非静态方法
注解 | 注解方式 | 解释 |
---|---|---|
@BClass | Class | 指定需要反射的类 |
@BClassName | Class | 指定需要反射的类 |
@BStaticMethod | Method | 注明是静态方法 |
@BMethod | Method | 注明是非静态方法 |
@BStaticField | Method | 注明是静态变量 |
@BField | Method | 注明是非静态变量 |
@BParamClass | Parameter | 注明该参数的Class,用于反射时寻找方法 |
@BParamClassName | Parameter | 注明该参数的Class,用于反射时寻找方法 |
Copyright 2022 Milk Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.