Search in sources :

Example 1 with SimpleTypeConverter

use of cn.taketoday.beans.SimpleTypeConverter in project today-framework by TAKETODAY.

the class QualifierAnnotationAutowireCandidateResolver method checkQualifiers.

/**
 * Match the given qualifier annotations against the candidate bean holder.
 */
protected boolean checkQualifiers(BeanDefinitionHolder bdHolder, Annotation[] annotationsToSearch) {
    if (ObjectUtils.isEmpty(annotationsToSearch)) {
        return true;
    }
    SimpleTypeConverter typeConverter = new SimpleTypeConverter();
    for (Annotation annotation : annotationsToSearch) {
        Class<? extends Annotation> type = annotation.annotationType();
        boolean checkMeta = true;
        boolean fallbackToMeta = false;
        if (isQualifier(type)) {
            if (!checkQualifier(bdHolder, annotation, typeConverter)) {
                fallbackToMeta = true;
            } else {
                checkMeta = false;
            }
        }
        if (checkMeta) {
            boolean foundMeta = false;
            for (Annotation metaAnn : type.getAnnotations()) {
                Class<? extends Annotation> metaType = metaAnn.annotationType();
                if (isQualifier(metaType)) {
                    foundMeta = true;
                    // Otherwise it is just a marker for a custom qualifier annotation.
                    if ((fallbackToMeta && ObjectUtils.isEmpty(AnnotationUtils.getValue(metaAnn))) || !checkQualifier(bdHolder, metaAnn, typeConverter)) {
                        return false;
                    }
                }
            }
            if (fallbackToMeta && !foundMeta) {
                return false;
            }
        }
    }
    return true;
}
Also used : SimpleTypeConverter(cn.taketoday.beans.SimpleTypeConverter) Annotation(java.lang.annotation.Annotation)

Example 2 with SimpleTypeConverter

use of cn.taketoday.beans.SimpleTypeConverter in project today-framework by TAKETODAY.

the class BeanProperties method copy.

/**
 * Ignore read-only properties
 */
@SuppressWarnings("unchecked")
private static void copy(Object source, BeanMetadata destination, Object destinationInstance, @Nullable TypeConverter converter, @Nullable String[] ignoreProperties) {
    if (converter == null) {
        converter = new SimpleTypeConverter();
    }
    if (ObjectUtils.isNotEmpty(ignoreProperties)) {
        Set<String> ignorePropertiesSet = Set.of(ignoreProperties);
        if (source instanceof Map) {
            for (Map.Entry<String, Object> entry : ((Map<String, Object>) source).entrySet()) {
                String propertyName = entry.getKey();
                if (!ignorePropertiesSet.contains(propertyName)) {
                    BeanProperty beanProperty = destination.getBeanProperty(propertyName);
                    if (beanProperty != null && beanProperty.isWriteable()) {
                        beanProperty.setValue(destinationInstance, entry.getValue(), converter);
                    }
                }
            }
        } else {
            BeanMetadata sourceMetadata = BeanMetadata.from(source);
            for (BeanProperty property : sourceMetadata) {
                if (property.isReadable()) {
                    String propertyName = property.getName();
                    if (!ignorePropertiesSet.contains(propertyName)) {
                        BeanProperty beanProperty = destination.getBeanProperty(propertyName);
                        if (beanProperty != null && beanProperty.isWriteable()) {
                            beanProperty.setValue(destinationInstance, property.getValue(source), converter);
                        }
                    }
                }
            }
        }
    } else {
        if (source instanceof Map) {
            for (Map.Entry<String, Object> entry : ((Map<String, Object>) source).entrySet()) {
                String propertyName = entry.getKey();
                BeanProperty beanProperty = destination.getBeanProperty(propertyName);
                if (beanProperty != null && beanProperty.isWriteable()) {
                    beanProperty.setValue(destinationInstance, entry.getValue(), converter);
                }
            }
        } else {
            BeanMetadata sourceMetadata = BeanMetadata.from(source);
            for (BeanProperty property : sourceMetadata) {
                if (property.isReadable()) {
                    String propertyName = property.getName();
                    BeanProperty beanProperty = destination.getBeanProperty(propertyName);
                    if (beanProperty != null && beanProperty.isWriteable()) {
                        beanProperty.setValue(destinationInstance, property.getValue(source), converter);
                    }
                }
            }
        }
    }
}
Also used : SimpleTypeConverter(cn.taketoday.beans.SimpleTypeConverter) BeanMetadata(cn.taketoday.beans.BeanMetadata) Map(java.util.Map) BeanProperty(cn.taketoday.beans.BeanProperty)

Example 3 with SimpleTypeConverter

use of cn.taketoday.beans.SimpleTypeConverter in project today-infrastructure by TAKETODAY.

the class QualifierAnnotationAutowireCandidateResolver method checkQualifiers.

/**
 * Match the given qualifier annotations against the candidate bean holder.
 */
