public static void addSimpleClassNames(final boolean includeSubtypes, final Class... classes) { SimpleClassNameCache.addSimpleClassNames(includeSubtypes, classes); }
/** * Allows you to add additional classes to the cache that you wish to be * accessed via the simple class name. * * @param includeSubtypes if true then all implementations are included. * @param classes the classes to be included. */ public static void addSimpleClassNames(final boolean includeSubtypes, final Class... classes) { initialise(); for (final Class clazz : classes) { final Set<Class> existingClasses = getClassesFromId(clazz.getSimpleName()); if (null == existingClasses || !existingClasses.contains(clazz)) { if (includeSubtypes) { addSimpleClassNames(idToClasses, clazz); } else { addSimpleClassName(idToClasses, clazz); } } } }
String className = null; if (null != id && !id.contains(".")) { initialise(); nonArrayId = StringUtils.capitalize(nonArrayId); final Set<Class> classesForId = getClassesFromId(nonArrayId); if (null != clazz) { className = classNameTmp; addIdClasses(nonArrayId, Sets.newHashSet(clazz)); break; final Class clazz = getClassFromId(nonArrayId, idToClass); if (null != clazz) { className = clazz.getName(); baseTypeToIdToClass.put(baseClass, idToClass); addIdClass(nonArrayId, matchedClass, idToClass);
private static Map<String, Set<Class>> createIdToClasses() { final Map<String, Set<Class>> map = new ConcurrentHashMap<>(); for (final Class baseClass : baseClasses) { addSimpleClassNames(map, baseClass); } for (final Class clazz : ReflectionUtil.getAnnotatedTypes(JsonSimpleClassName.class)) { addSimpleClassName(map, clazz); } return map; }
id = null; } else { initialise(); id = nonArrayClass.getSimpleName(); final Set<Class> classesForId = getClassesFromId(id); if (null == classesForId || classesForId.isEmpty()) { if (null != classPackage && corePackages.contains(classPackage.getName())) { addIdClasses(id, Sets.newHashSet(nonArrayClass)); } else { id = null;
public static void initialise() { if (!initialised) { baseClasses = createParentClasses(); idToClasses = createIdToClasses(); initialised = true; } }
/** * Gets the class name for a given id. * * @param id the simple class name or full class name. * @return the full class name or the original id if one could not be found. * @throws IllegalArgumentException if there are multiple classes with the same id. */ public static String getClassName(final String id) { return SimpleClassNameCache.getClassName(id); }
/** * Gets a simple class name for the given class. * * @param clazz the class to get the simple class name for. * @return the simple class name or the full class name if the clazz is * unknown or a conflict is found. */ public static String getSimpleClassName(final Class<?> clazz) { return SimpleClassNameCache.getSimpleClassName(clazz); }
/** * Resets the caches. */ public static void reset() { baseClasses = createParentClasses(); idToClasses = createIdToClasses(); corePackages = new LinkedHashSet<>(DEFAULT_CORE_PACKAGES); baseTypeToIdToClass = new ConcurrentHashMap<>(); }
/** * Gets the class name for a given id. * * @param id the simple class name or full class name. * @return the full class name or the original id if one could not be found. * @throws IllegalArgumentException if there are multiple classes with the same id. */ public static String getClassName(final String id) { return getClassName(id, null); }
@Override public void serialize(final Class<?> value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonGenerationException { jgen.writeString(SimpleClassNameCache.getSimpleClassName(value)); } }
public static void addSimpleClassNames(final boolean includeSubtypes, final Class... classes) { SimpleClassNameCache.addSimpleClassNames(includeSubtypes, classes); }
/** * Gets the class name for a given id. * * @param id the simple class name or full class name. * @param baseType the base type of the ID - used to help resolve conflicting IDs * @return the full class name or the original id if one could not be found. * @throws IllegalArgumentException if there are multiple classes with the same id. */ public static String getClassName(final String id, final JavaType baseType) { return SimpleClassNameCache.getClassName(id, baseType); }
@Override public void serialize(final Object value, final JsonGenerator g, final SerializerProvider provider) throws IOException { String str; Class<?> cls = value.getClass(); if (cls == String.class) { str = (String) value; } else if (cls.isEnum()) { // 24-Sep-2015, tatu: Minor improvement over older (2.6.2 and before) code: at least // use name/toString() variation for as per configuration Enum<?> en = (Enum<?>) value; if (provider.isEnabled(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)) { str = en.toString(); } else { str = en.name(); } } else if (value instanceof Date) { provider.defaultSerializeDateKey((Date) value, g); return; } else if (cls == Class.class) { str = SimpleClassNameCache.getSimpleClassName(((Class<?>) value)); } else { str = value.toString(); } g.writeFieldName(str); } }