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:org.artifactory.repo.RepositoryConfigurationBase.java

/**
 * Extract from an Enum the {@link javax.xml.bind.annotation.XmlEnumValue} that are associated with its fields.
 *
 * @param clazz The class that is to be introspected
 * @return A map that maps {@link javax.xml.bind.annotation.XmlEnumValue#value()} to the enum name itself.
 *//*  www.j  a va 2 s  . com*/
protected Map<String, String> extractXmlValueFromEnumAnnotations(Class clazz) {
    Map<String, String> annotationToName = Maps.newHashMap();
    Field[] fields = clazz.getFields();
    for (Field field : fields) {
        if (field.isAnnotationPresent(XmlEnumValue.class)) {
            XmlEnumValue annotation = field.getAnnotation(XmlEnumValue.class);
            annotationToName.put(annotation.value(), field.getName());
        }
    }
    return annotationToName;
}

From source file:pl.maciejwalkowiak.plist.FieldSerializer.java

private String serializeField(Object o, Field field) {
    String result = "";

    int modifiers = field.getModifiers();

    if (!Modifier.isTransient(modifiers) && !Modifier.isStatic(modifiers)) {
        ReflectionUtils.makeAccessible(field);

        if (!field.isAnnotationPresent(PlistIgnore.class)) {
            result = processField(field, o).toString();
        } else {/*from  w  w w  .  jav  a  2s .c o m*/
            logger.debug("field {} is ignored", field.getName());
        }
    }

    return result;
}

From source file:com.utest.dao.AuditTrailInterceptor.java

@SuppressWarnings("unchecked")
private Field[] getAllFields(final Class objectClass) {

    final List<Field> fields = new ArrayList<Field>();
    for (final Method method : objectClass.getMethods()) {
        if (method.isAnnotationPresent(Audit.class)) {
            try {
                final Field methodField = objectClass.getDeclaredField(
                        method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4));
                if (methodField != null) {
                    fields.add(methodField);
                }//  w ww  .  java2  s . co m
            } catch (final Exception e) {
            }
        }
    }
    for (final Field field : objectClass.getDeclaredFields()) {
        if (field.isAnnotationPresent(Audit.class)) {
            fields.add(field);
        }
    }

    return fields.toArray(new Field[fields.size()]);

}

From source file:net.ymate.platform.core.beans.impl.DefaultBeanFactory.java

/**
 * IoC/*  ww w . ja  v  a2  s .  c  om*/
 *
 * @param targetClass  (???)
 * @param targetObject 
 * @throws Exception ?
 */
protected void __initBeanIoC(Class<?> targetClass, Object targetObject) throws Exception {
    Field[] _fields = targetClass.getDeclaredFields();
    if (_fields != null && _fields.length > 0) {
        for (Field _field : _fields) {
            if (_field.isAnnotationPresent(Inject.class)) {
                Object _injectObj = null;
                if (_field.isAnnotationPresent(By.class)) {
                    By _injectBy = _field.getAnnotation(By.class);
                    _injectObj = this.getBean(_injectBy.value());
                } else {
                    _injectObj = this.getBean(_field.getType());
                }
                if (_injectObj != null) {
                    _field.setAccessible(true);
                    _field.set(targetObject, _injectObj);
                }
            }
        }
    }
}

From source file:com.qwazr.search.annotations.AnnotatedIndexService.java

/**
 * Create a new index service. A class with Index and IndexField annotations.
 *
 * @param indexService         the IndexServiceInterface to use
 * @param indexDefinitionClass an annotated class
 *//*from ww w  .j  ava 2s  .co m*/
public AnnotatedIndexService(IndexServiceInterface indexService, Class<T> indexDefinitionClass) {
    Objects.requireNonNull(indexService, "The indexService parameter is null");
    Objects.requireNonNull(indexDefinitionClass, "The indexDefinition parameter is null");
    this.indexService = indexService;
    this.annotatedService = indexService instanceof AnnotatedServiceInterface
            ? (AnnotatedServiceInterface) indexService
            : null;
    this.indexDefinitionClass = indexDefinitionClass;
    Index index = indexDefinitionClass.getAnnotation(Index.class);
    Objects.requireNonNull(index, "This class does not declare any Index annotation: " + indexDefinitionClass);
    schemaName = index.schema();
    indexName = index.name();
    similarityClass = index.similarityClass();
    Field[] fields = indexDefinitionClass.getDeclaredFields();
    if (fields != null && fields.length > 0) {
        fieldMap = new HashMap<>();
        indexFieldMap = new HashMap<>();
        for (Field field : fields) {
            if (!field.isAnnotationPresent(IndexField.class))
                continue;
            field.setAccessible(true);
            IndexField indexField = field.getDeclaredAnnotation(IndexField.class);
            String indexName = StringUtils.isEmpty(indexField.name()) ? field.getName() : indexField.name();
            indexFieldMap.put(indexName, indexField);
            fieldMap.put(indexName, field);
        }
    } else {
        fieldMap = null;
        indexFieldMap = null;
    }
}

