Example usage for java.lang.reflect Field getAnnotations

List of usage examples for java.lang.reflect Field getAnnotations

Introduction

In this page you can find the example usage for java.lang.reflect Field getAnnotations.

Prototype

public Annotation[] getAnnotations() 

Source Link

Usage

From source file:org.onehippo.forge.utilities.hst.simpleocm.build.NodeBuilderImpl.java

/**
 * Builds the properties and child nodes for a class.
 *
 * @param node   the node to populate/*from www .j a v a 2  s. co m*/
 * @param object the annotated object
 * @param clazz  the class, to get the annotations from
 * @throws org.hippoecm.hst.content.beans.ContentNodeBindingException
 *          if building a property / child node fails
 */
private void buildPropertiesForClass(final Node node, final Object object, final Class<?> clazz)
        throws ContentNodeBindingException {
    logger.debug("Building properties for class '{}'", clazz);
    final Field fields[] = clazz.getDeclaredFields();
    for (Field field : fields) {
        final Annotation annotations[] = field.getAnnotations();
        for (Annotation annotation : annotations) {
            if (!(annotation instanceof JcrPath)) {
                continue;
            }
            String relativePath = ((JcrPath) annotation).value();
            if (StringUtils.isBlank(relativePath)) {
                relativePath = field.getName();
            }
            final Class<? extends Converter> converterClass = ((JcrPath) annotation).converter();
            if (!Converter.class.equals(converterClass)) {
                try {
                    logger.debug("Building property '{}' for field '{}' with converter '{}'",
                            new Object[] { relativePath, field.getName(), converterClass });
                    converterClass.newInstance().buildProperty(node, relativePath, field, object);
                } catch (IllegalAccessException accessException) {
                    throw new ContentNodeBindingException(
                            "Error building the property '" + relativePath + "' for class " + clazz,
                            accessException);
                } catch (InstantiationException instantiationException) {
                    throw new ContentNodeBindingException(
                            "Error building the property '" + relativePath + "' for class " + clazz,
                            instantiationException);
                }
            } else {
                logger.debug("Building property '{}' for field '{}'", relativePath, field.getName());
                buildProperty(node, relativePath, field, object);
            }
        }
    }
}

From source file:org.sybila.parasim.core.impl.InjectionField.java

public InjectionField(Field field, Object target) {
    Validate.notNull(field);//from w  ww  . ja v  a2s  .c  o m
    Validate.notNull(target);
    inject = field.getAnnotation(Inject.class);
    Validate.notNull(inject,
            "Field without " + Inject.class.getName() + " annotation can't be considered as injection point.");
    this.field = field;
    this.target = target;
    this.qualifier = ReflectionUtils.loadQualifier(field.getAnnotations());
}

From source file:org.sybila.parasim.core.impl.ProvidingField.java

public ProvidingField(Field field, Object target) {
    Validate.notNull(field);/*from  ww w .  j  av  a2 s .  c om*/
    Validate.notNull(target);
    provide = field.getAnnotation(Provide.class);
    Validate.notNull(provide,
            "Field without " + Provide.class.getName() + " annotation can't be providing point.");
    this.field = field;
    this.target = target;
    this.qualifier = ReflectionUtils.loadQualifier(field.getAnnotations());
}

From source file:jp.co.nemuzuka.core.controller.AbsController.java

/**
 * ActionForm./*from  www .ja v  a 2s  .  co m*/
 * @ActionForm??????????
 * ??????
 * @param clazz 
 */
