Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
nnjun committed Feb 19, 2022
2 parents 67ee128 + 7bdb7db commit 562d983
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 6 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Black反射库 · BlackReflection

**[English Version](README_EN.md)**

![](https://img.shields.io/badge/language-java-brightgreen.svg)

在日常开发中,会经常使用反射调用隐藏api或者其他方法,此时需要一个非常方便就能指定位置的反射库,此反射库采用接口式的写法,可快速的复制源码上的方法,打上注解,反射代码则会自动生成,不用额外的编写反射代码。
Expand Down Expand Up @@ -89,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);
Expand All @@ -104,7 +106,7 @@ BRTestReflection.get().testStaticInvoke("static", 0);
BRTestReflection.get(testReflection).testContextInvoke("context", 0);
```

反射变量
反射变量
```java
// 静态变量
String staticValue = BRTestReflection.get().sStaticValue();
Expand All @@ -113,7 +115,7 @@ String staticValue = BRTestReflection.get().sStaticValue();
String contextValue = BRTestReflection.get(testReflection).mContextValue();
```

设置变量
设置变量
```java
// 静态变量
BRTestReflection.get().setsStaticValue(staticValue + " changed");
Expand All @@ -129,8 +131,8 @@ BRTestReflection是程序自动生成的类,生成规则是BR + ClassName

注解 | 注解方式 | 解释
---|---|---
@BClass | Class | 指定需要反射的类
@BClassName | Class | 指定需要反射的类
@BClass | Type(Class) | 指定需要反射的类
@BClassName | Type(Class) | 指定需要反射的类
@BConstructor | Method | 注明是构造方法
@BStaticMethod | Method | 注明是静态方法
@BMethod | Method | 注明是非静态方法
Expand Down
177 changes: 177 additions & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
@@ -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.* <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>;
}
```
### 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.
> ```

0 comments on commit 562d983

Please sign in to comment.