Search code examples
javagwtgwt-jackson-apt

GWT-Jackson-APT Couldn't make a guess for classname


After getting the rest of it all working with samples, I tied to serialize my actual underlying object, and found that it would always kick back an error about not being able to guess about the class I'm trying to serialize. This is highly simplified example of what I'm trying to do, along with annotations that seem to make sense to me for how to do it.

I want to serialize a List<> of primitive (or boxed primitive) objects, in this case one int & one string. My actual class is all primitive (or boxed primitive) types also.

@JSONMapper
public static interface TestMapper extends ObjectMapper<TestElmt>{
    TestMapper INSTANCE = new Webworkers_TestMapperImpl();
}

public static class TestElmt {

    List<test> inerVar = new ArrayList<>();

    public void addElement(test elmt){
        inerVar.add(elmt);
    }
    public List<test> getElements(){
        return inerVar;
    }

}

@JSONMapper
public static class test{

    public static test_MapperImpl MAPPER = new test_MapperImpl();

    int x;
    String y;

    test(int X,String Y){
        x = X;
        y = Y;
    }
}

But the error I get is:

Error:java: error while creating source file java.lang.IllegalArgumentException: couldn't make a guess for client.myEnclosingClass.test


Solution

  • The code in the question has two issues that will not allows it to compile :

    First the test class should be named as Test - capital T - instead of test - small t -.

    Second there should be a no args constructor on class test, otherwise the deserializers wont know how to create a new instance of the class, it will be generated but will have a compilation error in its create method.

    if we change the test class like this all should work

    @JSONMapper
    public static class Test {
    
        public static Test_MapperImpl MAPPER = new Test_MapperImpl();
    
        int x;
        String y;
    
        public Test() {
        }
    
        Test(int X, String Y){
                x = X;
                y = Y;
        }
    }
    

    this is because gwt-jackson-apt makes some assumptions and uses some conventions to generate the underlying serializers/deserializers.