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:net.ceos.project.poi.annotated.annotation.XlsElementTest.java

/**
 * Test default configuration./*  ww  w. j  ava  2s  . co  m*/
 */
@Test
public void checkDefaultConfiguration() {
    Class<XMenFactory.DefaultConfig> oC = XMenFactory.DefaultConfig.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);

            assertEquals(xlsElement.comment(), "");
            assertEquals(xlsElement.commentRules(), "");
            assertEquals(xlsElement.decorator(), "");
            assertEquals(xlsElement.formatMask(), "");
            assertEquals(xlsElement.transformMask(), "");
            assertEquals(xlsElement.isFormula(), false);
            assertEquals(xlsElement.formula(), "");
            assertEquals(xlsElement.customizedRules(), "");
            assertEquals(xlsElement.columnWidthInUnits(), 0);
            assertEquals(xlsElement.parentSheet(), false);
        }
    }
}

From source file:py.una.pol.karaku.dao.entity.interceptors.InterceptorHandler.java

/**
 * Intercepta un atributo de un bean especifico.
 * //w w w  .  j av  a2 s  . com
 * <p>
 * Reglas:
 * <ol>
 * <li>Si el item es un atributo normal, invocar a su respectivo
 * interceptor.
 * </p>
 * <li>Si es una coleccin, y tiene tiene la anotacin {@link OneToMany}, y
 * su {@link CascadeType} es {@link CascadeType#ALL}, entonces se propaga la
 * intercepcin a los miembros de la coleccin. </p>
 * 
 * @param op
 *            Operacin actual.
 * @param field
 *            campo sobre el cual se esta ejecutando.
 * @param bean
 *            objeto que esta siendo interceptado.
 */
private void intercept(@Nonnull Operation op, @Nonnull Field field, @Nonnull Object bean) {

    if (field.getAnnotation(OneToMany.class) != null) {
        OneToMany otm = field.getAnnotation(OneToMany.class);
        CascadeType[] cascade = otm.cascade();
        if (cascade != null && ListHelper.contains(cascade, CascadeType.ALL)) {
            field.setAccessible(true);
            Collection<?> c = (Collection<?>) ReflectionUtils.getField(field, bean);
            if (Hibernate.isInitialized(c) && ListHelper.hasElements(c)) {
                for (Object o : c) {
                    this.intercept(op, o);
                }
            }
        }
        return;
    }
    field.setAccessible(true);
    Class<?> type = field.getType();

    Set<Interceptor> typeInterceptors = addAll(byType.get(void.class), byType.get(type));

    Annotation[] annons = field.getAnnotations();

    Set<Interceptor> annonInterceptors = new HashSet<Interceptor>();
    if (byAnnotation.get(void.class) != null) {
        annonInterceptors.addAll(byAnnotation.get(void.class));
    }

    for (Annotation an : annons) {
        if (byAnnotation.get(an.annotationType()) != null) {
            annonInterceptors.addAll(byAnnotation.get(an.annotationType()));
        }
    }

    typeInterceptors.retainAll(annonInterceptors);

    for (Interceptor bi : typeInterceptors) {
        if (this.isAssignable(field) && bi.interceptable(op, field, bean)) {
            bi.intercept(op, field, bean);
        }
    }
}

From source file:io.mapzone.arena.csw.CswRequest.java

