Example usage for java.lang.reflect Field getAnnotation

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

Introduction

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

Prototype

public <T extends Annotation> T getAnnotation(Class<T> annotationClass) 

Source Link

Usage

From source file:Main.java

/**
 * Gets the given class's {@link Field}s marked with the annotation of the
 * specified class./*from w  ww .j ava  2s  .  com*/
 * <p>
 * Unlike {@link Class#getFields()}, the result will include any non-public
 * fields, including fields defined in supertypes of the given class.
 * </p>
 * 
 * @param c The class to scan for annotated fields.
 * @param annotationClass The type of annotation for which to scan.
 * @param fields The list to which matching fields will be added.
 */
public static <A extends Annotation> void getAnnotatedFields(final Class<?> c, final Class<A> annotationClass,
        final List<Field> fields) {
    if (c == null)
        return;

    // check supertypes for annotated fields first
    getAnnotatedFields(c.getSuperclass(), annotationClass, fields);
    for (final Class<?> iface : c.getInterfaces()) {
        getAnnotatedFields(iface, annotationClass, fields);
    }

    for (final Field f : c.getDeclaredFields()) {
        final A ann = f.getAnnotation(annotationClass);
        if (ann != null)
            fields.add(f);
    }
}

From source file:Main.java

/**
 * Returns all fields declared in the class passed as argument or in its super classes annotated with
 * the supplied annotation.//from w w w  .j a  v  a2 s  .c o  m
 */
public static List<Field> getAllDeclaredField(Class<?> clazz, Class<? extends Annotation> annotationClass) {
    final List<Field> result = new LinkedList<Field>();
    for (final Field field : clazz.getDeclaredFields()) {
        final Object jakeOption = field.getAnnotation(annotationClass);
        if (jakeOption != null) {
            result.add(field);
        }
    }
    final Class<?> superClass = clazz.getSuperclass();
    if (superClass != null) {
        result.addAll(getAllDeclaredField(superClass, annotationClass));
    }
    return result;
}

From source file:Main.java

public static <A extends Annotation> A getAnnotation(Field field, Class<A> clazz) {
    A result = null;//w  ww  .  jav  a2 s  .co m
    if (field != null && clazz != null) {
        result = field.getAnnotation(clazz);
    }
    return result;
}

From source file:com.acuityph.commons.jpa.JpaUtils.java

/**
 * @param entityClass//from   w w w  .j  a  v  a  2  s.  co  m
 *        the entity class to inspect
 * @return the {@link Field} annotated with @Id, or null if no such field
 *         was found
 */
public static Field determineIdField(final Class<?> entityClass) {
    Field idField = null;
    Class<?> cl = entityClass;
    while (idField == null && cl != null && cl != Object.class) {
        for (final Field field : cl.getDeclaredFields()) {
            if (field.getAnnotation(Id.class) != null) {
                idField = field;
                break;
            }
        }
        if (idField == null) {
            cl = cl.getSuperclass();
        }
    }
    return idField;
}

From source file:springobjectmapper.ReflectionHelper.java

public static Field getAnnotatedField(Class<?> c, Class<? extends Annotation> annotationType) {
    for (Field field : getFields(c)) {
        Annotation annotation = field.getAnnotation(annotationType);
        if (annotation != null) {
            return field;
        }/*from   w  w w . ja v  a2s.  co m*/
    }
    throw new RuntimeException("Field with annotation " + annotationType.getCanonicalName()
            + " was not found on type " + c.getName() + "!");
}

From source file:com.hortonworks.streamline.storage.util.StorageUtils.java

public static List<Pair<Field, String>> getSearchableFieldValues(Storable storable)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    List<Pair<Field, String>> res = new ArrayList<>();
    for (Field field : storable.getClass().getDeclaredFields()) {
        if (field.getAnnotation(SearchableField.class) != null) {
            Object val = ReflectionHelper.invokeGetter(field.getName(), storable);
            if (val != null) {
                res.add(Pair.of(field, val instanceof String ? (String) val : val.toString()));
            }//from   ww w.  jav a  2 s  .  c  o  m
        }
    }
    return res;
}

From source file:de.micromata.genome.util.runtime.config.LocalSettingsConfigUtils.java

/**
 * Inits the from local settings./*from w  ww .j  av  a 2 s. c  o  m*/
 *
 * @param bean the bean
 * @param localSettings the local settings
 */
public static void initFromLocalSettings(LocalSettingsConfigModel bean, LocalSettings localSettings) {
    List<Field> fields = PrivateBeanUtils.findAllFields(bean.getClass(),
            FieldMatchers.hasAnnotation(ALocalSettingsPath.class));
    for (Field field : fields) {
        ALocalSettingsPath lsp = field.getAnnotation(ALocalSettingsPath.class);
        String key = lsp.key();
        if ("<fieldName>".equals(key) == true) {
            key = field.getName();
        }
        PrivateBeanUtils.writeField(bean, field, localSettings.get(bean.buildKey(key), lsp.defaultValue()));
    }
}

From source file:de.cosmocode.palava.salesforce.sync.NullFieldCollector.java

private static String nameOf(Field field) {
    final XmlElementRef ref = field.getAnnotation(XmlElementRef.class);
    if (ref == null || StringUtils.isBlank(ref.name())) {
        return field.getName();
    } else {/*from w w  w  .  j a  v  a 2 s. c  o  m*/
        return ref.name();
    }
}

From source file:de.micromata.genome.util.runtime.config.LocalSettingsConfigUtils.java

/**
 * To properties in section./*from  ww w . ja v a  2  s  .  c o m*/
 *
 * @param bean the bean
 * @param writer the writer
 */
public static void toPropertiesInSection(LocalSettingsConfigModel bean, LocalSettingsWriter writer) {
    List<Field> fields = PrivateBeanUtils.findAllFields(bean.getClass(),
            FieldMatchers.hasAnnotation(ALocalSettingsPath.class));
    for (Field field : fields) {
        ALocalSettingsPath lsp = field.getAnnotation(ALocalSettingsPath.class);
        String key = lsp.key();
        if ("<fieldName>".equals(key) == true) {
            key = field.getName();
        }
        String val = (String) PrivateBeanUtils.readField(bean, field);
        writer.put(bean.buildKey(key), val, lsp.comment());
    }
}

From source file:cn.wanghaomiao.seimi.utils.StructValidator.java

public static boolean validateAnno(Object object) {
    for (Field field : object.getClass().getDeclaredFields()) {
        NotNull notNullCheck = field.getAnnotation(NotNull.class);
        if (notNullCheck != null) {
            try {
                Object val = FieldUtils.readField(field, object, true);
                if (StringUtils.isBlank(String.valueOf(val))) {
                    logger.error("Field={}.{} can not be null!", object.getClass().getSimpleName(),
                            field.getName());
                    return false;
                }//from ww w  .j ava 2 s.com
            } catch (IllegalAccessException e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
    return true;
}