Java 17 java.lang.reflect.InaccessibleObjectException:无法将字段设为私有最终 java.time.ZoneOffset

Posted

技术标签:

【中文标题】Java 17 java.lang.reflect.InaccessibleObjectException:无法将字段设为私有最终 java.time.ZoneOffset【英文标题】:Java 17 java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.time.ZoneOffset 【发布时间】:2022-01-05 16:20:49 【问题描述】:

我一直在尝试将 json 字符串转换为我的 java 程序可以使用的变量,但是当我运行它时它不起作用。 (responseString 是一个有效的 json 字符串)

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; 

我相信这可能与代码“public OffsetDateTime getLastinstall() return lastinstall; ”的最后一部分有关,因为错误提到了 java.time.OffsetDateTime,这就是调用它的地方。 当我运行它时,它给出了一个错误:

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)

请帮助解决这个问题。我用于响应字符串的 json 是:


    "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"

【问题讨论】:

您希望如何设置lastinstall(或同一班级的state)?你有其他字段的 setter 方法,但是这两个没有... 我已经尝试添加 setter 方法,它没有任何区别。 我认为问题不在于它的设置。 我认为当我在第一行给它 json 时它会被设置。 根据堆栈跟踪,似乎是 [java] modules 的问题。你是如何运行你的程序的?从命令行?来自 IDE?通过 Maven? 【参考方案1】:

在我看来,Jackson 尝试序列化 OffsetDateTime 字段时出现问题 - 它试图访问 OffsetDateTime 的私有“偏移”属性。

需要告诉杰克逊如何正确序列化 OffsetDateTime。

如果您需要库未提供的格式,您可以添加另一个 Jackson 库依赖项来处理此问题,或者编写自定义对象映射器。

见Jackson date-format for OffsetDateTime in Spring Boot

【讨论】:

以上是关于Java 17 java.lang.reflect.InaccessibleObjectException:无法将字段设为私有最终 java.time.ZoneOffset的主要内容,如果未能解决你的问题,请参考以下文章

java.lang.reflect.InvocationTargetException

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

java.lang.reflect.InvocationTargetException 怎么解决

java.lang.reflect.Constructor

java.lang.reflect操作对象属性(域)的值

java.lang.reflect.Method