Example usage for java.lang.reflect Field isAnnotationPresent

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

Introduction

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

Prototype

@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) 

Source Link

Usage

From source file:com.conversantmedia.mapreduce.tool.annotation.handler.MaraAnnotationUtil.java

/**
 *
 * @param clazz            the class to reflect
 * @param annotationClasses   the annotations to look for
 * @return               the first field annotated with the provided list
 *//* w w  w.j a  v  a2s.c  o m*/
@SuppressWarnings("unchecked")
public Field findAnnotatedField(Class<?> clazz, Class<? extends Annotation>... annotationClasses) {
    for (Field field : clazz.getDeclaredFields()) {
        for (Class<? extends Annotation> annotationClass : annotationClasses) {
            if (field.isAnnotationPresent(annotationClass)) {
                return field;
            }
        }
    }
    return null;
}

From source file:com.conversantmedia.mapreduce.tool.annotation.handler.MaraAnnotationUtil.java

/**
 *
 * @param clazz            the class to reflect
 * @param annotationClasses   the annotations to look for
 * @return               the fields annotated with the provided list
 *//*  www  . ja v a  2s.  com*/
@SuppressWarnings("unchecked")
public List<Field> findAnnotatedFields(Class<?> clazz, Class<? extends Annotation>... annotationClasses) {
    List<Field> fields = new ArrayList<>();
    for (Field field : clazz.getDeclaredFields()) {
        for (Class<? extends Annotation> annotationClass : annotationClasses) {
            if (field.isAnnotationPresent(annotationClass)) {
                fields.add(field);
            }
        }
    }
    return fields;
}

From source file:com.nonninz.robomodel.RoboModel.java

List<Field> getSavedFields() { //TODO: cache results
    final List<Field> savedFields = new ArrayList<Field>();

    final Field[] declaredFields = getClass().getDeclaredFields();
    boolean saved;
    for (final Field field : declaredFields) {

        saved = false;//from ww  w  .j a v a 2  s.c  o m
        saved = saved || field.isAnnotationPresent(Save.class); // If @Save is present, save it
        saved = saved || Modifier.isPublic(field.getModifiers()); // If it is public, save it
        saved = saved && !Modifier.isStatic(field.getModifiers()); // If it is static, don't save it
        saved = saved && !field.isAnnotationPresent(Exclude.class); // If @Exclude, don't save it

        if (saved) {
            savedFields.add(field);
        }
    }

    return savedFields;
}

From source file:com.threewks.thundr.bigmetrics.service.BigMetricsServiceImpl.java

protected Map<Field, FieldProcessor<?>> findFieldProcessors(Class<?> eventClass) {
    List<Field> fields = Arrays.asList(ReflectUtil.getSupportedFields(eventClass, Object.class));
    Map<Field, FieldProcessor<?>> processors = new LinkedHashMap<>();
    for (Field field : fields) {
        if (!field.isSynthetic() && !Modifier.isTransient(field.getModifiers())
                && !field.isAnnotationPresent(Ignore.class)) {
            field.setAccessible(true);/* w  w w.ja v a 2  s .c om*/
            FieldProcessor<?> processor = determineProcessor(field);
            processors.put(field, processor);
        }
    }
    return processors;
}

From source file:net.larry1123.elec.util.test.config.AbstractConfigTest.java

protected ArrayList<Field> getFields(Class thisClass) {
    ArrayList<Field> fields = new ArrayList<Field>();
    while (thisClass != null) {
        for (Field field : thisClass.getDeclaredFields()) {
            if (field.isAnnotationPresent(ConfigField.class)) {
                fields.add(field);/*from   w  w w . j  ava2  s .  c  o  m*/
            }
        }
        thisClass = thisClass.getSuperclass();
    }
    return fields;
}

From source file:com.impetus.kundera.configure.MetamodelConfiguration.java

private void processGeneratedValueAnnotation(Class<?> clazz, String persistenceUnit, EntityMetadata m,
        Map<String, IdDiscriptor> entityNameToKeyDiscriptorMap) {
    GeneratedValueProcessor processer = new GeneratedValueProcessor();
    String pu = m.getPersistenceUnit();

    String clientFactoryName = getClientFactoryName(persistenceUnit);

    if (pu != null && pu.equals(persistenceUnit)
            || clientFactoryName.equalsIgnoreCase("com.impetus.client.rdbms.RDBMSClientFactory")) {
        Field f = (Field) m.getIdAttribute().getJavaMember();

        if (f.isAnnotationPresent(GeneratedValue.class)) {
            processer.process(clazz, f, m, entityNameToKeyDiscriptorMap);
        }/*from ww  w.ja v  a  2s  .  c o m*/
    }
}

From source file:com.impetus.kundera.metadata.processor.TableProcessor.java

/**
 * Populate metadata.//from  www  .  j av a2  s .c om
 * 
 * @param metadata
 *            the metadata
 * @param clazz
 *            the clazz
 */
