在日常开发中,会经常使用反射调用隐藏api或者其他方法,此时需要一个非常方便就能指定位置的反射库,此反射库采用接口式的写法,可快速的复制源码上的方法,打上注解,反射代码则会自动生成,不用额外的编写反射代码。
allprojects {
repositories {
...
// 加入仓库
maven { url 'https://jitpack.io' }
}
}
implementation 'com.github.CodingGay.BlackReflection:core:1.0.9'
annotationProcessor 'com.github.CodingGay.BlackReflection:compiler:1.0.9'
1. 如果你需要反射 top.niunaijun.app.bean.TestReflection 中的各种方法,参考:MainActivity.java
public class TestReflection {
public static final String TAG = "TestConstructor";
public String mContextValue = "context value";
public static String sStaticValue = "static value";
public TestReflection(String a) {
Log.d(TAG, "Constructor called :" + a);
}
public TestReflection(String a, String b) {
Log.d(TAG, "Constructor called : a = " + a + ", b = " + b);
}
public String testContextInvoke(String a, int b) {
Log.d(TAG, "Context invoke: a = " + a + ", b = " + b);
return a + b;
}
public static String testStaticInvoke(String a, int b) {
Log.d(TAG, "Static invoke: a = " + a + ", b = " + b);
return a + b;
}
public static String testParamClassName(String a, int b) {
Log.d(TAG, "testParamClassName: a = " + a + ", b = " + b);
return a + b;
}
}
可以写成如下接口
@BClass(top.niunaijun.app.bean.TestReflection.class)
public interface TestReflection {
@BConstructor
top.niunaijun.app.bean.TestReflection _new(String a, String b);
@BConstructor
top.niunaijun.app.bean.TestReflection _new(String a);
@BMethod
String testContextInvoke(String a, int b);
@BStaticMethod
String testStaticInvoke(String a, int b);
@BStaticMethod
String testParamClassName(@BParamClassName("java.lang.String") Object a, int b);
@BField
String mContextValue();
@BStaticField
String sStaticValue();
}
构造函数:
TestReflection testReflection = BRTestReflection.get()._new("a");
TestReflection testReflection = BRTestReflection.get()._new("a", "b");
反射方法:
// 静态方法
BRTestReflection.get().testStaticInvoke("static", 0);
// 上下文方法
BRTestReflection.get(testReflection).testContextInvoke("context", 0);
反射变量:
// 静态变量
String staticValue = BRTestReflection.get().sStaticValue();
// 上下文变量
String contextValue = BRTestReflection.get(testReflection).mContextValue();
设置变量:
// 静态变量
BRTestReflection.get()._set_sStaticValue(staticValue + " changed");
// 上下文变量
BRTestReflection.get(testReflection)._set_mContextValue(contextValue + " changed");
BRTestReflection是程序自动生成的类,生成规则是BR + ClassName
- BRTestReflection.get() 用于调用静态方法
- BRTestReflection.get(caller) 用于调用非静态方法
注解 | 注解方式 | 解释 |
---|---|---|
@BClass | Type(Class) | 指定需要反射的类 |
@BClassName | Type(Class) | 指定需要反射的类 |
@BConstructor | Method | 注明是构造方法 |
@BStaticMethod | Method | 注明是静态方法 |
@BMethod | Method | 注明是非静态方法 |
@BStaticField | Method | 注明是静态变量 |
@BField | Method | 注明是非静态变量 |
@BParamClass | Parameter | 注明该参数的Class,用于反射时寻找方法 |
@BParamClassName | Parameter | 注明该参数的Class,用于反射时寻找方法 |
-keep class top.niunaijun.blackreflection.** {*; }
-keep @top.niunaijun.blackreflection.annotation.BClass class * {*;}
-keep @top.niunaijun.blackreflection.annotation.BClassName class * {*;}
-keep @top.niunaijun.blackreflection.annotation.BClassNameNotProcess class * {*;}
-keepclasseswithmembernames class * {
@top.niunaijun.blackreflection.annotation.BField.* <methods>;
@top.niunaijun.blackreflection.annotation.BFieldNotProcess.* <methods>;
@top.niunaijun.blackreflection.annotation.BFieldSetNotProcess.* <methods>;
@top.niunaijun.blackreflection.annotation.BFieldCheckNotProcess.* <methods>;
@top.niunaijun.blackreflection.annotation.BMethod.* <methods>;
@top.niunaijun.blackreflection.annotation.BStaticField.* <methods>;
@top.niunaijun.blackreflection.annotation.BStaticMethod.* <methods>;
@top.niunaijun.blackreflection.annotation.BMethodCheckNotProcess.* <methods>;
@top.niunaijun.blackreflection.annotation.BConstructor.* <methods>;
@top.niunaijun.blackreflection.annotation.BConstructorNotProcess.* <methods>;
}
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.