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.MinValueRule.java

@Override
protected String getValidationRule(Field field) {
    StringBuilder sb = new StringBuilder();
    Min min = field.getAnnotation(Min.class);
    sb.append("Min value allowed: ").append(min.value());
    return sb.toString();
}

From source file:org.jsonschema2pojo.integration.JacksonViewIT.java

private Annotation jsonViewTest(String annotationStyle, Class<? extends Annotation> annotationType)
        throws ClassNotFoundException, NoSuchFieldException {
    ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/views/views.json", "com.example",
            config("annotationStyle", annotationStyle));

    Class<?> generatedType = resultsClassLoader.loadClass("com.example.Views");
    Field fieldInView = generatedType.getDeclaredField("inView");
    assertThat(fieldInView.getAnnotation(annotationType), notNullValue());

    Field fieldNotInView = generatedType.getDeclaredField("notInView");
    assertThat(fieldNotInView.getAnnotation(annotationType), nullValue());

    return fieldInView.getAnnotation(annotationType);
}

From source file:com.github.abel533.mapperhelper.EntityHelper.java

/**
 * ?/*from   ww  w.java2s.co m*/
 *
 * @param entityClass
 */
public static synchronized void initEntityNameMap(Class<?> entityClass) {
    if (entityTableMap.get(entityClass) != null) {
        return;
    }
    //??
    EntityTable entityTable = null;
    if (entityClass.isAnnotationPresent(Table.class)) {
        Table table = entityClass.getAnnotation(Table.class);
        if (!table.name().equals("")) {
            entityTable = new EntityTable();
            entityTable.setTable(table);
        }
    }
    if (entityTable == null) {
        entityTable = new EntityTable();
        //??????@Table
        entityTable.name = camelhumpToUnderline(entityClass.getSimpleName());
    }
    //
    List<Field> fieldList = getAllField(entityClass, null);
    Set<EntityColumn> columnSet = new HashSet<EntityColumn>();
    Set<EntityColumn> pkColumnSet = new HashSet<EntityColumn>();
    for (Field field : fieldList) {
        //
        if (field.isAnnotationPresent(Transient.class)) {
            continue;
        }
        EntityColumn entityColumn = new EntityColumn();
        if (field.isAnnotationPresent(Id.class)) {
            entityColumn.setId(true);
        }
        String columnName = null;
        if (field.isAnnotationPresent(Column.class)) {
            Column column = field.getAnnotation(Column.class);
            columnName = column.name();
        }
        if (columnName == null || columnName.equals("")) {
            columnName = camelhumpToUnderline(field.getName());
        }
        entityColumn.setProperty(field.getName());
        entityColumn.setColumn(columnName.toUpperCase());
        entityColumn.setJavaType(field.getType());
        //order by
        if (field.isAnnotationPresent(OrderBy.class)) {
            OrderBy orderBy = field.getAnnotation(OrderBy.class);
            if (orderBy.value().equals("")) {
                entityColumn.setOrderBy("ASC");
            } else {
                entityColumn.setOrderBy(orderBy.value());
            }
        }
        // - Oracle?MySqlUUID
        if (field.isAnnotationPresent(SequenceGenerator.class)) {
            SequenceGenerator sequenceGenerator = field.getAnnotation(SequenceGenerator.class);
            if (sequenceGenerator.sequenceName().equals("")) {
                throw new RuntimeException(entityClass + "" + field.getName()
                        + "@SequenceGeneratorsequenceName!");
            }
            entityColumn.setSequenceName(sequenceGenerator.sequenceName());
        } else if (field.isAnnotationPresent(GeneratedValue.class)) {
            GeneratedValue generatedValue = field.getAnnotation(GeneratedValue.class);
            if (generatedValue.generator().equals("UUID")) {
                if (field.getType().equals(String.class)) {
                    entityColumn.setUuid(true);
                } else {
                    throw new RuntimeException(field.getName()
                            + " - @GeneratedValue?UUID?String");
                }
            } else if (generatedValue.generator().equals("JDBC")) {
                if (Number.class.isAssignableFrom(field.getType())) {
                    entityColumn.setIdentity(true);
                    entityColumn.setGenerator("JDBC");
                } else {
                    throw new RuntimeException(field.getName()
                            + " - @GeneratedValue?UUID?String");
                }
            } else {
                //?generator??idsql,mysql=CALL IDENTITY(),hsqldb=SELECT SCOPE_IDENTITY()
                //??generator
                if (generatedValue.strategy() == GenerationType.IDENTITY) {
                    //mysql
                    entityColumn.setIdentity(true);
                    if (!generatedValue.generator().equals("")) {
                        String generator = null;
                        MapperHelper.IdentityDialect identityDialect = MapperHelper.IdentityDialect
                                .getDatabaseDialect(generatedValue.generator());
                        if (identityDialect != null) {
                            generator = identityDialect.getIdentityRetrievalStatement();
                        } else {
                            generator = generatedValue.generator();
                        }
                        entityColumn.setGenerator(generator);
                    }
                } else {
                    throw new RuntimeException(field.getName()
                            + " - @GeneratedValue?????:"
                            + "\n1.?@GeneratedValue(generator=\"UUID\")"
                            + "\n2.useGeneratedKeys@GeneratedValue(generator=\\\"JDBC\\\")  "
                            + "\n3.mysql?@GeneratedValue(strategy=GenerationType.IDENTITY[,generator=\"Mysql\"])");
                }
            }
        }
        columnSet.add(entityColumn);
        if (entityColumn.isId()) {
            pkColumnSet.add(entityColumn);
        }
    }
    entityTable.entityClassColumns = columnSet;
    if (pkColumnSet.size() == 0) {
        entityTable.entityClassPKColumns = columnSet;
    } else {
        entityTable.entityClassPKColumns = pkColumnSet;
    }
    //
    entityTableMap.put(entityClass, entityTable);
}

From source file:com.cognifide.slice.persistence.impl.serializer.RecursiveSerializer.java

private String retrievePropertyName(Field field) {
    final JcrProperty jcrPropAnnotation = field.getAnnotation(JcrProperty.class);
    final String overridingPropertyName = jcrPropAnnotation.value();
    final String fieldName = field.getName();

    return StringUtils.defaultIfEmpty(overridingPropertyName, fieldName);
}

From source file:com.geodevv.testing.irmina.MockedOrSpiedInjectionPointScanner.java

public List<InjectionPointDefinition> scanForSpies(Class<?> aClass) {

    ArrayList<InjectionPointDefinition> result = Lists.newArrayList();

    for (Field field : aClass.getDeclaredFields()) {
        Spy annotation = field.getAnnotation(Spy.class);

        if (annotation != null) {
            List<Annotation> annotations = getQualifierOrNamed(field);
            result.add(new InjectionPointDefinition(annotations, field.getType()));
        }/*from  w  ww  .j  a  v  a  2s  .c o m*/
    }

    return result;
}

From source file:com.geodevv.testing.irmina.MockedOrSpiedInjectionPointScanner.java

public List<InjectionPointDefinition> scanForMock(Class<?> aClass) {

    ArrayList<InjectionPointDefinition> result = Lists.newArrayList();

    for (Field field : aClass.getDeclaredFields()) {
        Mock annotation = field.getAnnotation(Mock.class);

        if (annotation != null) {
            List<Annotation> annotations = getQualifierOrNamed(field);
            result.add(new InjectionPointDefinition(annotations, field.getType()));
        }/*from w ww  .j a v a 2 s.  c  om*/
    }

    return result;
}

From source file:natalia.dymnikova.configuration.ConfigBeanPostProcessor.java

private void injectConfiguration(Object bean, String beanName, Field field) {
    final String path = field.getAnnotation(ConfigValue.class).value();
    try {//from ww  w . jav  a2 s . c o  m
        setField(field, bean, convert(field.getGenericType(), environment.config, path));
    } catch (final com.typesafe.config.ConfigException e) {
        throw unchecked(e, "Failed to apply configuration '{}' to property '{}' of bean '{}' of type {}", path,
                field.getName(), beanName, bean.getClass().getName());
    }
}

From source file:br.com.renatoccosta.regexrenamer.api.element.Element.java

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();

    sb.append("<").append(getId());

    Field[] fields = this.getClass().getDeclaredFields();
    for (Field f : fields) {
        Parameter p = f.getAnnotation(Parameter.class);
        if (p != null) {
            String alias = p.alias().equals("") ? f.getName() : p.alias();

            try {
                sb.append(" ").append(alias).append("='").append(PropertyUtils.getProperty(this, f.getName()))
                        .append("'");
            } catch (IllegalAccessException ex) {
            } catch (InvocationTargetException ex) {
            } catch (NoSuchMethodException ex) {
            }/*from   w  ww.  j  a va2 s.com*/
        }
    }

    return sb.toString();
}

From source file:com.github.rvesse.airline.model.MetadataLoader.java

private static Map<Class<? extends Annotation>, Set<Integer>> loadPartials(Field field) {
    Map<Class<? extends Annotation>, Set<Integer>> partials = new HashMap<>();

    Annotation partialsAnnotation = field.getAnnotation(Partials.class);
    if (partialsAnnotation != null) {
        for (Partial partial : ((Partials) partialsAnnotation).value()) {
            collectPartial(partials, partial);
        }//  w ww.j a  v a 2  s  .  co  m
    }
    Annotation partialAnnotation = field.getAnnotation(Partial.class);
    if (partialAnnotation != null) {
        collectPartial(partials, (Partial) partialAnnotation);
    }

    return partials;
}

From source file:jp.gr.java_conf.ka_ka_xyz.processor.AnnotationProcessor.java

private void registerFieldAnnotation(Field field) {
    Column column = field.getAnnotation(Column.class);
    if (column != null) {
        String colName = column.name();
        fieldColMap.put(field.getName(), colName);
    }/*from   w w w.  j av  a2 s  .  co m*/

}