protected Map<String, Object> assembleParams() {
    Map<String, Object> result = new HashMap();

    // only public fields are allowed
    for (Field f : getClass().getFields()) {
        RequestParam a = f.getAnnotation(RequestParam.class);
        if (a != null) {
            try {
                Object value = f.get(this);
                if (value instanceof Config) {
                    result.put(a.value(), ((Config) value).get());
                } else {
                    result.put(a.value(), value);
                }/*from  www.  java2  s .  c  o  m*/
            } catch (RuntimeException e) {
                throw e;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
    return result;
}

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

/**
 * Test initialization of the title attribute over multiples types with
 * specific value./*from w w w  .  j  ava2s .  c om*/
 */
@Test
public void checkTitleAttribute() {
    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 (f.getName().equals("dateAttribute")) {
                assertEquals(xlsElement.title(), "Date value");
            } else if (f.getName().equals("stringAttribute")) {
                assertEquals(xlsElement.title(), "String value");
            } else if (f.getName().equals("integerAttribute")) {
                assertEquals(xlsElement.title(), "Integer value");
            } else if (f.getName().equals("doubleAttribute1")) {
                assertEquals(xlsElement.title(), "Double value 1");
            } else if (f.getName().equals("doubleAttribute2")) {
                assertEquals(xlsElement.title(), "Double value 2");
            } else if (f.getName().equals("sum")) {
                assertEquals(xlsElement.title(), "Sum double 1 & double 2");
            }
        }
    }
}

From source file:com.github.strawberry.guice.config.ConfigLoader.java

@Override
public Option load(Field field) throws Exception {
    return getFromProperties(properties, field, field.getAnnotation(Config.class));
}

From source file:microsoft.exchange.webservices.data.core.EwsUtilities.java

/**
 * Builds the enum to schema mapping dictionary.
 *
 * @param c class type//from   w w w. j  a v  a  2 s  .  c  om
 * @return The mapping from enum to schema name
 */
private static Map<String, String> buildEnumToSchemaDict(Class<?> c) {
    Map<String, String> dict = new HashMap<String, String>();
    Field[] fields = c.getFields();
    for (Field f : fields) {
        if (f.isEnumConstant() && f.isAnnotationPresent(EwsEnum.class)) {
            EwsEnum ewsEnum = f.getAnnotation(EwsEnum.class);
            String fieldName = f.getName();
            String schemaName = ewsEnum.schemaName();
            if (!schemaName.isEmpty()) {
                dict.put(fieldName, schemaName);
            }
        }
    }
    return dict;
}

From source file:microsoft.exchange.webservices.data.core.EwsUtilities.java

/**
 * Builds the schema to enum mapping dictionary.
 *
 * @param <E> Type of the enum.//from w  ww  .jav  a2 s.c o m
 * @param c   Class
 * @return The mapping from enum to schema name
 */
private static <E extends Enum<E>> Map<String, String> buildSchemaToEnumDict(Class<E> c) {
    Map<String, String> dict = new HashMap<String, String>();

    Field[] fields = c.getDeclaredFields();
    for (Field f : fields) {
        if (f.isEnumConstant() && f.isAnnotationPresent(EwsEnum.class)) {
            EwsEnum ewsEnum = f.getAnnotation(EwsEnum.class);
            String fieldName = f.getName();
            String schemaName = ewsEnum.schemaName();
            if (!schemaName.isEmpty()) {
                dict.put(schemaName, fieldName);
            }
        }
    }
    return dict;
}

From source file:py.una.pol.karaku.jsf.utils.RequiredPhaseListener.java

private void processHtmlInput(HtmlInputText htmlInputText) {

    int max = htmlInputText.getMaxlength();
    if (max <= 0) {
        Field f = getField(htmlInputText);
        if (f != null) {
            Size s = f.getAnnotation(Size.class);
            if (s != null) {
                max = s.max();//w  w  w  .  ja  v  a  2  s  .c  o m
            }
            Column c = f.getAnnotation(Column.class);
            if (c != null && c.scale() > 0) {
                max = c.scale();
            }
        }
    }
    htmlInputText.setMaxlength(max);

    if (getField(htmlInputText) != null) {
        // TODO ver cuando ya tiene
        Field f = getField(htmlInputText);
        Pattern p = f.getAnnotation(Pattern.class);
        if (p != null && p.regexp() != null) {
            htmlInputText.setOnkeypress(getFunction(p.regexp()));
        }
    }
}

From source file:py.una.pol.karaku.log.LogPostProcessor.java

/**
 * Revisa todos los mtodos o campos que tengan la anotacin {@link Log} y
 * le asigna el valor el Log del bean en cuestin.
 * /*from  ww  w.  ja v  a2 s .c  o  m*/
 * <br />
 * 
 * {@inheritDoc}
 * 
 * @param bean
 *            bean a procesar
 * @param beanName
 *            nombre del bean
 * @return el mismo bean
 * @throws BeansException
 *             nunca
 */
@Override
public Object postProcessBeforeInitialization(final Object bean, final String beanName) {

    ReflectionUtils.doWithFields(bean.getClass(), new FieldCallback() {

        @Override
        public void doWith(Field field) throws IllegalAccessException {

            if (!Modifier.isFinal(field.getModifiers())) {
                field.setAccessible(true);
                Log log = field.getAnnotation(Log.class);
                field.set(bean, getLogger(bean, log));
            }
        }

    }, new FieldFilter() {

        @Override
        public boolean matches(Field field) {

            return field.getAnnotation(Log.class) != null;
        }
    });

    ReflectionUtils.doWithMethods(bean.getClass(), new MethodCallback() {

        @Override
        public void doWith(Method method) throws IllegalAccessException {

            Log log = method.getAnnotation(Log.class);
            try {
                method.invoke(bean, getLogger(bean, log));
            } catch (InvocationTargetException e) {
                LOGGER.warn("Error extracting proxy object from {}", bean.getClass().getName(), e);
            }
        }
    }, new MethodFilter() {

        @Override
        public boolean matches(Method method) {

            return method.getAnnotation(Log.class) != null;

        }
    });

    return bean;

}