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:com.astamuse.asta4d.web.form.flow.base.StepAwaredValidationFormHelper.java

static final Object getValidationTargetByAnnotation(Object form, String step) {
    String cacheKey = step + "@" + form.getClass().getName();
    Optional<Field> oField = ValidationTargetFieldCache.get(cacheKey);

    if (oField == null) {
        List<Field> list = new ArrayList<>(ClassUtil.retrieveAllFieldsIncludeAllSuperClasses(form.getClass()));
        Iterator<Field> it = list.iterator();
        while (it.hasNext()) {
            Field f = it.next();
            StepAwaredValidationTarget vtAnno = f.getAnnotation(StepAwaredValidationTarget.class);
            if (vtAnno == null) {
                continue;
            }//from   w  w w .j  ava  2s  . c  o m
            String representingStep = vtAnno.value();
            if (step.equals(representingStep)) {
                oField = Optional.of(f);
                break;
            }
        }
        if (oField == null) {
            oField = Optional.empty();
        }
        if (Configuration.getConfiguration().isCacheEnable()) {
            Map<String, Optional<Field>> newCache = new HashMap<>(ValidationTargetFieldCache);
            newCache.put(cacheKey, oField);
            ValidationTargetFieldCache = newCache;
        }
    }

    if (oField.isPresent()) {
        try {
            return FieldUtils.readField(oField.get(), form, true);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    } else {
        return form;
    }
}

From source file:springfox.bean.validators.plugins.Validators.java

public static <T extends Annotation> Optional<T> validatorFromExpandedParameter(
        ParameterExpansionContext context, Class<T> annotationType) {

    Field field = context.getField().getRawMember();
    return Optional.fromNullable(field.getAnnotation(annotationType));
}

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

public static List<Pair<Field, Object>> getAnnotatedFieldValues(Storable storable,
        Class<? extends Annotation> clazz)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    List<Pair<Field, Object>> res = new ArrayList<>();
    for (Field field : storable.getClass().getDeclaredFields()) {
        if (field.getAnnotation(clazz) != null) {
            Object val = ReflectionHelper.invokeGetter(field.getName(), storable);
            if (val != null) {
                res.add(Pair.of(field, val));
            }//from w  w w .  j  a  v  a  2 s .c  o m
        }
    }
    return res;
}

From source file:cn.wanghaomiao.seimi.core.SeimiBeanResolver.java

public static <T> T parse(Class<T> target, String text) throws Exception {
    T bean = target.newInstance();/*from  w  ww.  j ava2 s.  c  o  m*/
    final List<Field> props = new LinkedList<>();
    ReflectionUtils.doWithFields(target, new ReflectionUtils.FieldCallback() {
        @Override
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            props.add(field);
        }
    });
    JXDocument jxDocument = new JXDocument(text);
    for (Field f : props) {
        Xpath xpathInfo = f.getAnnotation(Xpath.class);
        if (xpathInfo != null) {
            String xpath = xpathInfo.value();
            List<Object> res = jxDocument.sel(xpath);
            boolean accessFlag = f.isAccessible();
            f.setAccessible(true);
            f.set(bean, defaultCastToTargetValue(target, f, res));
            f.setAccessible(accessFlag);
        }
    }
    return bean;
}

From source file:net.eledge.android.toolkit.db.internal.SQLBuilder.java

public static String getIdField(Class<?> clazz) {
    for (Field field : clazz.getFields()) {
        if (field.isAnnotationPresent(Id.class)) {
            Column column = field.getAnnotation(Column.class);
            return StringUtils.defaultIfBlank(column.name(), field.getName().toLowerCase(Locale.ENGLISH));
        }/*from www. j a va2s . com*/
    }
    return null;
}

From source file:iterator.Reflection.java

public static <A extends Annotation> A findFieldAnnotation(Class<?> clazz, String fieldName,
        Class<A> annotationClass) {
    Field f = findField(clazz, fieldName);
    return f != null ? f.getAnnotation(annotationClass) : null;
}

From source file:com.stratio.deep.commons.utils.AnnotationUtils.java

/**
 * Returns the field name as known by the datastore. If the provided field object DeepField annotation
 * specifies the fieldName property, the value of this property will be returned, otherwise the java field name
 * will be returned.//from  ww  w.  j  a  v a 2 s  .  c  om
 *
 * @param field the Field object associated to the property for which we want to resolve the name.
 * @return the field name.
 */
public static String deepFieldName(Field field) {

    DeepField annotation = field.getAnnotation(DeepField.class);
    if (StringUtils.isNotEmpty(annotation.fieldName())) {
        return annotation.fieldName();
    } else {
        return field.getName();
    }
}

From source file:com.bosscs.spark.commons.utils.AnnotationUtils.java

/**
 * Returns the field name as known by the datastore. If the provided field object DeepField annotation
 * specifies the fieldName property, the value of this property will be returned, otherwise the java field name
 * will be returned.//  ww w  .j  a v  a 2s.com
 *
 * @param field the Field object associated to the property for which we want to resolve the name.
 * @return the field name.
 */
public static String deepFieldName(Field field) {

    HadoopField annotation = field.getAnnotation(HadoopField.class);
    if (StringUtils.isNotEmpty(annotation.fieldName())) {
        return annotation.fieldName();
    } else {
        return field.getName();
    }
}

From source file:com.jim.im.offline.repo.MessageConverter.java

private static ImMessage convert(DBObject dbObject) {
    ImMessage imMessage = new ImMessage();
    imMessage.setCreateTime(0L);//w  ww.jav  a2s. c  o  m
    Map map = dbObject.toMap();
    for (String key : FIELD_KEYS) {
        Field field = FIELD_MAP.get(key);
        org.springframework.data.mongodb.core.mapping.Field annotation = field.getAnnotation(ALIAS_NAME);
        if (annotation != null)
            key = annotation.value();
        field.setAccessible(true);
        Object value = getValue(field, map.get(key));
        if (value == null)
            continue;
        ReflectionUtils.setField(field, imMessage, value);
    }

    return imMessage;
}

From source file:com.bosscs.spark.commons.utils.AnnotationUtils.java

/**
 * Return a pair of Field[] whose left element is
 * the array of keys fields.// w  w  w  . j a v  a  2 s  .  c  om
 * The right element contains the array of all other non-key fields.
 *
 * @param clazz the Class object
 * @return a pair object whose first element contains key fields, and whose second element contains all other columns.
 */
public static Pair<Field[], Field[]> filterKeyFields(Class clazz) {
    Field[] filtered = filterDeepFields(clazz);
    List<Field> keys = new ArrayList<>();
    List<Field> others = new ArrayList<>();

    for (Field field : filtered) {
        if (isKey(field.getAnnotation(HadoopField.class))) {
            keys.add(field);
        } else {
            others.add(field);
        }
    }

    return Pair.create(keys.toArray(new Field[keys.size()]), others.toArray(new Field[others.size()]));
}