Search in sources :

Example 1 with DefaultValue

use of act.inject.DefaultValue in project actframework by actframework.

the class ActionContextParamLoader method findContextSpecificLoader.

@Override
protected ParamValueLoader findContextSpecificLoader(String bindName, BeanSpec spec) {
    HeaderVariable headerVariable = spec.getAnnotation(HeaderVariable.class);
    if (null != headerVariable) {
        return new HeaderValueLoader(headerVariable.value(), spec);
    }
    SessionVariable sessionVariable = spec.getAnnotation(SessionVariable.class);
    if (null != sessionVariable) {
        return new SessionValueLoader(sessionVariable.value(), spec);
    }
    if (spec.hasAnnotation(PartialPath.class)) {
        return new PartialPathLoader(bindName);
    }
    DefaultValue def = spec.getAnnotation(DefaultValue.class);
    Class<?> rawType = spec.rawType();
    ParamValueLoader loader = binder(spec, bindName);
    if (null == loader) {
        Resolve resolve = spec.getAnnotation(Resolve.class);
        if (null != resolve) {
            Class<? extends StringValueResolver>[] resolvers = resolve.value();
            for (Class<? extends StringValueResolver> resolverClass : resolvers) {
                StringValueResolver resolver = injector.get(resolverClass);
                if (rawType.isAssignableFrom(resolver.targetType())) {
                    loader = new StringValueResolverValueLoader(ParamKey.of(bindName), resolver, null, def, rawType);
                }
            }
        }
    }
    if (null == loader) {
        Annotation[] aa = spec.allAnnotations();
        for (Annotation a : aa) {
            Resolve resolve = AnnotationUtil.tagAnnotation(a, Resolve.class);
            if (null != resolve) {
                Class<? extends StringValueResolver>[] resolvers = resolve.value();
                for (Class<? extends StringValueResolver> resolverClass : resolvers) {
                    StringValueResolver resolver = injector.get(resolverClass);
                    resolver.attributes($.evaluate(a));
                    if (rawType.isAssignableFrom(resolver.targetType())) {
                        loader = new StringValueResolverValueLoader(ParamKey.of(bindName), resolver, null, def, rawType);
                        break;
                    }
                }
            }
        }
    }
    if (null == loader) {
        StringValueResolver resolver = null;
        Param param = spec.getAnnotation(Param.class);
        if (null != param) {
            Class<? extends StringValueResolver> resolverClass = param.resolverClass();
            if (Param.DEFAULT_RESOLVER.class != resolverClass) {
                resolver = injector.get(resolverClass);
            }
        }
        if (null == resolver) {
            resolver = resolverManager.resolver(rawType, spec);
        }
        loader = (null != resolver) ? new StringValueResolverValueLoader(ParamKey.of(bindName), resolver, param, def, rawType) : buildLoader(ParamKey.of(bindName), spec);
    }
    return loader;
}
Also used : HeaderVariable(act.inject.HeaderVariable) SessionVariable(act.inject.SessionVariable) Resolve(org.osgl.mvc.annotation.Resolve) Annotation(java.lang.annotation.Annotation) StringValueResolver(org.osgl.util.StringValueResolver) DefaultValue(act.inject.DefaultValue) Param(org.osgl.mvc.annotation.Param)

Example 2 with DefaultValue

use of act.inject.DefaultValue in project actframework by actframework.

the class ParamValueLoaderService method findLoader.

private ParamValueLoader findLoader(ParamKey paramKey, BeanSpec spec) {
    if (provided(spec, injector)) {
        return ProvidedValueLoader.get(spec, injector);
    }
    String name = spec.name();
    ParamKey key = paramKey.child(name);
    ParamValueLoader loader = binder(spec, key.toString());
    if (null != loader) {
        return loader;
    }
    Class fieldType = spec.rawType();
    StringValueResolver resolver = resolverManager.resolver(fieldType, spec);
    if (null != resolver) {
        DefaultValue def = spec.getAnnotation(DefaultValue.class);
        return new StringValueResolverValueLoader(key, resolver, null, def, fieldType);
    }
    return buildLoader(key, spec);
}
Also used : DefaultValue(act.inject.DefaultValue)

Example 3 with DefaultValue

use of act.inject.DefaultValue 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)3 DependencyInjector (act.inject.DependencyInjector)1 HeaderVariable (act.inject.HeaderVariable)1 SessionVariable (act.inject.SessionVariable)1 Annotation (java.lang.annotation.Annotation)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 Param (org.osgl.mvc.annotation.Param)1 Resolve (org.osgl.mvc.annotation.Resolve)1 Const (org.osgl.util.Const)1 StringValueResolver (org.osgl.util.StringValueResolver)1