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:br.com.renatoccosta.regexrenamer.element.base.ElementFactory.java

public static void setParameter(Element element, Field field, Object value) throws InvalidParameterException {
    try {/*from   w ww  .  j ava 2s .c o m*/
        //for security reasons, only parameters can be set with this method
        if (field.getAnnotation(Parameter.class) != null) {
            if (value instanceof String) {
                value = utilsBean.getConvertUtils().convert((String) value, field.getType());
            }

            PropertyUtils.setProperty(element, field.getName(), value);
        } else {
            throw createInvalidParameterException(field.getName(), null);
        }
    } catch (Exception ex) {
        throw createInvalidParameterException(field.getName(), ex);
    }
}

From source file:com.anteam.demo.codec.common.CoderUtil.java

public static Object encode(Object source, EncryptAlgorithm encryptAlgorithm, byte[] key)
        throws EncoderException {
    if (source == null || encryptAlgorithm == null) {
        return null;
    }/*from  w  w  w.j  a  v a2 s  .  com*/
    Object result = source;
    if (source instanceof byte[]) {
        return CoderUtil.encode((byte[]) source, encryptAlgorithm, key);
    } else if (source instanceof String) {
        return CoderUtil.encode((String) source, encryptAlgorithm, key);
    }
    Field[] fields = source.getClass().getDeclaredFields();
    for (Field field : fields) {
        Encrypted encrypted = field.getAnnotation(Encrypted.class);
        if (encrypted != null) {
            ReflectionUtils.makeAccessible(field);
            if (!Modifier.isStatic(field.getModifiers())) {
                try {
                    field.set(source, CoderUtil.encode(field.get(source)));
                } catch (IllegalAccessException e) {
                    LOG.error("?:" + source.getClass().getName() + ":" + field.getName(), e);
                }
            }
        }
    }
    return result;
}

From source file:com.anteam.demo.codec.common.CoderUtil.java

public static Object decode(Object source, EncryptAlgorithm encryptAlgorithm, byte[] key)
        throws DecoderException {
    if (source == null || encryptAlgorithm == null) {
        return null;
    }//from  w  ww .  jav  a  2s  .c  o m
    Object result = source;
    if (source instanceof byte[]) {
        return CoderUtil.decode((byte[]) source, encryptAlgorithm, key);
    } else if (source instanceof String) {
        return CoderUtil.decode((String) source, encryptAlgorithm, key);
    }
    Field[] fields = source.getClass().getDeclaredFields();
    for (Field field : fields) {
        Encrypted encrypted = field.getAnnotation(Encrypted.class);
        if (encrypted != null) {
            ReflectionUtils.makeAccessible(field);
            if (!Modifier.isStatic(field.getModifiers())) {
                try {
                    field.set(source, CoderUtil.decode(field.get(source)));
                } catch (IllegalAccessException e) {
                    LOG.error("?:" + source.getClass().getName() + ":" + field.getName(), e);
                }
            }
        }
    }
    return result;
}

From source file:cn.xdf.thinkutils.db2.util.sql.InsertSqlBuilder.java

/**
 * ,?//from w  w w . ja v a 2 s.  c o m
 * 
 * @return
 * @throws DBException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 */
public static ArrayListEx getFieldsAndValue(Object entity)
        throws DBException, IllegalArgumentException, IllegalAccessException {
    // TODO Auto-generated method stub
    ArrayListEx arrayList = new ArrayListEx();
    if (entity == null) {
        throw new DBException("?");
    }
    Class<?> clazz = entity.getClass();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {

        if (!DBUtils.isTransient(field)) {
            if (DBUtils.isBaseDateType(field)) {
                PrimaryKey annotation = field.getAnnotation(PrimaryKey.class);
                if (annotation != null && annotation.autoIncrement()) {

                } else {
                    String columnName = DBUtils.getColumnByField(field);
                    field.setAccessible(true);
                    arrayList.add((columnName != null && !columnName.equals("")) ? columnName : field.getName(),
                            field.get(entity) == null ? null : field.get(entity).toString());
                }

            }
        }
    }
    return arrayList;
}

From source file:com.changhong.util.db.util.sql.InsertSqlBuilder.java

/**
 * ,?//from   w w w  .  j  ava2s  . c o  m
 * 
 * @return
 * @throws CHDBException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 */
public static CHArrayList getFieldsAndValue(Object entity)
        throws CHDBException, IllegalArgumentException, IllegalAccessException {
    // TODO Auto-generated method stub
    CHArrayList arrayList = new CHArrayList();
    if (entity == null) {
        throw new CHDBException("?");
    }
    Class<?> clazz = entity.getClass();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {

        if (!DBUtils.isTransient(field)) {
            if (DBUtils.isBaseDateType(field)) {
                CHPrimaryKey annotation = field.getAnnotation(CHPrimaryKey.class);
                if (annotation == null || !annotation.autoIncrement()) {
                    String columnName = DBUtils.getColumnByField(field);
                    columnName = (columnName != null && !columnName.equals("")) ? columnName : field.getName();
                    field.setAccessible(true);
                    String val = DBUtils.toString(field.get(entity));
                    arrayList.add(columnName, val);
                }

            }
        }
    }
    return arrayList;
}

From source file:com.dianping.avatar.cache.util.CacheAnnotationUtils.java

/**
 * Retrieve the cache key values from entity instance
 *//*from  w w  w .  j a v a  2 s. c  o  m*/
public static Object[] getCacheKeyValues(Object entity) {
    if (entity == null) {
        throw new IllegalArgumentException("Entity is null.");
    }

    Class<?> cz = entity.getClass();

    Cache cache = cz.getAnnotation(Cache.class);

    if (cache == null) {
        throw new SystemException("The entity must be annotated by Cache.");
    }

    Field[] fields = ClassUtils.getDeclaredFields(cz);

    final List<OrderedField> cacheFields = new ArrayList<OrderedField>();

    // Extract annotated fields
    for (int i = 0; i < fields.length; i++) {
        Field f = fields[i];
        CacheParam fCache = f.getAnnotation(CacheParam.class);
        if (fCache != null) {
            cacheFields.add(new OrderedField(f, i, fCache.order()));
        }
    }

    // Extract declared fields
    for (int i = 0; i < cache.fields().length; i++) {
        String fieldName = cache.fields()[i];
        if (fieldName.isEmpty()) {
            continue;
        }
        Field f = ReflectionUtils.findField(cz, fieldName);
        if (f == null) {
            throw new IllegalArgumentException(
                    "Invalid cahce parameter " + fieldName + ", the filed is not exists.");
        }

        cacheFields.add(new OrderedField(f, i, -Integer.MAX_VALUE + i));
    }

    Collections.sort(cacheFields);

    Object[] values = new Object[cacheFields.size()];

    for (int i = 0; i < cacheFields.size(); i++) {
        OrderedField oField = cacheFields.get(i);

        ReflectionUtils.makeAccessible((Field) oField.field);

        values[i] = ReflectionUtils.getField((Field) oField.field, entity);
    }

    return values;
}

From source file:ea.compoment.db.util.sql.InsertSqlBuilder.java

/**
 * ,?/*from   w  ww.ja v a  2  s  .  c  om*/
 * 
 * @return
 * @throws DBException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 */
public static NVArrayList getFieldsAndValue(Object entity)
        throws DBException, IllegalArgumentException, IllegalAccessException {
    NVArrayList arrayList = new NVArrayList();
    if (entity == null) {
        throw new DBException("?");
    }
    Class<?> clazz = entity.getClass();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {

        if (!DBAnnoUtils.isTransient(field)) {
            if (DBAnnoUtils.isBaseDateType(field)) {
                PrimaryKey annotation = field.getAnnotation(PrimaryKey.class);
                if (annotation != null && annotation.autoIncrement()) {

                } else {
                    String columnName = DBAnnoUtils.getColumnByField(field);
                    field.setAccessible(true);
                    arrayList.add((columnName != null && !columnName.equals("")) ? columnName : field.getName(),
                            field.get(entity) == null ? null : field.get(entity).toString());
                }

            }
        }
    }
    return arrayList;
}

From source file:tv.arte.resteventapi.core.presentation.decoration.RestEventApiDecorationProvider.java

/**
 * Retrieve a decoration context related to a given bean
 * //from w  ww .  ja v a2 s .co m
 * @param bean A given bean
 * @return A map with field names as keys and appropriate objects as values
 */
public static Map<String, ?> getBeanDecorationContext(final Object bean) {
    Map<String, Object> decorationContext = new LinkedHashMap<String, Object>();

    //Construct Hateoas self referencing link
    if (hrefedBeansControllerMethodMapping.containsKey(bean.getClass())) {
        Method method = hrefedBeansControllerMethodMapping.get(bean.getClass());
        final Map<String, Object> paramValue = new LinkedHashMap<String, Object>();

        ReflectionUtils.doWithFields(bean.getClass(), new FieldCallback() {
            public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {

                String hrefedValue = field.getAnnotation(HrefedParamField.class).value();

                if (StringUtils.isBlank(hrefedValue)) {
                    hrefedValue = field.getName();
                }

                Object fieldValue = null;
                try {
                    fieldValue = PropertyUtils.getProperty(bean, field.getName());
                } catch (Exception e) {
                    throw new RestEventApiRuntimeException("Unable to access the bean property value.");
                }
                if (fieldValue != null) {
                    paramValue.put(hrefedValue, fieldValue);
                }
            }
        }, new FieldFilter() {
            public boolean matches(Field field) {
                return field.isAnnotationPresent(HrefedParamField.class);
            }
        });

        decorationContext.put(HATEOAS_HREF_KEY, RestEventApiControllerLinkBuilder
                .linkTo(method.getDeclaringClass(), method, paramValue, conversionService));
    }

    return decorationContext;
}

From source file:com.alading.library.util.db.util.sql.TAInsertSqlBuilder.java

/**
 * ,?/*from  w w w .  jav  a  2s  .c o  m*/
 * 
 * @return
 * @throws TADBException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 */
public static TAArrayList getFieldsAndValue(Object entity)
        throws TADBException, IllegalArgumentException, IllegalAccessException {
    // TODO Auto-generated method stub
    TAArrayList arrayList = new TAArrayList();
    if (entity == null) {
        throw new TADBException("?");
    }
    Class<?> clazz = entity.getClass();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {

        if (!TADBUtils.isTransient(field)) {
            if (TADBUtils.isBaseDateType(field)) {
                TAPrimaryKey annotation = field.getAnnotation(TAPrimaryKey.class);
                if (annotation != null && annotation.autoIncrement()) {

                } else {
                    String columnName = TADBUtils.getColumnByField(field);
                    field.setAccessible(true);
                    arrayList.add((columnName != null && !columnName.equals("")) ? columnName : field.getName(),
                            field.get(entity) == null ? null : field.get(entity).toString());
                }

            }
        }
    }
    return arrayList;
}

From source file:com.witness.utils.db.util.sql.TAInsertSqlBuilder.java

/**
 * ,?/*from   w  ww  .  j a va2s.  co m*/
 * 
 * @return
 * @throws TADBException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 */
public static TAArrayList getFieldsAndValue(Object entity)
        throws TADBException, IllegalArgumentException, IllegalAccessException {
    // TODO Auto-generated method stub
    TAArrayList arrayList = new TAArrayList();
    if (entity == null) {
        throw new TADBException("?");
    }
    Class<?> clazz = entity.getClass();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {

        if (!TADBUtils.isTransient(field)) {
            if (TADBUtils.isBaseDateType(field)) {
                DBPrimaryKey annotation = field.getAnnotation(DBPrimaryKey.class);
                if (annotation != null && annotation.autoIncrement()) {

                } else {
                    String columnName = TADBUtils.getColumnByField(field);
                    field.setAccessible(true);
                    arrayList.add((columnName != null && !columnName.equals("")) ? columnName : field.getName(),
                            field.get(entity) == null ? null : field.get(entity).toString());
                }

            }
        }
    }
    return arrayList;
}