Package com.addthis.codec.reflection

Examples of com.addthis.codec.reflection.CodableClassInfo


    public static String encodeString(Object object) throws Exception {
        if (object instanceof SuperCodable) {
            ((SuperCodable) object).preEncode();
        }
        KVPairs kv = new KVPairs();
        CodableClassInfo fieldMap = Fields.getClassFieldMap(object.getClass());
        if (fieldMap.size() == 0) {
            return object.toString();
        }
        String stype = fieldMap.getClassName(object);
        if (stype != null) {
            kv.putValue(fieldMap.getClassField(), stype);
        }
        for (Iterator<CodableFieldInfo> fields = fieldMap.values().iterator(); fields.hasNext(); ) {
            CodableFieldInfo field = fields.next();
            Object value = field.get(object);
            if (value == null) {
                continue;
            }
View Full Code Here


            String stype = (String) typeValue.unwrapped();
            try {
                Class<T> normalType = (Class<T>) pluginMap.getClass(stype);
                ConfigObject aliasDefaults = pluginMap.aliasDefaults(stype);
                ConfigObject fieldValues = configObject.withoutKey(classField).withFallback(aliasDefaults);
                CodableClassInfo normalInfo = getOrCreateClassInfo(normalType);
                return createAndPopulate(normalInfo, normalType, fieldValues);
            } catch (ClassNotFoundException e) {
                String helpMessage = Plugins.classNameSuggestions(pluginRegistry, pluginMap, stype);
                throw new ConfigException.UnresolvedSubstitution(configObject.origin(), helpMessage, e);
            }
        }

        /* if no chance of instantiating current type, try to get a new type from various special syntax/ settings */
        if ((type == null) || Modifier.isAbstract(type.getModifiers()) || Modifier.isInterface(type.getModifiers())) {
            T maybeSingleKey = hydrateSingleKeyObject(pluginMap, configObject);
            if (maybeSingleKey != null) {
                return maybeSingleKey;
            }

            /* inlined types syntax ie "{ type-value: some-value, some-field: some-other-value, ...}".
             * Opt-in is on a per alias basis, and the target type must be unambiguous amongst aliases
             * that have opted in. The recognized alias label is then replaced with the _primary field. */
            String matched = null;
            for (String alias : pluginMap.inlinedAliases()) {
                if (configObject.get(alias) != null) {
                    if (matched != null) {
                        String message = String.format(
                                "no type specified, more than one key, and both %s and %s match for inlined types.",
                                matched, alias);
                        throw new ConfigException.Parse(configObject.origin(), message);
                    }
                    matched = alias;
                }
            }
            if (matched != null) {
                Class<T> inlinedType = (Class<T>) pluginMap.getClassIfConfigured(matched);
                assert inlinedType != null : "matched is always a key from the pluginMap's inlinedAliases set";
                CodableClassInfo inlinedInfo = getOrCreateClassInfo(inlinedType);
                ConfigObject aliasDefaults = pluginMap.aliasDefaults(matched);
                ConfigValue configValue = configObject.get(matched);
                String primaryField = (String) aliasDefaults.get("_primary").unwrapped();
                ConfigObject fieldValues =  configObject.withoutKey(matched).toConfig()
                                                        .withValue(primaryField, configValue).root()
                                                        .withFallback(aliasDefaults);
                return createAndPopulate(inlinedInfo, inlinedType, fieldValues);
            }

            /* lastly, check for a _default type. */
            Class<T> defaultType = (Class<T>) pluginMap.defaultSugar();
            if (defaultType != null) {
                CodableClassInfo defaultInfo = getOrCreateClassInfo(defaultType);
                ConfigObject aliasDefaults = pluginMap.aliasDefaults("_default");
                ConfigObject fieldValues = configObject.withFallback(aliasDefaults);
                return createAndPopulate(defaultInfo, defaultType, fieldValues);
            }

View Full Code Here

            return createAndPopulate(info, type, configObject);
        }
    }

    private <T> T createAndPopulate(Class<T> type, ConfigObject configObject) {
        CodableClassInfo info = getOrCreateClassInfo(type);
        return createAndPopulate(info, type, configObject);
    }
View Full Code Here

                    ConfigObject onlyObject = resolvedConfig.root().withOnlyKey(onlyUnusedKey);
                    if (resolvedConfig.hasPath(primaryFieldName)) {
                        onlyObject = onlyObject.withFallback(
                                resolvedConfig.getValue(primaryFieldName).atKey(onlyUnusedKey));
                    }
                    CodableClassInfo primaryInfo = getOrCreateClassInfo(
                            field.getTypeOrComponentType());
                    PluginMap primaryMap = primaryInfo.getPluginMap();
                    value = hydrateSingleKeyObject(primaryMap, onlyObject);
                    if (value != null) {
                        usedPrimaryField = true;
                    } else {
                        throw new ConfigException.BadPath(config.origin(), "unrecognized key(s) " + unusedKeys.toString());
View Full Code Here

        }
    }

    // like the one in Fields.java, but non-static and using a possibly non-default registry
    public 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

        try {
            if (object instanceof SuperCodable) {
                ((SuperCodable) object).preEncode();
            }
            Class objectClass = object.getClass();
            CodableClassInfo classInfo = Fields.getClassFieldMap(objectClass);
            if (objectClass.isArray()) {
                encodeArray(object, objectClass, buf);
            } else if (classInfo.size() == 0 && !(object instanceof Codable)) {
                encodeNative(object, buf);
            } else {
                buf.out.write(1);
                writeStringHelper(classInfo.getClassName(object), buf.out());
                for (Iterator<CodableFieldInfo> fields = classInfo.values().iterator(); fields.hasNext();) {
                    CodableFieldInfo field = fields.next();
                    long beginSize = buf.out.size();
                    encodeField(field.get(object), field, buf, statistics);
                    if (statistics != null) {
                        long endSize = buf.out.size();
View Full Code Here

            log.trace("decodeObject: " + type + " " + buf);
        }
        if (Fields.isNative(type)) {
            return decodeNative(type, buf);
        } else {
            CodableClassInfo classInfo = Fields.getClassFieldMap(type);
            if (classInfo.size() == 0 && classInfo.getPluginMap() == null) {
                return decodeNative(type, buf);
            }
            return decodeObject(classInfo, null, buf);
        }
    }
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

        if ((type != null) && type.isAssignableFrom(ConfigValue.class)) {
            return ConfigValueFactory.fromAnyRef(config.unwrapped(),
                                                 "unchanged for raw ConfigValue field "
                                                 + config.origin().description());
        }
        CodableClassInfo typeInfo = new CodableClassInfo(type, pluginRegistry.config(), pluginRegistry);
        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

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.