BlackReflection provides a series of API to use Java Reflection easily.

Overview

Black反射库 · BlackReflection

English Version

在日常开发中,会经常使用反射调用隐藏api或者其他方法,此时需要一个非常方便就能指定位置的反射库,此反射库采用接口式的写法,可快速的复制源码上的方法,打上注解,反射代码则会自动生成,不用额外的编写反射代码。

使用方式

准备

Step 1. 根目录Gradle文件加入

allprojects {
    repositories {
        ...
        // 加入仓库
        maven { url 'https://jitpack.io' }
    }
}

Step 2. 需要使用的模块内引入

implementation 'com.github.CodingGay.BlackReflection:core:1.0.9'
annotationProcessor 'com.github.CodingGay.BlackReflection:compiler:1.0.9'

Demo

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();
}

2. build一次,让我生成相关的代码。

3. 可以尽情的反射代码

构造函数:

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.* ;
    @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.
You might also like...

Java Constraint Solver to solve vehicle routing, employee rostering, task assignment, conference scheduling and other planning problems.

OptaPlanner www.optaplanner.org Looking for Quickstarts? OptaPlanner’s quickstarts have moved to optaplanner-quickstarts repository. Quick development

Jan 2, 2023

Alibaba Java Diagnostic Tool Arthas/Alibaba Java诊断利器Arthas

Alibaba Java Diagnostic Tool Arthas/Alibaba Java诊断利器Arthas

Arthas Arthas is a Java Diagnostic tool open sourced by Alibaba. Arthas allows developers to troubleshoot production issues for Java applications with

Jan 4, 2023

Java rate limiting library based on token/leaky-bucket algorithm.

Java rate limiting library based on token/leaky-bucket algorithm.

Java rate-limiting library based on token-bucket algorithm. Advantages of Bucket4j Implemented on top of ideas of well known algorithm, which are by d

Jan 8, 2023

Object-Oriented Java primitives, as an alternative to Google Guava and Apache Commons

Project architect: @victornoel ATTENTION: We're still in a very early alpha version, the API may and will change frequently. Please, use it at your ow

Dec 27, 2022

Dex : The Data Explorer -- A data visualization tool written in Java/Groovy/JavaFX capable of powerful ETL and publishing web visualizations.

Dex : The Data Explorer -- A data visualization tool written in Java/Groovy/JavaFX capable of powerful ETL and publishing web visualizations.

Dex Dex : The data explorer is a powerful tool for data science. It is written in Groovy and Java on top of JavaFX and offers the ability to: Read in

Jan 8, 2023

Google core libraries for Java

Guava: Google Core Libraries for Java Guava is a set of core Java libraries from Google that includes new collection types (such as multimap and multi

Jan 1, 2023

Java regular expressions made easy.

JavaVerbalExpressions VerbalExpressions is a Java library that helps to construct difficult regular expressions. Getting Started Maven Dependency: de

Dec 30, 2022

MinIO Client SDK for Java

MinIO Java SDK for Amazon S3 Compatible Cloud Storage MinIO Java SDK is Simple Storage Service (aka S3) client to perform bucket and object operations

Jan 3, 2023

java port of Underscore.js

underscore-java Requirements Java 1.8 and later or Java 11. Installation Include the following in your pom.xml for Maven: dependencies dependency

Dec 6, 2022
Releases(1.1.4)
Owner
null
Discord4J is a fast, powerful, unopinionated, reactive library to enable quick and easy development of Discord bots for Java, Kotlin, and other JVM languages using the official Discord Bot API.

Discord4J is a fast, powerful, unopinionated, reactive library to enable quick and easy development of Discord bots for Java, Kotlin, and other JVM languages using the official Discord Bot API.

null 1.5k Jan 4, 2023
A Java API for checking if text contains profanity via the alt-profanity-checker Python library.

ProfanityCheckerAPI A Java API for checking if text contains profanity via the alt-profanity-checker Python library. It uses jep to run and interpret

William 2 Feb 19, 2022
🌏🎮 Integrate data provided from Minecraft server with Web API.

MCWebIntegration ?? ?? Integrate data provided from Minecraft server with Web API.

yude 2 Oct 14, 2021
TheRandomAPI is an API that is responsible for generating random responses to different queries.

TheRandomAPI is an API that is responsible for generating random responses to different queries.

Elian Remaggi 1 Apr 2, 2022
A simple figura api extention that allow you to change your avatar, or upload it with script

A simple figura api extention that allow you to change your avatar, or upload it with script

null 4 Apr 14, 2022
Java lib for monitoring directories or individual files via java.nio.file.WatchService

ch.vorburger.fswatch Java lib for monitoring directories or individual files based on the java.nio.file.WatchService. Usage Get it from Maven Central

Michael Vorburger ⛑️ 21 Jan 7, 2022
Tencent Kona JDK11 is a no-cost, production-ready distribution of the Open Java Development Kit (OpenJDK), Long-Term Support(LTS) with quarterly updates. Tencent Kona JDK11 is certified as compatible with the Java SE standard.

Tencent Kona JDK11 Tencent Kona JDK11 is a no-cost, production-ready distribution of the Open Java Development Kit (OpenJDK), Long-Term Support(LTS) w

Tencent 268 Dec 16, 2022
This repository contains Java programs to become zero to hero in Java.

This repository contains Java programs to become zero to hero in Java. Data Structure programs topic wise are also present to learn data structure problem solving in Java. Programs related to each and every concep are present from easy to intermidiate level

Sahil Batra 15 Oct 9, 2022
An open-source Java library for Constraint Programming

Documentation, Support and Issues Contributing Download and installation Choco-solver is an open-source Java library for Constraint Programming. Curre

null 607 Jan 3, 2023
Java Constraint Programming solver

https://maven-badges.herokuapp.com/maven-central/org.jacop/jacop/badge.svg [] (https://maven-badges.herokuapp.com/maven-central/org.jacop/jacop/) JaCo

null 202 Dec 30, 2022