Skip to content

Commit b72b56c

Browse files
committed
Further work on deferring forcing of access (to reduce likelihood of unnecessary calls), this time for serialization side
1 parent 8dbf55f commit b72b56c

File tree

6 files changed

+45
-17
lines changed

6 files changed

+45
-17
lines changed

src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMember.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,12 @@ public final boolean addIfNotPresent(Annotation a) {
133133
* @since 2.7
134134
*/
135135
public final void fixAccess(boolean force) {
136-
ClassUtil.checkAndFixAccess(getMember(), force);
136+
Member m = getMember();
137+
if (m != null) { // may be null for virtual members
138+
ClassUtil.checkAndFixAccess(m, force);
139+
}
137140
}
138-
141+
139142
/**
140143
* @deprecated Since 2.7 use {@link #fixAccess(boolean)} instead
141144
*/

src/main/java/com/fasterxml/jackson/databind/ser/AnyGetterWriter.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class AnyGetterWriter
2424
protected JsonSerializer<Object> _serializer;
2525

2626
protected MapSerializer _mapSerializer;
27-
27+
2828
@SuppressWarnings("unchecked")
2929
public AnyGetterWriter(BeanProperty property,
3030
AnnotatedMember accessor, JsonSerializer<?> serializer)
@@ -37,6 +37,14 @@ public AnyGetterWriter(BeanProperty property,
3737
}
3838
}
3939

40+
/**
41+
* @since 0.8.3
42+
*/
43+
public void fixAccess(SerializationConfig config) {
44+
_accessor.fixAccess(
45+
config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
46+
}
47+
4048
public void getAndSerialize(Object bean, JsonGenerator gen, SerializerProvider provider)
4149
throws Exception
4250
{

src/main/java/com/fasterxml/jackson/databind/ser/BeanPropertyWriter.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,17 @@ public void setNonTrivialBaseType(JavaType t) {
406406
_nonTrivialBaseType = t;
407407
}
408408

409+
/**
410+
* Method called to ensure that the mutator has proper access rights to
411+
* be called, as per configuration. Overridden by implementations that
412+
* have mutators that require access, fields and setters.
413+
*
414+
* @since 2.8.3
415+
*/
416+
public void fixAccess(SerializationConfig config) {
417+
_member.fixAccess(config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
418+
}
419+
409420
/*
410421
/***********************************************************
411422
/* JDK Serializability

src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializerBuilder.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,24 @@ public JsonSerializer<?> build()
179179
properties = NO_PROPERTIES;
180180
} else {
181181
properties = _properties.toArray(new BeanPropertyWriter[_properties.size()]);
182+
if (_config.isEnabled(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS)) {
183+
for (int i = 0, end = properties.length; i < end; ++i) {
184+
properties[i].fixAccess(_config);
185+
}
186+
}
187+
}
188+
if (_anyGetter != null) {
189+
_anyGetter.fixAccess(_config);
190+
}
191+
if (_typeId != null) {
192+
if (_config.isEnabled(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS)) {
193+
_typeId.fixAccess(_config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
194+
}
182195
}
183196
return new BeanSerializer(_beanDesc.getType(), this,
184197
properties, _filteredProperties);
185198
}
186-
199+
187200
/**
188201
* Factory method for constructing an "empty" serializer; one that
189202
* outputs no properties (but handles JSON objects properly, including

src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializerFactory.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,6 @@ protected JsonSerializer<Object> constructBeanSerializer(SerializerProvider prov
425425

426426
AnnotatedMember anyGetter = beanDesc.findAnyGetter();
427427
if (anyGetter != null) {
428-
if (config.canOverrideAccessModifiers()) {
429-
anyGetter.fixAccess(config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
430-
}
431428
JavaType type = anyGetter.getType();
432429
// copied from BasicSerializerFactory.buildMapSerializer():
433430
boolean staticTyping = config.isEnabled(MapperFeature.USE_STATIC_TYPING);
@@ -586,16 +583,11 @@ protected List<BeanPropertyWriter> findBeanProperties(SerializerProvider prov,
586583
PropertyBuilder pb = constructPropertyBuilder(config, beanDesc);
587584

588585
ArrayList<BeanPropertyWriter> result = new ArrayList<BeanPropertyWriter>(properties.size());
589-
final boolean fixAccess = config.canOverrideAccessModifiers();
590-
final boolean forceAccess = fixAccess && config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS);
591586
for (BeanPropertyDefinition property : properties) {
592587
final AnnotatedMember accessor = property.getAccessor();
593588
// Type id? Requires special handling:
594589
if (property.isTypeId()) {
595-
if (accessor != null) { // only add if we can access... but otherwise?
596-
if (fixAccess) {
597-
accessor.fixAccess(forceAccess);
598-
}
590+
if (accessor != null) {
599591
builder.setTypeId(accessor);
600592
}
601593
continue;
@@ -790,9 +782,6 @@ protected BeanPropertyWriter _constructWriter(SerializerProvider prov,
790782
throws JsonMappingException
791783
{
792784
final PropertyName name = propDef.getFullName();
793-
if (prov.canOverrideAccessModifiers()) {
794-
accessor.fixAccess(prov.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
795-
}
796785
JavaType type = accessor.getType();
797786
BeanProperty.Std property = new BeanProperty.Std(name, type, propDef.getWrapperName(),
798787
pb.getClassAnnotations(), accessor, propDef.getMetadata());

src/main/java/com/fasterxml/jackson/databind/ser/PropertyBuilder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,15 @@ protected BeanPropertyWriter buildWriter(SerializerProvider prov,
120120
switch (inclusion) {
121121
case NON_DEFAULT:
122122
// 11-Nov-2015, tatu: This is tricky because semantics differ between cases,
123-
// so that if enclosing class has this, we may need to values of property,
123+
// so that if enclosing class has this, we may need to access values of property,
124124
// whereas for global defaults OR per-property overrides, we have more
125125
// static definition. Sigh.
126126
// First: case of class specifying it; try to find POJO property defaults
127127
if (_defaultInclusion.getValueInclusion() == JsonInclude.Include.NON_DEFAULT) {
128+
// 07-Sep-2016, tatu: may also need to front-load access forcing now
129+
if (prov.isEnabled(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS)) {
130+
am.fixAccess(_config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
131+
}
128132
valueToSuppress = getPropertyDefaultValue(propDef.getName(), am, actualType);
129133
} else {
130134
valueToSuppress = getDefaultValue(actualType);

0 commit comments

Comments
 (0)