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

bug: class initialization can re-enter B2JsonHandlerMap.getHandler #86

Open
bwbeach opened this issue Sep 6, 2019 · 1 comment
Open

Comments

@bwbeach
Copy link
Contributor

bwbeach commented Sep 6, 2019

This test case triggers the problem:

    /**
     * A regression test for a case where a class has a field with a default value,
     * and the class of the default value has a class initializer.
     */
    @Test
    public void testClassInitializationInDefaultValue() {
        B2Json.fromJsonOrThrowRuntime("{}", TestClassInit_ClassWithDefaultValue.class);
    }

    private static class TestClassInit_ClassWithDefaultValue {

        @B2Json.optionalWithDefault(defaultValue = "{}")
        private final TestClassInit_ClassThatDoesInitializition objThatDoesInit;

        @B2Json.constructor(params = "objThatDoesInit")
        private TestClassInit_ClassWithDefaultValue(TestClassInit_ClassThatDoesInitializition objThatDoesInit) {
            this.objThatDoesInit = objThatDoesInit;
        }
    }

    private static class TestClassInit_ClassThatDoesInitializition {

        private static TestClassInit_ClassThatDoesInitializition defaultValue =
                B2Json.fromJsonOrThrowRuntime("{}", TestClassInit_ClassThatDoesInitializition.class);

        @B2Json.constructor(params = "")
        TestClassInit_ClassThatDoesInitializition() {}
    }
@bwbeach
Copy link
Contributor Author

bwbeach commented Sep 6, 2019

I hadn't thought about class initialization before. Apparently you can use reflection on a class, like B2Json does, without triggering initialization. But when you try and create an instance, it does have to initialize the class. (That part was clear.)

When the B2JsonHandlerMap is checking default values, it's still holding the lock, and hasn't yet committed to using the handlers it's setting up, but the class initialization code is trying to use them.

Not sure how to fix this.

bwbeach added a commit that referenced this issue Sep 7, 2019
See the comments in `B2JsonHandlerMap` for the new approach to
checking default values.

Testing: unit tests
bwbeach added a commit that referenced this issue Oct 18, 2019
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