Package com.astamuse.asta4d.data.annotation

Examples of com.astamuse.asta4d.data.annotation.ContextData


    private final static InstanceWireTarget createInstanceTarget(Object instance) throws DataOperationException {
        List<String> reverseTargetScopes = Context.getCurrentThreadContext().getConfiguration().getReverseInjectableScopes();

        InstanceWireTarget target = new InstanceWireTarget();
        Class<?> cls = instance.getClass();
        ContextData cd;

        // at first, retrieve methods information

        // TODO we should use class name to confirm contextdata annotation
        // because they are possibly from different class loader. The problem is
        // whether it is a problem?
        Method[] mtds = cls.getMethods();
        for (Method method : mtds) {
            cd = findAnnotation(method.getAnnotations());
            if (cd != null) {
                // cd = method.getAnnotation(ContextData.class);
                MethodInfo mi = new MethodInfo();

                mi.method = method;

                boolean isGet = false;
                boolean isSet = false;
                if (StringUtils.isEmpty(cd.name())) {
                    String name = method.getName();
                    if (name.startsWith("set")) {
                        name = name.substring(3);
                        isSet = true;
                    } else if (name.startsWith("get")) {
                        name = name.substring(3);
                        isGet = true;
                    } else if (name.startsWith("is")) {
                        name = name.substring(2);
                        isSet = true;
                    } else {

                        switch (method.getParameterTypes().length) {
                        case 0:
                            isGet = true;
                            break;
                        case 1:
                            isSet = true;
                            break;
                        default:
                            String msg = String.format("Method [%s]:[%s] can not be treated as a getter or setter method.", cls.getName(),
                                    method.toGenericString());
                            throw new DataOperationException(msg);
                        }
                    }
                    char[] cs = name.toCharArray();
                    cs[0] = Character.toLowerCase(cs[0]);
                    mi.name = new String(cs);

                } else {
                    mi.name = cd.name();
                    int typeLength = method.getParameterTypes().length;
                    if (typeLength == 0) {
                        isGet = true;
                    } else if (typeLength == 1) {
                        isSet = true;
                    } else {
                        String msg = String.format(
                                "Only one parameter is allowed on a method declared with ContextData annoataion.(%s:%s)", cls.getName(),
                                mi.name);
                        throw new DataOperationException(msg);
                    }
                }
                mi.scope = cd.scope();

                if (isGet) {
                    // only if the reverse value is explicitly set to true and
                    // the scope is contained in the allowing reverse injection
                    // list
                    if (cd.reverse()) {
                        if (reverseTargetScopes.contains(mi.scope)) {
                            mi.type = method.getReturnType();
                            mi.fixForPrimitiveType();
                            target.getMethodList.add(mi);
                        } else {
                            String msg = String.format(
                                    "Only scope in [%s] can be marked as reverse injectable but found scope as %s (%s:%s).",
                                    reverseTargetScopes.toString(), mi.scope, cls.getName(), mi.name);
                            Asta4DWarningException awe = new Asta4DWarningException(msg);
                            logger.warn(msg, awe);
                        }
                    }
                }

                if (isSet) {
                    mi.type = method.getParameterTypes()[0];
                    mi.fixForPrimitiveType();
                    target.setMethodList.add(mi);
                }

            }
        }

        // then retrieve fields information
        String objCls = Object.class.getName();
        Field[] flds;
        FieldInfo fi;
        while (!cls.getName().equals(objCls)) {
            flds = cls.getDeclaredFields();
            for (Field field : flds) {
                cd = findAnnotation(field.getAnnotations());
                if (cd != null) {
                    fi = new FieldInfo();
                    fi.field = field;
                    fi.type = field.getType();
                    if (StringUtils.isEmpty(cd.name())) {
                        fi.name = field.getName();
                    } else {
                        fi.name = cd.name();
                    }
                    fi.scope = cd.scope();
                    fi.fixForPrimitiveType();
                    target.setFieldList.add(fi);

                    if (cd.reverse()) {
                        if (reverseTargetScopes.contains(fi.scope)) {
                            target.getFieldList.add(fi);
                        } else {
                            String msg = String.format(
                                    "Only scope in [%s] can be marked as reverse injectable but found scope as %s (%s:%s).",
View Full Code Here


            return targetList;
        }
        Annotation[][] annotations = method.getParameterAnnotations();
        String[] parameterNames = paranamer.lookupParameterNames(method);
        TargetInfo target;
        ContextData cd;
        for (int i = 0; i < types.length; i++) {
            target = new TargetInfo();
            cd = findAnnotation(annotations[i]);
            target.name = cd == null ? "" : cd.name();
            target.scope = cd == null ? "" : cd.scope();
            if (StringUtils.isEmpty(target.name)) {
                target.name = parameterNames[i];
            }
            target.type = types[i];
            target.fixForPrimitiveType();
View Full Code Here

        }
        return targetList;
    }

    private final static ContextData findAnnotation(Annotation[] annotations) {
        ContextData cd = null;
        Class<?> cls;
        for (Annotation annotation : annotations) {
            cls = annotation.annotationType();
            if (cls.equals(ContextData.class)) {
                cd = (ContextData) annotation;
View Full Code Here

        }

    }

    private ContextData gen(final String scope, final String name, final TypeUnMacthPolicy typeUnMatch) {
        return new ContextData() {

            @Override
            public Class<? extends Annotation> annotationType() {
                return ContextData.class;
            }
View Full Code Here

        List<String> reverseTargetScopes = Configuration.getConfiguration().getReverseInjectableScopes();

        InstanceWireTarget target = new InstanceWireTarget();
        Class<?> cls = instance.getClass();

        ContextData cd;

        // at first, retrieve methods information

        // TODO we should use class name to confirm contextdata annotation
        // because they are possibly from different class loader. The problem is
        // whether it is a problem?
        Method[] mtds = cls.getMethods();
        for (Method method : mtds) {
            cd = findContextDataAnnotation(method.getAnnotations());
            if (cd != null) {
                // cd = method.getAnnotation(ContextData.class);
                MethodInfo mi = new MethodInfo();

                mi.method = method;

                boolean isGet = false;
                boolean isSet = false;
                String declaredName = cd.name();

                if (StringUtils.isEmpty(declaredName)) {
                    String name = method.getName();
                    if (name.startsWith("set")) {
                        name = name.substring(3);
                        isSet = true;
                    } else if (name.startsWith("get")) {
                        name = name.substring(3);
                        isGet = true;
                    } else if (name.startsWith("is")) {
                        name = name.substring(2);
                        isSet = true;
                    } else {

                        switch (method.getParameterTypes().length) {
                        case 0:
                            isGet = true;
                            break;
                        case 1:
                            isSet = true;
                            break;
                        default:
                            String msg = String.format("Method [%s]:[%s] can not be treated as a getter or setter method.", cls.getName(),
                                    method.toGenericString());
                            throw new DataOperationException(msg);

                        }
                    }
                    char[] cs = name.toCharArray();
                    cs[0] = Character.toLowerCase(cs[0]);
                    mi.name = new String(cs);

                } else {
                    mi.name = declaredName;
                    int typeLength = method.getParameterTypes().length;
                    if (typeLength == 0) {
                        isGet = true;
                    } else if (typeLength == 1) {
                        isSet = true;
                    } else {
                        String msg = String.format(
                                "Only one parameter is allowed on a method declared with ContextData annoataion.(%s:%s)", cls.getName(),
                                mi.name);
                        throw new DataOperationException(msg);
                    }
                }
                mi.scope = cd.scope();

                if (isGet) {
                    // only if the reverse value is explicitly set to true and
                    // the scope is contained in the allowing reverse injection
                    // list
                    if (cd.reverse()) {
                        if (reverseTargetScopes.contains(mi.scope)) {
                            mi.type = method.getReturnType();
                            mi.fixForPrimitiveType();
                            target.getMethodList.add(mi);
                        } else {
                            String msg = String.format(
                                    "Only scope in [%s] can be marked as reverse injectable but found scope as %s (%s:%s).",
                                    reverseTargetScopes.toString(), mi.scope, cls.getName(), mi.name);
                            Asta4DWarningException awe = new Asta4DWarningException(msg);
                            logger.warn(msg, awe);
                        }
                    }
                }

                if (isSet) {
                    mi.type = method.getParameterTypes()[0];
                    mi.fixForPrimitiveType();

                    ContextDataSet cdSet = findContextDataSetAnnotation(mi.type.getAnnotations());
                    if (cdSet == null) {
                        mi.isContextDataSet = false;
                    } else {
                        mi.isContextDataSet = true;
                    }

                    target.setMethodList.add(mi);
                }

            }
        }

        // then retrieve fields information
        String objCls = Object.class.getName();
        Field[] flds;
        FieldInfo fi;
        while (!cls.getName().equals(objCls)) {
            flds = cls.getDeclaredFields();
            for (Field field : flds) {
                cd = findContextDataAnnotation(field.getAnnotations());
                if (cd != null) {
                    fi = new FieldInfo();
                    fi.field = field;
                    fi.type = field.getType();

                    String delcaredName = cd == null ? "" : cd.name();
                    if (StringUtils.isEmpty(delcaredName)) {
                        fi.name = field.getName();
                    } else {
                        fi.name = cd.name();
                    }
                    fi.scope = cd == null ? "" : cd.scope();
                    fi.fixForPrimitiveType();

                    ContextDataSet cdSet = findContextDataSetAnnotation(fi.type.getAnnotations());
                    if (cdSet == null) {
                        fi.isContextDataSet = false;
                    } else {
                        fi.isContextDataSet = true;
                    }

                    target.setFieldList.add(fi);

                    if (cd.reverse()) {//
                        if (reverseTargetScopes.contains(fi.scope)) {
                            target.getFieldList.add(fi);
                        } else {
                            String msg = String.format(
                                    "Only scope in [%s] can be marked as reverse injectable but found scope as %s (%s:%s).",
View Full Code Here

            return targetList;
        }
        Annotation[][] annotations = method.getParameterAnnotations();
        String[] parameterNames = paranamer.lookupParameterNames(method);
        TargetInfo target;
        ContextData cd;
        ContextDataSet cdSet;
        for (int i = 0; i < types.length; i++) {
            target = new TargetInfo();
            target.type = types[i];

            cd = findContextDataAnnotation(annotations[i]);
            cdSet = findContextDataSetAnnotation(target.type.getAnnotations());
            target.name = cd == null ? "" : cd.name();
            target.scope = cd == null ? "" : cd.scope();
            if (StringUtils.isEmpty(target.name)) {
                target.name = parameterNames[i];
            }

            if (cdSet == null) {
View Full Code Here

        }
        return targetList;
    }

    private final static ContextData findContextDataAnnotation(Annotation[] annotations) {
        ContextData cd = null;
        Class<?> cls;
        for (Annotation annotation : annotations) {
            cls = annotation.annotationType();
            if (cls.equals(ContextData.class)) {
                cd = (ContextData) annotation;
View Full Code Here

        List<String> reverseTargetScopes = Configuration.getConfiguration().getReverseInjectableScopes();

        InstanceWireTarget target = new InstanceWireTarget();
        Class<?> cls = instance.getClass();

        ContextData cd;

        // at first, retrieve methods information

        // TODO we should use class name to confirm contextdata annotation
        // because they are possibly from different class loader. The problem is
        // whether it is a problem?
        Method[] mtds = cls.getMethods();
        for (Method method : mtds) {
            cd = ConvertableAnnotationRetriever.retrieveAnnotation(ContextData.class, method.getAnnotations());
            if (cd != null) {
                // cd = method.getAnnotation(ContextData.class);
                MethodInfo mi = new MethodInfo();

                mi.method = method;

                boolean isGet = false;
                boolean isSet = false;
                String declaredName = cd.name();

                if (StringUtils.isEmpty(declaredName)) {
                    String name = method.getName();
                    if (name.startsWith("set")) {
                        name = name.substring(3);
                        isSet = true;
                    } else if (name.startsWith("get")) {
                        name = name.substring(3);
                        isGet = true;
                    } else if (name.startsWith("is")) {
                        name = name.substring(2);
                        isSet = true;
                    } else {

                        switch (method.getParameterTypes().length) {
                        case 0:
                            isGet = true;
                            break;
                        case 1:
                            isSet = true;
                            break;
                        default:
                            String msg = String.format("Method [%s]:[%s] can not be treated as a getter or setter method.", cls.getName(),
                                    method.toGenericString());
                            throw new DataOperationException(msg);

                        }
                    }
                    char[] cs = name.toCharArray();
                    cs[0] = Character.toLowerCase(cs[0]);
                    mi.name = new String(cs);

                } else {
                    mi.name = declaredName;
                    int typeLength = method.getParameterTypes().length;
                    if (typeLength == 0) {
                        isGet = true;
                    } else if (typeLength == 1) {
                        isSet = true;
                    } else {
                        String msg = String.format(
                                "Only one parameter is allowed on a method declared with ContextData annoataion.(%s:%s)", cls.getName(),
                                mi.name);
                        throw new DataOperationException(msg);
                    }
                }
                mi.scope = cd.scope();
                mi.typeUnMatch = cd.typeUnMatch();

                if (isGet) {
                    // only if the reverse value is explicitly set to true and
                    // the scope is contained in the allowing reverse injection
                    // list
                    if (cd.reverse()) {
                        if (reverseTargetScopes.contains(mi.scope)) {
                            mi.type = method.getReturnType();
                            mi.fixForPrimitiveType();
                            target.getMethodList.add(mi);
                        } else {
                            String msg = String.format(
                                    "Only scope in [%s] can be marked as reverse injectable but found scope as %s (%s:%s).",
                                    reverseTargetScopes.toString(), mi.scope, cls.getName(), mi.name);
                            Asta4DWarningException awe = new Asta4DWarningException(msg);
                            logger.warn(msg, awe);
                        }
                    }
                }

                if (isSet) {
                    mi.type = method.getParameterTypes()[0];
                    mi.isContextDataHolder = ContextDataHolder.class.isAssignableFrom(mi.type);
                    mi.fixForPrimitiveType();

                    ContextDataSet cdSet = ConvertableAnnotationRetriever
                            .retrieveAnnotation(ContextDataSet.class, mi.type.getAnnotations());
                    if (cdSet == null) {
                        mi.contextDataSetFactory = null;
                    } else {
                        mi.contextDataSetFactory = cdSet.factory().newInstance();
                        mi.isContextDataSetSingletonInContext = cdSet.singletonInContext();
                    }

                    target.setMethodList.add(mi);
                }

            }
        }

        // then retrieve fields information
        String objCls = Object.class.getName();
        Field[] flds;
        FieldInfo fi;
        while (!cls.getName().equals(objCls)) {
            flds = cls.getDeclaredFields();
            for (Field field : flds) {
                cd = ConvertableAnnotationRetriever.retrieveAnnotation(ContextData.class, field.getAnnotations());
                if (cd != null) {
                    fi = new FieldInfo();
                    fi.field = field;
                    fi.type = field.getType();
                    fi.isContextDataHolder = ContextDataHolder.class.isAssignableFrom(fi.type);

                    String delcaredName = cd == null ? "" : cd.name();
                    if (StringUtils.isEmpty(delcaredName)) {
                        fi.name = field.getName();
                    } else {
                        fi.name = cd.name();
                    }
                    fi.scope = cd == null ? "" : cd.scope();
                    fi.typeUnMatch = cd.typeUnMatch();
                    fi.fixForPrimitiveType();

                    ContextDataSet cdSet = ConvertableAnnotationRetriever
                            .retrieveAnnotation(ContextDataSet.class, fi.type.getAnnotations());
                    if (cdSet == null) {
                        fi.contextDataSetFactory = null;
                    } else {
                        fi.contextDataSetFactory = cdSet.factory().newInstance();
                        fi.isContextDataSetSingletonInContext = cdSet.singletonInContext();
                    }

                    target.setFieldList.add(fi);

                    if (cd.reverse()) {//
                        if (reverseTargetScopes.contains(fi.scope)) {
                            target.getFieldList.add(fi);
                        } else {
                            String msg = String.format(
                                    "Only scope in [%s] can be marked as reverse injectable but found scope as %s (%s:%s).",
View Full Code Here

            return targetList;
        }
        Annotation[][] annotations = method.getParameterAnnotations();
        String[] parameterNames = paranamer.lookupParameterNames(method);
        TargetInfo target;
        ContextData cd;
        ContextDataSet cdSet;
        for (int i = 0; i < types.length; i++) {
            target = new TargetInfo();
            target.type = types[i];
            target.isContextDataHolder = ContextDataHolder.class.isAssignableFrom(target.type);

            cd = ConvertableAnnotationRetriever.retrieveAnnotation(ContextData.class, annotations[i]);
            cdSet = ConvertableAnnotationRetriever.retrieveAnnotation(ContextDataSet.class, target.type.getAnnotations());
            target.name = cd == null ? "" : cd.name();
            target.scope = cd == null ? "" : cd.scope();
            target.typeUnMatch = cd == null ? TypeUnMacthPolicy.DEFAULT_VALUE : cd.typeUnMatch();
            if (StringUtils.isEmpty(target.name)) {
                target.name = parameterNames[i];
            }

            if (cdSet == null) {
View Full Code Here

TOP

Related Classes of com.astamuse.asta4d.data.annotation.ContextData

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.