Skip to content

Commit f4289bb

Browse files
committed
Bit more refactoring to prepare for #1381
1 parent 6492fb2 commit f4289bb

8 files changed

Lines changed: 86 additions & 77 deletions

File tree

src/main/java/com/fasterxml/jackson/databind/AnnotationIntrospector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ public TypeResolverBuilder<?> findPropertyContentTypeResolver(MapperConfig<?> co
473473
public JacksonInject.Value findInjectableValue(AnnotatedMember m) {
474474
// 05-Apr-2017, tatu: Just for 2.9, call deprecated method to help
475475
// with some cases of overrides for legacy code
476-
Object id = findInjectableValue(m);
476+
Object id = findInjectableValueId(m);
477477
if (id != null) {
478478
return JacksonInject.Value.forId(id);
479479
}

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.concurrent.*;
55
import java.util.concurrent.atomic.AtomicReference;
66

7+
import com.fasterxml.jackson.annotation.JacksonInject;
78
import com.fasterxml.jackson.annotation.JsonCreator;
89
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
910
import com.fasterxml.jackson.core.JsonLocation;
@@ -431,7 +432,7 @@ public ValueInstantiator _valueInstantiatorInstance(DeserializationConfig config
431432
PropertyName name = (argDef == null) ? null : argDef.getFullName();
432433
AnnotatedParameter arg = ctor.getParameter(0);
433434
properties[0] = constructCreatorProperty(ctxt, beanDesc, name, 0, arg,
434-
intr.findInjectableValueId(arg));
435+
intr.findInjectableValue(arg));
435436
creators.addPropertyCreator(ctor, isCreator, properties);
436437
} else {
437438
/*boolean added = */ _handleSingleArgumentConstructor(ctxt, beanDesc, vchecker, intr, creators,
@@ -460,7 +461,7 @@ public ValueInstantiator _valueInstantiatorInstance(DeserializationConfig config
460461
for (int i = 0; i < argCount; ++i) {
461462
final AnnotatedParameter param = ctor.getParameter(i);
462463
BeanPropertyDefinition propDef = (propDefs == null) ? null : propDefs[i];
463-
Object injectId = intr.findInjectableValueId(param);
464+
JacksonInject.Value injectId = intr.findInjectableValue(param);
464465
final PropertyName name = (propDef == null) ? null : propDef.getFullName();
465466

466467
if (propDef != null && propDef.isExplicitlyNamed()) {
@@ -692,17 +693,17 @@ protected boolean _handleSingleArgumentConstructor(DeserializationContext ctxt,
692693
for (int i = 0; i < argCount; ++i) {
693694
final AnnotatedParameter param = factory.getParameter(i);
694695
BeanPropertyDefinition propDef = (propDefs == null) ? null : propDefs[i];
695-
Object injectId = intr.findInjectableValueId(param);
696+
JacksonInject.Value injectable = intr.findInjectableValue(param);
696697
final PropertyName name = (propDef == null) ? null : propDef.getFullName();
697698

698699
if (propDef != null && propDef.isExplicitlyNamed()) {
699700
++explicitNameCount;
700-
properties[i] = constructCreatorProperty(ctxt, beanDesc, name, i, param, injectId);
701+
properties[i] = constructCreatorProperty(ctxt, beanDesc, name, i, param, injectable);
701702
continue;
702703
}
703-
if (injectId != null) {
704+
if (injectable != null) {
704705
++injectCount;
705-
properties[i] = constructCreatorProperty(ctxt, beanDesc, name, i, param, injectId);
706+
properties[i] = constructCreatorProperty(ctxt, beanDesc, name, i, param, injectable);
706707
continue;
707708
}
708709
NameTransformer unwrapper = intr.findUnwrappingNameTransformer(param);
@@ -718,7 +719,7 @@ protected boolean _handleSingleArgumentConstructor(DeserializationContext ctxt,
718719
if (isCreator) {
719720
if (name != null && !name.isEmpty()) {
720721
++implicitNameCount;
721-
properties[i] = constructCreatorProperty(ctxt, beanDesc, name, i, param, injectId);
722+
properties[i] = constructCreatorProperty(ctxt, beanDesc, name, i, param, injectable);
722723
continue;
723724
}
724725
}
@@ -820,7 +821,7 @@ protected void _reportUnwrappedCreatorProperty(DeserializationContext ctxt,
820821
protected SettableBeanProperty constructCreatorProperty(DeserializationContext ctxt,
821822
BeanDescription beanDesc, PropertyName name, int index,
822823
AnnotatedParameter param,
823-
Object injectableValueId)
824+
JacksonInject.Value injectable)
824825
throws JsonMappingException
825826
{
826827
final DeserializationConfig config = ctxt.getConfig();
@@ -848,6 +849,9 @@ protected SettableBeanProperty constructCreatorProperty(DeserializationContext c
848849
}
849850
// Note: contextualization of typeDeser _should_ occur in constructor of CreatorProperty
850851
// so it is not called directly here
852+
853+
Object injectableValueId = (injectable == null) ? null : injectable.getId();
854+
851855
SettableBeanProperty prop = new CreatorProperty(name, type, property.getWrapperName(),
852856
typeDeser, beanDesc.getClassAnnotations(), param, index, injectableValueId,
853857
metadata);
@@ -902,7 +906,7 @@ protected boolean _checkIfCreatorPropertyBased(AnnotationIntrospector intr,
902906
}
903907
// If explicit name, or inject id, property-based
904908
if (((propDef != null) && propDef.isExplicitlyNamed())
905-
|| (intr.findInjectableValueId(creator.getParameter(0)) != null)) {
909+
|| (intr.findInjectableValue(creator.getParameter(0)) != null)) {
906910
return true;
907911
}
908912
if (propDef != null) {

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,10 @@ public int hashCode() {
176176
@Override
177177
public boolean equals(Object o) {
178178
if (o == this) return true;
179-
if (!ClassUtil.hasClass(o, getClass())) {
180-
return false;
181-
}
182-
return ((AnnotatedConstructor) o)._constructor == _constructor;
179+
return ClassUtil.hasClass(o, getClass())
180+
&& (((AnnotatedConstructor) o)._constructor == _constructor);
183181
}
184-
182+
185183
/*
186184
/**********************************************************
187185
/* JDK serialization handling

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,8 @@ public int hashCode() {
141141
@Override
142142
public boolean equals(Object o) {
143143
if (o == this) return true;
144-
if (!ClassUtil.hasClass(o, getClass())) {
145-
return false;
146-
}
147-
return ((AnnotatedField) o)._field == _field;
144+
return ClassUtil.hasClass(o, getClass())
145+
&& (((AnnotatedField) o)._field == _field);
148146
}
149147

150148
@Override

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

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,7 @@ protected AnnotatedMethod(Serialization ser)
5151
_method = null;
5252
_serialization = ser;
5353
}
54-
55-
/**
56-
* Method that constructs a new instance with settings (annotations, parameter annotations)
57-
* of this instance, but with different physical {@link Method}.
58-
*/
59-
public AnnotatedMethod withMethod(Method m) {
60-
return new AnnotatedMethod(_typeContext, m, _annotations, _paramAnnotations);
61-
}
62-
54+
6355
@Override
6456
public AnnotatedMethod withAnnotations(AnnotationMap ann) {
6557
return new AnnotatedMethod(_typeContext, _method, ann, _paramAnnotations);
@@ -178,10 +170,7 @@ public void setValue(Object pojo, Object value) throws IllegalArgumentException
178170
{
179171
try {
180172
_method.invoke(pojo, value);
181-
} catch (IllegalAccessException e) {
182-
throw new IllegalArgumentException("Failed to setValue() with method "
183-
+getFullName()+": "+e.getMessage(), e);
184-
} catch (InvocationTargetException e) {
173+
} catch (IllegalAccessException | InvocationTargetException e) {
185174
throw new IllegalArgumentException("Failed to setValue() with method "
186175
+getFullName()+": "+e.getMessage(), e);
187176
}
@@ -192,10 +181,7 @@ public Object getValue(Object pojo) throws IllegalArgumentException
192181
{
193182
try {
194183
return _method.invoke(pojo, (Object[]) null);
195-
} catch (IllegalAccessException e) {
196-
throw new IllegalArgumentException("Failed to getValue() with method "
197-
+getFullName()+": "+e.getMessage(), e);
198-
} catch (InvocationTargetException e) {
184+
} catch (IllegalAccessException | InvocationTargetException e) {
199185
throw new IllegalArgumentException("Failed to getValue() with method "
200186
+getFullName()+": "+e.getMessage(), e);
201187
}
@@ -260,12 +246,10 @@ public int hashCode() {
260246
@Override
261247
public boolean equals(Object o) {
262248
if (o == this) return true;
263-
if (!ClassUtil.hasClass(o, getClass())) {
264-
return false;
265-
}
266-
return ((AnnotatedMethod) o)._method == _method;
249+
return ClassUtil.hasClass(o, getClass())
250+
&& (((AnnotatedMethod) o)._method == _method);
267251
}
268-
252+
269253
/*
270254
/**********************************************************
271255
/* JDK serialization handling

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.lang.reflect.Modifier;
44
import java.util.*;
55

6+
import com.fasterxml.jackson.annotation.JacksonInject;
67
import com.fasterxml.jackson.annotation.JsonCreator;
78
import com.fasterxml.jackson.annotation.JsonProperty;
89

@@ -645,37 +646,41 @@ protected void _addSetterMethod(Map<String, POJOPropertyBuilder> props,
645646
boolean ignore = (ai == null) ? false : ai.hasIgnoreMarker(m);
646647
_property(props, implName).addSetter(m, pn, nameExplicit, visible, ignore);
647648
}
648-
649+
649650
protected void _addInjectables(Map<String, POJOPropertyBuilder> props)
650651
{
651652
final AnnotationIntrospector ai = _annotationIntrospector;
652-
// first fields, then methods
653+
// first fields, then methods, to allow overriding
653654
for (AnnotatedField f : _classDef.fields()) {
654-
_doAddInjectable(ai.findInjectableValueId(f), f);
655+
_doAddInjectable(ai.findInjectableValue(f), f);
655656
}
656657

657658
for (AnnotatedMethod m : _classDef.memberMethods()) {
658659
// for now, only allow injection of a single arg (to be changed in future?)
659660
if (m.getParameterCount() != 1) {
660661
continue;
661662
}
662-
_doAddInjectable(ai.findInjectableValueId(m), m);
663+
_doAddInjectable(ai.findInjectableValue(m), m);
663664
}
664665
}
665666

666-
protected void _doAddInjectable(Object id, AnnotatedMember m)
667+
protected void _doAddInjectable(JacksonInject.Value injectable, AnnotatedMember m)
667668
{
668-
if (id == null) {
669+
if (injectable == null) {
669670
return;
670671
}
672+
Object id = injectable.getId();
671673
if (_injectables == null) {
672674
_injectables = new LinkedHashMap<Object, AnnotatedMember>();
673675
}
674676
AnnotatedMember prev = _injectables.put(id, m);
675677
if (prev != null) {
676-
String type = id.getClass().getName();
677-
throw new IllegalArgumentException("Duplicate injectable value with id '"
678-
+String.valueOf(id)+"' (of type "+type+")");
678+
// 12-Apr-2017, tatu: Let's allow masking of Field by Method
679+
if (prev.getClass() == m.getClass()) {
680+
String type = id.getClass().getName();
681+
throw new IllegalArgumentException("Duplicate injectable value with id '"
682+
+String.valueOf(id)+"' (of type "+type+")");
683+
}
679684
}
680685
}
681686

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.fasterxml.jackson.databind.deser.inject;
2+
3+
import com.fasterxml.jackson.annotation.JacksonInject;
4+
5+
import com.fasterxml.jackson.databind.BaseMapTest;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
8+
9+
public class InvalidInjectionTest extends BaseMapTest
10+
{
11+
static class BadBean1 {
12+
@JacksonInject protected String prop1;
13+
@JacksonInject protected String prop2;
14+
}
15+
16+
static class BadBean2 {
17+
@JacksonInject("x") protected String prop1;
18+
@JacksonInject("x") protected String prop2;
19+
}
20+
21+
/*
22+
/**********************************************************
23+
/* Unit tests
24+
/**********************************************************
25+
*/
26+
27+
private final ObjectMapper MAPPER = new ObjectMapper();
28+
29+
public void testInvalidDup() throws Exception
30+
{
31+
try {
32+
MAPPER.readValue("{}", BadBean1.class);
33+
fail("Should not pass");
34+
} catch (InvalidDefinitionException e) {
35+
verifyException(e, "Duplicate injectable value");
36+
}
37+
try {
38+
MAPPER.readValue("{}", BadBean2.class);
39+
fail("Should not pass");
40+
} catch (InvalidDefinitionException e) {
41+
verifyException(e, "Duplicate injectable value");
42+
}
43+
}
44+
45+
}

src/test/java/com/fasterxml/jackson/databind/deser/TestInjectables.java renamed to src/test/java/com/fasterxml/jackson/databind/deser/inject/TestInjectables.java

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
package com.fasterxml.jackson.databind.deser;
1+
package com.fasterxml.jackson.databind.deser.inject;
22

33
import com.fasterxml.jackson.annotation.*;
44
import com.fasterxml.jackson.databind.*;
5-
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
65

76
public class TestInjectables extends BaseMapTest
87
{
@@ -22,16 +21,6 @@ static class InjectedBean
2221
public void injectThird(long v) {
2322
third = v;
2423
}
25-
}
26-
27-
static class BadBean1 {
28-
@JacksonInject protected String prop1;
29-
@JacksonInject protected String prop2;
30-
}
31-
32-
static class BadBean2 {
33-
@JacksonInject("x") protected String prop1;
34-
@JacksonInject("x") protected String prop2;
3524
}
3625

3726
static class CtorBean {
@@ -94,7 +83,7 @@ static class TransientBean {
9483

9584
public int value;
9685
}
97-
86+
9887
/*
9988
/**********************************************************
10089
/* Unit tests
@@ -128,7 +117,6 @@ public void testWithCtors() throws Exception
128117
assertEquals("Bubba", bean.name);
129118
}
130119

131-
// [Issue-13]
132120
public void testTwoInjectablesViaCreator() throws Exception
133121
{
134122
CtorBean2 bean = MAPPER.readerFor(CtorBean2.class)
@@ -140,19 +128,6 @@ public void testTwoInjectablesViaCreator() throws Exception
140128
assertEquals("Bob", bean.name);
141129
}
142130

143-
public void testInvalidDup() throws Exception
144-
{
145-
try {
146-
MAPPER.readValue("{}", BadBean1.class);
147-
} catch (InvalidDefinitionException e) {
148-
verifyException(e, "Duplicate injectable value");
149-
}
150-
try {
151-
MAPPER.readValue("{}", BadBean2.class);
152-
} catch (InvalidDefinitionException e) {
153-
verifyException(e, "Duplicate injectable value");
154-
}
155-
}
156131

157132
public void testIssueGH471() throws Exception
158133
{

0 commit comments

Comments
 (0)