From c46178e593bf34895386c4bd119990946a2d9958 Mon Sep 17 00:00:00 2001 From: jay2015 <328008932@qq.com> Date: Tue, 9 Jul 2019 12:17:55 +0800 Subject: [PATCH] Added feature 'Calling methods with outer instance' --- README.md | 4 +- README_CN.md | 4 +- build.gradle | 2 +- src/main/kotlin/okreflect/OkReflect.kt | 56 ++++++++++++++++++++------ src/test/java/UseCaseTest.java | 35 ++++++++++++---- 5 files changed, 76 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 1e27b00..7dc19d1 100644 --- a/README.md +++ b/README.md @@ -159,7 +159,7 @@ allprojects { ``` ```groovy dependencies { - implementation 'com.github.zeshaoaaa:OkReflect:0.0.9' + implementation 'com.github.zeshaoaaa:OkReflect:0.1.0' } ``` ### Maven @@ -175,6 +175,6 @@ dependencies { com.github.zeshaoaaa OkReflect - 0.0.9 + 0.1.0 ``` diff --git a/README_CN.md b/README_CN.md index 480c97f..d05c1de 100644 --- a/README_CN.md +++ b/README_CN.md @@ -162,7 +162,7 @@ allprojects { ``` ```groovy dependencies { - implementation 'com.github.zeshaoaaa:OkReflect:0.0.9' + implementation 'com.github.zeshaoaaa:OkReflect:0.1.0' } ``` ### Maven @@ -178,6 +178,6 @@ dependencies { com.github.zeshaoaaa OkReflect - 0.0.9 + 0.1.0 ``` diff --git a/build.gradle b/build.gradle index cf431de..dee9e89 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,7 @@ plugins { apply plugin: 'maven' group 'com.github.zeshaoaaa' -version '0.0.9' +version '0.1.0' sourceCompatibility = 1.8 diff --git a/src/main/kotlin/okreflect/OkReflect.kt b/src/main/kotlin/okreflect/OkReflect.kt index 2d74838..f9be94c 100644 --- a/src/main/kotlin/okreflect/OkReflect.kt +++ b/src/main/kotlin/okreflect/OkReflect.kt @@ -71,6 +71,11 @@ class OkReflect { */ private var result: Any? = null + /** + * Whether call methods with outer instance or not. + */ + private var withOuterInstance = false + /** * @param className: The name of the class that you want to create. * @@ -89,6 +94,17 @@ class OkReflect { this.clazz = clazz } + /** + * @param instance: The instance that you want to use for calling methods. + * + * Constructor of OkReflect. + */ + constructor(instance: Any) { + withOuterInstance = true + this.instance = instance + this.clazz = instance.javaClass + } + /** * Set the parameters of the constructor of the class that you want to create. */ @@ -370,16 +386,20 @@ class OkReflect { * and set or get the field that you want. */ private fun realGet(returnFlag: Int): T? { - val needInstance = returnFlag == RETURN_FLAG_INSTANCE || returnFlag == RETURN_FLAG_RESULT - if (needInstance) { - verifyClassInfo() - verifyConstructorArgs() - } - if (clazz == null) { - this.clazz = Class.forName(className!!) - } - if (createCalled) { - initInstance() + if (!withOuterInstance) { + val needInstance = returnFlag == RETURN_FLAG_INSTANCE || returnFlag == RETURN_FLAG_RESULT + if (needInstance) { + verifyClassInfo() + verifyConstructorArgs() + } + if (clazz == null) { + this.clazz = Class.forName(className!!) + } + if (createCalled) { + initInstance() + invokeMethods() + } + } else { invokeMethods() } setFields() @@ -388,10 +408,10 @@ class OkReflect { } /** - * If there is no constructor parameters, there will throw an exception + * If there is no constructor parameters for the , there will throw an exception */ private fun verifyConstructorArgs() { - if (constructorArgs == null) { + if (!withOuterInstance && constructorArgs == null) { throw NullPointerException( "you have to call create() method, or else you will get nothing." ) @@ -402,7 +422,7 @@ class OkReflect { * If there is no class info, there will throw an exception */ private fun verifyClassInfo() { - if (clazz == null && className == null && className!!.isEmpty()) { + if (!withOuterInstance && clazz == null && className == null && className!!.isEmpty()) { throw java.lang.NullPointerException( "you must specify the className or class." ) @@ -489,6 +509,16 @@ class OkReflect { return OkReflect(clazz) } + /** + * @param instance: The instance that you want to use. + * + * Set the instance for methods that you want to call. + */ + @JvmStatic + fun on(instance: Any): OkReflect { + return OkReflect(instance) + } + /** * Change the accessibility of the methods and constructors. */ diff --git a/src/test/java/UseCaseTest.java b/src/test/java/UseCaseTest.java index fc953ba..8d0469b 100644 --- a/src/test/java/UseCaseTest.java +++ b/src/test/java/UseCaseTest.java @@ -1,6 +1,7 @@ import okreflect.OkReflect; import org.jetbrains.annotations.NotNull; import org.junit.Assert; +import org.junit.Ignore; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -177,13 +178,6 @@ public void testSetFinalFieldOfInstance() { assert finalField.equals("changed"); } - @Test - public void testSetFinalFieldOfClass() { - String finalField = OkReflect.on("TestClass") - .set("finalString", "changed") - .get("finalString"); - assert finalField.equals("changed"); - } @Test public void testGetClass() { @@ -209,5 +203,32 @@ public void testSetStaticFinalFieldOfClass() { assert finalField.equals("changed"); } + @Test + public void testCallMethodFromOuterInstance() { + TestClass testClass = new TestClass(); + String name = OkReflect.on(testClass) + .call("getName") + .get(); + assert name.equals("default"); + } + + @Test + public void testSetFieldFromOuterInstance() { + TestClass testClass = new TestClass(); + String name = OkReflect.on(testClass) + .set("name", "Alex") + .get("name"); + assert name.equals("Alex"); + } + + @Ignore + @Test + public void testSetFinalFieldOfClass() { + String finalField = OkReflect.on("TestClass") + .set("finalString", "changed") + .get("finalString"); + assert finalField.equals("changed"); + } + }