Skip to content

Commit e916bcf

Browse files
committed
Minor work wr #3352, improving exception messages to help in troubleshooting
1 parent 1155b5c commit e916bcf

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerBuilder.java

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ public void addProperty(SettableBeanProperty prop)
196196
* currently built bean.
197197
*/
198198
public void addBackReferenceProperty(String referenceName, SettableBeanProperty prop)
199+
throws JsonMappingException
199200
{
200201
if (_backRefProperties == null) {
201202
_backRefProperties = new HashMap<String, SettableBeanProperty>(4);
@@ -204,7 +205,11 @@ public void addBackReferenceProperty(String referenceName, SettableBeanProperty
204205
// NOT work (2 failing unit tests). Not 100% clear why, but for now force
205206
// access set early; unfortunate, but since it works....
206207
if (_config.canOverrideAccessModifiers()) {
207-
prop.fixAccess(_config);
208+
try {
209+
prop.fixAccess(_config);
210+
} catch (IllegalArgumentException e) {
211+
_handleBadAccess(e);
212+
}
208213
}
209214
_backRefProperties.put(referenceName, prop);
210215
// 16-Jan-2018, tatu: As per [databind#1878] we may want to leave it as is, to allow
@@ -221,12 +226,17 @@ public void addBackReferenceProperty(String referenceName, SettableBeanProperty
221226
public void addInjectable(PropertyName propName, JavaType propType,
222227
Annotations contextAnnotations, AnnotatedMember member,
223228
Object valueId)
229+
throws JsonMappingException
224230
{
225231
if (_injectables == null) {
226232
_injectables = new ArrayList<ValueInjector>();
227233
}
228234
if ( _config.canOverrideAccessModifiers()) {
229-
member.fixAccess(_config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
235+
try {
236+
member.fixAccess(_config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
237+
} catch (IllegalArgumentException e) {
238+
_handleBadAccess(e);
239+
}
230240
}
231241
_injectables.add(new ValueInjector(propName, propType, member, valueId));
232242
}
@@ -368,6 +378,7 @@ public boolean hasIgnorable(String name) {
368378
* information collected.
369379
*/
370380
public JsonDeserializer<?> build()
381+
throws JsonMappingException
371382
{
372383
Collection<SettableBeanProperty> props = _properties.values();
373384
_fixAccess(props);
@@ -494,6 +505,7 @@ protected JsonDeserializer<?> createBuilderBasedDeserializer(JavaType valueType,
494505
*/
495506

496507
protected void _fixAccess(Collection<SettableBeanProperty> mainProps)
508+
throws JsonMappingException
497509
{
498510
/* 07-Sep-2016, tatu: Ideally we should be able to avoid forcing
499511
* access to properties that are likely ignored, but due to
@@ -519,7 +531,11 @@ protected void _fixAccess(Collection<SettableBeanProperty> mainProps)
519531
continue;
520532
}
521533
*/
522-
prop.fixAccess(_config);
534+
try {
535+
prop.fixAccess(_config);
536+
} catch (IllegalArgumentException e) {
537+
_handleBadAccess(e);
538+
}
523539
}
524540
}
525541

@@ -528,7 +544,11 @@ protected void _fixAccess(Collection<SettableBeanProperty> mainProps)
528544
/*
529545
if (_backRefProperties != null) {
530546
for (SettableBeanProperty prop : _backRefProperties.values()) {
531-
prop.fixAccess(_config);
547+
try {
548+
prop.fixAccess(_config);
549+
} catch (IllegalArgumentException e) {
550+
_handleBadAccess(e);
551+
}
532552
}
533553
}
534554
*/
@@ -538,10 +558,18 @@ protected void _fixAccess(Collection<SettableBeanProperty> mainProps)
538558
// be left as-is? May reconsider based on feedback
539559

540560
if (_anySetter != null) {
541-
_anySetter.fixAccess(_config);
561+
try {
562+
_anySetter.fixAccess(_config);
563+
} catch (IllegalArgumentException e) {
564+
_handleBadAccess(e);
565+
}
542566
}
543567
if (_buildMethod != null) {
544-
_buildMethod.fixAccess(_config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
568+
try {
569+
_buildMethod.fixAccess(_config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
570+
} catch (IllegalArgumentException e) {
571+
_handleBadAccess(e);
572+
}
545573
}
546574
}
547575

@@ -577,4 +605,24 @@ protected boolean _findCaseInsensitivity() {
577605
return (B == null) ? _config.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)
578606
: B.booleanValue();
579607
}
608+
609+
/**
610+
* Helper method for linking root cause to "invalid type definition" exception;
611+
* needed for troubleshooting issues with forcing access on later JDKs
612+
* (as module definition boundaries are more strictly enforced).
613+
*
614+
* @since 2.13.2
615+
*/
616+
protected void _handleBadAccess(IllegalArgumentException e0)
617+
throws JsonMappingException
618+
{
619+
try {
620+
_context.reportBadTypeDefinition(_beanDesc, e0.getMessage());
621+
} catch (DatabindException e) {
622+
if (e.getCause() == null) {
623+
e.initCause(e0);
624+
}
625+
throw e;
626+
}
627+
}
580628
}

src/main/java/com/fasterxml/jackson/databind/util/ClassUtil.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,8 +1007,10 @@ public static void checkAndFixAccess(Member member, boolean evenIfAlreadyPublic)
10071007
} catch (RuntimeException se) {
10081008
if ("InaccessibleObjectException".equals(se.getClass().getSimpleName())) {
10091009
throw new IllegalArgumentException(String.format(
1010-
"Failed to call `setAccess()` on %s '%s' due to `%s`, problem: %s",
1011-
member.getClass().getSimpleName(), member.getName(), se.getClass().getName(), se.getMessage()),
1010+
"Failed to call `setAccess()` on %s '%s' (of class %s) due to `%s`, problem: %s",
1011+
member.getClass().getSimpleName(), member.getName(),
1012+
nameOf(member.getDeclaringClass()),
1013+
se.getClass().getName(), se.getMessage()),
10121014
se);
10131015
}
10141016
throw se;

0 commit comments

Comments
 (0)