Skip to content

Commit 12f909a

Browse files
ClassUtil.checkAndFixAccess: ignore errors re: setAccessible, it's very common in JPMS world
1 parent 6af6725 commit 12f909a

File tree

1 file changed

+15
-24
lines changed

1 file changed

+15
-24
lines changed

src/main/java/tools/jackson/databind/util/ClassUtil.java

+15-24
Original file line numberDiff line numberDiff line change
@@ -846,41 +846,32 @@ public static boolean checkAndFixAccess(Member member, boolean evenIfAlreadyPubl
846846
// We know all members are also accessible objects...
847847
AccessibleObject ao = (AccessibleObject) member;
848848

849-
// 14-Jan-2009, tatu: It seems safe and potentially beneficial to
850-
// always to make it accessible (latter because it will force
851-
// skipping checks we have no use for...), so let's always call it.
852849
try {
853850
// 15-Apr-2021, tatu: With JDK 14+ we will be hitting access limitations
854851
// esp. wrt JDK types so let's change a bit
855852
final Class<?> declaringClass = member.getDeclaringClass();
856853
boolean isPublic = Modifier.isPublic(member.getModifiers())
857854
&& Modifier.isPublic(declaringClass.getModifiers());
858855
if (!isJDKClass(declaringClass) && (!isPublic || evenIfAlreadyPublic)) {
859-
return ao.trySetAccessible();
856+
ao.setAccessible(true);
857+
return true;
860858
} else {
861859
return isPublic;
862860
}
863-
} catch (SecurityException se) {
864-
// 17-Apr-2009, tatu: This can fail on platforms like
865-
// Google App Engine); so let's only fail if we really needed it...
866-
if (!ao.isAccessible()) {
867-
Class<?> declClass = member.getDeclaringClass();
868-
throw new IllegalArgumentException("Cannot access "+member+" (from class "+declClass.getName()
869-
+"; failed to set access: "+exceptionMessage(se));
870-
}
871-
// 14-Apr-2021, tatu: [databind#3118] Java 9/JPMS causes new fails...
872-
// But while our baseline is Java 8, must check name
873-
return true;
874-
} catch (RuntimeException se) {
875-
if ("InaccessibleObjectException".equals(se.getClass().getSimpleName())) {
876-
throw new IllegalArgumentException(String.format(
877-
"Failed to call `setAccess()` on %s '%s' (of class %s) due to `%s`, problem: %s",
878-
member.getClass().getSimpleName(), member.getName(),
879-
nameOf(member.getDeclaringClass()),
880-
se.getClass().getName(), se.getMessage()),
881-
se);
861+
} catch (SecurityException e) {
862+
return false;
863+
} catch (RuntimeException e) {
864+
// 2025-03-24, scs: Since Java module system, it is common to be unable to see
865+
// members of other modules. We can't assume we can crack open arbitrary types anymore.
866+
// We'd love to catch this more explicitly, but Android... >:(
867+
if ("InaccessibleObjectException".equals(e.getClass().getSimpleName())) {
868+
return false;
882869
}
883-
throw se;
870+
throw new IllegalArgumentException(String.format(
871+
"Failed to call `setAccess()` on %s '%s' (of class %s) due to `%s`, problem: %s",
872+
member.getClass().getSimpleName(), member.getName(),
873+
nameOf(member.getDeclaringClass()),
874+
e.getClass().getName(), e.getMessage()), e);
884875
}
885876
}
886877

0 commit comments

Comments
 (0)