Search in sources :

Example 1 with Const

use of org.osgl.util.Const in project actframework by actframework.

the class ConfigurationByteCodeScanner method scanFinished.

@Override
public void scanFinished(final String className) {
    E.unexpectedIf(S.neq(this.className, className), "oops");
    if (staticConfigurationFields.isEmpty()) {
        return;
    }
    final DependencyInjector injector = app().injector();
    final Map<String, String> staticConfigurationFields = new HashMap<>(this.staticConfigurationFields);
    app().jobManager().on(SysEventId.PRE_START, new Runnable() {

        @Override
        public void run() {
            Class<?> theClass = app().classForName(className);
            for (Map.Entry<String, String> entry : staticConfigurationFields.entrySet()) {
                String fieldName = entry.getKey();
                String conf = entry.getValue();
                Field field = $.fieldOf(theClass, fieldName, false);
                DefaultValue defaultValue = field.getAnnotation(DefaultValue.class);
                field.setAccessible(true);
                Class<?> fieldType = field.getType();
                boolean isConst = false;
                boolean isVal = false;
                BeanSpec valueSpec = BeanSpec.of(field, injector);
                if (Const.class.isAssignableFrom(fieldType)) {
                    valueSpec = BeanSpec.of(field, injector).componentSpec();
                    isConst = true;
                } else if ($.Val.class.isAssignableFrom(fieldType)) {
                    valueSpec = BeanSpec.of(field, injector).componentSpec();
                    isVal = true;
                }
                ConfigurationValueLoader loader = app().getInstance(ConfigurationValueLoader.class);
                Map<String, String> map = C.newMap("value", conf);
                if (null != defaultValue) {
                    map.put("defaultValue", defaultValue.value());
                }
                loader.init(map, valueSpec);
                Object value = loader.get();
                try {
                    if (isConst) {
                        Const<?> fieldValue = $.cast(field.get(null));
                        if (null == fieldValue) {
                            fieldValue = $.constant(value);
                            field.set(null, fieldValue);
                        } else {
                            Field fv = Const.class.getDeclaredField("v");
                            fv.setAccessible(true);
                            fv.set(fieldValue, value);
                        }
                    } else if (isVal) {
                        $.Val<?> fieldValue = $.cast(field.get(null));
                        if (null == fieldValue) {
                            fieldValue = $.val(value);
                            field.set(null, fieldValue);
                        } else {
                            Field fv = $.Var.class.getDeclaredField("v");
                            fv.setAccessible(true);
                            fv.set(fieldValue, value);
                        }
                    } else {
                        field.set(null, value);
                    }
                } catch (Exception e) {
                    throw E.unexpected(e, "failed to set configuration value[%] to field[%s]", value, field);
                }
            }
        }
    });
}
Also used : DependencyInjector(act.inject.DependencyInjector) org.osgl.$(org.osgl.$) HashMap(java.util.HashMap) Const(org.osgl.util.Const) ConfigurationValueLoader(org.osgl.inject.loader.ConfigurationValueLoader) Field(java.lang.reflect.Field) DefaultValue(act.inject.DefaultValue) BeanSpec(org.osgl.inject.BeanSpec) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

DefaultValue (act.inject.DefaultValue)1 DependencyInjector (act.inject.DependencyInjector)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 org.osgl.$ (org.osgl.$)1 BeanSpec (org.osgl.inject.BeanSpec)1 ConfigurationValueLoader (org.osgl.inject.loader.ConfigurationValueLoader)1 Const (org.osgl.util.Const)1