Skip to content

Commit

Permalink
Added feature 'Calling methods with outer instance'
Browse files Browse the repository at this point in the history
  • Loading branch information
zeshaoaaa committed Jul 9, 2019
1 parent be759ba commit c46178e
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 25 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ allprojects {
```
```groovy
dependencies {
implementation 'com.github.zeshaoaaa:OkReflect:0.0.9'
implementation 'com.github.zeshaoaaa:OkReflect:0.1.0'
}
```
### Maven
Expand All @@ -175,6 +175,6 @@ dependencies {
<dependency>
<groupId>com.github.zeshaoaaa</groupId>
<artifactId>OkReflect</artifactId>
<version>0.0.9</version>
<version>0.1.0</version>
</dependency>
```
4 changes: 2 additions & 2 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ allprojects {
```
```groovy
dependencies {
implementation 'com.github.zeshaoaaa:OkReflect:0.0.9'
implementation 'com.github.zeshaoaaa:OkReflect:0.1.0'
}
```
### Maven
Expand All @@ -178,6 +178,6 @@ dependencies {
<dependency>
<groupId>com.github.zeshaoaaa</groupId>
<artifactId>OkReflect</artifactId>
<version>0.0.9</version>
<version>0.1.0</version>
</dependency>
```
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ plugins {
apply plugin: 'maven'

group 'com.github.zeshaoaaa'
version '0.0.9'
version '0.1.0'

sourceCompatibility = 1.8

Expand Down
56 changes: 43 additions & 13 deletions src/main/kotlin/okreflect/OkReflect.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
*/
Expand Down Expand Up @@ -370,16 +386,20 @@ class OkReflect {
* and set or get the field that you want.
*/
private fun <T> 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()
Expand All @@ -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."
)
Expand All @@ -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."
)
Expand Down Expand Up @@ -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.
*/
Expand Down
35 changes: 28 additions & 7 deletions src/test/java/UseCaseTest.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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() {
Expand All @@ -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");
}


}

0 comments on commit c46178e

Please sign in to comment.