Skip to content

Commit

Permalink
improve mixin non-getter annotation match, for issue #1474
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 12, 2023
1 parent 0a6b904 commit 0f0b5b1
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 2 deletions.
24 changes: 24 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,10 @@ private static boolean isJSONField(Annotation[] annotations) {
}

public static void getters(Class objectClass, Consumer<Method> methodConsumer) {
getters(objectClass, null, methodConsumer);
}

public static void getters(Class objectClass, Class mixinSource, Consumer<Method> methodConsumer) {
if (objectClass == null) {
return;
}
Expand Down Expand Up @@ -919,6 +923,11 @@ boolean record = isRecord(objectClass);
break;
case "equals":
case "hashCode":
case "wait":
case "notify":
case "notifyAll":
case "toString":
case "getClass":
methodSkip = true;
break;
default:
Expand Down Expand Up @@ -1021,6 +1030,21 @@ && findAnnotation(method, JSONField.class) == null) {
}
}

if (!nameMatch && mixinSource != null) {
Method mixinMethod = getMethod(mixinSource, method);
if (mixinMethod != null) {
Annotation[] annotations = getAnnotations(mixinMethod);
for (Annotation annotation : annotations) {
if (annotation.annotationType() == JSONField.class) {
JSONField jsonField = (JSONField) annotation;
if (jsonField != null && jsonField.serialize()) {
nameMatch = true;
}
}
}
}
}

if (!nameMatch) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,9 @@ boolean record = BeanUtils.isRecord(objectClass);
});
}

BeanUtils.getters(objectClass, method -> {
Class mixIn = provider.getMixIn(objectClass);

BeanUtils.getters(objectClass, mixIn, method -> {
fieldInfo.init();
fieldInfo.features = writerFieldFeatures;
fieldInfo.format = beanInfo.format;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ public ObjectWriter createObjectWriter(

BeanInfo beanInfo = new BeanInfo();
provider.getBeanInfo(beanInfo, objectClass);
Class mixIn = provider.getMixIn(objectClass);

if (beanInfo.serializer != null && ObjectWriter.class.isAssignableFrom(beanInfo.serializer)) {
try {
Expand Down Expand Up @@ -251,7 +252,7 @@ boolean record = BeanUtils.isRecord(objectClass);
});
}

BeanUtils.getters(objectClass, method -> {
BeanUtils.getters(objectClass, mixIn, method -> {
fieldInfo.init();
fieldInfo.features |= writerFieldFeatures;
fieldInfo.format = beanInfo.format;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.alibaba.fastjson2.issues_1000;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.annotation.JSONField;
import com.alibaba.fastjson2.annotation.JSONType;
import org.junit.jupiter.api.Test;

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

public class Issue1474 {
@Test
public void test() {
JSON.mixIn(ProducerRecord.class, ProducerRecordMixin.class);

ProducerRecord record = new ProducerRecord("abc");
assertEquals("{\"topic\":\"abc\"}", JSON.toJSONString(record));
}

public interface ProducerRecordMixin {
@JSONField
String topic();
}

public static class ProducerRecord {
private final String topic;

public ProducerRecord(String topic) {
this.topic = topic;
}

public String topic() {
return topic;
}
}

@Test
public void test1() {
JSON.mixIn(ProducerRecord1.class, ProducerRecordMixin1.class);

ProducerRecord1 record = new ProducerRecord1("abc");
assertEquals("{\"topic\":\"abc\"}", JSON.toJSONString(record));
}

@JSONType(serializeFeatures = JSONWriter.Feature.FieldBased)
public interface ProducerRecordMixin1 {
}

public static class ProducerRecord1 {
private final String topic;

public ProducerRecord1(String topic) {
this.topic = topic;
}

public String topic() {
return topic;
}
}

@Test
public void test2() {
JSON.mixIn(ProducerRecord2.class, ProducerRecordMixin2.class);

ProducerRecord2 record = new ProducerRecord2("abc");
assertEquals("{\"topic\":\"abc\"}", JSON.toJSONString(record));
}

public interface ProducerRecordMixin2 {
@JSONField
String topic();
}

static class ProducerRecord2 {
private final String topic;

public ProducerRecord2(String topic) {
this.topic = topic;
}

public String topic() {
return topic;
}
}
}

0 comments on commit 0f0b5b1

Please sign in to comment.