Search in sources :

Example 6 with BeanSpec

use of org.osgl.inject.BeanSpec in project actframework by actframework.

the class JsonDTOClassManager method extractBeanSpec.

private void extractBeanSpec(List<BeanSpec> beanSpecs, Method method, Class host) {
    Type[] paramTypes = method.getGenericParameterTypes();
    int sz = paramTypes.length;
    if (0 == sz) {
        return;
    }
    Annotation[][] annotations = method.getParameterAnnotations();
    for (int i = 0; i < sz; ++i) {
        Type type = paramTypes[i];
        if (type instanceof TypeVariable && !Modifier.isStatic(method.getModifiers())) {
            // explore type variable impl
            TypeVariable typeVar = $.cast(type);
            String typeVarName = typeVar.getName();
            // find all generic types on host
            Map<String, Class> typeVarLookup = Generics.buildTypeParamImplLookup(host);
            type = typeVarLookup.get(typeVarName);
            if (null == type) {
                throw new UnexpectedException("Cannot determine concrete type of method parameter %s", typeVarName);
            }
        }
        Annotation[] anno = annotations[i];
        BeanSpec spec = BeanSpec.of(type, anno, injector);
        if (ParamValueLoaderService.providedButNotDbBind(spec, injector)) {
            continue;
        }
        String dbBindName = dbBindName(spec);
        if (null != dbBindName) {
            beanSpecs.add(BeanSpec.of(String.class, new Annotation[0], dbBindName, injector));
        } else {
            beanSpecs.add(spec);
        }
    }
}
Also used : UnexpectedException(org.osgl.exception.UnexpectedException) BeanSpec(org.osgl.inject.BeanSpec) Annotation(java.lang.annotation.Annotation)

Example 7 with BeanSpec

use of org.osgl.inject.BeanSpec in project actframework by actframework.

the class ParamValueLoaderService method fieldLoaders.

private <T> Map<Field, ParamValueLoader> fieldLoaders(Class<T> beanClass) {
    Map<Field, ParamValueLoader> fieldLoaders = fieldRegistry.get(beanClass);
    if (null == fieldLoaders) {
        Map<Field, ParamValueLoader> newFieldLoaders = new HashMap<>();
        for (Field field : $.fieldsOf(beanClass, true)) {
            if (shouldWaive(field)) {
                continue;
            }
            BeanSpec spec = BeanSpec.of(field, injector);
            ParamValueLoader loader = paramValueLoaderOf(spec, null);
            boolean provided = (loader instanceof ProvidedValueLoader);
            if (null != loader && !provided) {
                newFieldLoaders.put(field, loader);
            }
        }
        fieldLoaders = fieldRegistry.putIfAbsent(beanClass, newFieldLoaders);
        if (null == fieldLoaders) {
            fieldLoaders = newFieldLoaders;
        }
    }
    return fieldLoaders;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BeanSpec(org.osgl.inject.BeanSpec)

Example 8 with BeanSpec

use of org.osgl.inject.BeanSpec 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)

Example 9 with BeanSpec

use of org.osgl.inject.BeanSpec in project actframework by actframework.

the class Endpoint method generateSampleQuery.

private String generateSampleQuery(BeanSpec spec, String bindName, Set<Type> typeChain, List<String> nameChain) {
    Class<?> type = spec.rawType();
    String specName = spec.name();
    if (S.notBlank(specName)) {
        nameChain.add(specName);
    }
    if ($.isSimpleType(type)) {
        Object o = generateSampleData(spec, typeChain, nameChain);
        if (null == o) {
            return "";
        }
        return bindName + "=" + o;
    }
    if (type.isArray()) {
        // TODO handle datetime component type
        Class<?> elementType = type.getComponentType();
        BeanSpec elementSpec = BeanSpec.of(elementType, Act.injector());
        if ($.isSimpleType(elementType)) {
            Object o = generateSampleData(elementSpec, typeChain, nameChain);
            if (null == o) {
                return "";
            }
            return bindName + "=" + o + "&" + bindName + "=" + generateSampleData(elementSpec, typeChain, nameChain);
        }
    } else if (Collection.class.isAssignableFrom(type)) {
        // TODO handle datetime component type
        List<Type> typeParams = spec.typeParams();
        Type elementType = typeParams.isEmpty() ? Object.class : typeParams.get(0);
        BeanSpec elementSpec = BeanSpec.of(elementType, null, Act.injector());
        if ($.isSimpleType(elementSpec.rawType())) {
            Object o = generateSampleData(elementSpec, typeChain, nameChain);
            if (null == o) {
                return "";
            }
            return bindName + "=" + o + "&" + bindName + "=" + generateSampleData(elementSpec, typeChain, nameChain);
        }
    } else if (Map.class.isAssignableFrom(type)) {
        LOGGER.warn("Map not supported yet");
        return "";
    } else if (ReadableInstant.class.isAssignableFrom(type)) {
        return bindName + "=<datetime>";
    }
    if (null != stringValueResolver(type)) {
        return bindName + "=" + S.random(5);
    }
    List<String> queryPairs = new ArrayList<>();
    List<Field> fields = $.fieldsOf(type);
    for (Field field : fields) {
        if (ParamValueLoaderService.shouldWaive(field)) {
            continue;
        }
        String fieldBindName = bindName + "." + field.getName();
        String pair = generateSampleQuery(BeanSpec.of(field, Act.injector()), fieldBindName, C.newSet(typeChain), C.newList(nameChain));
        if (S.notBlank(pair)) {
            queryPairs.add(pair);
        }
    }
    return S.join(queryPairs).by("&").get();
}
Also used : BeanSpec(org.osgl.inject.BeanSpec) ISObject(org.osgl.storage.ISObject)

Example 10 with BeanSpec

use of org.osgl.inject.BeanSpec in project actframework by actframework.

the class CliContextParamLoader method findParamOptionLoaders.

private void findParamOptionLoaders(Method m, CommandMethodMetaInfo methodMetaInfo, List<OptionLoader> optionLoaders) {
    Type[] types = m.getGenericParameterTypes();
    int len = types.length;
    if (len == 0) {
        return;
    }
    Annotation[][] allAnnotations = m.getParameterAnnotations();
    for (int i = len - 1; i >= 0; --i) {
        Type type = types[i];
        Annotation[] annotations = allAnnotations[i];
        BeanSpec spec = BeanSpec.of(type, annotations, null, injector);
        String bindName = tryFindBindName(annotations, spec.name());
        if (null == bindName) {
            bindName = methodMetaInfo.param(i).name();
        }
        ParamValueLoader loader = findContextSpecificLoader(bindName, spec);
        if (loader instanceof OptionLoader) {
            optionLoaders.add((OptionLoader) loader);
        } else if (!$.isSimpleType(spec.rawType())) {
        }
    }
}
Also used : BeanSpec(org.osgl.inject.BeanSpec) Annotation(java.lang.annotation.Annotation)

Aggregations

BeanSpec (org.osgl.inject.BeanSpec)16 Annotation (java.lang.annotation.Annotation)4 Test (org.junit.Test)4 Genie (org.osgl.inject.Genie)4 DependencyInjector (act.inject.DependencyInjector)2 UnexpectedException (org.osgl.exception.UnexpectedException)2 App (act.app.App)1 DefaultValue (act.inject.DefaultValue)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Inject (javax.inject.Inject)1 Ignore (org.junit.Ignore)1 org.osgl.$ (org.osgl.$)1 ConfigurationValueLoader (org.osgl.inject.loader.ConfigurationValueLoader)1 ISObject (org.osgl.storage.ISObject)1