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.validation.PatternRule.java

@Override
protected boolean validate(Field field, Object dataObject) {
    boolean valid = true;

    Pattern pattern = field.getAnnotation(Pattern.class);
    if (pattern != null) {
        try {//  ww w  .jav  a  2 s . c o  m
            String value = BeanUtils.getProperty(dataObject, field.getName());
            //null values are concerned a match that way optional field should work.
            if (value != null) {
                valid = value.matches(pattern.regexp());
            }
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
            throw new OpenStorefrontRuntimeException("Unexpected error occur trying to get value from object.",
                    ex);
        }
    }
    return valid;
}

From source file:stormy.pythian.core.description.ReferencedStateDescriptionFactory.java

@SuppressWarnings("unchecked")
public List<ReferencedStateDescription> createDescriptions(Class<?> componentClass) {
    List<ReferencedStateDescription> descriptions = new ArrayList<>();

    Set<Field> fields = getFields(componentClass, withAnnotation(State.class));

    for (Field field : fields) {
        checkSupportedType(field);/*from w w w. j  a  va  2  s.  c  o m*/

        State annotation = field.getAnnotation(State.class);

        ReferencedStateDescription description = new ReferencedStateDescription(annotation.name(),
                annotation.description());
        descriptions.add(description);
    }

    return descriptions;
}

From source file:edu.usu.sdl.openstorefront.validation.RequiredRule.java

@Override
protected boolean validate(Field field, Object dataObject) {
    boolean valid = true;

    NotNull notNull = field.getAnnotation(NotNull.class);
    if (notNull != null) {
        try {/*from ww w.  j  av a  2s . c o m*/
            String value = BeanUtils.getProperty(dataObject, field.getName());

            //This also considers Blank as null
            if (StringUtils.isBlank(value)) {
                valid = false;
            }
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
            throw new OpenStorefrontRuntimeException("Unexpected error occur trying to get value from object.",
                    ex);
        }
    }
    return valid;
}

From source file:com.excilys.ebi.utils.spring.log.slf4j.InjectLoggerAnnotationBeanPostProcessor.java

/**
 * Processes a bean's fields for injection if it has a {@link InjectLogger}
 * annotation./*from   w  w w.  java2s  . c  o m*/
 */
protected void processLogger(final Object bean) {
    final Class<?> clazz = bean.getClass();

    ReflectionUtils.doWithFields(clazz, new FieldCallback() {
        public void doWith(Field field) {
            Annotation annotation = field.getAnnotation(InjectLogger.class);

            if (annotation != null) {
                int modifiers = field.getModifiers();
                Assert.isTrue(!Modifier.isStatic(modifiers),
                        "InjectLogger annotation is not supported on static fields");
                Assert.isTrue(!Modifier.isFinal(modifiers),
                        "InjectLogger annotation is not supported on final fields");

                ReflectionUtils.makeAccessible(field);

                Logger logger = LoggerFactory.getLogger(clazz);

                ReflectionUtils.setField(field, bean, logger);
            }
        }
    });
}

From source file:com.jedi.oracle.OracleCustomType.java

@Override
public void readSQL(SQLInput stream, String typeName) throws SQLException {
    List<Field> fields = FieldUtils.getFieldsListWithAnnotation(this.getClass(), OracleObjectMapping.class);
    List<Field> ordering = Ordering.natural().nullsFirst().onResultOf(new Function<Field, Integer>() {
        public Integer apply(Field field) {
            OracleObjectMapping mapping = field.getAnnotation(OracleObjectMapping.class);
            return mapping.index();
        }/*from ww  w .ja va2  s. c om*/
    }).sortedCopy(fields);

    for (Field field : ordering) {
        int oracleType = field.getAnnotation(OracleObjectMapping.class).oracleType();
        Object value = OracleTypeUtils.getValue(stream, oracleType);
        if (value != null) {
            if (!field.getType().isInstance(value)) {
                try {
                    value = SqlTypeConverter.convert(value, field.getType());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            try {
                field.setAccessible(true);
                field.set(this, value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

    }
}

From source file:edu.usu.sdl.openstorefront.validation.SizeRule.java

@Override
protected boolean validate(Field field, Object dataObject) {
    boolean valid = true;

    Size size = field.getAnnotation(Size.class);
    if (size != null) {
        try {//from w w w.j a va2s .co m
            String value = BeanUtils.getProperty(dataObject, field.getName());
            if (value != null) {
                if (value.length() < size.min() || value.length() > size.max()) {
                    valid = false;
                }
            } else {
                if (size.min() > 0) {
                    valid = false;
                }
            }
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
            throw new OpenStorefrontRuntimeException("Unexpected error occur trying to get value from object.",
                    ex);
        }
    }
    return valid;
}

From source file:com.greenline.hrs.admin.web.war.conf.InjectBeanPostProcessor.java

private void injectPropertyToBean(final Object bean) {
    ReflectionUtils.doWithFields(bean.getClass(), new ReflectionUtils.FieldCallback() {

        @Override/*from  w w w.  j av a  2s.c  o  m*/
        public void doWith(Field field) throws IllegalAccessException {
            InjectingProperty annotation = field.getAnnotation(InjectingProperty.class);
            if (annotation != null) {
                Object strValue = propertyConfigurer.getProperty(annotation.value());
                if (null != strValue) {
                    Object value = typeConverter.convertIfNecessary(strValue, field.getType());
                    ReflectionUtils.makeAccessible(field);
                    if (Modifier.isStatic(field.getModifiers())) {
                        field.set(null, value);
                    } else {
                        field.set(bean, value);
                    }
                }
            }
        }
    });
}

From source file:com.sqewd.open.dal.reflect.test.AnnotationTest.java

public void run() {
    try {//w w w .ja va 2s .c  o  m
        EntityClassLoader loader = new EntityClassLoader();
        loader.loadClass("com.sqewd.open.dal.reflect.test.TestEntity");

        Class.forName("com.sqewd.open.dal.reflect.test.TestEntity");
        TestEntity entity = new TestEntity();

        JsonRootName re = entity.getClass().getAnnotation(JsonRootName.class);
        if (re != null) {
            System.out.println("ROOT : " + re.value());
        }
        Field[] fields = entity.getClass().getDeclaredFields();
        if (fields != null && fields.length > 0) {
            for (Field fd : fields) {
                if (fd.isAnnotationPresent(JsonProperty.class)) {
                    JsonProperty attr = fd.getAnnotation(JsonProperty.class);
                    System.out.println("Field : " + fd.getName() + " --> " + attr.value());
                }
            }
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.github.tddts.jet.config.spring.postprocessor.LoadContentAnnotationBeanPostProcessor.java

private String getFileName(Object target, Field field) throws IllegalAccessException {
    LoadContent annotation = field.getAnnotation(LoadContent.class);

    if (annotation.value().isEmpty()) {
        return (String) field.get(target);
    }/*w ww  .ja va2s.com*/

    if (annotation.property()) {
        return applicationProperties.getProperty(annotation.value());
    } else {
        return annotation.value();
    }
}

From source file:edu.usu.sdl.openstorefront.validation.MaxValueRule.java

@Override
protected boolean validate(Field field, Object dataObject) {
    boolean valid = true;

    Max max = field.getAnnotation(Max.class);
    if (max != null) {
        try {/*from   ww  w . j a  v a  2  s  .c  o  m*/
            String value = BeanUtils.getProperty(dataObject, field.getName());
            if (value != null) {
                try {
                    BigDecimal numberValue = new BigDecimal(value);
                    if (numberValue.compareTo(BigDecimal.valueOf(max.value())) == 1) {
                        valid = false;
                    }
                } catch (NumberFormatException e) {
                    throw new OpenStorefrontRuntimeException("This annotation is for numbers only",
                            "Programming error");
                }
            }
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
            throw new OpenStorefrontRuntimeException("Unexpected error occur trying to get value from object.",
                    ex);
        }
    }
    return valid;
}