diff --git a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/SimpleClassNameIdResolver.java b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/SimpleNameIdResolver.java similarity index 88% rename from src/main/java/com/fasterxml/jackson/databind/jsontype/impl/SimpleClassNameIdResolver.java rename to src/main/java/com/fasterxml/jackson/databind/jsontype/impl/SimpleNameIdResolver.java index e7852d9ae1..88c2a8f3fd 100644 --- a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/SimpleClassNameIdResolver.java +++ b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/SimpleNameIdResolver.java @@ -16,13 +16,10 @@ /** * {@link com.fasterxml.jackson.databind.jsontype.TypeIdResolver} implementation * that converts between (JSON) Strings and simple Java class names via {@link Class#getSimpleName()}. - *

- * Note that this implementation is identical to {@link TypeNameIdResolver} except that instead of - * {@link TypeNameIdResolver#_defaultTypeId(Class)}, this implementation uses {@link Class#getSimpleName()}. * * @since 2.16 */ -public class SimpleClassNameIdResolver +public class SimpleNameIdResolver extends TypeIdResolverBase { protected final MapperConfig _config; @@ -42,12 +39,9 @@ public class SimpleClassNameIdResolver */ protected final Map _idToType; - /** - * @since 2.11 - */ protected final boolean _caseInsensitive; - protected SimpleClassNameIdResolver(MapperConfig config, JavaType baseType, + protected SimpleNameIdResolver(MapperConfig config, JavaType baseType, ConcurrentHashMap typeToId, HashMap idToType) { @@ -58,7 +52,7 @@ protected SimpleClassNameIdResolver(MapperConfig config, JavaType baseType, _caseInsensitive = config.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_VALUES); } - public static SimpleClassNameIdResolver construct(MapperConfig config, JavaType baseType, + public static SimpleNameIdResolver construct(MapperConfig config, JavaType baseType, Collection subtypes, boolean forSer, boolean forDeser) { // sanity check @@ -86,7 +80,7 @@ public static SimpleClassNameIdResolver construct(MapperConfig config, JavaTy // no name? Need to figure out default; for now, let's just // use non-qualified class name Class cls = t.getType(); - String id = t.hasName() ? t.getName() : cls.getSimpleName(); // not {@code _defaultTypeId(cls);} + String id = t.hasName() ? t.getName() : _defaultTypeId(cls); if (forSer) { typeToId.put(cls.getName(), id); } @@ -107,11 +101,11 @@ public static SimpleClassNameIdResolver construct(MapperConfig config, JavaTy } } } - return new SimpleClassNameIdResolver(config, baseType, typeToId, idToType); + return new SimpleNameIdResolver(config, baseType, typeToId, idToType); } @Override - public JsonTypeInfo.Id getMechanism() { return JsonTypeInfo.Id.SIMPLE_CLASS_NAME; } + public JsonTypeInfo.Id getMechanism() { return JsonTypeInfo.Id.SIMPLE_NAME; } @Override public String idFromValue(Object value) { @@ -140,7 +134,7 @@ protected String idFromClass(Class clazz) } if (name == null) { // And if still not found, let's choose default? - name = cls.getSimpleName(); + name = _defaultTypeId(cls); } _typeToId.put(key, name); } @@ -191,4 +185,21 @@ public String getDescForKnownTypeIds() { public String toString() { return String.format("[%s; id-to-type=%s]", getClass().getName(), _idToType); } + + /* + /********************************************************* + /* Helper methods + /********************************************************* + */ + + /** + * If no name was explicitly given for a class, we will just + * use simple class name + */ + protected static String _defaultTypeId(Class cls) + { + String n = cls.getName(); + int ix = Math.max(n.lastIndexOf('.'), n.lastIndexOf('$')); + return (ix < 0) ? n : n.substring(ix+1); + } } diff --git a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/StdTypeResolverBuilder.java b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/StdTypeResolverBuilder.java index 569359c0dd..ed6ff31fd9 100644 --- a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/StdTypeResolverBuilder.java +++ b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/StdTypeResolverBuilder.java @@ -368,8 +368,8 @@ protected TypeIdResolver idResolver(MapperConfig config, return ClassNameIdResolver.construct(baseType, config, subtypeValidator); case MINIMAL_CLASS: return MinimalClassNameIdResolver.construct(baseType, config, subtypeValidator); - case SIMPLE_CLASS_NAME: - return SimpleClassNameIdResolver.construct(config, baseType, subtypes, forSer, forDeser); + case SIMPLE_NAME: + return SimpleNameIdResolver.construct(config, baseType, subtypes, forSer, forDeser); case NAME: return TypeNameIdResolver.construct(config, baseType, subtypes, forSer, forDeser); case NONE: // hmmh. should never get this far with 'none' diff --git a/src/test/java/com/fasterxml/jackson/databind/jsontype/JsonTypeInfoSimpleClassName4061Test.java b/src/test/java/com/fasterxml/jackson/databind/jsontype/JsonTypeInfoSimpleClassName4061Test.java index a3195c6ce0..95bd7c5d49 100644 --- a/src/test/java/com/fasterxml/jackson/databind/jsontype/JsonTypeInfoSimpleClassName4061Test.java +++ b/src/test/java/com/fasterxml/jackson/databind/jsontype/JsonTypeInfoSimpleClassName4061Test.java @@ -14,7 +14,7 @@ public class JsonTypeInfoSimpleClassName4061Test extends BaseMapTest { @JsonTypeInfo( - use = JsonTypeInfo.Id.SIMPLE_CLASS_NAME) + use = JsonTypeInfo.Id.SIMPLE_NAME) @JsonSubTypes({ @JsonSubTypes.Type(value = InnerSub4061A.class), @JsonSubTypes.Type(value = InnerSub4061B.class) @@ -38,7 +38,7 @@ static class MinimalInnerSub4061A extends MinimalInnerSuper4061 { } static class MinimalInnerSub4061B extends MinimalInnerSuper4061 { } @JsonTypeInfo( - use = JsonTypeInfo.Id.SIMPLE_CLASS_NAME) + use = JsonTypeInfo.Id.SIMPLE_NAME) @JsonSubTypes({ @JsonSubTypes.Type(value = MixedSub4061A.class), @JsonSubTypes.Type(value = MixedSub4061B.class) @@ -131,7 +131,7 @@ public void testMixedMinimalClass() throws Exception } @JsonTypeInfo( - use = JsonTypeInfo.Id.SIMPLE_CLASS_NAME) + use = JsonTypeInfo.Id.SIMPLE_NAME) @JsonSubTypes({ @JsonSubTypes.Type(value = BasicSub4061A.class), @JsonSubTypes.Type(value = BasicSub4061B.class)