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.aw.core.dao.bean.meta.BeanMetaInfo.java

private void build() {
    //beanClass = bean.getClass();
    hbmUtil = HbmUtilFactory.newInstance();

    // datos a nivel de clase
    DAOSqlTable daoSqlTable = (DAOSqlTable) beanClass.getAnnotation(DAOSqlTable.class);
    DAOHbmTable daoHbmTable = (DAOHbmTable) beanClass.getAnnotation(DAOHbmTable.class);
    DAOHbmTableEx daoHbmTableEx = (DAOHbmTableEx) beanClass.getAnnotation(DAOHbmTableEx.class);
    if (daoSqlTable != null) {
        mainTable = daoSqlTable.value();
    } else if (daoHbmTable != null) {
        mainHbmEntity = daoHbmTable.value();
        mainTable = hbmUtil.getTableName(mainHbmEntity);
    } else if (daoHbmTableEx != null) {
        mainHbmEntity = daoHbmTableEx.tabla();
        mainTable = hbmUtil.getTableName(mainHbmEntity);
        mainSufijoCorto = daoHbmTableEx.sufijoCorto();
        mainSufijoLargo = daoHbmTableEx.sufijoLargo();
    }/*from w  w w . j a  va  2 s .com*/

    // datos a nivel de campo/atributo
    Field[] fields = beanClass.getFields();
    if (fields.length == 0)
        throw new IllegalArgumentException("Class " + beanClass + " need to have public atributes");
    for (Field field : fields) {
        ColumnInfo columnInfo = null;
        DAOSqlColumn daoSqlColumn = field.getAnnotation(DAOSqlColumn.class);
        DAOHbmColumn daoHbmColumn = field.getAnnotation(DAOHbmColumn.class);
        DAOHbmColumnEx daoHbmColumnEx = field.getAnnotation(DAOHbmColumnEx.class);
        if (daoSqlColumn != null) {
            columnInfo = buildSqlColumn(field.getName(), daoSqlColumn.value());
        } else if (daoHbmColumn != null) {
            columnInfo = buildHbmColumn(field.getName(), mainHbmEntity, daoHbmColumn.value());
        } else if (daoHbmColumnEx != null) {
            columnInfo = buildHbmColumn(field.getName(), daoHbmColumnEx.table(), daoHbmColumnEx.column());
        } else {
            // tratar de hacer match con atributos de hibernate
            columnInfo = buildHbmColumn(field.getName(), mainHbmEntity, field.getName());
        }

        if (columnInfo.isPk() && columnInfo.getTable().equals(mainTable))
            idColumnsMap.put(columnInfo.getBeanField(), columnInfo);
        else
            nonIdColumnsMap.put(columnInfo.getBeanField(), columnInfo);

    }

}

From source file:com.khubla.cbean.CBeanType.java

private Field[] findLazyLoadFields() throws CBeanException {
    final List<Field> fieldList = new ArrayList<Field>();
    for (int i = 0; i < persistableFields.length; i++) {
        final Field field = persistableFields[i];
        final Property property = field.getAnnotation(Property.class);
        /*// w ww  .  j  av  a 2  s. c o m
         * check if it's a lazy load
         */
        if (null != property) {
            if ((false == property.cascadeLoad()) && (true == CBean.isEntity(field.getType()))) {
                fieldList.add(field);
            }
        }
    }
    final Field[] ret = new Field[fieldList.size()];
    fieldList.toArray(ret);
    return ret;
}

From source file:me.henrytao.observableorm.orm.ObservableModel.java

public Map<String, Object> serialize() throws IllegalAccessException {
    Map<String, Object> map = new HashMap<>();
    Field[] fields = getDeclaredFields();
    for (Field f : fields) {
        if (!f.isAnnotationPresent(Column.class)) {
            continue;
        }//  w  w  w.j a va 2 s.com
        Column column = f.getAnnotation(Column.class);
        if (!column.serialize()) {
            continue;
        }
        f.setAccessible(true);
        String name = column.name();
        Object value = f.get(this);
        Class type = f.getType();
        Serializer serializer = serializerMap.get(type);
        if (value == null) {
            continue;
        } else if (serializer != null) {
            map.put(name, serializer.serialize(value));
        } else if (ObservableModel.class.isAssignableFrom(type)) {
            // todo: need to test
            map.put(name, ((ObservableModel) value).serialize());
        } else {
            map.put(name, value);
        }
    }
    return map;
}

From source file:name.ikysil.beanpathdsl.codegen.Context.java

private <A extends Annotation> Map<Class<?>, A> scanForAnnotation(Class<A> annotationClass) {
    Map<Class<?>, A> result = new HashMap<>();
    Set<Class<?>> types = reflections.getTypesAnnotatedWith(annotationClass);
    for (Class<?> type : types) {
        result.put(resolveClass(type), type.getAnnotation(annotationClass));
    }//from  w ww .ja v a  2  s.  co m
    Set<Field> fields = reflections.getFieldsAnnotatedWith(annotationClass);
    for (Field field : fields) {
        result.put(resolveClass(field.getType()), field.getAnnotation(annotationClass));
    }
    Set<Method> methods = reflections.getMethodsAnnotatedWith(annotationClass);
    for (Method method : methods) {
        scanMethod(result, method, annotationClass);
    }
    methods = reflections.getMethodsWithAnyParamAnnotated(annotationClass);
    for (Method method : methods) {
        scanMethod(result, method, annotationClass);
    }
    return result;
}

From source file:net.ceos.project.poi.annotated.annotation.XlsElementTest.java

/**
 * Test initialization of the comment attribute with specific value.
 *//*from  w w  w .j a va 2s  . c o  m*/
@Test
public void checkCommentAttribute() {
    Class<XMenFactory.Cyclops> oC = XMenFactory.Cyclops.class;

    List<Field> fL = Arrays.asList(oC.getDeclaredFields());
    for (Field f : fL) {
        // Process @XlsElement
        if (f.isAnnotationPresent(XlsElement.class)) {

            XlsElement xlsElement = (XlsElement) f.getAnnotation(XlsElement.class);

            if (StringUtils.isNotBlank(xlsElement.comment())) {
                assertEquals(xlsElement.comment(), "This is a comment");
            }
        }
    }
}

From source file:net.ceos.project.poi.annotated.annotation.XlsElementTest.java

/**
 * Test initialization of the decorator attribute with specific value.
 *///  ww w.  java2  s . c o m
@Test
public void checkDecoratorAttribute() {
    Class<XMenFactory.Cyclops> oC = XMenFactory.Cyclops.class;

    List<Field> fL = Arrays.asList(oC.getDeclaredFields());
    for (Field f : fL) {
        // Process @XlsElement
        if (f.isAnnotationPresent(XlsElement.class)) {

            XlsElement xlsElement = (XlsElement) f.getAnnotation(XlsElement.class);

            if (StringUtils.isNotBlank(xlsElement.decorator())) {
                assertEquals(xlsElement.decorator(), "extendedIntDecorator");
            }
        }
    }
}

From source file:net.ceos.project.poi.annotated.annotation.XlsElementTest.java

/**
 * Test initialization of the formatMask attribute with specific value.
 *///from w ww. ja  v a 2  s .  co m
@Test
public void checkFormatMaskAttribute() {
    Class<XMenFactory.Cyclops> oC = XMenFactory.Cyclops.class;

    List<Field> fL = Arrays.asList(oC.getDeclaredFields());
    for (Field f : fL) {
        // Process @XlsElement
        if (f.isAnnotationPresent(XlsElement.class)) {

            XlsElement xlsElement = (XlsElement) f.getAnnotation(XlsElement.class);

            if (StringUtils.isNotBlank(xlsElement.formatMask())) {
                assertEquals(xlsElement.formatMask(), "0.000");
            }
        }
    }
}

From source file:net.ceos.project.poi.annotated.annotation.XlsElementTest.java

/**
 * Test initialization of the transformMask attribute with specific value.
 *//*ww w  .j a  v  a  2s .  c om*/
@Test
public void checkTransformMaskAttribute() {
    Class<XMenFactory.Cyclops> oC = XMenFactory.Cyclops.class;

    List<Field> fL = Arrays.asList(oC.getDeclaredFields());
    for (Field f : fL) {
        // Process @XlsElement
        if (f.isAnnotationPresent(XlsElement.class)) {

            XlsElement xlsElement = (XlsElement) f.getAnnotation(XlsElement.class);

            if (StringUtils.isNotBlank(xlsElement.transformMask())) {
                assertEquals(xlsElement.transformMask(), "0.0");
            }
        }
    }
}

From source file:net.ceos.project.poi.annotated.annotation.XlsElementTest.java

/**
 * Test initialization of the isFormula & formula attribute with specific
 * value.//from   ww w.ja  va 2 s . co m
 */
@Test
public void checkFormulaAttribute() {
    Class<XMenFactory.Cyclops> oC = XMenFactory.Cyclops.class;

    List<Field> fL = Arrays.asList(oC.getDeclaredFields());
    for (Field f : fL) {
        // Process @XlsElement
        if (f.isAnnotationPresent(XlsElement.class)) {

            XlsElement xlsElement = (XlsElement) f.getAnnotation(XlsElement.class);

            if (xlsElement.isFormula()) {
                assertEquals(xlsElement.formula(), "SUM(E3,F3)");
            }
        }
    }
}

From source file:com.logsniffer.util.value.ConfigInjector.java

@Override
public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException {
    ReflectionUtils.doWithFields(bean.getClass(), new FieldCallback() {
        @Override//from w ww  .  ja va  2 s. c o  m
        public void doWith(final Field field) throws IllegalArgumentException, IllegalAccessException {
            LOGGER.debug("Injecting value={} for bean={}", field.getName(), beanName);
            field.setAccessible(true);
            final String key = field.getAnnotation(Configured.class).value();
            final String defaultValue = field.getAnnotation(Configured.class).defaultValue();
            final Class<?> targetValueType = (Class<?>) ((ParameterizedType) field.getGenericType())
                    .getActualTypeArguments()[0];
            field.set(bean, new ConfigValue<Object>() {
                private String oldTextValue;
                private Object oldValue;

                @Override
                public Object get() {
                    String textValue = getSource().getValue(key);
                    if (textValue == null) {
                        textValue = defaultValue;
                    }
                    if (oldTextValue != null && oldTextValue.equals(textValue)) {
                        return oldValue;
                    } else {
                        oldValue = conversionService.convert(textValue, targetValueType);
                        oldTextValue = textValue;
                        return oldValue;
                    }
                }
            });

        }
    }, new FieldFilter() {
        @Override
        public boolean matches(final Field field) {
            return field.getType().equals(ConfigValue.class) && field.isAnnotationPresent(Configured.class);
        }
    });

    return bean;
}