Skip to content

Commit be2a3e6

Browse files
committed
Merge branch '2.10' into 2.11
2 parents 8ac584f + 85e7a39 commit be2a3e6

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

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

+29
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,33 @@ public Class<?> findMixInClassFor(Class<?> cls)
9999
public int localSize() {
100100
return (_localMixIns == null) ? 0 : _localMixIns.size();
101101
}
102+
103+
/**
104+
* Method that may be called for optimization purposes, to see if calls to
105+
* mix-in resolver may be avoided. Return value of {@code true} means that
106+
* it is possible that a mix-in class will be found; {@code false} that no
107+
* mix-in will ever be found. In latter case caller can avoid calls altogether.
108+
*<p>
109+
* Note that the reason for "empty" resolvers is to use "null object" for simplifying
110+
* code.
111+
*
112+
* @return True, if this resolver MAY have mix-ins to apply; false if not (it
113+
* is "empty")
114+
*
115+
* @since 2.10.1
116+
*/
117+
public boolean hasMixIns() {
118+
if (_localMixIns == null) {
119+
// if neither local mix-ins nor overrides, no mix-ins
120+
if (_overrides == null) {
121+
return false;
122+
}
123+
// or, if no local mix-ins and can delegate to resolver
124+
if (_overrides instanceof SimpleMixInResolver) {
125+
return ((SimpleMixInResolver) _overrides).hasMixIns();
126+
}
127+
}
128+
// cannot rule out the possibility, so...
129+
return true;
130+
}
102131
}

src/test/java/com/fasterxml/jackson/databind/mixins/TestMixinSerForMethods.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fasterxml.jackson.annotation.*;
77
import com.fasterxml.jackson.databind.*;
88
import com.fasterxml.jackson.databind.introspect.ClassIntrospector;
9+
import com.fasterxml.jackson.databind.introspect.SimpleMixInResolver;
910
import com.fasterxml.jackson.databind.introspect.ClassIntrospector.MixInResolver;
1011

1112
public class TestMixinSerForMethods
@@ -147,11 +148,18 @@ public void testIntermediateMixin2() throws IOException
147148
assertEquals(Integer.valueOf(42), result.get("x"));
148149
}
149150

151+
public void testSimpleMixInResolverHasMixins() {
152+
SimpleMixInResolver simple = new SimpleMixInResolver(null);
153+
assertFalse(simple.hasMixIns());
154+
simple.addLocalDefinition(String.class, Number.class);
155+
assertTrue(simple.hasMixIns());
156+
}
157+
150158
// [databind#688]
151159
public void testCustomResolver() throws IOException
152160
{
153161
ObjectMapper mapper = new ObjectMapper();
154-
mapper.setMixInResolver(new ClassIntrospector.MixInResolver() {
162+
MixInResolver res = new ClassIntrospector.MixInResolver() {
155163
@Override
156164
public Class<?> findMixInClassFor(Class<?> target) {
157165
if (target == EmptyBean.class) {
@@ -164,9 +172,13 @@ public Class<?> findMixInClassFor(Class<?> target) {
164172
public MixInResolver copy() {
165173
return this;
166174
}
167-
});
175+
};
176+
mapper.setMixInResolver(res);
168177
Map<String,Object> result = writeAndMap(mapper, new SimpleBean());
169178
assertEquals(1, result.size());
170179
assertEquals(Integer.valueOf(42), result.get("x"));
180+
181+
SimpleMixInResolver simple = new SimpleMixInResolver(res);
182+
assertTrue(simple.hasMixIns());
171183
}
172184
}

0 commit comments

Comments
 (0)