Example usage for java.lang Class getAnnotation

List of usage examples for java.lang Class getAnnotation

Introduction

In this page you can find the example usage for java.lang Class getAnnotation.

Prototype

@SuppressWarnings("unchecked")
public <A extends Annotation> A getAnnotation(Class<A> annotationClass) 

Source Link

Usage

From source file:edu.mayo.cts2.framework.webapp.service.ServiceUtils.java

/**
 * Find annotations./*  w w  w. j a  va  2  s  .  c  o  m*/
 *
 * @param <A> the generic type
 * @param clazz the clazz
 * @param annotationType the annotation type
 * @return the collection
 */
public static <A extends Annotation> Collection<A> findAnnotations(Class<?> clazz, Class<A> annotationType) {
    Collection<A> returnCollection = new HashSet<A>();

    Assert.notNull(clazz, "Class must not be null");
    A annotation = clazz.getAnnotation(annotationType);
    if (annotation != null) {
        returnCollection.add(annotation);
    }

    for (Class<?> ifc : clazz.getInterfaces()) {
        Collection<A> annotations = findAnnotations(ifc, annotationType);
        if (annotations != null) {
            returnCollection.addAll(annotations);
        }
    }
    if (!Annotation.class.isAssignableFrom(clazz)) {
        for (Annotation ann : clazz.getAnnotations()) {
            Collection<A> annotations = findAnnotations(ann.annotationType(), annotationType);
            if (annotations != null) {
                returnCollection.addAll(annotations);
            }
        }
    }
    Class<?> superClass = clazz.getSuperclass();
    if (superClass == null || superClass == Object.class) {
        return returnCollection;
    }

    returnCollection.addAll(findAnnotations(superClass, annotationType));

    return returnCollection;
}

From source file:Main.java

public static <T extends Annotation> T getAnnotation(Class<?> clazz, Class<T> aClazz) {
    //Check class hierarchy
    for (Class<?> c = clazz; c != null; c = c.getSuperclass()) {
        T anno = c.getAnnotation(aClazz);
        if (anno != null) {
            return anno;
        }/*www  .  ja v  a  2 s . c o  m*/
    }

    //Check interfaces (breadth first)
    Queue<Class<?>> q = new LinkedList<Class<?>>();
    q.add(clazz);
    while (!q.isEmpty()) {
        Class<?> c = q.remove();
        if (c != null) {
            if (c.isInterface()) {
                T anno = c.getAnnotation(aClazz);
                if (anno != null) {
                    return anno;
                }
            } else {
                q.add(c.getSuperclass());
            }
            q.addAll(Arrays.asList(c.getInterfaces()));
        }
    }

    return null;
}

From source file:com.clustercontrol.accesscontrol.util.ObjectPrivilegeUtil.java

/** ???????? */
public static List<ObjectPrivilegeInfo> getObjectPrivilegeEntities(Class<?> objectPrivilegeClass,
        String objectId, String roleId) {
    HinemosEntityManager em = new JpaTransactionManager().getEntityManager();
    HinemosObjectPrivilege hinemosObjectPrivilege = objectPrivilegeClass
            .getAnnotation(HinemosObjectPrivilege.class);
    if (hinemosObjectPrivilege == null) {
        // HinemosObjectPrivilege????????null?
        return null;
    }//w w w .j  a v  a2 s  .c  om
    String objectType = hinemosObjectPrivilege.objectType();
    return em.createNamedQuery("ObjectPrivilegeInfo.findByObjectIdTypeRoleId", ObjectPrivilegeInfo.class)
            .setParameter("objectType", objectType).setParameter("objectId", objectId)
            .setParameter("roleId", roleId).getResultList();
}

From source file:me.tfeng.play.avro.AvroHelper.java

public static boolean isAvroClient(Class<?> clientClass) {
    return clientClass.getAnnotation(AvroClient.class) != null;
}

From source file:com.impetus.core.DefaultKunderaEntity.java

/**
 * Sets the schema and pu.//  www. j  a  v  a 2s.c  om
 *
 * @param clazz
 *            the clazz
 * @param metadata
 *            the metadata
 */
private static void setSchemaAndPU(Class<?> clazz, EntityMetadata metadata) {
    Table table = clazz.getAnnotation(Table.class);
    if (table != null) {
        metadata.setTableName(!StringUtils.isBlank(table.name()) ? table.name() : clazz.getSimpleName());
        String schemaStr = table.schema();

        MetadataUtils.setSchemaAndPersistenceUnit(metadata, schemaStr,
                em.getEntityManagerFactory().getProperties());
    } else {
        metadata.setTableName(clazz.getSimpleName());
        metadata.setSchema((String) em.getEntityManagerFactory().getProperties().get("kundera.keyspace"));
    }

    if (metadata.getPersistenceUnit() == null) {
        metadata.setPersistenceUnit(getPersistenceUnit());
    }
}

From source file:org.constretto.spring.EnvironmentAnnotationConfigurer.java

@SuppressWarnings("unchecked")
public static Environment findEnvironmentAnnotation(Class beanClass) {
    if (beanClass.isAnnotationPresent(Environment.class)) {
        return (Environment) beanClass.getAnnotation(Environment.class);
    } else {//from   w  w w . j av  a 2  s.c o  m
        return findEnvironmentMetaAnnotation(new HashSet<Annotation>(), beanClass.getAnnotations());
    }
}

From source file:com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBReflector.java

private static boolean isDocumentType(Class<?> clazz) {
    return (clazz.getAnnotation(DynamoDBTable.class) != null)
            || (clazz.getAnnotation(DynamoDBDocument.class) != null);
}

From source file:it.uniroma2.sag.kelp.data.representation.structure.StructureElementFactory.java

/**
 * Returns the identifier of a given class
 * //ww  w .  ja v  a 2 s  .c  o m
 * @param c
 *            the class whose identifier is requested
 * @return the class identifier
 */
public static String getStructureElementIdentifier(Class<? extends StructureElement> c) {
    String structureElementAbbreviation;
    if (c.isAnnotationPresent(JsonTypeName.class)) {
        JsonTypeName info = c.getAnnotation(JsonTypeName.class);
        structureElementAbbreviation = info.value();

    } else {
        structureElementAbbreviation = c.getSimpleName().toUpperCase();
    }
    return structureElementAbbreviation;
}

From source file:com.dianping.avatar.cache.util.CacheAnnotationUtils.java

/**
 * Retrieve cache category for class <tt>cz</tt> that have to be annotated
 * by {@link Cache}//from w  w w .  j a v  a2s  .com
 */
public static String getCacheCategory(Class<?> cz) {
    if (cz == null) {
        throw new IllegalArgumentException("Parameter cz is null.");
    }

    Cache cache = cz.getAnnotation(Cache.class);

    if (cache == null) {
        throw new SystemException("The cz must be annotated by Cache.");
    }

    return cache.category();
}

From source file:com.khubla.cbean.CBean.java

/**
 * check if a class is an entity/*from w  ww  .  j ava  2  s. co  m*/
 */
public static boolean isEntity(Class<?> clazz) throws CBeanException {
    try {
        final Entity entity = clazz.getAnnotation(Entity.class);
        if (null != entity) {
            return true;
        }
        return false;
    } catch (final Exception e) {
        throw new CBeanException(e);
    }
}