Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
SerializerFeature.SkipTransientField support java.beans.Transient, is…
Browse files Browse the repository at this point in the history
…sue #952
  • Loading branch information
wenshao committed Dec 19, 2016
1 parent 6326029 commit 206b3c3
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1473,13 +1473,11 @@ private void _list(Class<?> clazz, MethodVisitor mw, FieldInfo fieldInfo, Contex
}

private void _filters(MethodVisitor mw, FieldInfo property, Context context, Label _end) {
if (property.field != null) {
if (Modifier.isTransient(property.field.getModifiers())) {
mw.visitVarInsn(ALOAD, context.var("out"));
mw.visitLdcInsn(SerializerFeature.SkipTransientField.mask);
mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "isEnabled", "(I)Z");
mw.visitJumpInsn(IFNE, _end);
}
if (property.fieldTransient) {
mw.visitVarInsn(ALOAD, context.var("out"));
mw.visitLdcInsn(SerializerFeature.SkipTransientField.mask);
mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "isEnabled", "(I)Z");
mw.visitJumpInsn(IFNE, _end);
}

_notWriteDefault(mw, property, context, _end);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/alibaba/fastjson/util/FieldInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ public FieldInfo(String name, //
if (field != null) {
int modifiers = field.getModifiers();
fieldAccess = ((modifiers & Modifier.PUBLIC) != 0 || method == null);
fieldTransient = Modifier.isTransient(modifiers);
fieldTransient = Modifier.isTransient(modifiers)
|| TypeUtils.isTransient(method);
} else {
fieldAccess = false;
fieldTransient = false;
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/alibaba/fastjson/util/TypeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.alibaba.fastjson.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -85,6 +86,10 @@ public class TypeUtils {
private static boolean optionalClassInited = false;
private static Class<?> optionalClass;

private static boolean transientClassInited = false;
private static Class<? extends Annotation> transientClass;


public static String castToString(Object value) {
if (value == null) {
return null;
Expand Down Expand Up @@ -1753,4 +1758,27 @@ public static boolean isProxy(Class<?> clazz) {

return false;
}

public static boolean isTransient(Method method) {
if (method == null) {
return false;
}

if (!transientClassInited) {
try {
transientClass = (Class<? extends Annotation>) Class.forName("java.beans.Transient");
} catch (Exception e) {
// skip
} finally {
transientClassInited = true;
}
}

if (transientClass != null) {
Annotation annotation = method.getAnnotation(transientClass);
return annotation != null;
}

return false;
}
}
30 changes: 30 additions & 0 deletions src/test/java/com/alibaba/json/bvt/bug/Issue952.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.alibaba.json.bvt.bug;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import junit.framework.TestCase;

import java.beans.Transient;

/**
* Created by wenshao on 19/12/2016.
*/
public class Issue952 extends TestCase {
public void test_for_issue() throws Exception {
Model model = new Model();
model.id = 1001;

String text = JSON.toJSONString(model, SerializerFeature.SkipTransientField);

assertEquals("{}", text);
}

public static class Model {
private int id;

@Transient
public int getId() {
return id;
}
}
}

0 comments on commit 206b3c3

Please sign in to comment.