Skip to content

Commit

Permalink
Fix bug 'Set final field failed'
Browse files Browse the repository at this point in the history
  • Loading branch information
zeshaoaaa committed Jul 6, 2019
1 parent 357ef8b commit 58e0a84
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 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.6'
implementation 'com.github.zeshaoaaa:OkReflect:0.0.7'
}
```
### Maven
Expand All @@ -175,6 +175,6 @@ dependencies {
<dependency>
<groupId>com.github.zeshaoaaa</groupId>
<artifactId>OkReflect</artifactId>
<version>0.0.6</version>
<version>0.0.7</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.6'
implementation 'com.github.zeshaoaaa:OkReflect:0.0.7'
}
```
### Maven
Expand All @@ -178,6 +178,6 @@ dependencies {
<dependency>
<groupId>com.github.zeshaoaaa</groupId>
<artifactId>OkReflect</artifactId>
<version>0.0.6</version>
<version>0.0.7</version>
</dependency>
```
20 changes: 17 additions & 3 deletions src/main/kotlin/OkReflect.kt
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ class OkReflect {
private fun verifyResult() {
if (result == null) {
throw java.lang.NullPointerException(
"you cannot call the method with the return value of last method when it's null.")
"you cannot call the method with the return value of last method when it's null."
)
}
}

Expand Down Expand Up @@ -307,6 +308,18 @@ class OkReflect {
}
}

/**
* Get the class.
*/
fun getClazz(): Class<*>? {
return try {
realGet(RETURN_FLAG_CLASS)
} catch (e: java.lang.Exception) {
printError(e)
null
}
}

/**
* Get the instance no matter result have value or not.
*/
Expand Down Expand Up @@ -358,7 +371,7 @@ class OkReflect {
}

/**
* If there is no constructor paramters, there will throw an exception
* If there is no constructor parameters, there will throw an exception
*/
private fun verifyConstructorArgs() {
if (constructorArgs == null) {
Expand Down Expand Up @@ -412,7 +425,8 @@ class OkReflect {
instance as T
}
}
else -> instance as T
RETURN_FLAG_INSTANCE -> instance as T
else -> clazz as T
}
} catch (e: Exception) {
printError(e)
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/UseCaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,11 @@ public void testSetFinalFieldByOkReflect() {
assert finalField.equals("changed");
}

@Test
public void testGetClass() {
Class clazz = OkReflect.on("TestClass")
.getClazz();
assert clazz.equals(TestClass.class);
}

}

0 comments on commit 58e0a84

Please sign in to comment.