Example usage for java.lang.reflect Field getAnnotations

List of usage examples for java.lang.reflect Field getAnnotations

Introduction

In this page you can find the example usage for java.lang.reflect Field getAnnotations.

Prototype

public Annotation[] getAnnotations() 

Source Link

Usage

From source file:dirty.mockito.junit.rules.ActiveTestRule.java

/**
 * @param unitTest/*from   w w  w . jav a2  s. co  m*/
 *        the JUnit test we're intercepting
 * @param clazz
 *        the class we're scanning
 */
@SuppressWarnings("deprecation")
private void scan(final Object unitTest, final Class<?> clazz) {
    final AnnotationEngine annotationEngine = new GlobalConfiguration().getAnnotationEngine();
    final Field[] fields = clazz.getDeclaredFields();
    for (final Field field : fields) {
        for (final Annotation annotation : field.getAnnotations()) {
            final Object mock = annotationEngine.createMockFor(annotation, field);
            if (mock != null) {
                try {
                    Reflection.set(field).of(unitTest).to(mock);
                } catch (final IllegalAccessException e) {
                    throw new MockitoException("Problems initializing fields annotated with " + annotation, e);
                }

                if (mock instanceof EntityManager) {
                    registerMockEntityManagerFactory((EntityManager) mock);
                }
                beanFactory.registerSingleton(field.getName(), mock);
            }
        }
    }
}

From source file:org.jdal.ui.bind.ControlInitializerSupport.java

/**
 * Get field or method annotations//ww w  .  j  a v a 2s  .com
 * @param property
 * @param clazz
 * @return
 */
protected Annotation[] getAnnotations(String property, Class<?> clazz) {
    Field field = ReflectionUtils.findField(clazz, property);
    Annotation[] fa = new Annotation[] {};

    if (field != null) {
        fa = field.getAnnotations();
    }

    Method method = BeanUtils.getPropertyDescriptor(clazz, property).getReadMethod();
    if (method != null) {
        Annotation[] ma = method.getAnnotations();
        Annotation[] annotations = (Annotation[]) ArrayUtils.addAll(fa, ma);
        return annotations;
    }

    return fa;
}

From source file:py.una.pol.karaku.dao.entity.interceptors.CaseSensitiveInterceptor.java

@Override
public boolean interceptable(Operation op, Field f, Object bean) {

    if (op == Operation.DELETE || f.isAnnotationPresent(CaseSensitive.class)) {
        return false;
    }/* w w  w  . j a  va2  s .  com*/
    boolean interceptable = true;
    for (Annotation a : f.getAnnotations()) {
        if (a.annotationType().isAnnotationPresent(CaseSensitive.class)) {
            interceptable = false;
            break;
        }
    }

    return interceptable;
}

From source file:ve.zoonosis.model.bean.AbstractForm.java

protected final void autoCreateValidateForm(Class<? extends EntidadGlobal>... entitys) {
    for (Class<? extends EntidadGlobal> e : entitys) {
        Field[] fields = ReflectionUtils.getAllFields(e);
        for (Field field : fields) {
            Annotation[] annotation = field.getAnnotations();
            if (annotation.length > 0) {
                boolean add = false;
                for (Annotation annotation1 : annotation) {
                    if (annotation1.annotationType().getAnnotation(JacksonAnnotation.class) == null) {
                        add = true;//from  w w w  .j a v a  2s  . c o  m
                    }
                }
                if (add) {
                    try {
                        Field f = ReflectionUtils.getField(this, field.getName());
                        if (f != null && Component.class.isAssignableFrom(f.getType())) {
                            Component c = ReflectionUtils.runGetter(f, this);
                            agregarValidEvent(c, formularioActionListener);
                        }
                    } catch (java.lang.NoSuchFieldError ex) {

                    }
                }
            }
        }
    }
}

From source file:com.dalaran.annotation.validator.Validator.java

public boolean validate(Activity a) {
    Field[] declaredFields = a.getClass().getDeclaredFields();
    boolean valid = false;
    for (Field field : declaredFields) {
        Annotation[] annotations = field.getAnnotations();
        for (Annotation annotation : annotations) {
            if (annotation.annotationType().equals(Length.class)) {
                boolean b = checkToValidate(a, field, (Length) annotation);
                if (b) {
                    valid = b;//from   w w  w .  j a v a2 s  .  c  o m
                }
            }
            if (annotation.annotationType().equals(Email.class)) {
                boolean b = checkToValidateEmail(a, field, (Email) annotation);
                if (b) {
                    valid = b;
                }
            }
            if (annotation.annotationType().equals(EqualsWith.class)) {
                boolean b = checkToValidateEquals(a, field, (EqualsWith) annotation);
                if (b) {
                    valid = b;
                }
            }
        }
    }
    return valid;
}

From source file:com.dalaran.annotation.validator.Validator.java

public boolean validate(Fragment a) {
    Field[] declaredFields = a.getClass().getDeclaredFields();
    boolean valid = false;
    for (Field field : declaredFields) {
        Annotation[] annotations = field.getAnnotations();
        for (Annotation annotation : annotations) {
            if (annotation.annotationType().equals(Length.class)) {
                boolean b = checkToValidate(a, field, (Length) annotation);
                if (b) {
                    valid = b;/*from  w ww  .ja v a 2  s.c  o m*/
                }
            }
            if (annotation.annotationType().equals(Email.class)) {
                boolean b = checkToValidateEmail(a, field, (Email) annotation);
                if (b) {
                    valid = b;
                }
            }
            if (annotation.annotationType().equals(EqualsWith.class)) {
                boolean b = checkToValidateEquals(a.getActivity(), field, (EqualsWith) annotation);
                if (b) {
                    valid = b;
                }
            }
        }
    }
    return valid;
}

From source file:ar.com.zauber.commons.repository.utils.SpringInjectionInterceptor.java

/**
 * @param persistibleClasses persistible classes that may requiere dependency
 *                           injection/*from   ww w  .ja va 2s . c  o m*/
 */
public SpringInjectionInterceptor(final List<Class<?>> persistibleClasses) {
    Validate.noNullElements(persistibleClasses);

    for (final Class<?> clazz : persistibleClasses) {
        final List<Entry<Field, String>> fields = new LinkedList<Entry<Field, String>>();
        for (final Annotation annotation : clazz.getAnnotations()) {
            if (annotation instanceof Configurable) {
                final List<Field> clazzFields = new LinkedList<Field>();
                Class<?> c = clazz;
                while (c != null) {
                    clazzFields.addAll(Arrays.asList(c.getDeclaredFields()));
                    c = c.getSuperclass();
                }

                for (final Field field : clazzFields) {
                    for (final Annotation a : field.getAnnotations()) {
                        if (a instanceof Qualifier) {
                            String n = ((Qualifier) a).value();
                            if (StringUtils.isBlank(n)) {
                                n = field.getName();
                            }
                            final String name = n;
                            fields.add(new Entry<Field, String>() {
                                public Field getKey() {
                                    return field;
                                }

                                public String getValue() {
                                    return name;
                                }

                                public String setValue(final String value) {
                                    return null;
                                }
                            });
                        }
                    }
                }
            }
        }

        if (fields.size() > 0) {
            dependencyCache.put(clazz,
                    new DependencyInjection(fields, InitializingBean.class.isAssignableFrom(clazz)));
        }
    }
}

From source file:org.kuali.rice.krad.web.form.UifFormManager.java

/**
 * Retrieves the session form based on the formkey and updates the non session transient
 * variables on the request form from the session form.
 *
 * @param requestForm/*from w  w  w . j  a  v a 2 s  .  co  m*/
 * @param formKey
 */
public void updateFormWithSession(UifFormBase requestForm, String formKey) {
    UifFormBase sessionForm = sessionForms.get(formKey);
    if (sessionForm == null) {
        return;
    }

    if (!sessionForm.getClass().isAssignableFrom(requestForm.getClass())) {
        throw new RuntimeException(
                "Session form mismatch, session form class not assignable from request form class");
    }

    List<Field> fields = new ArrayList<Field>();
    fields = getAllFields(fields, sessionForm.getClass(), UifFormBase.class);
    for (Field field : fields) {
        boolean copyValue = true;
        for (Annotation an : field.getAnnotations()) {
            if (an instanceof SessionTransient) {
                copyValue = false;
            }
        }

        if (copyValue && ObjectPropertyUtils.isReadableProperty(sessionForm, field.getName())
                && ObjectPropertyUtils.isWritableProperty(sessionForm, field.getName())) {
            Object fieldValue = ObjectPropertyUtils.getPropertyValue(sessionForm, field.getName());
            ObjectPropertyUtils.setPropertyValue(requestForm, field.getName(), fieldValue);
        }
    }
}

From source file:com.sap.csc.poc.ems.persistence.initial.entitlement.EntitlementItemAttributeDataInitializer.java

@Override
public Collection<EntitlementItemAttribute> create() {
    ArrayList<EntitlementItemAttribute> attributes = new ArrayList<>();
    // Software License
    EntitlementType sl = entitlementTypeDataInitializer.getByName("SL");
    // Software License - Item Attributes
    Field[] itemFields = FieldUtils.getAllFields(SoftwareLicenseEntitlementItem.class);
    sl.setItemAttributes(new ArrayList<>(itemFields.length));
    for (Field itemField : itemFields) {
        if (!Modifier.isStatic(itemField.getModifiers()) &&
        // Exclusion from JPA annotations
                CollectionUtils.isEmpty(CollectionUtils.intersection(
                        // Annotations
                        Arrays.asList(itemField.getAnnotations()).stream()
                                .map(annotation -> annotation.annotationType()).collect(Collectors.toList()),
                        // Exclusions
                        EXCLUSION_PROPERTY_ATTRIBUTES)))
            sl.getItemAttributes()/*  w  ww  . j a  v a2  s  . c  om*/
                    .add(generateEntitlementAttributes(itemField, new EntitlementItemAttribute()));
    }
    for (EntitlementItemAttribute itemAttribute : sl.getItemAttributes()) {
        itemAttribute.setEntitlementType(sl);
        attributes.add(itemAttribute);
    }

    return attributes;
}

From source file:com.sap.csc.poc.ems.persistence.initial.entitlement.EntitlementHeaderAttributeDataInitializer.java

@Override
public Collection<EntitlementHeaderAttribute> create() {
    ArrayList<EntitlementHeaderAttribute> attributes = new ArrayList<>();
    // Software License
    EntitlementType sl = entitlementTypeDataInitializer.getByName("SL");
    // Software License - Header Attributes
    Field[] headerFields = FieldUtils.getAllFields(SoftwareLicenseEntitlementHeader.class);
    sl.setHeaderAttributes(new ArrayList<>(headerFields.length));
    for (Field headerField : headerFields) {
        if (!Modifier.isStatic(headerField.getModifiers()) &&
        // Exclusion from JPA annotations
                CollectionUtils.isEmpty(CollectionUtils.intersection(
                        // Annotations
                        Arrays.asList(headerField.getAnnotations()).stream()
                                .map(annotation -> annotation.annotationType()).collect(Collectors.toList()),
                        // Exclusions
                        EXCLUSION_PROPERTY_ATTRIBUTES)))
            sl.getHeaderAttributes()/*from  w  ww .  ja  va2 s.c om*/
                    .add(generateEntitlementAttributes(headerField, new EntitlementHeaderAttribute()));
    }
    for (EntitlementHeaderAttribute headerAttribute : sl.getHeaderAttributes()) {
        headerAttribute.setEntitlementType(sl);
        attributes.add(headerAttribute);
    }

    return attributes;
}