Skip to content

Code generator not handling fields with custom adapter #381

@huehodaniel

Description

@huehodaniel

Using avaje-jsonb-generator 3.4 with Java 24.

I'm trying to use the @Json annotation to generate an adapter for a plain POJO that includes objects of complex classes with hand-made adapters annotated with @CustomAdapter, but the processor is trying to generate another adapter and failing. Minimal example:

// Example.java
package org.hueho.issue;

import io.avaje.jsonb.Json;

@Json
public class Example {
    private int code;
    private WrapMap map;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public WrapMap getMap() {
        return map;
    }

    public void setMap(WrapMap map) {
        this.map = map;
    }
}
// WrapMap.java
package org.hueho.issue;

import java.util.*;

public class WrapMap extends AbstractMap<String, String> {
    private final Map<String, String> delegate;

    public WrapMap(Map<String, String> map) {
        delegate = map;
    }

    @Override
    public Set<Entry<String, String>> entrySet() {
        return delegate.entrySet();
    }
}
// WrapMapJsonAdapter.java
package org.hueho.issue;

import io.avaje.json.JsonAdapter;
import io.avaje.json.JsonReader;
import io.avaje.json.JsonWriter;
import io.avaje.jsonb.CustomAdapter;
import io.avaje.jsonb.Jsonb;
import io.avaje.jsonb.Types;

import java.util.Map;

@CustomAdapter
public class WrapMapJsonAdapter implements JsonAdapter<WrapMap> {
    private final JsonAdapter<Map<String, String>> stringMapJsonAdapter;

    public WrapMapJsonAdapter(Jsonb jsonb) {
        this.stringMapJsonAdapter = jsonb.adapter(Types.newParameterizedType(Map.class, String.class, String.class));
    }

    @Override
    public WrapMap fromJson(JsonReader reader) {
        return new WrapMap(stringMapJsonAdapter.fromJson(reader));
    }

    @Override
    public void toJson(JsonWriter writer, WrapMap wrapMap) {
        writer.beginObject();
        wrapMap.forEach((key, value) -> {
            writer.name(key);
            writer.value(value);
        });
        writer.endObject();
    }
}

Maven truncated output:

[INFO] Recompiling the module because of changed source code.
[INFO] Compiling 19 source files with javac [debug target 24] to target\classes
[INFO] org.hueho.issue.WrapMap, non accessible field delegate
[INFO] Skipped writing JsonAdapter for org.hueho.issue.WrapMap due to non accessible fields
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] org.hueho.issue.WrapMap, non public field delegate with no matching setter or constructor?

Note that the issue also happens if I annotate the Example#map field with @Json.Serialize and change the @CustomAdapter annotation to use global = false.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions