Search in sources :

Example 76 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project cuba by cuba-platform.

the class DataContextImpl method setPropertyValue.

protected void setPropertyValue(Entity entity, MetaProperty property, @Nullable Object value, boolean checkEquals) {
    if (!property.isReadOnly()) {
        ((AbstractInstance) entity).setValue(property.getName(), value, checkEquals);
    } else {
        AnnotatedElement annotatedElement = property.getAnnotatedElement();
        if (annotatedElement instanceof Field) {
            Field field = (Field) annotatedElement;
            field.setAccessible(true);
            try {
                field.set(entity, value);
            } catch (IllegalAccessException e) {
                throw new RuntimeException("Unable to set property value", e);
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) AbstractInstance(com.haulmont.chile.core.model.impl.AbstractInstance) AnnotatedElement(java.lang.reflect.AnnotatedElement)

Example 77 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project cuba by cuba-platform.

the class MetadataTools method isOwningSide.

/**
 * Determine whether the given property is on the owning side of an association.
 */
public boolean isOwningSide(MetaProperty metaProperty) {
    checkNotNullArgument(metaProperty, "metaProperty is null");
    if (!metaProperty.getRange().isClass())
        return false;
    AnnotatedElement el = metaProperty.getAnnotatedElement();
    for (Annotation annotation : el.getAnnotations()) {
        if (annotation instanceof ManyToOne)
            return true;
        if (annotation instanceof OneToMany || annotation instanceof OneToOne)
            return el.isAnnotationPresent(JoinColumn.class) || el.isAnnotationPresent(JoinTable.class);
        if (annotation instanceof ManyToMany)
            return el.isAnnotationPresent(JoinTable.class);
    }
    return false;
}
Also used : AnnotatedElement(java.lang.reflect.AnnotatedElement) Annotation(java.lang.annotation.Annotation)

Example 78 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project cuba by cuba-platform.

the class MetadataLoader method postProcessProperty.

protected void postProcessProperty(MetaClass metaClass, MetaProperty metaProperty) {
    // init inverse properties
    MetaProperty inverseProp = metaProperty.getInverse();
    if (inverseProp != null && inverseProp.getInverse() == null) {
        ((MetaPropertyImpl) inverseProp).setInverse(metaProperty);
    }
    if (metaProperty.getRange() == null || !metaProperty.getRange().isClass())
        return;
    AnnotatedElement annotatedElement = metaProperty.getAnnotatedElement();
    OnDelete onDelete = annotatedElement.getAnnotation(OnDelete.class);
    if (onDelete != null) {
        Map<String, Object> metaAnnotations = metaClass.getAnnotations();
        MetaProperty[] properties = (MetaProperty[]) metaAnnotations.get(OnDelete.class.getName());
        properties = ArrayUtils.add(properties, metaProperty);
        metaAnnotations.put(OnDelete.class.getName(), properties);
    }
    OnDeleteInverse onDeleteInverse = annotatedElement.getAnnotation(OnDeleteInverse.class);
    if (onDeleteInverse != null) {
        Map<String, Object> metaAnnotations = metaProperty.getRange().asClass().getAnnotations();
        MetaProperty[] properties = (MetaProperty[]) metaAnnotations.get(OnDeleteInverse.class.getName());
        properties = ArrayUtils.add(properties, metaProperty);
        metaAnnotations.put(OnDeleteInverse.class.getName(), properties);
    }
}
Also used : AnnotatedElement(java.lang.reflect.AnnotatedElement) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 79 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project geronimo-xbean by apache.

the class MetaAnnotatedElement method getDeclaredMetaAnnotations.

private static Collection<Annotation> getDeclaredMetaAnnotations(Class<? extends Annotation> clazz) {
    Map<Class, Annotation> map = new HashMap<Class, Annotation>();
    for (Annotation annotation : clazz.getDeclaredAnnotations()) {
        map.put(annotation.annotationType(), annotation);
    }
    List<Annotation[]> groups = new ArrayList<Annotation[]>();
    Class<? extends Annotation> metatype = getMetatype(clazz);
    if (metatype != null) {
        try {
            Class<?> def = clazz.getClassLoader().loadClass(clazz.getName() + "$$");
            List<AnnotatedElement> elements = new ArrayList<AnnotatedElement>();
            elements.addAll(asList(def.getDeclaredFields()));
            elements.addAll(asList(def.getDeclaredConstructors()));
            elements.addAll(asList(def.getDeclaredMethods()));
            for (Method method : def.getDeclaredMethods()) {
                for (Annotation[] array : method.getParameterAnnotations()) {
                    groups.add(array);
                }
            }
            for (Constructor constructor : def.getDeclaredConstructors()) {
                for (Annotation[] array : constructor.getParameterAnnotations()) {
                    groups.add(array);
                }
            }
            for (AnnotatedElement element : elements) {
                groups.add(element.getDeclaredAnnotations());
            }
            for (Annotation[] annotations : groups) {
                if (contains(annotations, clazz)) {
                    for (Annotation annotation : annotations) {
                        map.put(annotation.annotationType(), annotation);
                    }
                }
            }
        } catch (ClassNotFoundException e) {
        // inner class is optional
        }
    }
    map.remove(Target.class);
    map.remove(Retention.class);
    map.remove(Documented.class);
    map.remove(metatype);
    map.remove(clazz);
    return map.values();
}
Also used : HashMap(java.util.HashMap) Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) AnnotatedElement(java.lang.reflect.AnnotatedElement) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation)

Example 80 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project newts by OpenNMS.

the class MergeSort method createCmdLineParser.

private CmdLineParser createCmdLineParser() {
    return new CmdLineParser(this) {

        @SuppressWarnings("rawtypes")
        @Override
        public void addArgument(final Setter setter, Argument a) {
            Setter newSetter = setter;
            if (setter instanceof MethodSetter) {
                newSetter = new Setter() {

                    @SuppressWarnings("unchecked")
                    @Override
                    public void addValue(Object value) throws CmdLineException {
                        setter.addValue(value);
                    }

                    @Override
                    public Class getType() {
                        return setter.getType();
                    }

                    @Override
                    public boolean isMultiValued() {
                        return false;
                    }

                    @Override
                    public FieldSetter asFieldSetter() {
                        return setter.asFieldSetter();
                    }

                    @Override
                    public AnnotatedElement asAnnotatedElement() {
                        return setter.asAnnotatedElement();
                    }
                };
            }
            super.addArgument(newSetter, a);
        }
    };
}
Also used : CmdLineParser(org.kohsuke.args4j.CmdLineParser) Argument(org.kohsuke.args4j.Argument) MethodSetter(org.kohsuke.args4j.spi.MethodSetter) FieldSetter(org.kohsuke.args4j.spi.FieldSetter) MethodSetter(org.kohsuke.args4j.spi.MethodSetter) Setter(org.kohsuke.args4j.spi.Setter) FieldSetter(org.kohsuke.args4j.spi.FieldSetter) AnnotatedElement(java.lang.reflect.AnnotatedElement) CmdLineException(org.kohsuke.args4j.CmdLineException)

Aggregations

AnnotatedElement (java.lang.reflect.AnnotatedElement)106 Method (java.lang.reflect.Method)23 Annotation (java.lang.annotation.Annotation)17 Field (java.lang.reflect.Field)17 ArrayList (java.util.ArrayList)12 Test (org.junit.Test)11 HashMap (java.util.HashMap)9 Test (org.junit.jupiter.api.Test)8 Member (java.lang.reflect.Member)7 LinkedHashSet (java.util.LinkedHashSet)7 List (java.util.List)7 Constructor (java.lang.reflect.Constructor)6 Type (java.lang.reflect.Type)6 Map (java.util.Map)6 HashSet (java.util.HashSet)5 By (org.openqa.selenium.By)5 Statement (org.junit.runners.model.Statement)4 FindBy (org.openqa.selenium.support.FindBy)4 EntityType (com.querydsl.codegen.EntityType)3 Collection (java.util.Collection)3