简体   繁体   中英

Java 17 java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.time.ZoneOffset

I have been trying to convert a json string to variables my java program can use but when i run this it doesnt work. (responseString is a valid json string)

GetConversion conversion = new ObjectMapper().readValue(responseString, GetConversion.class);


class GetConversion{
private State state;
private Swupdate swupdate;
private String type;
private String name;
private String modelid;
private String manufacturername;
private String productname;
private Capabilities capabilities;
private Config config;
private String uniqueid;
private String swversion;
private String swconfigid;
private String productid;

public State getState() { return state; }
public void setState(State value) { this.state = value; }

public Swupdate getSwupdate() { return swupdate; }
public void setSwupdate(Swupdate value) { this.swupdate = value; }

public String getType() { return type; }
public void setType(String value) { this.type = value; }

public String getName() { return name; }
public void setName(String value) { this.name = value; }

public String getModelid() { return modelid; }
public void setModelid(String value) { this.modelid = value; }

public String getManufacturername() { return manufacturername; }
public void setManufacturername(String value) { this.manufacturername = value; }

public String getProductname() { return productname; }
public void setProductname(String value) { this.productname = value; }

public Capabilities getCapabilities() { return capabilities; }
public void setCapabilities(Capabilities value) { this.capabilities = value; }

public Config getConfig() { return config; }
public void setConfig(Config value) { this.config = value; }

public String getUniqueid() { return uniqueid; }
public void setUniqueid(String value) { this.uniqueid = value; }

public String getSwversion() { return swversion; }
public void setSwversion(String value) { this.swversion = value; }

public String getSwconfigid() { return swconfigid; }
public void setSwconfigid(String value) { this.swconfigid = value; }

public String getProductid() { return productid; }
public void setProductid(String value) { this.productid = value; }
}

class Capabilities {
   private boolean certified;
   private Control control;
   private Streaming streaming;

    public boolean getCertified() { return certified; }
    public void setCertified(boolean value) { this.certified = value; }

    public Control getControl() { return control; }
    public void setControl(Control value) { this.control = value; }

    public Streaming getStreaming() { return streaming; }
    public void setStreaming(Streaming value) { this.streaming = value; }
}

class Control {
    private long mindimlevel;
    private long maxlumen;
    private String colorgamuttype;
    private double[][] colorgamut;
    private CT ct;

    public long getMindimlevel() { return mindimlevel; }
    public void setMindimlevel(long value) { this.mindimlevel = value; }

    public long getMaxlumen() { return maxlumen; }
    public void setMaxlumen(long value) { this.maxlumen = value; }

    public String getColorgamuttype() { return colorgamuttype; }
    public void setColorgamuttype(String value) { this.colorgamuttype = value; }

    public double[][] getColorgamut() { return colorgamut; }
    public void setColorgamut(double[][] value) { this.colorgamut = value; }

    public CT getCT() { return ct; }
    public void setCT(CT value) { this.ct = value; }
}

class CT {
    private long min;
    private long max;

    public long getMin() { return min; }
    public void setMin(long value) { this.min = value; }

    public long getMax() { return max; }
    public void setMax(long value) { this.max = value; }
}

class Streaming {
    private boolean renderer;
    private boolean proxy;

    public boolean getRenderer() { return renderer; }
    public void setRenderer(boolean value) { this.renderer = value; }

    public boolean getProxy() { return proxy; }
    public void setProxy(boolean value) { this.proxy = value; }
}

class Config {
    private String archetype;
    private String function;
    private String direction;
    private Startup startup;

    public String getArchetype() { return archetype; }
    public void setArchetype(String value) { this.archetype = value; }

    public String getFunction() { return function; }
    public void setFunction(String value) { this.function = value; }

    public String getDirection() { return direction; }
    public void setDirection(String value) { this.direction = value; }

    public Startup getStartup() { return startup; }
    public void setStartup(Startup value) { this.startup = value; }
}

class Startup {
    private String mode;
    private boolean configured;

    public String getMode() {
        return mode;
    }

    public void setMode(String value) {
        this.mode = value;
    }

    public boolean getConfigured() {
        return configured;
    }

    public void setConfigured(boolean value) {
        this.configured = value;
    }
}

class State {
    private boolean on;
    private long bri;
    private long hue;
    private long sat;
    private String effect;
    private double[] xy;
    private long ct;
    private String alert;
    private String colormode;
    private String mode;
    private boolean reachable;

    public boolean getOn() { return on; }
    public void setOn(boolean value) { this.on = value; }

    public long getBri() { return bri; }
    public void setBri(long value) { this.bri = value; }

    public long getHue() { return hue; }
    public void setHue(long value) { this.hue = value; }

    public long getSat() { return sat; }
    public void setSat(long value) { this.sat = value; }

    public String getEffect() { return effect; }
    public void setEffect(String value) { this.effect = value; }

    public double[] getXy() { return xy; }
    public void setXy(double[] value) { this.xy = value; }

    public long getCT() { return ct; }
    public void setCT(long value) { this.ct = value; }

    public String getAlert() { return alert; }
    public void setAlert(String value) { this.alert = value; }