@SuppressWarnings("rawtypes")
protected void setActionForm(Class clazz) {

    //@ActionForm?????????????
    Field[] fields = clazz.getDeclaredFields();
    for (Field target : fields) {
        Annotation[] annos = target.getAnnotations();
        for (Annotation targetAnno : annos) {
            if (targetAnno instanceof ActionForm) {

                //?
                Object obj = null;
                try {
                    target.setAccessible(true);
                    obj = target.getType().newInstance();
                    BeanUtil.copy(request, obj);
                    target.set(this, obj);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}

From source file:org.springmodules.validation.bean.conf.loader.annotation.AnnotationBeanValidationConfigurationLoader.java

/**
 * Handles all the property level annotations of the given class and manipulates the given validation configuration
 * accordingly. The property level annotations can either be placed on the <code>setter</code> methods of the
 * properties or on the appropriate class fields.
 *
 * @param clazz The annotated class.// w w w .j  ava2  s .com
 * @param configuration The bean validation configuration to mainpulate.
 */
protected void handlePropertyAnnotations(Class clazz, MutableBeanValidationConfiguration configuration) {
    // extracting & handling all property annotation placed on property fields
    List<Field> fields = extractFieldFromClassHierarchy(clazz);
    for (Field field : fields) {
        String fieldName = field.getName();
        PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(clazz, fieldName);
        if (descriptor != null) {
            Annotation[] annotations = field.getAnnotations();
            handleProprtyAnnotations(annotations, clazz, descriptor, configuration);
        }
    }

    // extracting & handling all property annotations placed on property setters
    PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(clazz);
    for (PropertyDescriptor descriptor : descriptors) {
        Method writeMethod = descriptor.getWriteMethod();
        if (writeMethod == null) {
            continue;
        }
        Annotation[] annotations = writeMethod.getAnnotations();
        handleProprtyAnnotations(annotations, clazz, descriptor, configuration);
    }
}

From source file:org.onehippo.forge.utilities.hst.simpleocm.load.BeanLoaderImpl.java

/**
 * Loads all annotated field of a class.
 *
 * @param node the node to load the values from
 * @param bean the bean to populate/*ww  w. java  2 s . co  m*/
 * @param clazz the class that specifies the fields to load
 * @throws RepositoryException if loading the fields fails
 * @throws InstantiationException if instantiating an object for a field fails
 * @throws IllegalAccessException if accessing a field fails
 * @throws ContentNodeBindingException if setting / loading the field falue fails
 */
private void loadFieldsForClass(final Node node, final Object bean, final Class<?> clazz)
        throws RepositoryException, InstantiationException, IllegalAccessException,
        ContentNodeBindingException {
    logger.debug("Loading fields for class '{}' from node '{}'", clazz, node.getPath());
    for (Field field : clazz.getDeclaredFields()) {
        final Annotation annotations[] = field.getAnnotations();
        for (Annotation annotation : annotations) {
            if (!(annotation instanceof JcrPath)) {
                continue;
            }
            String relativePath = ((JcrPath) annotation).value();
            if (StringUtils.isBlank(relativePath)) {
                relativePath = field.getName();
            }
            final Class<? extends Converter> converterClass = ((JcrPath) annotation).converter();
            if (!Converter.class.equals(converterClass)) {
                logger.debug("Load field '{}' with custom converter '{}' from '{}'",
                        new Object[] { field.getName(), converterClass, node.getPath() + "/" + relativePath });
                converterClass.newInstance().setFieldValue(bean, field, node, relativePath);
            } else {
                logger.debug("Load field '{}' from '{}' ", field.getName(),
                        node.getPath() + "/" + relativePath);
                setFieldValue(bean, field, node, relativePath);
            }
        }
    }
}

From source file:com.taobao.adfs.util.Utilities.java

public static String getTimeFormatFromField(Field field) {
    if (field == null)
        return null;
    if (!field.getType().equals(long.class) && !field.getType().equals(Long.class))
        return null;
    for (Annotation annotation : field.getAnnotations()) {
        if (annotation instanceof LongTime)
            return ((LongTime) annotation).value();
    }//from   w w  w  . j  av a2 s.c om
    return null;
}

From source file:org.omnaest.utils.reflection.ReflectionUtils.java

/**
 * Returns all {@link Field#getAnnotations()} as {@link List}
 * //from   w w w .  j  av  a  2  s. c om
 * @param field
 * @return
 */
public static List<Annotation> annotationList(Field field) {
    //    
    List<Annotation> retlist = new ArrayList<Annotation>();

    //
    if (field != null) {
        //
        retlist.addAll(Arrays.asList(field.getAnnotations()));
    }

    //
    return retlist;
}

From source file:com.expressui.core.util.BeanPropertyType.java

private boolean initFieldAnnotations(Class containerType) {
    boolean foundProperty = false;

    Field field;
    try {/*w  w  w .j  av  a2  s.  c  o  m*/
        field = containerType.getDeclaredField(id);
        foundProperty = true;
        Annotation[] fieldAnnotations = field.getAnnotations();
        Collections.addAll(annotations, fieldAnnotations);
    } catch (NoSuchFieldException e) {
        // no need to get annotations if field doesn't exist
    }

    return foundProperty;
}

From source file:org.ops4j.pax.exam.junit.extender.impl.internal.CallableTestMethodImpl.java

/**
 * Tests if the given field has the {@link @Inject} annotation.
 * Due to some osgi quirks, currently direct getAnnotation( Inject.class ) does not work..:(
 *
 * @param field field to be tested/*from  w w  w.  ja v a2 s.com*/
 *
 * @return trze if it has the Inject annotation. Otherwise false.
 */
public boolean isInjectionField(Field field) {
    // Usually, this should be enough.
    if (field.getAnnotation(Inject.class) != null) {
        return true;
    } else {
        // the above one fails in some cases currently (returns null) while annotation is there.
        // So this is a fallback:
        for (Annotation annot : field.getAnnotations()) {
            if (annot.annotationType().getName().equals(Inject.class.getName())) {
                return true;
            }
        }
    }

    return false;
}