Skip to content

Commit

Permalink
improve proxy support, for issue #1396
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Apr 21, 2023
1 parent b721c7a commit 64450df
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 12 deletions.
53 changes: 41 additions & 12 deletions core/src/main/java/com/alibaba/fastjson2/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import com.alibaba.fastjson2.writer.ObjectWriterAdapter;

import java.lang.annotation.Annotation;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.*;
import java.math.BigDecimal;
import java.math.BigInteger;
Expand Down Expand Up @@ -1521,14 +1524,22 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
Object value;
if (name == null) {
name = methodName;
if (name.startsWith("get")) {
name = name.substring(3);
if (name.isEmpty()) {
boolean with = false;
int prefix;
if ((name.startsWith("get") || (with = name.startsWith("with")))
&& name.length() > (prefix = with ? 4 : 3)
) {
char[] chars = new char[name.length() - prefix];
name.getChars(prefix, name.length(), chars, 0);
if (chars[0] >= 'A' && chars[0] <= 'Z') {
chars[0] = (char) (chars[0] + 32);
}
String fieldName = new String(chars);
if (fieldName.isEmpty()) {
throw new JSONException("This method '" + methodName + "' is an illegal getter");
}
name = Character.toLowerCase(name.charAt(0)) + name.substring(1);

value = get(name);
value = get(fieldName);
if (value == null) {
return null;
}
Expand Down Expand Up @@ -1559,6 +1570,21 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
} else if ("size".equals(name)) {
return this.size();
} else {
Class<?> declaringClass = method.getDeclaringClass();
if (declaringClass.isInterface()
&& method.getParameterCount() == 0
&& !Modifier.isAbstract(method.getModifiers())
) {
// interface default method
MethodHandles.Lookup lookup = JDKUtils.trustedLookup(declaringClass);
MethodHandle methodHandle = lookup.findSpecial(
declaringClass,
method.getName(),
MethodType.methodType(returnType),
declaringClass
);
return methodHandle.invoke(proxy);
}
throw new JSONException("This method '" + methodName + "' is not a getter");
}
} else {
Expand All @@ -1568,14 +1594,16 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
}
}

Function typeConvert = JSONFactory
.getDefaultObjectReaderProvider()
.getTypeConvert(
value.getClass(), method.getGenericReturnType()
);
if (value != null && !returnType.isInstance(value)) {
Function typeConvert = JSONFactory
.getDefaultObjectReaderProvider()
.getTypeConvert(
value.getClass(), method.getGenericReturnType()
);

if (typeConvert != null) {
return typeConvert.apply(value);
if (typeConvert != null) {
value = typeConvert.apply(value);
}
}

return value;
Expand Down Expand Up @@ -1789,6 +1817,7 @@ public Object eval(JSONPath path) {

/**
* if value instance of Map or Collection, return size, other return 0
*
* @param key
* @since 2.0.24
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.alibaba.fastjson2.issues_1000;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

public class Issue1396 {
@Test
public void test() {
String name = "bob";
User user = new User(name);
JSONObject userJsonObject = JSONObject.from(user, JSONWriter.Feature.IgnoreNonFieldGetter);
String userJsonString = JSONObject.toJSONString(user);

// ...
// transport via http/redis/file
// ...

// Normal interface method call
LivingObject normal = new User(name);
assertEquals(name, normal.getSignature().toString()); // -> "bob"
assertEquals(name, normal.getSignatureString()); // -> "bob"
assertEquals(name, normal.withSignatureString()); // -> "bob"

// Normal deserialize
LivingObject deserializeLiving = userJsonObject.to(LivingObject.class);
assertEquals(name, deserializeLiving.getSignature().toString()); // -> "bob"
assertEquals(name, deserializeLiving.getSignatureString()); // -> "bob"
assertEquals(name, deserializeLiving.withSignatureString()); // -> This method 'withSignatureString' is not a getter

// Unknown instance type
LivingObject living = JSONObject.parseObject(userJsonString, LivingObject.class);
assertNull(living.getSignature().toString()); // -> "null"
assertEquals(name, living.getSignatureString()); // -> "bob"
assertEquals(name, living.withSignatureString()); // -> This method 'withSignatureString' is not a getter
}

public interface LivingObject {
String getName();

String withName();

default Signature getSignature() {
return new Signature(getName());
}

default String getSignatureString() {
return new Signature(getName()).toString();
}

default String withSignatureString() {
return withName();
}
}

public static class Signature {
private String sign;

public Signature(String sign) {
this.sign = sign;
}

@Override
public String toString() {
return sign;
}
}

public static class User
implements LivingObject {
private String name;

public User(String name) {
this.name = name;
}

public String getName() {
return name;
}

public String withName() {
return name;
}
}
}

0 comments on commit 64450df

Please sign in to comment.