private void populateMetadata(EntityMetadata metadata, Class<?> clazz) {
    Table table = clazz.getAnnotation(Table.class);
    boolean isEmbeddable = false;
    // Set Name of persistence object
    metadata.setTableName(table.name());
    // Add named/native query related application metadata.
    addNamedNativeQueryMetadata(clazz);
    // set schema name and persistence unit name (if provided)
    String schemaStr = table.schema();
    Index idx = clazz.getAnnotation(Index.class);
    List<String> colToBeIndexed = null;
    if (idx != null) {
        if (idx.columns() != null && idx.columns().length != 0) {
            colToBeIndexed = Arrays.asList(idx.columns());
        }
    }
    if (schemaStr == null) {
        LOG.error("It is mandatory to specify Schema alongwith Table name:" + table.name()
                + ". This entity won't be persisted");
        throw new InvalidEntityDefinitionException("It is mandatory to specify Schema alongwith Table name:"
                + table.name() + ". This entity won't be persisted");
    }
    MetadataUtils.setSchemaAndPersistenceUnit(metadata, schemaStr);

    metadata.setType(com.impetus.kundera.metadata.model.EntityMetadata.Type.COLUMN_FAMILY);
    // scan for fields

    for (Field f : clazz.getDeclaredFields()) {
        /* Scan @Id field */
        if (f.isAnnotationPresent(Id.class)) {
            LOG.debug(f.getName() + " => Id");
            populateIdAccessorMethods(metadata, clazz, f);
            populateIdColumn(metadata, clazz, f);
        }

        else if (f.isAnnotationPresent(Embedded.class)) {
            /* Scan @Embedded fields */
            metadata.setType(com.impetus.kundera.metadata.model.EntityMetadata.Type.SUPER_COLUMN_FAMILY);
            Class embeddedFieldClass = f.getType();
            isEmbeddable = true;

            if (Collection.class.isAssignableFrom(embeddedFieldClass)) {
                LOG.warn(f.getName()
                        + " was annotated with @Embedded, and shouldn't have been a java Collection field, it won't be persisted");
            } else {
                // An @Embedded attribute will be a DTO (@Embeddable)
                populateEmbeddedFieldIntoMetadata(metadata, f, embeddedFieldClass);
                /* TODO: Bad code, see how to remove this */
                metadata.addToEmbedCollection(embeddedFieldClass);
            }

        }

        else if (f.isAnnotationPresent(ElementCollection.class)) {
            /* Scan @ElementCollection fields */
            metadata.setType(com.impetus.kundera.metadata.model.EntityMetadata.Type.SUPER_COLUMN_FAMILY);
            Class elementCollectionFieldClass = f.getType();
            isEmbeddable = true;

            // An @ElementCollection must be a java collection, a generic
            // class must be declared (@Embeddable)
            if (Collection.class.isAssignableFrom(elementCollectionFieldClass)) {
                Class elementCollectionGenericClass = PropertyAccessorHelper.getGenericClass(f);
                populateElementCollectionIntoMetadata(metadata, f, elementCollectionGenericClass);

                /* TODO: Bad code, see how to remove this */
                metadata.addToEmbedCollection(elementCollectionGenericClass);
            } else {
                LOG.warn(f.getName()
                        + " was annotated with @ElementCollection but wasn't a java Collection field, it won't be persisted");
            }
        } else {
            /* if any valid JPA annotation? */
            String name = getValidJPAColumnName(clazz, f);
            if (null != name) {
                // additional check for not to load Unnecessary column
                // objects in JVM.
                if (!isEmbeddable) {
                    metadata.addColumn(name, new com.impetus.kundera.metadata.model.Column(name, f,
                            colToBeIndexed != null ? colToBeIndexed.contains(name) : false));
                }
                EmbeddedColumn embeddedColumn = new EmbeddedColumn(name, f);
                metadata.addEmbeddedColumn(name, embeddedColumn);
                embeddedColumn.addColumn(name, f);
            }
        }

        /* Scan for Relationship field */
        addRelationIntoMetadata(clazz, f, metadata);
    }

    // TODO: Below if/else block is possibly not required, should be removed
    if (isEmbeddable) {
        Map<String, com.impetus.kundera.metadata.model.Column> cols = metadata.getColumnsMap();
        cols.clear();
        cols = null;
        metadata.setType(Type.SUPER_COLUMN_FAMILY);
    } else {
        Map<String, EmbeddedColumn> embeddedColumns = metadata.getEmbeddedColumnsMap();
        embeddedColumns.clear();
        embeddedColumns = null;
        metadata.setType(Type.COLUMN_FAMILY);
    }

}

From source file:com.impetus.kundera.validation.rules.RelationAttributeRule.java

/**
 * @param relationField/*from   w  w  w .  j a v a  2s.com*/
 * @param annotate
 * @return
 */
private Boolean validateManyToOne(Field relationField, Annotation annotate) {
    // taking field's type as foreign entity, ignoring "targetEntity"

    Class<?> targetEntity = relationField.getType();

    boolean isJoinedByTable = relationField.isAnnotationPresent(JoinTable.class);

    if (relationField.isAnnotationPresent(AssociationOverride.class)) {
        AssociationOverride annotation = relationField.getAnnotation(AssociationOverride.class);
        JoinColumn[] joinColumns = annotation.joinColumns();

        //validate if more than one  join column is defined
        validateJoinColumns(joinColumns);

        JoinTable joinTable = annotation.joinTable();
        //validate if join table is null
        onJoinTable(joinTable);
    }
    // join table not valid for Many to one check
    else if (isJoinedByTable) {
        throw new UnsupportedOperationException("@JoinTable not supported for many to one association");
    }

    return true;
}

From source file:com.jsmartframework.web.manager.BeanHelper.java

void setAuthFields(Class<?> clazz) {
    if (!authFields.containsKey(clazz)) {
        List<Field> fields = new ArrayList<>();

        for (Field field : getBeanFields(clazz)) {
            if (!field.isAnnotationPresent(AuthField.class)) {
                continue;
            }/* w w  w.j a  v  a2 s .  c o m*/
            fields.add(field);
        }
        authFields.put(clazz, fields.toArray(new Field[fields.size()]));
    }
}