From 889b54ed252b2d9aeef34df1e4e46c572f58aec4 Mon Sep 17 00:00:00 2001 From: Nephren <1124855130@qq.com> Date: Sat, 19 Feb 2022 00:27:23 +0800 Subject: [PATCH 1/2] Update README.md and README_EN.md --- README.md | 6 +- README_EN.md | 177 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 181 insertions(+), 2 deletions(-) create mode 100644 README_EN.md diff --git a/README.md b/README.md index 5eb608a..53df0fd 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Black反射库 · BlackReflection +**[English Version](README_EN.md)** + ![](https://img.shields.io/badge/language-java-brightgreen.svg) 在日常开发中,会经常使用反射调用隐藏api或者其他方法,此时需要一个非常方便就能指定位置的反射库,此反射库采用接口式的写法,可快速的复制源码上的方法,打上注解,反射代码则会自动生成,不用额外的编写反射代码。 @@ -129,8 +131,8 @@ BRTestReflection是程序自动生成的类,生成规则是BR + ClassName 注解 | 注解方式 | 解释 ---|---|--- -@BClass | Class | 指定需要反射的类 -@BClassName | Class | 指定需要反射的类 +@BClass | Type(Class) | 指定需要反射的类 +@BClassName | Type(Class) | 指定需要反射的类 @BConstructor | Method | 注明是构造方法 @BStaticMethod | Method | 注明是静态方法 @BMethod | Method | 注明是非静态方法 diff --git a/README_EN.md b/README_EN.md new file mode 100644 index 0000000..a933841 --- /dev/null +++ b/README_EN.md @@ -0,0 +1,177 @@ +# Java Reflection Library · BlackReflection + +![](https://img.shields.io/badge/language-java-brightgreen.svg) + +BlackReflection provides a series of API to use Java Reflection easily. Developer can use annotation to assign class, field and method. Then it will generate the reflection code automatically, developer don't need to write extra code to achieve Java Reflection. + +## Usage + +### Preparation + +#### Step 1. Configure your build.gradle (in top level directory) +```gradle +allprojects { + repositories { + ... + // Add + maven { url 'https://jitpack.io' } + } +} +``` +#### Step 2. Add BlackReflection dependencies +```gradle +implementation 'com.github.CodingGay.BlackReflection:core:1.0.5' +annotationProcessor 'com.github.CodingGay.BlackReflection:compiler:1.0.5' +``` + +### Demo +#### 1. If you need to reflect the methods of top.niunaijun.app.bean.TestReflection, please refer to:[MainActivity.java](https://github.com/CodingGay/BlackReflection/blob/main/app/src/main/java/top/niunaijun/app/MainActivity.java) +```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; + } +} + +``` +You can write an interface like this: +```java +@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(); +} + +``` +#### 2. Build your project, it will generate the code automatically. + +#### 3. Write the code heartily! +Constructor +```java +TestReflection testReflection = BRTestReflection.get()._new("a"); +TestReflection testReflection = BRTestReflection.get()._new("a", "b"); +``` + +Reflect Methods +```java +// Static Method +BRTestReflection.get().testStaticInvoke("static", 0); + +// Non-static Method +BRTestReflection.get(testReflection).testContextInvoke("context", 0); +``` + +Reflect Fields +```java +// Static Field +String staticValue = BRTestReflection.get().sStaticValue(); + +// Non-static Field +String contextValue = BRTestReflection.get(testReflection).mContextValue(); +``` + +Set the value of Fields +```java +// Static Field +BRTestReflection.get().setsStaticValue(staticValue + " changed"); + +// Non-static Field +BRTestReflection.get(testReflection).setmContextValue(contextValue + " changed"); +``` +BRTestReflection is a class which generated by the program automatically. +Generation rule: BR + ClassName +- BRTestReflection.get() ------> It is used to invoke static method +- BRTestReflection.get(caller) ------> It is used to invoke non-static method +#### Annotation API + +Annotation | Target | Description +---|---|--- +@BClass | Type(Class) | Assign the class which you want to manipulate +@BClassName | Type(Class) | Assign the class which you want to manipulate +@BConstructor | Method | Assign the constructor +@BStaticMethod | Method | Assign the static method +@BMethod | Method | Assign the non-static method +@BStaticField | Method | Assign the static field +@BField | Method | Assign the non-static field +@BParamClass | Parameter | Assign the class of parameter +@BParamClassName | Parameter | Assign the class of parameter + +### Obfuscation rules +``` +-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.* ; + @top.niunaijun.blackreflection.annotation.BFieldNotProcess.* ; + @top.niunaijun.blackreflection.annotation.BFieldSetNotProcess.* ; + @top.niunaijun.blackreflection.annotation.BFieldCheckNotProcess.* ; + @top.niunaijun.blackreflection.annotation.BMethod.* ; + @top.niunaijun.blackreflection.annotation.BStaticField.* ; + @top.niunaijun.blackreflection.annotation.BStaticMethod.* ; + @top.niunaijun.blackreflection.annotation.BMethodCheckNotProcess.* ; + @top.niunaijun.blackreflection.annotation.BConstructor.* ; + @top.niunaijun.blackreflection.annotation.BConstructorNotProcess.* ; +} +``` +### License + +> ``` +> 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. +> ``` From 7bdb7dba1d8f52cca2700e44b91c99a865f041e4 Mon Sep 17 00:00:00 2001 From: Nephren <1124855130@qq.com> Date: Sat, 19 Feb 2022 00:31:40 +0800 Subject: [PATCH 2/2] Update README.md and README_EN.md --- README.md | 8 ++++---- README_EN.md | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 53df0fd..915f671 100644 --- a/README.md +++ b/README.md @@ -91,13 +91,13 @@ public interface TestReflection { #### 2. build一次,让我生成相关的代码。 #### 3. 可以尽情的反射代码 -构造函数 +构造函数: ```java TestReflection testReflection = BRTestReflection.get()._new("a"); TestReflection testReflection = BRTestReflection.get()._new("a", "b"); ``` -反射方法 +反射方法: ```java // 静态方法 BRTestReflection.get().testStaticInvoke("static", 0); @@ -106,7 +106,7 @@ BRTestReflection.get().testStaticInvoke("static", 0); BRTestReflection.get(testReflection).testContextInvoke("context", 0); ``` -反射变量 +反射变量: ```java // 静态变量 String staticValue = BRTestReflection.get().sStaticValue(); @@ -115,7 +115,7 @@ String staticValue = BRTestReflection.get().sStaticValue(); String contextValue = BRTestReflection.get(testReflection).mContextValue(); ``` -设置变量 +设置变量: ```java // 静态变量 BRTestReflection.get().setsStaticValue(staticValue + " changed"); diff --git a/README_EN.md b/README_EN.md index a933841..d72d00f 100644 --- a/README_EN.md +++ b/README_EN.md @@ -89,13 +89,13 @@ public interface TestReflection { #### 2. Build your project, it will generate the code automatically. #### 3. Write the code heartily! -Constructor +Constructor: ```java TestReflection testReflection = BRTestReflection.get()._new("a"); TestReflection testReflection = BRTestReflection.get()._new("a", "b"); ``` -Reflect Methods +Reflect Methods: ```java // Static Method BRTestReflection.get().testStaticInvoke("static", 0); @@ -104,7 +104,7 @@ BRTestReflection.get().testStaticInvoke("static", 0); BRTestReflection.get(testReflection).testContextInvoke("context", 0); ``` -Reflect Fields +Reflect Fields: ```java // Static Field String staticValue = BRTestReflection.get().sStaticValue(); @@ -113,7 +113,7 @@ String staticValue = BRTestReflection.get().sStaticValue(); String contextValue = BRTestReflection.get(testReflection).mContextValue(); ``` -Set the value of Fields +Set the value of Fields: ```java // Static Field BRTestReflection.get().setsStaticValue(staticValue + " changed");