From source file:es.caib.sgtsic.utils.ejb.AbstractService.java

public T wideFind(Object id) {

    boolean isBorrable = true;
    T item = this.find(id);

    if (item == null) {
        return item;
    }/*from  w  w  w.  ja  v a  2  s  .co  m*/

    for (Field f : entityClass.getDeclaredFields()) {

        boolean hasToManyAnnotations = (f.isAnnotationPresent(OneToMany.class))
                || (f.isAnnotationPresent(ManyToMany.class));

        if (hasToManyAnnotations) {

            try {
                f.setAccessible(true);
                isBorrable = isBorrable && ((List) f.get(item)).isEmpty();
                f.setAccessible(false);
            } catch (IllegalArgumentException | IllegalAccessException ex) {
                Logger.getLogger(AbstractService.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }

    return item;
}

From source file:org.apache.nifi.registry.security.authentication.IdentityProviderFactory.java

private void performFieldInjection(final IdentityProvider instance, final Class loginIdentityProviderClass)
        throws IllegalArgumentException, IllegalAccessException {
    for (final Field field : loginIdentityProviderClass.getDeclaredFields()) {
        if (field.isAnnotationPresent(IdentityProviderContext.class)) {
            // make the method accessible
            final boolean isAccessible = field.isAccessible();
            field.setAccessible(true);/*from ww  w  .  j  av  a  2 s  . c  om*/

            try {
                // get the type
                final Class<?> fieldType = field.getType();

                // only consider this field if it isn't set yet
                if (field.get(instance) == null) {
                    // look for well known types
                    if (NiFiRegistryProperties.class.isAssignableFrom(fieldType)) {
                        // nifi properties injection
                        field.set(instance, properties);
                    }
                }

            } finally {
                field.setAccessible(isAccessible);
            }
        }
    }

    final Class parentClass = loginIdentityProviderClass.getSuperclass();
    if (parentClass != null && IdentityProvider.class.isAssignableFrom(parentClass)) {
        performFieldInjection(instance, parentClass);
    }
}

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

/**
 * @param relationField/*from w  ww .j  a v a 2  s.  com*/
 * @param annotate
 * @return
 */
private Boolean validateOneToOne(Field relationField, Annotation annotate) {

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

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

        validateJoinColumns(joinColumns);

        JoinTable joinTable = annotation.joinTable();
        onJoinTable(joinTable);
    } else if (isJoinedByTable) {
        throw new UnsupportedOperationException("@JoinTable not supported for one to one association");
    }
    return true;
}

From source file:com.all.testing.MockInyectRunner.java

@SuppressWarnings("unchecked")
private Map<Class, Object> initMocksAndStubs(Object testInstance, List<Field> fields) {
    MockitoAnnotations.initMocks(testInstance);
    HashMap<Class, Object> result = new HashMap<Class, Object>();
    for (Field testField : fields) {
        if (testField.isAnnotationPresent(Mock.class)) {
            result.put(testField.getType(), getFieldValue(testInstance, testField));
        } else if (testField.isAnnotationPresent(Stub.class)) {
            result.put(testField.getType(), createStub(testField, testInstance));
        }//from w ww . j av  a 2s.co  m
    }
    return result;
}

From source file:it.unibas.spicy.persistence.object.operators.AnalyzeFields.java

private void generateLeafProperty(IClassNode currentNode, Field field) {
    if (logger.isDebugEnabled())
        logger.debug("Found a primitive or wrapperType field: " + field);
    PersistentProperty property = generateField(field);
    if (field.isAnnotationPresent(ID.class)) {
        currentNode.setId(property);//ww  w .  j  a  v  a  2  s. c o  m
    } else {
        currentNode.addPersistentProperty(property);
    }
}