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.syncope.hibernate.HibernateEnhancer.java

public static void main(final String[] args) throws Exception {

    if (args.length != 1) {
        throw new IllegalArgumentException("Expecting classpath as single argument");
    }//from   w ww .  ja  va  2s. c o  m

    ClassPool classPool = ClassPool.getDefault();
    classPool.appendClassPath(args[0]);

    PathMatchingResourcePatternResolver resResolver = new PathMatchingResourcePatternResolver(
            classPool.getClassLoader());
    CachingMetadataReaderFactory cachingMetadataReaderFactory = new CachingMetadataReaderFactory();

    for (Resource resource : resResolver.getResources("classpath*:org/syncope/core/**/*.class")) {

        MetadataReader metadataReader = cachingMetadataReaderFactory.getMetadataReader(resource);
        if (metadataReader.getAnnotationMetadata().isAnnotated(Entity.class.getName())) {

            Class entity = Class.forName(metadataReader.getClassMetadata().getClassName());
            classPool.appendClassPath(new ClassClassPath(entity));
            CtClass ctClass = ClassPool.getDefault().get(entity.getName());
            if (ctClass.isFrozen()) {
                ctClass.defrost();
            }
            ClassFile classFile = ctClass.getClassFile();
            ConstPool constPool = classFile.getConstPool();

            for (Field field : entity.getDeclaredFields()) {
                if (field.isAnnotationPresent(Lob.class)) {
                    AnnotationsAttribute typeAttr = new AnnotationsAttribute(constPool,
                            AnnotationsAttribute.visibleTag);
                    Annotation typeAnnot = new Annotation("org.hibernate.annotations.Type", constPool);
                    typeAnnot.addMemberValue("type",
                            new StringMemberValue("org.hibernate.type.StringClobType", constPool));
                    typeAttr.addAnnotation(typeAnnot);

                    CtField lobField = ctClass.getDeclaredField(field.getName());
                    lobField.getFieldInfo().addAttribute(typeAttr);
                }
            }

            ctClass.writeFile(args[0]);
        }
    }
}

From source file:Util.java

public static boolean isFieldAnnotationPresent(Class<?> clazz, String fieldName,
        Class<? extends Annotation> annotationClass) throws NoSuchFieldException {
    Field field = clazz.getDeclaredField(fieldName);
    return (field.isAnnotationPresent(annotationClass));
}

From source file:net.eledge.android.toolkit.db.internal.SQLBuilder.java

public static String getIdField(Class<?> clazz) {
    for (Field field : clazz.getFields()) {
        if (field.isAnnotationPresent(Id.class)) {
            Column column = field.getAnnotation(Column.class);
            return StringUtils.defaultIfBlank(column.name(), field.getName().toLowerCase(Locale.ENGLISH));
        }/*from  ww  w . jav  a  2s.c o  m*/
    }
    return null;
}

From source file:ClassUtil.java

/**
 * Retrieving fields list of specified class and which
 * are annotated by incoming annotation class
 * If recursively is true, retrieving fields from all class hierarchy
 *
 * @param clazz - where fields are searching
 * @param annotationClass - specified annotation class
 * @param recursively param//from   w w  w .jav  a  2  s.  co  m
 * @return list of annotated fields
 */
public static Field[] getAnnotatedDeclaredFields(Class clazz, Class<? extends Annotation> annotationClass,
        boolean recursively) {
    Field[] allFields = getDeclaredFields(clazz, recursively);
    List<Field> annotatedFields = new LinkedList<Field>();

    for (Field field : allFields) {
        if (field.isAnnotationPresent(annotationClass))
            annotatedFields.add(field);
    }

    return annotatedFields.toArray(new Field[annotatedFields.size()]);
}

From source file:com.impetus.kundera.graph.ObjectGraphUtils.java

/**
 * Validates and set id, in case not set and intended for auto generation.
 * // www. j av  a 2s  .c  om
 * @param idField
 *            id field
 * @param idValue
 *            value of id attribute.
 * @return returns true if id is not set and @GeneratedValue annotation is
 *         present. Else false.
 */
public static boolean onAutoGenerateId(Field idField, Object idValue) {
    if (idField.isAnnotationPresent(GeneratedValue.class)) {
        return !isIdSet(idValue, idField);
    }

    return false;
}

From source file:com.softfeeder.rules.core.Utils.java

/**
 * /*from  w ww .  java2  s.c  om*/
 * @param context
 * @param target
 */
public static void fieldsInjection(RuleContext context, Object target) {
    if (context != null) {
        for (Field field : target.getClass().getDeclaredFields()) {
            if (field.isAnnotationPresent(Inject.class)) {
                try {
                    BeanUtils.setProperty(target, field.getName(), context.getValue(field.getName()));
                } catch (InvocationTargetException | IllegalAccessException ex) {
                    LOG.info(String.format("Invalid field injection %s", ex.getMessage()));
                }
            }
        }
    }
}

From source file:it.unibas.spicy.persistence.object.ClassUtility.java

public static boolean isTransient(Field field) {
    return field.isAnnotationPresent(Transient.class);
}

From source file:atg.tools.dynunit.DynUnit.java

public static void stop(final Object testInstance) {
    logger.entry(testInstance);//  w w w.ja  v  a 2  s  .  c o  m
    boolean foundNucleus = false;
    final Field[] fields = testInstance.getClass().getDeclaredFields();
    for (final Field field : fields) {
        if (field.isAnnotationPresent(Nuke.class)) {
            foundNucleus = true;
            try {
                final Nucleus nucleus = (Nucleus) FieldUtils.readField(field, testInstance, true);
                logger.info("Found Nucleus: {}.", nucleus.getAbsoluteName());
                if (nucleus.isRunning()) {
                    logger.info("Stopping Nucleus.");
                    nucleus.stopService();
                }
            } catch (IllegalAccessException e) {
                logger.catching(e);
                logger.error("Can't access test instance's Nucleus. Strange.");
            } catch (ServiceException e) {
                logger.catching(e);
                logger.warn("Problem stopping Nucleus.");
            }
        }
    }
    if (!foundNucleus) {
        logger.error("Couldn't find nucleus field to stop!");
    }
    logger.exit();
}

From source file:br.com.bea.androidtools.api.model.EntityUtils.java

public static final <E extends Entity<?>> List<Field> columnFields(final Class<E> targetClass) {
    if (null == EntityUtils.columnsCache.get(targetClass.getName().hashCode())) {
        final List<Field> fields = new LinkedList<Field>();
        for (final Field field : targetClass.getDeclaredFields())
            if (field.isAnnotationPresent(Column.class))
                fields.add(field);/*from   w  w  w  .  j a v  a2  s  . co  m*/
        EntityUtils.columnsCache.put(targetClass.getName().hashCode(), fields);
    }
    return EntityUtils.columnsCache.get(targetClass.getName().hashCode());
}

From source file:br.com.bea.androidtools.api.model.EntityUtils.java

public static final <E extends ValueObject> List<Field> metadataFields(final Class<E> targetClass) {
    if (null == EntityUtils.metadatasCache.get(targetClass.getName().hashCode())) {
        final List<Field> fields = new LinkedList<Field>();
        for (final Field field : targetClass.getDeclaredFields())
            if (field.isAnnotationPresent(Metadata.class))
                fields.add(field);//  ww  w.j  a  v a  2s .c  o  m
        EntityUtils.metadatasCache.put(targetClass.getName().hashCode(), fields);
    }
    return EntityUtils.metadatasCache.get(targetClass.getName().hashCode());
}