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

FieldUtils may have concurrency issues #51

Open
matebase opened this issue Nov 17, 2022 · 0 comments
Open

FieldUtils may have concurrency issues #51

matebase opened this issue Nov 17, 2022 · 0 comments

Comments

@matebase
Copy link

for example: String id=FieldUtils.getFieldValue(next, "id")

public class Base implements Serializable {
    private String id;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
}
public class Son extends Base implements Serializable {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
public class TestCase {

    public static void main(String[] args) {

        List<Son> records = new ArrayList<>();
        List<Son> records2 = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 1000; i++) {
            long l = random.nextLong();
            Son son = new Son();
            son.setId(l + "");
            records.add(son);
            Son son2 = new Son();
            son2.setId(l + "");
            records2.add(son2);
        }
        new Thread(() -> {
            Iterator<Son> iterator = records.iterator();
            while (iterator.hasNext()) {
                Son next = iterator.next();
                String id = FieldUtils.getFieldValue(next, "id");
            }
        }).start();

        new Thread(() -> {
            Iterator<Son> iterator = records2.iterator();
            while (iterator.hasNext()) {
                Son next = iterator.next();
                String id = FieldUtils.getFieldValue(next, "id");
            }
        }).start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Exception in thread "Thread-1" java.lang.IllegalStateException: Could not access method or field: Class org.springframework.util.ReflectionUtils can not access a member of class com.Base with modifiers "private"
at org.springframework.util.ReflectionUtils.handleReflectionException(ReflectionUtils.java:107)
at org.springframework.util.ReflectionUtils.getField(ReflectionUtils.java:652)
at com.alibaba.spring.util.FieldUtils.getFieldValue(FieldUtils.java:72)
at com.alibaba.spring.util.FieldUtils.getFieldValue(FieldUtils.java:28)
at com.TestCase.lambda$main$1(TestCase.java:41)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant