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.zhuangjy.dao.ReflectionUtil.java

private static void getFieldsByAnnotation(List<Field> list, Class<? extends Annotation> annotation, Class clz) {
    if (clz.equals(Object.class)) {
        return;/*  w  ww. ja  va  2s . c o m*/
    }
    Field[] fields = clz.getDeclaredFields();
    if (fields == null || fields.length == 0) {
        return;
    }
    for (Field f : fields) {
        f.setAccessible(true);
        if (f.getAnnotation(annotation) != null) {
            list.add(f);
        }
    }
}

From source file:com.ocs.dynamo.test.MockUtil.java

/**
 * Registers all fields that are annotated with "@Mock" as beans in the Spring context
 * /*  www  .  j  a  v  a 2  s.co m*/
 * @param factory
 * @param subject
 * @param clazz
 */
public static void registerMocks(ConfigurableListableBeanFactory factory, Object subject, Class<?> clazz) {
    try {
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            if (field.getAnnotation(Mock.class) != null) {
                factory.registerSingleton(field.getName(), field.get(subject));
            }
        }
        if (clazz.getSuperclass() != null) {
            registerMocks(factory, subject, clazz.getSuperclass());
        }
    } catch (Exception e) {
        throw new OCSRuntimeException(e.getMessage(), e);
    }
}

From source file:com.bynder.sdk.util.Utils.java

/**
 * Method called for each field in a query object. It extracts the different fields with
 * {@link ApiField} annotation and, if needed, converts it according to the conversion type
 * defined.//from  ww w.jav  a  2  s .c om
 *
 * @param field Field information.
 * @param query Query object.
 * @param params Parameters name/value pairs to send to the API.
 *
 * @throws IllegalAccessException If the Field object is inaccessible.
 */
private static void convertField(final Field field, final Object query, final Map<String, String> params)
        throws IllegalAccessException {
    field.setAccessible(true);
    ApiField apiField = field.getAnnotation(ApiField.class);

    if (field.get(query) != null && apiField != null) {
        if (apiField.conversionType() == ConversionType.NONE) {
            params.put(apiField.name(), field.get(query).toString());
        } else {
            if (apiField.conversionType() == ConversionType.METAPROPERTY_FIELD) {
                MetapropertyField metapropertyField = (MetapropertyField) field.get(query);
                params.put(String.format("%s.%s", apiField.name(), metapropertyField.getMetapropertyId()),
                        StringUtils.join(metapropertyField.getOptionsIds(), Utils.STR_COMMA));
            } else if (apiField.conversionType() == ConversionType.LIST_FIELD) {
                List<?> listField = (List<?>) field.get(query);
                params.put(apiField.name(), StringUtils.join(listField, Utils.STR_COMMA));
            }
        }
    }
    field.setAccessible(false);
}

From source file:edu.uchicago.lowasser.flaginjection.Flags.java

public static void addFlagBindings(Binder binder, TypeLiteral<?> literal) {
    for (Field field : literal.getRawType().getDeclaredFields()) {
        if (field.isAnnotationPresent(Flag.class)) {
            Flag annot = field.getAnnotation(Flag.class);
            addFlagBinding(binder, annot, literal.getFieldType(field));
        }/* ww w .jav a2s.  c om*/
    }
    for (Constructor<?> constructor : literal.getRawType().getDeclaredConstructors()) {
        List<TypeLiteral<?>> parameterTypes = literal.getParameterTypes(constructor);
        Annotation[][] parameterAnnotations = constructor.getParameterAnnotations();
        for (int i = 0; i < parameterTypes.size(); i++) {
            Annotation[] annotations = parameterAnnotations[i];
            TypeLiteral<?> typ = parameterTypes.get(i);
            for (Annotation annot : annotations) {
                if (annot instanceof Flag) {
                    addFlagBinding(binder, (Flag) annot, typ);
                }
            }
        }
    }
}

From source file:edu.brown.utils.ClassUtil.java

/**
 * Create a mapping from Field handles to their corresponding Annotation
 * @param <A>//from w  ww. ja  v a  2  s  . c  o m
 * @param fields
 * @param annotationClass
 * @return
 */
public static <A extends Annotation> Map<Field, A> getFieldAnnotations(Field fields[],
        Class<A> annotationClass) {
    Map<Field, A> ret = new LinkedHashMap<Field, A>();
    for (Field f : fields) {
        A a = f.getAnnotation(annotationClass);
        if (a != null)
            ret.put(f, a);
    }
    return (ret);
}

From source file:kina.utils.UtilMongoDB.java

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

    kina.annotations.Field fieldAnnotation = field.getAnnotation(kina.annotations.Field.class);
    Key genericKeyAnnotation = field.getAnnotation(Key.class);
    String fieldName = null;

    if (fieldAnnotation != null) {
        fieldName = fieldAnnotation.fieldName();
    } else if (genericKeyAnnotation != null) {
        fieldName = genericKeyAnnotation.fieldName();
    }

    if (StringUtils.isNotEmpty(fieldName)) {
        return fieldName;
    } else {
        return field.getName();
    }
}

From source file:com.google.code.simplestuff.bean.BusinessObjectAnnotationParser.java

static Collection<Field> collectAnnotatedFields(Class<? extends Object> clazz) {
    final List<Field> businessFields = new ArrayList<Field>();

    ReflectionUtils.doWithFields(clazz, new FieldCallback() {

        // simply add each found field to the list
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            businessFields.add(field);/*from  ww  w .j a v a 2  s  . c  o  m*/
        }

    }, new FieldFilter() {

        // match fields with the "@BusinessField" annotation
        public boolean matches(Field field) {
            return (field.getAnnotation(BusinessField.class) != null);
        }

    });

    return businessFields;
}

From source file:com.seer.datacruncher.jpa.dao.MongoDbDao.java

/**
 * Gets table field name by class field.
 * Example: fieldId = 'idApplication', return = 'id_application'
 *
 * @param fieldId//from w  w w .j  a  v  a2s . co m
 * @return
 */
private static String getFieldName(String fieldId) {
    for (Field field : DatastreamEntity.class.getDeclaredFields()) {
        if (field.getName() == fieldId) {
            return field.getAnnotation(Column.class).name();
        }
    }
    return null;
}

From source file:net.ceos.project.poi.annotated.bean.ObjectMaskBuilder.java

/**
 * Transform BigDecimal values to validate.
 * //from  w  w w.java2 s  .c  o m
 * @param fieldName
 * @param value
 * @return
 */
private static BigDecimal transformValuesToValidate(String fieldName, BigDecimal value) {
    ObjectMask a = new ObjectMask();
    try {
        Field f = a.getClass().getDeclaredField(fieldName);

        if (f.isAnnotationPresent(XlsElement.class)) {
            XlsElement xlsAnnotation = (XlsElement) f.getAnnotation(XlsElement.class);

            if (StringUtils.isNotBlank(xlsAnnotation.transformMask())) {
                DecimalFormat df = new DecimalFormat(xlsAnnotation.transformMask());
                String formattedValue = df.format((BigDecimal) value);
                value = BigDecimal.valueOf(Double.valueOf(formattedValue.replace(",", ".")));
            }
        }

    } catch (NoSuchFieldException | SecurityException e) {
        e.printStackTrace();
    }
    return value;
}

From source file:net.ceos.project.poi.annotated.bean.ObjectMaskBuilder.java

/**
 * Transform Double values to validate.//ww w.  j a  v  a2  s. co m
 * 
 * @param fieldName
 * @param value
 * @return
 */
private static Double transformValuesToValidate(String fieldName, Double value) {
    ObjectMask a = new ObjectMask();

    try {
        Field f = a.getClass().getDeclaredField(fieldName);

        if (f.isAnnotationPresent(XlsElement.class)) {
            XlsElement xlsAnnotation = (XlsElement) f.getAnnotation(XlsElement.class);

            if (StringUtils.isNotBlank(xlsAnnotation.transformMask())) {
                DecimalFormat df = new DecimalFormat(xlsAnnotation.transformMask());
                String formattedValue = df.format((Double) value);
                value = Double.valueOf(formattedValue.replace(",", "."));
            }
        }

    } catch (NoSuchFieldException | SecurityException e) {
        e.printStackTrace();
    }
    return value;
}