@@ -28,12 +28,15 @@ final class ConventionSetPrivateFieldImpl implements Convention {
28
28
@ Override
29
29
public void apply (final ClassModelBuilder <?> classModelBuilder ) {
30
30
for (PropertyModelBuilder <?> propertyModelBuilder : classModelBuilder .getPropertyModelBuilders ()) {
31
- if (propertyModelBuilder .getPropertyAccessor () instanceof PropertyAccessorImpl ) {
32
- PropertyAccessorImpl <?> defaultAccessor = (PropertyAccessorImpl <?>) propertyModelBuilder .getPropertyAccessor ();
33
- PropertyMetadata <?> propertyMetaData = defaultAccessor .getPropertyMetadata ();
34
- if (!propertyMetaData .isDeserializable () && isPrivate (propertyMetaData .getField ().getModifiers ())) {
35
- setPropertyAccessor (propertyModelBuilder );
36
- }
31
+ if (!(propertyModelBuilder .getPropertyAccessor () instanceof PropertyAccessorImpl )) {
32
+ throw new CodecConfigurationException (format ("The SET_PRIVATE_FIELDS_CONVENTION is not compatible with "
33
+ + "propertyModelBuilder instance that have custom implementations of org.bson.codecs.pojo.PropertyAccessor: %s" ,
34
+ propertyModelBuilder .getPropertyAccessor ().getClass ().getName ()));
35
+ }
36
+ PropertyAccessorImpl <?> defaultAccessor = (PropertyAccessorImpl <?>) propertyModelBuilder .getPropertyAccessor ();
37
+ PropertyMetadata <?> propertyMetaData = defaultAccessor .getPropertyMetadata ();
38
+ if (!propertyMetaData .isDeserializable () && isPrivate (propertyMetaData .getField ().getModifiers ())) {
39
+ setPropertyAccessor (propertyModelBuilder );
37
40
}
38
41
}
39
42
}
@@ -49,6 +52,12 @@ private static final class PrivateProperyAccessor<T> implements PropertyAccessor
49
52
50
53
private PrivateProperyAccessor (final PropertyAccessorImpl <T > wrapped ) {
51
54
this .wrapped = wrapped ;
55
+ try {
56
+ wrapped .getPropertyMetadata ().getField ().setAccessible (true );
57
+ } catch (Exception e ) {
58
+ throw new CodecConfigurationException (format ("Unable to make private field accessible '%s' in %s" ,
59
+ wrapped .getPropertyMetadata ().getName (), wrapped .getPropertyMetadata ().getDeclaringClassName ()), e );
60
+ }
52
61
}
53
62
54
63
@ Override
@@ -64,8 +73,7 @@ public <S> void set(final S instance, final T value) {
64
73
field .set (instance , value );
65
74
} catch (Exception e ) {
66
75
throw new CodecConfigurationException (format ("Unable to set value for property '%s' in %s" ,
67
- wrapped .getPropertyMetadata ().getName (),
68
- wrapped .getPropertyMetadata ().getDeclaringClassName ()), e );
76
+ wrapped .getPropertyMetadata ().getName (), wrapped .getPropertyMetadata ().getDeclaringClassName ()), e );
69
77
}
70
78
}
71
79
}
0 commit comments