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:edu.usu.sdl.openstorefront.core.util.EntityUtil.java

/**
 * Compares to consume fields of two object of the same type Note this won't
 * work with proxy object./*from www.  j a  v a  2 s . co  m*/
 *
 * @param original
 * @param compare
 * @return compare value (0 if equals)
 */
public static int compareConsumeFields(Object original, Object compare) {
    int value = 0;
    if (original != null && compare == null) {
        value = -1;
    } else if (original == null && compare != null) {
        value = 1;
    } else if (original != null && compare != null) {
        if (original.getClass().isInstance(compare)) {
            List<Field> fields = getAllFields(original.getClass());
            for (Field field : fields) {
                ConsumeField consume = (ConsumeField) field.getAnnotation(ConsumeField.class);
                if (consume != null) {
                    try {
                        field.setAccessible(true);
                        value = compareObjects((Comparable) field.get(original),
                                (Comparable) field.get(compare));
                        if (value != 0) {
                            break;
                        }
                    } catch (IllegalArgumentException | IllegalAccessException ex) {
                        throw new OpenStorefrontRuntimeException("Can't compare object fields", ex);
                    }
                }
            }
        } else {
            throw new OpenStorefrontRuntimeException("Can't compare different object types", "Check objects");
        }
    }
    return value;
}

From source file:net.ostis.sc.memory.SCKeynodesBase.java

private static boolean checkKeynode(SCSession session, SCSegment defaultSegment, Field field)
        throws IllegalArgumentException, IllegalAccessException {
    Keynode annotation = field.getAnnotation(Keynode.class);

    if (annotation != null) {
        String keynodeName = annotation.value();

        if (StringUtils.isEmpty(keynodeName))
            keynodeName = field.getName();

        SCAddr keynode = session.findByIdtf(keynodeName, defaultSegment);
        Validate.notNull(keynode,//from w w w.  j  av  a2 s .  c o  m
                "Not found keynode with URI \"" + defaultSegment.getURI() + "/" + keynodeName + "\"");

        field.set(null, keynode);

        if (log.isDebugEnabled())
            log.debug(defaultSegment.getURI() + "/" + keynodeName + " --> " + field.getName());

        return true;
    } else {
        return false;
    }
}

From source file:net.ostis.sc.memory.SCKeynodesBase.java

private static boolean checkKeynodeURI(SCSession session, Field field)
        throws IllegalArgumentException, IllegalAccessException {
    KeynodeURI keynodeURI = field.getAnnotation(KeynodeURI.class);

    if (keynodeURI != null) {
        String[] comp = URIUtils.splitByIdtf(keynodeURI.value());

        SCSegment segment = session.openSegment(comp[0]);

        SCAddr keynode = session.findByIdtf(comp[1], segment);
        Validate.notNull(keynode);//from  w w  w  .  j  ava  2s.  c o  m

        field.set(null, keynode);

        if (log.isDebugEnabled())
            log.debug(keynodeURI.value() + " --> " + field.getName());

        return true;
    } else {
        return false;
    }
}

From source file:co.jirm.mapper.definition.SqlParameterDefinition.java

private static <T extends Annotation> T getAnnotation(Class<?> k, String value, Class<T> a) {
    try {/*  w  ww  . j  a  v  a2 s.  co  m*/
        Field f = k.getDeclaredField(value);
        return f.getAnnotation(a);
    } catch (SecurityException e) {
        throw new RuntimeException(e);
    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.diversityarrays.dal.db.DalDatabaseUtil.java

static public void addEntityFields(Class<? extends DalEntity> entityClass, DalResponseBuilder responseBuilder) {

    responseBuilder.addResponseMeta("SCol");

    for (Field fld : entityClass.getDeclaredFields()) {
        if (!Modifier.isStatic(fld.getModifiers())) {
            Column column = fld.getAnnotation(Column.class);
            if (column != null) {

                DalResponseBuilder builder = responseBuilder.startTag("SCol");

                builder.attribute("Required", column.nullable() ? "0" : "1");

                int colSize = 11;
                Class<?> fieldType = fld.getType();
                if (String.class == fieldType) {
                    colSize = column.length();
                }/*from  w w  w . j a  v  a  2  s.co  m*/
                builder.attribute("ColSize", Integer.toString(colSize));

                builder.attribute("Description", "");

                builder.attribute("Name", column.name());

                // TODO Synchronise with the Perl DAL code
                builder.attribute("DataType", fieldType.getSimpleName().toLowerCase());

                builder.endTag();
            }
        }
    }
}

From source file:com.serli.chell.framework.form.FormStructure.java

private static boolean isRequiredField(Field field, boolean multiValued) {
    if (field.getAnnotation(Required.class) != null) {
        return true;
    } else if (multiValued) {
        Size annotation = field.getAnnotation(Size.class);
        return (annotation != null && (annotation.min() > 0 || annotation.value() > 0));
    } else {/* w  ww .jav a 2 s .  c om*/
        Length annotation = field.getAnnotation(Length.class);
        return (annotation != null && (annotation.min() > 0 || annotation.value() > 0));
    }
}

From source file:com.ykun.commons.utils.excel.ExcelUtils.java

/**
 * ???headersExcelField/*w ww. jav a2s  . c om*/
 *
 * @param <T>  the type parameter
 * @param list ??
 * @return List headers
 */
public static <T> List<String> getHeaders(List<T> list) {
    List<String> headers = new ArrayList<String>();
    if (list != null && list.size() > 0) {
        T t = list.get(0);
        Field[] fields = t.getClass().getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            ExcelField excelField = field.getAnnotation(ExcelField.class);
            if (excelField != null && !excelField.ignore()) {
                headers.add(excelField.value());
            } else if (excelField != null && excelField.ignore()) {
            } else {
                headers.add(field.getName());
            }
        }
    }
    return headers;
}

From source file:org.querybyexample.jpa.JpaUtil.java

public static <T> boolean isPk(ManagedType<T> mt, SingularAttribute<? super T, ?> attr) {
    try {//  w  ww.  j  av  a2 s .  c o  m
        Method m = BeanUtils.findMethod(mt.getJavaType(), "get" + WordUtils.capitalize(attr.getName()));
        if (m != null && m.getAnnotation(Id.class) != null) {
            return true;
        }

        Field field = mt.getJavaType().getField(attr.getName());
        return field.getAnnotation(Id.class) != null;
    } catch (Exception e) {
        return false;
    }
}

From source file:com.alfresco.orm.ORMUtil.java

/**
 * /*from www  .j  ava2 s. co  m*/
 * @param alfrescoContent
 * @throws SecurityException
 * @throws NoSuchMethodException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 * @throws ORMException
 */
public static void executeCustomeMethodForProperty(final AlfrescoContent alfrescoContent,
        final BeanFactory beanFactory) throws SecurityException, NoSuchMethodException,
        IllegalArgumentException, IllegalAccessException, InvocationTargetException, ORMException {
    NodeRef nodeRef = getNodeRef(alfrescoContent);
    List<Field> fields = new ArrayList<Field>();
    ReflectionUtil.getFields(alfrescoContent.getClass(), fields);
    for (Field field : fields) {
        if (field.isAnnotationPresent(SetProperty.class)) {
            SetProperty setProperty = field.getAnnotation(SetProperty.class);
            if (StringUtils.isNotEmpty(setProperty.setPropertMethodName())) {
                Object target = getTargetServiceBean(setProperty.springBeanID(), beanFactory);
                Method customeMethod = target.getClass().getMethod(setProperty.setPropertMethodName(),
                        NodeRef.class, AlfrescoORM.class);
                customeMethod.invoke(target, nodeRef, alfrescoContent);
            } else {
                throw new ORMException("Please set cutome method name to set property");
            }
        }
    }
}

From source file:com.frand.easyandroid.db.sql.FFSqlBuilder.java

public static FFArrayList getFieldsAndValue(Object entity)
        throws FFDBException, IllegalArgumentException, IllegalAccessException {
    FFArrayList arrayList = new FFArrayList();
    if (entity == null) {
        throw new FFDBException("?");
    }//ww  w  .j a  va2 s  .  com
    Class<?> clazz = entity.getClass();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        if (!FFDBUtils.isTransient(field) && FFFieldUtil.isBaseDateType(field)) {
            FFPrimaryKey annotation = field.getAnnotation(FFPrimaryKey.class);
            if (annotation != null && annotation.autoIncrement()) {
            } else {
                String columnName = FFDBUtils.getColumnByField(field);
                field.setAccessible(true);
                if (columnName == null || columnName.equals("")) {
                    columnName = field.getName();
                }
                String value = "";
                if (field.getType().equals(Date.class)) {
                    value = field.get(entity) != null ? FFDBUtils.dateToString((Date) field.get(entity)) : "";
                } else {
                    value = field.get(entity) != null ? field.get(entity).toString() : "";
                }
                arrayList.add(columnName, value);
            }
        }
    }
    return arrayList;
}