Package com.addthis.codec.reflection

Examples of com.addthis.codec.reflection.CodableClassInfo


        }
    }

    // like the one in Fields.java, but non-static and using a possibly non-default registry
    CodableClassInfo getOrCreateClassInfo(Class<?> clazz) {
        CodableClassInfo fieldMap = fieldMaps.get(clazz);
        if (fieldMap == null) {
            fieldMap = new CodableClassInfo(clazz, globalConfig, pluginRegistry);
            fieldMaps.put(clazz, fieldMap);
        }
        return fieldMap;
    }
View Full Code Here


        if ((type != null) && type.isAssignableFrom(ConfigValue.class)) {
            return ConfigValueFactory.fromAnyRef(config.unwrapped(),
                                                 "unchanged for raw ConfigValue field "
                                                 + config.origin().description());
        }
        CodableClassInfo typeInfo = codec.getOrCreateClassInfo(type);
        PluginMap pluginMap = typeInfo.getPluginMap();
        ConfigValue valueOrResolvedRoot = resolveType(type, config, pluginMap);
        if (valueOrResolvedRoot.valueType() != ConfigValueType.OBJECT) {
            return valueOrResolvedRoot;
        }
        ConfigObject root = (ConfigObject) valueOrResolvedRoot;
View Full Code Here

        }
        return expandSugarSkipResolve(type, root, codec);
    }

    private static ConfigObject expandSugarSkipResolve(Class<?> type, ConfigObject root, CodecConfig codec) {
        CodableClassInfo resolvedTypeInfo = codec.getOrCreateClassInfo(type);
        ConfigObject fieldDefaults = resolvedTypeInfo.getFieldDefaults().root();
        for (CodableFieldInfo fieldInfo : resolvedTypeInfo.values()) {
            String fieldName = fieldInfo.getName();
            ConfigValue fieldValue = root.get(fieldName);
            if ((fieldValue == null) && (fieldDefaults.get(fieldName) != null)) {
                ConfigValue fieldDefault = fieldDefaults.get(fieldName);
                fieldValue = ConfigValueFactory.fromAnyRef(
View Full Code Here

        }
        try {
            if (object instanceof SuperCodable) {
                ((SuperCodable) object).preEncode();
            }
            CodableClassInfo classInfo = Fields.getClassFieldMap(object.getClass());
            if (classInfo.size() == 0 && !(object instanceof Codable)) {
                return object;
            }
            obj = new JSONObject();
            String altType = classInfo.getClassName(object);
            if (altType != null) {
                obj.put(classInfo.getClassField(), altType);
            }
            for (Iterator<CodableFieldInfo> fields = classInfo.values().iterator(); fields.hasNext();) {
                CodableFieldInfo field = fields.next();
                Object value = field.get(object);
                if (value == null || value == JSONObject.NULL || field.isReadOnly()) {
                    continue;
                }
View Full Code Here

            } catch (NoSuchMethodException | IllegalAccessException ex) {
                throw new IllegalStateException("Attempted to decode enum type", ex);
            }
            return (T) json;
        }
        CodableClassInfo classInfo = Fields.getClassFieldMap(type);

        // json config is "unexpectedly" an array; if the base class has registered a handler, use it
        if (json instanceof JSONArray) {
            Class<?> arrarySugar = classInfo.getArraySugar();
            if (arrarySugar != null) {
                LineNumberInfo infoCopy = ((JSONArray) json).getMyLineNumberInfo();
                JSONObject magicWrapper = new JSONObject();
                magicWrapper.put(classInfo.getPluginMap().aliasDefaults("_array").toConfig().getString("_primary"), json, infoCopy, infoCopy);
                classInfo = Fields.getClassFieldMap(arrarySugar);
                json = magicWrapper;
                type = (Class<T>) arrarySugar;
            }
        }
        if (!(json instanceof JSONObject)) {
            return (T) json;
        }
        JSONObject jsonObj = (JSONObject) json;

        if (info == LineNumberInfo.MissingInfo) {
            info = jsonObj.getLineNumberInfo();
        }

        String classField = classInfo.getClassField();
        String stype = jsonObj.optString(classField, null);
        if ((stype == null) && Modifier.isAbstract(type.getModifiers()) &&
            (jsonObj.length() == 1)) {
            // if otherwise doomed to fail, try supporting "type-value : {...}"  syntax
            stype = jsonObj.keySet().iterator().next();
            jsonObj = jsonObj.getJSONObject(stype);
        }
        try {
            if (stype != null) {
                Class<?> atype = classInfo.getClass(stype);
                classInfo = Fields.getClassFieldMap(atype);
                type = (Class<T>) atype;
                jsonObj.remove(classField);
            }
        } catch (ClassNotFoundException ex) {
            String helpMessage = Plugins.classNameSuggestions(
                    PluginRegistry.defaultRegistry(), classInfo.getPluginMap(), stype);
            throw new CodecExceptionLineNumber(new ClassNotFoundException(helpMessage, ex),
                                               jsonObj.getValLineNumber(classField));
        } catch (Exception ex) {
            throw new CodecExceptionLineNumber(ex, jsonObj.getValLineNumber(classField));
        }
View Full Code Here

    /**
     * Construct an object of the requested type based on the default values and types (if the requested
     * is not a concrete class).
     */
    public <T> T newDefault(@Nonnull Class<T> type) {
        CodableClassInfo classInfo = getOrCreateClassInfo(type);
        return hydrateObject(classInfo, classInfo.getPluginMap(), type, ConfigFactory.empty().root());
    }
View Full Code Here

     * Instantiate an object of the requested type based on the provided config. The config should only contain
     * field and type information for the object to be constructed. Global defaults, plugin configuration, etc, are
     * provided by this CodecConfig instance's globalConfig and pluginRegistry fields.
     */
    public <T> T decodeObject(@Nonnull Class<T> type, @Nonnull Config config) {
        CodableClassInfo classInfo = getOrCreateClassInfo(type);
        return hydrateObject(classInfo, classInfo.getPluginMap(), type, config.root());
    }
View Full Code Here

        }
    }

    /** called when the expected type is a codable object, but we know very little about it */
    private <T> T hydrateObject(Class<T> type, ConfigValue configValue) {
        CodableClassInfo info = getOrCreateClassInfo(type);
        PluginMap pluginMap = info.getPluginMap();
        return hydrateValueAsObject(info, pluginMap, type, configValue);
    }
View Full Code Here

                    Class<T> arrayType = (Class<T>) pluginMap.arraySugar();
                    if (arrayType != null) {
                        Config aliasDefaults = pluginMap.aliasDefaults("_array").toConfig();
                        ConfigObject fieldsValues = configValue.atPath(aliasDefaults.getString("_primary")).root()
                                                               .withFallback(aliasDefaults);
                        CodableClassInfo arrayInfo = getOrCreateClassInfo(arrayType);
                        return createAndPopulate(arrayInfo, arrayType, fieldsValues);
                    } // else just let the error get thrown below
                }
            } else {

                /* for non-pluggable ValueCodable implementors */
                if (ValueCodable.class.isAssignableFrom(type)) {
                    try {
                        T objectShell = type.newInstance();
                        CodableClassInfo configInfo = (info != null) ? info : getOrCreateClassInfo(type);
                        Config fieldDefaults = configInfo.getFieldDefaults();
                        ((ValueCodable) objectShell).fromConfigValue(configValue, fieldDefaults.root());
                        if (objectShell instanceof SuperCodable) {
                            ((SuperCodable) objectShell).postDecode();
                        }
                        return objectShell;
View Full Code Here

        /* "type-value : {...}"  syntax; ie. if there is only one key, see if it would be a valid type */
        if (configObject.size() == 1) {
            String singleKeyName = configObject.keySet().iterator().next();
            try {
                Class<T> singleKeyType = (Class<T>) pluginMap.getClass(singleKeyName);
                CodableClassInfo singleKeyInfo = getOrCreateClassInfo(singleKeyType);
                ConfigObject aliasDefaults = pluginMap.aliasDefaults(singleKeyName);
                ConfigValue configValue = configObject.get(singleKeyName);
                if (configValue.valueType() != ConfigValueType.OBJECT) {
                    if (aliasDefaults.get("_primary") != null) {
                        // if value is not an object, try supporting _primary syntax to derive one
                        configValue = configValue.atPath((String) aliasDefaults.get("_primary").unwrapped()).root();
                    } else if (ValueCodable.class.isAssignableFrom(singleKeyType)) {
                        // see if the resolved type is innately okay with non-objects
                        try {
                            T objectShell = singleKeyType.newInstance();
                            Config fieldDefaults = singleKeyInfo.getFieldDefaults();
                            // do not merge objects between global defaults and user defaults (incl. alias defaults)
                            ConfigObject mergedDefaults = aliasDefaults;
                            for (Map.Entry<String, ConfigValue> pair : fieldDefaults.entrySet()) {
                                if (!mergedDefaults.containsKey(pair.getKey())) {
                                    mergedDefaults = mergedDefaults.withValue(pair.getKey(), pair.getValue());
View Full Code Here

TOP

Related Classes of com.addthis.codec.reflection.CodableClassInfo

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.