commons-lang icon indicating copy to clipboard operation
commons-lang copied to clipboard

fix InaccessibleObjectException on java-17

Open stefansimon76 opened this issue 1 year ago • 1 comments

"field.setAccessible(true);" throws an exception on java-17 because java.lang is not open for reflection. The problem cannot be worked around on the client side because "setAccessible(true)" is executed before evaluating the passed parameter "excludeFields". Both should be fixed with the change. The existing tests also cover the change.

stefansimon76 avatar Mar 11 '24 09:03 stefansimon76

@stefansimon76 This PR currently has zero tests and no helpful explanation in the PR description.

garydgregory avatar Mar 16 '24 03:03 garydgregory

Closing: No reply in 2 weeks.

garydgregory avatar Apr 01 '24 21:04 garydgregory

@garydgregory I think I get the same issue that is described here when running a test that compares 2 ArrayList instances with JDK17. Is this reproducible?

@Test
void testEqualsArrayList() {
	List<String> tracks = new ArrayList<>();
	List<String> deserializedTracks = new ArrayList<>();
	
	Assertions.assertTrue(EqualsBuilder.reflectionEquals(tracks, deserializedTracks));
}

This is the stacktrace.

java.lang.reflect.InaccessibleObjectException: Unable to make field private static final long java.util.ArrayList.serialVersionUID accessible: module java.base does not "opens java.util" to unnamed module @4de5031f
	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.AccessibleObject.setAccessible(AccessibleObject.java:130)
	at org.apache.commons.lang3.builder.EqualsBuilder.reflectionAppend(EqualsBuilder.java:1020)
	at org.apache.commons.lang3.builder.EqualsBuilder.reflectionAppend(EqualsBuilder.java:983)
	at org.apache.commons.lang3.builder.EqualsBuilder.reflectionEquals(EqualsBuilder.java:236)
	at org.apache.commons.lang3.builder.EqualsBuilder.reflectionEquals(EqualsBuilder.java:271)
	at org.apache.commons.lang3.builder.EqualsBuilder.reflectionEquals(EqualsBuilder.java:322)

akuhtz avatar May 27 '24 14:05 akuhtz

Hello @akuhtz If you want to use this feature, you have to give it permission, please see https://github.com/apache/commons-lang/blob/9babe58d895a0e4d6563268ca188794fbf066b38/pom.xml#L468

garydgregory avatar May 28 '24 13:05 garydgregory

Hello @garydgregory Thank you 👍

akuhtz avatar May 28 '24 14:05 akuhtz

@garydgregory based on the documentation of the EqualsBuilder.reflectionEquals static fields should not be compared, and java.util.ArrayList.serialVersionUID is a static field. Isn't it a bug in the EqualsBuilder.reflectionEquals as it doesn't behave in line with its description? So with proper behavior using workarounds like add opens for java native packages wouldn't be necessary?

Axinet avatar Nov 12 '24 11:11 Axinet