OkReflect

Project Url: zeshaoaaa/OkReflect
Introduction: OkReflect is a library that tries to help you use Java Reflection APIs with ease.
More: Author   ReportBugs   
Tags:

中文文档

OkReflect is a library that trying to help you use Java Reflection APIs with ease.

Introduction to OkReflect

Usage

1. Create the instance with the class name

// Java
String str = OkReflect.on("java.lang.String")
                .create("Hello OkReflect")
                .get();
// Kotlin
val str: String? = OkReflect.on("java.lang.String")
            .create("test")
            .get()

2. Create the instance by class

// Java
String str = OkReflect.on(String.class)
                .create("test")
                .get();
// Kotlin
val str: String? = OkReflect.on(String::class.java)
            .create("test")
            .get()

3. Set or get the field value from the superclass

// Java
TestClass testClass = new TestClass("Alex");
String superName = OkReflect.on(SuperTestClass.class)
                     .with(testClass)
                     .set("superName", "Tom")
                     .get("superName");
// Kotlin
val testClass = TestClass("Alex")
val superName: String? = OkReflect.on(SuperTestClass::class.java)
            .with(testClass)
            .set("Tom")
            .get("superName")

4. Invoke the method

// Java
String str = OkReflect
                .on(String.class)
                .create("Hello world")
                .call("substring", 6)
                .get();
// Kotlin
val str: String? = OkReflect
            .on(String::class.java)
            .create("Hello world")
            .call("substring", 6)     
            .get()

5. Invoke the methods and set the fields with the instance that created outside of OkReflect

// Java
TestClass testClass = new TestClass();
String name = OkReflect.on(testClass)
                .set("name", "Alex")
                .get("name");
// Kotlin
val testClass = TestClass()
val name: String? = OkReflect.on(testClass)
            .call("getName")
            .get("name")

6. Invoke the method with the return value of the last method

// Java
String str = OkReflect
                .on(String.class)
                .create("Hello world")
                .call("substring", 6)
                .callWithResult("substring", 4)
                .get();
// Kotlin
val str: String? = OkReflect
            .on(String::class.java)
            .create("Hello world")
            .call("substring", 6)
            .callWithResult("substring", 4)
            .get()

7. Get the instance after invoke the method

// Java
String str = OkReflect
                .on(String.class)
                .create("Hello world")
                .call("substring", 6)
                .getInstance();
// Kotlin
val str: String? = OkReflect
            .on(String::class.java)
            .create("Hello world")
            .call("substring", 6)
            .getInstance()

8. Get and set the field value of the instance

// Java
int i = OkReflect.on(TestClass.class)
                .set("i", 6)
                .get("i");
// Kotlin
val i: Int? = OkReflect.on(TestClass.class)
            .set("i", 6)
            .get("i")

9. Get and set the value of the static field of the class

// Java
String finalField = OkReflect.on("TestClass")
                .set("staticString", "changed")
                .get("staticString");
// Kotlin
val finalField: String? = OkReflect.on("TestClass")
            .set("staticString", "changed")
            .get("staticString")

10. Handle the exception with callback

// Java
String str = OkReflect
                .on("java.lang.String")
                .error(new OkReflect.OkReflectErrorCallback() {
                    @Override
                    public void onError(@NotNull Exception e) {
                        // handle the exception
                    }
                })
                .get();
// Kotlin
val str: String? = OkReflect
            .on("java.lang.String")
            .error {
                // handle the exception
            }
            .get()

11. Call the method with the type of parameters

// Java
Class classes[] = {String.class, Byte.class};
Object args[] = {"Tom", null};
String name = OkReflect.on(TestClass.class)
                .create()
                .callWithClass("setData2", classes, args)
                .get("name");
// Kotlin
val classes = arrayOf<Class<*>>(String::class.java, Byte::class.javaObjectType)
val args = arrayOf("Tom", null)
val name = OkReflect.on(TestClass::class.java)
            .create()
            .callWithClass("setData2", classes, *args)
            .get<String?>("name")

12. Simply set the field or call the method

// Java
String name = OkReflect.on(TestClass.class)
                .create()
                .simpleCall("getName");

name = OkReflect.on(TestClass.class)
                .create()
                .simpleSet("name", "Tom");
// Kotlin
var name = OkReflect.on(TestClass::class.java)
            .create()
            .simpleCall<String>("getName")

name = OkReflect.on(TestClass::class.java)
            .create()
            .simpleSet<String>("name", "Tom")

13. Use dynamic proxy

// First step of using dynamic proxy: declare the interface
public interface StringProxy {
  String substring(int beginIndex);
}
// Java
String substring = OkReflect.on("java.lang.String")
                .create("Hello World")
                .use(StringProxy.class)
                .substring(6);
// Kotlin
val substring = OkReflect.on("java.lang.String")
            .create("Hello World")
            .use(StringProxy::class.java)
            .substring(6)

Configuration

Gradle

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
dependencies {
    implementation 'com.github.zeshaoaaa:OkReflect:master-SNAPSHOT'
}

Maven

    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>
    <dependency>
        <groupId>com.github.zeshaoaaa</groupId>
        <artifactId>OkReflect</artifactId>
        <version>master-SNAPSHOT</version>
    </dependency>
Support Me
Apps
About Me
Google+: Trinea trinea
GitHub: Trinea