protected boolean checkQualifiers(BeanDefinitionHolder bdHolder, Annotation[] annotationsToSearch) {
    if (ObjectUtils.isEmpty(annotationsToSearch)) {
        return true;
    }
    SimpleTypeConverter typeConverter = new SimpleTypeConverter();
    for (Annotation annotation : annotationsToSearch) {
        Class<? extends Annotation> type = annotation.annotationType();
        boolean checkMeta = true;
        boolean fallbackToMeta = false;
        if (isQualifier(type)) {
            if (!checkQualifier(bdHolder, annotation, typeConverter)) {
                fallbackToMeta = true;
            } else {
                checkMeta = false;
            }
        }
        if (checkMeta) {
            boolean foundMeta = false;
            for (Annotation metaAnn : type.getAnnotations()) {
                Class<? extends Annotation> metaType = metaAnn.annotationType();
                if (isQualifier(metaType)) {
                    foundMeta = true;
                    // Otherwise it is just a marker for a custom qualifier annotation.
                    if ((fallbackToMeta && ObjectUtils.isEmpty(AnnotationUtils.getValue(metaAnn))) || !checkQualifier(bdHolder, metaAnn, typeConverter)) {
                        return false;
                    }
                }
            }
            if (fallbackToMeta && !foundMeta) {
                return false;
            }
        }
    }
    return true;
}
Also used : SimpleTypeConverter(cn.taketoday.beans.SimpleTypeConverter) Annotation(java.lang.annotation.Annotation)

Example 4 with SimpleTypeConverter

use of cn.taketoday.beans.SimpleTypeConverter in project today-infrastructure by TAKETODAY.

the class BeanProperties method copy.

/**
 * Ignore read-only properties
 */
@SuppressWarnings("unchecked")
private static void copy(Object source, BeanMetadata destination, Object destinationInstance, @Nullable TypeConverter converter, @Nullable String[] ignoreProperties) {
    if (converter == null) {
        converter = new SimpleTypeConverter();
    }
    if (ObjectUtils.isNotEmpty(ignoreProperties)) {
        Set<String> ignorePropertiesSet = Set.of(ignoreProperties);
        if (source instanceof Map) {
            for (Map.Entry<String, Object> entry : ((Map<String, Object>) source).entrySet()) {
                String propertyName = entry.getKey();
                if (!ignorePropertiesSet.contains(propertyName)) {
                    BeanProperty beanProperty = destination.getBeanProperty(propertyName);
                    if (beanProperty != null && beanProperty.isWriteable()) {
                        beanProperty.setValue(destinationInstance, entry.getValue(), converter);
                    }
                }
            }
        } else {
            BeanMetadata sourceMetadata = BeanMetadata.from(source);
            for (BeanProperty property : sourceMetadata) {
                if (property.isReadable()) {
                    String propertyName = property.getName();
                    if (!ignorePropertiesSet.contains(propertyName)) {
                        BeanProperty beanProperty = destination.getBeanProperty(propertyName);
                        if (beanProperty != null && beanProperty.isWriteable()) {
                            beanProperty.setValue(destinationInstance, property.getValue(source), converter);
                        }
                    }
                }
            }
        }
    } else {
        if (source instanceof Map) {
            for (Map.Entry<String, Object> entry : ((Map<String, Object>) source).entrySet()) {
                String propertyName = entry.getKey();
                BeanProperty beanProperty = destination.getBeanProperty(propertyName);
                if (beanProperty != null && beanProperty.isWriteable()) {
                    beanProperty.setValue(destinationInstance, entry.getValue(), converter);
                }
            }
        } else {
            BeanMetadata sourceMetadata = BeanMetadata.from(source);
            for (BeanProperty property : sourceMetadata) {
                if (property.isReadable()) {
                    String propertyName = property.getName();
                    BeanProperty beanProperty = destination.getBeanProperty(propertyName);
                    if (beanProperty != null && beanProperty.isWriteable()) {
                        beanProperty.setValue(destinationInstance, property.getValue(source), converter);
                    }
                }
            }
        }
    }
}
Also used : SimpleTypeConverter(cn.taketoday.beans.SimpleTypeConverter) BeanMetadata(cn.taketoday.beans.BeanMetadata) Map(java.util.Map) BeanProperty(cn.taketoday.beans.BeanProperty)

Example 5 with SimpleTypeConverter

use of cn.taketoday.beans.SimpleTypeConverter in project today-framework by TAKETODAY.

the class QualifierAnnotationAutowireCandidateResolver method checkQualifiers.

/**
 * Match the given qualifier annotations against the candidate bean definition.
 */
protected boolean checkQualifiers(BeanDefinition definition, Annotation[] annotationsToSearch) {
    if (ObjectUtils.isEmpty(annotationsToSearch)) {
        return true;
    }
    SimpleTypeConverter typeConverter = new SimpleTypeConverter();
    for (Annotation annotation : annotationsToSearch) {
        Class<? extends Annotation> type = annotation.annotationType();
        boolean checkMeta = true;
        boolean fallbackToMeta = false;
        if (isQualifier(type)) {
            if (!checkQualifier(definition, annotation, typeConverter)) {
                fallbackToMeta = true;
            } else {
                checkMeta = false;
            }
        }
        if (checkMeta) {
            boolean foundMeta = false;
            for (Annotation metaAnn : type.getAnnotations()) {
                Class<? extends Annotation> metaType = metaAnn.annotationType();
                if (isQualifier(metaType)) {
                    foundMeta = true;
                    // Otherwise it is just a marker for a custom qualifier annotation.
                    if ((fallbackToMeta && ObjectUtils.isEmpty(AnnotationUtils.getValue(metaAnn))) || !checkQualifier(definition, metaAnn, typeConverter)) {
                        return false;
                    }
                }
            }
            if (fallbackToMeta && !foundMeta) {
                return false;
            }
        }
    }
    return true;
}
Also used : SimpleTypeConverter(cn.taketoday.beans.SimpleTypeConverter) Annotation(java.lang.annotation.Annotation)

Aggregations

SimpleTypeConverter (cn.taketoday.beans.SimpleTypeConverter)5 Annotation (java.lang.annotation.Annotation)3 BeanMetadata (cn.taketoday.beans.BeanMetadata)2 BeanProperty (cn.taketoday.beans.BeanProperty)2 Map (java.util.Map)2