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

Illegal reflective access by com.fasterxml.jackson.databind.util.ClassUtil -> in field java.time.OffsetDateTime #2718

Closed
pepearruda opened this issue May 12, 2020 · 2 comments
Labels
duplicate Duplicate of an existing (usually earlier) issue

Comments

@pepearruda
Copy link

pepearruda commented May 12, 2020

This issue can be related to #2312, #2464, #2626, #2683.
There are some issues when deserializing a JSON file to a class that has a OffsetDateTime property:

public class Person {
    private String name;
    private OffsetDateTime date = OffsetDateTime.now();
    // class constructors, getters and setters ...
}

public class MainDemo {

    public static <T> List<T> loadFile(Class<T> type) {
        List<T> list = new ArrayList<>();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, type);
            File file = new File(MainDemo.class.getClassLoader().getResource(type.getSimpleName() + ".json").getFile());
            list = objectMapper.readValue(file, javaType); //WARNING here
        } catch (Exception ex) {
            System.out.println("Exception: " + ex);
        }
        return list;
    }

    public static void main(String[] args) {
        List<Person> list = loadFile(Person.class);
        System.out.println("list: " + list);
    }
}

Person.json in resources folder:

[ { "name": "John Doe" }, { "name": "Alice Doe" } ]

I see the following Warning:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.fasterxml.jackson.databind.util.ClassUtil (file:/C:/Users/user/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.11.0/jackson-databind-2.11.0.jar) to field java.time.OffsetDateTime.offset
WARNING: Please consider reporting this to the maintainers of com.fasterxml.jackson.databind.util.ClassUtil
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

After setting VM --illegal-access=debug I can see the following:

WARNING: Illegal reflective access by com.fasterxml.jackson.databind.util.ClassUtil (file:/C:/Users/user/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.11.0/jackson-databind-2.11.0.jar) to field java.time.OffsetDateTime.offset
	at com.fasterxml.jackson.databind.util.ClassUtil.checkAndFixAccess(ClassUtil.java:930)
	at com.fasterxml.jackson.databind.deser.impl.FieldProperty.fixAccess(FieldProperty.java:104)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBuilder._fixAccess(BeanDeserializerBuilder.java:492)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBuilder.build(BeanDeserializerBuilder.java:350)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:252)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:143)
	at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:414)
	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:481)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.resolve(BeanDeserializerBase.java:485)
	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.findContextualValueDeserializer(DeserializationContext.java:458)
	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.createContextual(CollectionDeserializer.java:182)
	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.createContextual(CollectionDeserializer.java:27)
	at com.fasterxml.jackson.databind.DeserializationContext.handleSecondaryContextualization(DeserializationContext.java:696)
	at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:496)
	at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:4669)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4478)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3339)
	at MainDemo.loadFile(MainDemo.java:16)
	at MainDemo.main(MainDemo.java:26)

Currently using JDK 14, and the following:

<dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.11.0</version>
</dependency>

This warning did not show up with the old java.util.Date, it is showing up now with the new java8 java.time.OffsetDateTime.

Any idea how to fix this?

(Updated after finding more related issues, I hope that this info can help)

@cowtowncoder
Copy link
Member

Yes, and I am not sure much can be done in this case, in general.

But for specific case of java 8 date/time, #2683 should help (to be part of 2.12.0), so I will close this issue as duplicate.

@cowtowncoder cowtowncoder added the duplicate Duplicate of an existing (usually earlier) issue label May 12, 2020
@ksiv
Copy link

ksiv commented Jul 2, 2021

Since it's related to Java Date/Time
I have this issue:
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.fasterxml.jackson.databind.util.ClassUtil (file://repository/com/fasterxml/jackson/core/jackson-databind/2.11.2/jackson-databind-2.11.2.jar) to field java.time.Duration.seconds
WARNING: Please consider reporting this to the maintainers of com.fasterxml.jackson.databind.util.ClassUtil
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of java.time.Duration (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (String)>org.springframework.boot.autoconfigure.jms.JmsPoolConnectionFactoryProperties["blockIfFullTimeout"])

And the workaround that helps is
ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());

Got it from here (all tribute goes to solution author)
https://stackoverflow.com/questions/45863678/json-parse-error-can-not-construct-instance-of-java-time-localdate-no-string-a/50509320#50509320

I hope It ll help someone as I pretty much have been here searching for solution as well as in all issues graphhopper/graphhopper#1440, issue FasterXML/jackson-modules-java8#184

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate Duplicate of an existing (usually earlier) issue
Projects
None yet
Development

No branches or pull requests

3 participants