Skip to content

Commit

Permalink
Minor work wr #3352, improving exception messages to help in troubles…
Browse files Browse the repository at this point in the history
…hooting
  • Loading branch information
cowtowncoder committed Jan 18, 2022
1 parent 1155b5c commit e916bcf
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ public void addProperty(SettableBeanProperty prop)
* currently built bean.
*/
public void addBackReferenceProperty(String referenceName, SettableBeanProperty prop)
throws JsonMappingException
{
if (_backRefProperties == null) {
_backRefProperties = new HashMap<String, SettableBeanProperty>(4);
Expand All @@ -204,7 +205,11 @@ public void addBackReferenceProperty(String referenceName, SettableBeanProperty
// NOT work (2 failing unit tests). Not 100% clear why, but for now force
// access set early; unfortunate, but since it works....
if (_config.canOverrideAccessModifiers()) {
prop.fixAccess(_config);
try {
prop.fixAccess(_config);
} catch (IllegalArgumentException e) {
_handleBadAccess(e);
}
}
_backRefProperties.put(referenceName, prop);
// 16-Jan-2018, tatu: As per [databind#1878] we may want to leave it as is, to allow
Expand All @@ -221,12 +226,17 @@ public void addBackReferenceProperty(String referenceName, SettableBeanProperty
public void addInjectable(PropertyName propName, JavaType propType,
Annotations contextAnnotations, AnnotatedMember member,
Object valueId)
throws JsonMappingException
{
if (_injectables == null) {
_injectables = new ArrayList<ValueInjector>();
}
if ( _config.canOverrideAccessModifiers()) {
member.fixAccess(_config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
try {
member.fixAccess(_config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
} catch (IllegalArgumentException e) {
_handleBadAccess(e);
}
}
_injectables.add(new ValueInjector(propName, propType, member, valueId));
}
Expand Down Expand Up @@ -368,6 +378,7 @@ public boolean hasIgnorable(String name) {
* information collected.
*/
public JsonDeserializer<?> build()
throws JsonMappingException
{
Collection<SettableBeanProperty> props = _properties.values();
_fixAccess(props);
Expand Down Expand Up @@ -494,6 +505,7 @@ protected JsonDeserializer<?> createBuilderBasedDeserializer(JavaType valueType,
*/

protected void _fixAccess(Collection<SettableBeanProperty> mainProps)
throws JsonMappingException
{
/* 07-Sep-2016, tatu: Ideally we should be able to avoid forcing
* access to properties that are likely ignored, but due to
Expand All @@ -519,7 +531,11 @@ protected void _fixAccess(Collection<SettableBeanProperty> mainProps)
continue;
}
*/
prop.fixAccess(_config);
try {
prop.fixAccess(_config);
} catch (IllegalArgumentException e) {
_handleBadAccess(e);
}
}
}

Expand All @@ -528,7 +544,11 @@ protected void _fixAccess(Collection<SettableBeanProperty> mainProps)
/*
if (_backRefProperties != null) {
for (SettableBeanProperty prop : _backRefProperties.values()) {
prop.fixAccess(_config);
try {
prop.fixAccess(_config);
} catch (IllegalArgumentException e) {
_handleBadAccess(e);
}
}
}
*/
Expand All @@ -538,10 +558,18 @@ protected void _fixAccess(Collection<SettableBeanProperty> mainProps)
// be left as-is? May reconsider based on feedback

if (_anySetter != null) {
_anySetter.fixAccess(_config);
try {
_anySetter.fixAccess(_config);
} catch (IllegalArgumentException e) {
_handleBadAccess(e);
}
}
if (_buildMethod != null) {
_buildMethod.fixAccess(_config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
try {
_buildMethod.fixAccess(_config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
} catch (IllegalArgumentException e) {
_handleBadAccess(e);
}
}
}

Expand Down Expand Up @@ -577,4 +605,24 @@ protected boolean _findCaseInsensitivity() {
return (B == null) ? _config.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)
: B.booleanValue();
}

/**
* Helper method for linking root cause to "invalid type definition" exception;
* needed for troubleshooting issues with forcing access on later JDKs
* (as module definition boundaries are more strictly enforced).
*
* @since 2.13.2
*/
protected void _handleBadAccess(IllegalArgumentException e0)
throws JsonMappingException
{
try {
_context.reportBadTypeDefinition(_beanDesc, e0.getMessage());
} catch (DatabindException e) {
if (e.getCause() == null) {
e.initCause(e0);
}
throw e;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1007,8 +1007,10 @@ public static void checkAndFixAccess(Member member, boolean evenIfAlreadyPublic)
} catch (RuntimeException se) {
if ("InaccessibleObjectException".equals(se.getClass().getSimpleName())) {
throw new IllegalArgumentException(String.format(
"Failed to call `setAccess()` on %s '%s' due to `%s`, problem: %s",
member.getClass().getSimpleName(), member.getName(), se.getClass().getName(), se.getMessage()),
"Failed to call `setAccess()` on %s '%s' (of class %s) due to `%s`, problem: %s",
member.getClass().getSimpleName(), member.getName(),
nameOf(member.getDeclaringClass()),
se.getClass().getName(), se.getMessage()),
se);
}
throw se;
Expand Down

0 comments on commit e916bcf

Please sign in to comment.