Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

全接口对象@Transient失效 #1903

Closed
juqkai opened this issue Jun 13, 2018 · 2 comments
Closed

全接口对象@Transient失效 #1903

juqkai opened this issue Jun 13, 2018 · 2 comments
Labels
Milestone

Comments

@juqkai
Copy link

juqkai commented Jun 13, 2018

一个全接口对象, 通过代理生成的, 在接口中只声明了get, set方法, 没有字段申明.

在get上添加@transient, 会将这个字段序列出来.

原因是FieldInfo中强制判断了必须要有field才会判断是否是fieldTransient

fastjson/src/main/java/com/alibaba/fastjson/util/FieldInfo.java

@wenshao
Copy link
Member

wenshao commented Jun 18, 2018

能提供testcase么?

@juqkai
Copy link
Author

juqkai commented Jun 19, 2018

public class Issues1903Test {
    // Issues1903 issues =
    public static void main(String[] args) {
        MapHandler mh = new MapHandler();
        mh.add("name", "test");
        mh.add("age", 20);

        Issues1903 issues = (Issues1903) Proxy.newProxyInstance(mh.getClass().getClassLoader(), new Class[]{Issues1903.class}, mh);
        System.out.println(issues.getName());
        System.out.println(issues.getAge());

        System.out.println(JSON.toJSON(issues).toString()); //正确结果: {"age":20}
        System.out.println(JSON.toJSONString(issues));  //正确结果: {"age":20}
    }
}

interface Issues1903{
    @Transient
    public String getName();
    public void setName(String name);

    public Integer getAge();
    public void setAge(Integer age);
}


class MapHandler implements InvocationHandler{
    Map<String, Object> map = new HashMap<>();

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String name = method.getName().substring(3);
        String first = String.valueOf(name.charAt(0));
        name = name.replaceFirst(first, first.toLowerCase());
        return map.get(name);
    }

    public void add(String key, Object val){
        map.put(key, val);
    }
}

@wenshao wenshao added the bug label Aug 4, 2018
@wenshao wenshao added this to the 1.2.50 milestone Aug 4, 2018
@wenshao wenshao modified the milestones: 1.2.50, 1.2.51 Aug 19, 2018
wenshao added a commit that referenced this issue Sep 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants