Skip to content

Commit 2cdae67

Browse files
committed
调整EasyReflect代理逻辑
1 parent c339617 commit 2cdae67

File tree

5 files changed

+45
-41
lines changed

5 files changed

+45
-41
lines changed

docs/MVP.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# MVP
22

3+
### 写在前面
4+
5+
MVP本身只是种编程分层架构思想,

utils/src/main/java/com/haoge/easyandroid/easy/EasyFormatter.kt

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.haoge.easyandroid.easy
22

33
import android.util.Log
4-
import com.haoge.easyandroid.tools.CommonUtil
54
import org.json.JSONArray
65
import org.json.JSONObject
76
import java.lang.reflect.Modifier
@@ -240,10 +239,7 @@ class EasyFormatter private constructor(private val builder: Builder) {
240239
field.isAccessible = true
241240
}
242241

243-
val value = field.get(any)
244-
if (CommonUtil.isEmpty(value)) {
245-
continue
246-
}
242+
val value = field.get(any) ?: continue
247243

248244
container[field.name] = value
249245
}

utils/src/main/java/com/haoge/easyandroid/easy/EasyReflect.kt

+16-17
Original file line numberDiff line numberDiff line change
@@ -167,26 +167,25 @@ class EasyReflect private constructor(val clazz: Class<*>, var instance:Any?){
167167
@Suppress("UNCHECKED_CAST")
168168
return Proxy.newProxyInstance(proxy.classLoader, arrayOf(proxy), {_, method, args ->
169169
try {
170-
return@newProxyInstance this@EasyReflect.call(method.name, *args)
170+
// 优先匹配存在的方法
171+
return@newProxyInstance this@EasyReflect.callWithReturn(method.name, *args).get()
171172
} catch (e:Exception) {
172-
val methodName = method.name
173-
if (methodName.startsWith("get")) {
174-
val name = methodName.substring(3,4).toLowerCase() + methodName.substring(4)
175-
// 当方法名为getter方法,读取成员变量字段或者读取map中对应key值
176-
try {
177-
val result = if (instance is Map<*, *>) {
178-
(instance as Map<String, *>)[name]
179-
} else {
180-
getFieldValue(name)
181-
}
182-
if (method.returnType.isInstance(result)) {
183-
return@newProxyInstance result
184-
}
185-
} catch (e:Exception) {
186-
// ignore, GO ON!
173+
try {
174+
val methodName = method.name
175+
if (methodName == "get" && args.size == 1 && args[0] is String) {
176+
return@newProxyInstance getFieldValue(args[0] as String)
177+
} else if (methodName == "set" && args.size == 2 && args[0] is String) {
178+
setField(args[0] as String, args[1])
179+
} else if (methodName.startsWith("get") && method.returnType != Void::class.java) {
180+
val name = methodName.substring(3,4).toLowerCase() + methodName.substring(4)
181+
return@newProxyInstance getFieldValue(name)
182+
} else if (methodName.startsWith("set") && args.size == 1) {
183+
val name = methodName.substring(3,4).toLowerCase() + methodName.substring(4)
184+
setField(name, args[0])
187185
}
186+
} catch (e:Exception) {
187+
// ignore
188188
}
189-
190189
return@newProxyInstance when (method.returnType.name) {
191190
"int", "byte", "char", "long", "double", "float", "short" -> 0
192191
"boolean" -> false

utils/src/main/java/com/haoge/easyandroid/tools/CommonUtil.kt

-17
This file was deleted.

utils/src/test/java/com/haoge/easyandroid/easy/EasyReflectTest.java

+25-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
*/
1717
@RunWith(JUnit4.class)
1818
public class EasyReflectTest {
19-
20-
EasyReflect reflect;
19+
private EasyReflect reflect;
2120

2221
@Before
2322
public void setUp() {
@@ -107,6 +106,30 @@ public void fields() {
107106
assertEquals(reflect.getFieldValue("name"), "new_name");
108107
}
109108

109+
@Test
110+
public void proxy() {
111+
ReflectProxy proxy = reflect.proxy(ReflectProxy.class);
112+
// 通过代理的同名方法进行调用
113+
assertEquals("Hello", proxy.getMessage("Hello"));
114+
proxy.varargsParams("Hello", "World");
115+
116+
// 通过getXXX/setXXX对指定字段进行赋值与读取
117+
proxy.setName("EasyReflect");
118+
assertEquals("EasyReflect", proxy.getName());
119+
120+
// 通过set/get方法对指定字段进行读取与赋值
121+
proxy.set("TAG", "NewValue");
122+
assertEquals("NewValue", proxy.get("TAG"));
123+
}
124+
}
125+
126+
interface ReflectProxy {
127+
String getMessage(String message);// 代理到TestReflectClass.getMessage方法
128+
void varargsParams(String ... args);// 代理到TestReflectClass.varargsParams方法
129+
String getName();// 读取TestReflectClass.name字段的实例
130+
void setName(String name);// 为TestReflectClass.name进行赋值
131+
Object get(String name);// 获取TestReflectClass指定name的字段
132+
void set(String name, Object value);// 获取TestReflectClass指定name的字段
110133
}
111134

112135
class TestReflectClass {

0 commit comments

Comments
 (0)