Package com.addthis.codec.reflection

Examples of com.addthis.codec.reflection.CodableClassInfo


    /**
     * Instantiate an object of the requested type using the provided config.
     */
    public <T> T decodeObject(Class<T> type, 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 non-standard object */
    private <T> T hydrateObject(Class<T> type, ConfigValue configValue) {
        CodableClassInfo info = getOrCreateClassInfo(type);
        PluginMap pluginMap = info.getPluginMap();
        return hydrateObject(info, pluginMap, type, configValue);
    }
View Full Code Here

        }
    }

    // like the one in Fields.java, but non-static and using a possibly non-default registry
    private CodableClassInfo getOrCreateClassInfo(Class<?> clazz) {
        CodableClassInfo fieldMap = fieldMaps.get(clazz);
        if (fieldMap == null) {
            fieldMap = new CodableClassInfo(clazz, pluginRegistry);
            fieldMaps.put(clazz, fieldMap);
        }
        return fieldMap;
    }
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

            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

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.