Skip to content

JDK 16 Illegal reflective access for Throwable.setCause() with PropertyNamingStrategy.UPPER_CAMEL_CASE #3275

Closed
@jsharper

Description

@jsharper

Describe the bug
Mapping a json string to an instance of RuntimeException with JDK 16 (which defaults to denying illegal reflective access) while using UPPER_CAMEL_CASE property naming strategy, fails with:

Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Failed to call `setAccess()` on Method 'setCause' due to `java.lang.reflect.InaccessibleObjectException`, problem: Unable to make final void java.lang.Throwable.setCause(java.lang.Throwable) accessible: module java.base does not "opens java.lang" to unnamed module

This manifests in parts of the AWS v1 SDK when an error is received from the AWS API endpoints. Note example report: #2464 (comment)

The simplified reproduce case below is based on what the failing AWS SDK code is doing internally in com.amazonaws.transform.JsonErrorUnmarshaller.unmarshall.

Version information
2.12.3, 2.12.5, 2.13.0-rc2

To Reproduce
Execute this code without any --add-opens params and with default --illegal-access setting of deny.

public class Test {
	public static void main(String[] args) throws Exception {
		ObjectMapper mapper = new ObjectMapper();
		mapper.setPropertyNamingStrategy(PropertyNamingStrategies.UPPER_CAMEL_CASE);

		String jsonString = "{\"message\":\"This is my runtime exception message\"}";
		JsonNode jsonContent = mapper.readTree(jsonString);

		throw mapper.treeToValue(jsonContent, RuntimeException.class);
	}
}

Expected behavior
It is expected that the code above would throw a RuntimeException with a message of "This is my runtime exception message". This is what happens when working around the issue with java parameter --add-opens java.base/java.lang=ALL-UNNAMED

Additional context
Using java parameter --illegal-access=debug results in the following additional info:

WARNING: Illegal reflective access by com.fasterxml.jackson.databind.util.ClassUtil (file:/xxxxxx/jackson-databind-2.13.0-rc2.jar) to method java.lang.Throwable.setCause(java.lang.Throwable)
	at com.fasterxml.jackson.databind.util.ClassUtil.checkAndFixAccess(ClassUtil.java:994)
	at com.fasterxml.jackson.databind.introspect.AnnotatedMember.fixAccess(AnnotatedMember.java:139)
	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.fixAccess(MethodProperty.java:95)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBuilder._fixAccess(BeanDeserializerBuilder.java:522)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBuilder.build(BeanDeserializerBuilder.java:373)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildThrowableDeserializer(BeanDeserializerFactory.java:455)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:112)
	at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:415)
	at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:350)
	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.findRootValueDeserializer(DeserializationContext.java:642)
	at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:4751)
	at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:4596)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2815)
	at com.fasterxml.jackson.databind.ObjectMapper.treeToValue(ObjectMapper.java:3279)
	at Test.main(Test.java:13)

Note that the issue does not manifest without mapper.setPropertyNamingStrategy(PropertyNamingStrategies.UPPER_CAMEL_CASE);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions