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.yimidida.shards.utils.ParameterUtil.java

public static Serializable extractPrimaryKey(Object object) {
    if (object != null) {
        Class<?> clazz = object.getClass();
        Field[] first = clazz.getDeclaredFields();
        Field[] second = clazz.getSuperclass().getDeclaredFields();

        Field[] fields = Arrays.copyOf(first, first.length + second.length);
        System.arraycopy(second, 0, fields, first.length, second.length);

        for (Field field : fields) {
            field.setAccessible(true);//www  .j a v a2s  .  com

            PrimaryKey primaryKey = field.getAnnotation(PrimaryKey.class);
            if (primaryKey != null) {
                try {
                    //0
                    Object result = field.get(object);
                    if (result != null && "0".equals(result.toString())) {
                        return null;
                    }
                    return (Serializable) result;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

    return null;
}

From source file:com.yimidida.shards.utils.ParameterUtil.java

public static Object generatePrimaryKey(Object object, Serializable id) {
    if (object != null) {
        Assert.notNull(id, "generated id can not be null.");

        Class<?> clazz = object.getClass();
        Field[] first = clazz.getDeclaredFields();
        Field[] second = clazz.getSuperclass().getDeclaredFields();

        Field[] fields = Arrays.copyOf(first, first.length + second.length);
        System.arraycopy(second, 0, fields, first.length, second.length);

        for (Field field : fields) {
            field.setAccessible(true);/* www . j a v  a2  s  .c  o m*/

            PrimaryKey primaryKey = field.getAnnotation(PrimaryKey.class);
            if (primaryKey != null) {
                try {
                    //set id
                    field.set(object, id);

                    return object;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

    return null;
}

From source file:edu.usu.sdl.openstorefront.core.util.EntityUtil.java

/**
 * Finds the PK field of an entity (there should only be one)
 *
 * @param <T>//from w  w w.j  a v  a2 s  . co  m
 * @param entity
 * @return the PK field or null if not found
 */
public static <T extends BaseEntity> Field getPKField(T entity) {
    Objects.requireNonNull(entity, "Entity must not be NULL");
    Field pkField = null;

    List<Field> fields = ReflectionUtil.getAllFields(entity.getClass());
    for (Field field : fields) {
        PK idAnnotation = field.getAnnotation(PK.class);
        if (idAnnotation != null) {
            pkField = field;
        }
    }
    return pkField;
}

From source file:com.netflix.paas.config.base.ConfigurationProxyUtils.java

static void assignFieldValues(final Object obj, Class<?> type, DynamicPropertyFactory propertyFactory,
        AbstractConfiguration configuration) throws Exception {

    // Iterate through all fields and set initial value as well as set up dynamic properties
    // where necessary
    for (final Field field : type.getFields()) {
        Configuration c = field.getAnnotation(Configuration.class);
        if (c == null)
            continue;

        String defaultValue = field.get(obj).toString();
        String name = ConfigurationProxyUtils.getPropertyName(field, c);
        Supplier<?> supplier = ConfigurationProxyUtils.getStaticSupplier(field.getType(), name, defaultValue,
                configuration);//from   www  .j  av  a2  s.  c o  m
        field.set(obj, supplier.get());

        if (field.getAnnotation(Dynamic.class) != null) {
            final PropertyWrapper<?> property;
            if (field.getType().isAssignableFrom(String.class)) {
                property = propertyFactory.getStringProperty(name, defaultValue);
            } else if (field.getType().isAssignableFrom(Integer.class)) {
                property = propertyFactory.getIntProperty(name,
                        defaultValue == null ? 0 : Integer.parseInt(defaultValue));
            } else if (field.getType().isAssignableFrom(Double.class)) {
                property = propertyFactory.getDoubleProperty(name,
                        defaultValue == null ? 0.0 : Double.parseDouble(defaultValue));
            } else if (field.getType().isAssignableFrom(Long.class)) {
                property = propertyFactory.getLongProperty(name,
                        defaultValue == null ? 0L : Long.parseLong(defaultValue));
            } else if (field.getType().isAssignableFrom(Boolean.class)) {
                property = propertyFactory.getBooleanProperty(name,
                        defaultValue == null ? false : Boolean.parseBoolean(defaultValue));
            } else {
                throw new RuntimeException("Unsupported type " + field.getType());
            }

            property.addCallback(new Runnable() {
                @Override
                public void run() {
                    try {
                        field.set(obj, property.getValue());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
}

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

/**
 * ,?//ww w  .  j ava 2  s  . c om
 * 
 * @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()) {
                    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.hand.hap.mybatis.util.OGNL.java

/**
 * FOR INTERNAL USE ONLY/*w w w . j  av  a  2s.co  m*/
 * 
 * @param parameter
 * @return
 */
public static String getOrderByClause_TL(Object parameter) {
    if (parameter == null) {
        return null;
    }
    StringBuilder sb = new StringBuilder(64);
    if (parameter instanceof BaseDTO) {
        String sortName = ((BaseDTO) parameter).getSortname();
        Field[] ids = DTOClassInfo.getIdFields(parameter.getClass());
        if (StringUtil.isNotEmpty(sortName)) {
            if (!COL_PATTERN.matcher(sortName).matches()) {
                throw new RuntimeException("Invalid sortname:" + sortName);
            }
            String order = ((BaseDTO) parameter).getSortorder();
            if (!("ASC".equalsIgnoreCase(order) || "DESC".equalsIgnoreCase(order) || order == null)) {
                throw new RuntimeException("Invalid sortorder:" + order);
            }
            String columnName = unCamel(sortName);
            Field[] mlfs = DTOClassInfo.getMultiLanguageFields(parameter.getClass());
            for (Field f : mlfs) {
                if (f.getName().equals(columnName)) {
                    if (f.getAnnotation(MultiLanguageField.class) == null) {
                        sb.append("b.");
                    } else {
                        sb.append("t.");
                    }
                    break;
                }
            }

            sb.append(columnName).append(" ");
            sb.append(StringUtils.defaultIfEmpty(order, "ASC"));

            if (ids.length > 0 && !ids[0].getName().equals(sortName)) {
                sb.append(",b.").append(DTOClassInfo.getColumnName(ids[0])).append(" ASC");
            }
        } else {
            if (ids.length > 0) {
                sb.append("b.").append(DTOClassInfo.getColumnName(ids[0])).append(" ASC");
            }
        }
    }
    return StringUtils.trimToNull(sb.toString());
}

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

/**
 * ,?/*from  ww w .j  ava 2s  .c om*/
 * 
 * @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:ea.compoment.db.util.sql.UpdateSqlBuilder.java

/**
 * ,?/*w w w. j  av  a  2  s. c  o  m*/
 * 
 * @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()) {
                    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:com.alading.library.util.db.util.sql.TAUpdateSqlBuilder.java

/**
 * ,?/*from  w w w.ja  va  2 s  .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)) {
                TAPrimaryKey annotation = field.getAnnotation(TAPrimaryKey.class);
                if (annotation == null || !annotation.autoIncrement()) {
                    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.TAUpdateSqlBuilder.java

/**
 * ,?//from w w w .j a  va  2 s .  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)) {
                DBPrimaryKey annotation = field.getAnnotation(DBPrimaryKey.class);
                if (annotation == null || !annotation.autoIncrement()) {
                    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;
}