    public String getColormode() { return colormode; }
    public void setColormode(String value) { this.colormode = value; }

    public String getMode() { return mode; }
    public void setMode(String value) { this.mode = value; }

    public boolean getReachable() { return reachable; }
    public void setReachable(boolean value) { this.reachable = value; }
}


class Swupdate {
    private String state;
    private OffsetDateTime lastinstall;

    public String getState() { return state; }

    public OffsetDateTime getLastinstall() { return lastinstall; }
}

I believe it may have something to do with the last part of the code "public OffsetDateTime getLastinstall() { return lastinstall; }" because the error mentions java.time.OffsetDateTime and this is where that is called. When I run this it gives a this error:

Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.time.ZoneOffset java.time.OffsetDateTime.offset accessible: module java.base does not "opens java.time" to unnamed module @3578436e
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:178)
at java.base/java.lang.reflect.Field.setAccessible(Field.java:172)
at com.fasterxml.jackson.databind.util.ClassUtil.checkAndFixAccess(ClassUtil.java:891)
at com.fasterxml.jackson.databind.deser.impl.FieldProperty.fixAccess(FieldProperty.java:102)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBuilder._fixAccess(BeanDeserializerBuilder.java:484)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBuilder.build(BeanDeserializerBuilder.java:350)
at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:245)
at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:137)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:411)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:349)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:264)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
at com.fasterxml.jackson.databind.DeserializationContext.findNonContextualValueDeserializer(DeserializationContext.java:467)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.resolve(BeanDeserializerBase.java:473)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:293)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
at com.fasterxml.jackson.databind.DeserializationContext.findNonContextualValueDeserializer(DeserializationContext.java:467)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.resolve(BeanDeserializerBase.java:473)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:293)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:477)
at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:4190)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4009)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)
at sendCommands.Loft.runGet(Loft.java:141)
at sendCommands.Loft.routine(Loft.java:103)
at sendCommands.Main.main(Main.java:7)

Help fix this pls. The json I used for response string is:

{
    "state": {
        "on": true,
        "bri": 198,
        "hue": 25360,
        "sat": 254,
        "effect": "none",
        "xy": [
            0.1749,
            0.6963
        ],
        "ct": 500,
        "alert": "none",
        "colormode": "xy",
        "mode": "homeautomation",
        "reachable": true
    },
    "swupdate": {
        "state": "noupdates",
        "lastinstall": "2021-08-13T13:51:54"
    },
    "type": "Extended color light",
    "name": "Hue color spot 6",
    "modelid": "LCG002",
    "manufacturername": "Signify Netherlands B.V.",
    "productname": "Hue color spot",
    "capabilities": {
        "certified": true,
        "control": {
            "mindimlevel": 200,
            "maxlumen": 300,
            "colorgamuttype": "C",
            "colorgamut": [
                [
                    0.6915,
                    0.3083
                ],
                [
                    0.17,
                    0.7
                ],
                [
                    0.1532,
                    0.0475
                ]
            ],
            "ct": {
                "min": 153,
                "max": 500
            }
        },
        "streaming": {
            "renderer": true,
            "proxy": true
        }
    },
    "config": {
        "archetype": "spotbulb",
        "function": "mixed",
        "direction": "downwards",
        "startup": {
            "mode": "safety",
            "configured": true
        }
    },
    "uniqueid": "00:17:88:01:08:3f:fb:56-0b",
    "swversion": "1.88.2",
    "swconfigid": "4AF23F6F",
    "productid": "Philips-LCG002-1-GU10ECLv2"
}

It looks to me like it's a problem with Jackson attempting to serialize an OffsetDateTime field - it's trying to access the OffsetDateTime's private 'offset' property.

Jackson needs to be told how to serialize OffsetDateTime properly.

You can either add another Jackson library dependency to take care of this, or write a custom object mapper, if you need a format not provided by the library.

See Jackson date-format for OffsetDateTime in Spring Boot

I encountered a similar issue when I was unit testing Twilio SMS.

I solved the issue by registering JavaTimeModule on the ObjectMapper.

        String json = "{\"status\": \"sent\"}";
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JavaTimeModule());
        var message = Message.fromJson(json, mapper);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question java.lang.reflect.InaccessibleObjectException: Unable to make field private final transient java.net.InetSocketAddress What does this error mean? java.lang.reflect.InaccessibleObjectException: Unable to make field private final int java.time.LocalDate.year java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.lang.Object java.util.Optional.value accessible: java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.util.Map sun.reflect.annotation.AnnotationInvocationHandler java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.util.Comparator java.util.TreeMap.comparator accessible:module java.lang.reflect.InaccessibleObjectException: Getting "java.lang.reflect.InaccessibleObjectException: Unable to make field private int (variable) (package) accessible" while trying to use JPA Why am I seeing `java.lang.reflect.InaccessibleObjectException: Unable to make private java.nio.DirectByteBuffer(long,int) accessible` on a mac java.lang.reflect.inaccessibleobjectexception unable to make jdk.internal.loader.classloaders java.lang.reflect.InaccessibleObjectException: Unable to make protected void java.net.URLClassLoader.addURL(java.net.URL) accessible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM