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:com.unboundid.scim2.common.utils.SchemaUtils.java

/**
 * Gets the schema for a class.  This will walk the inheritance tree looking
 * for information about the SCIM schema of the objects represented. This
 * information comes from annotations and introspection.
 *
 * @param cls the class to get the schema for.
 * @return the schema.// w ww .  java  2  s  .  c  o  m
 * @throws IntrospectionException if an exception occurs during introspection.
 */
public static SchemaResource getSchema(final Class<?> cls) throws IntrospectionException {
    Schema schemaAnnotation = cls.getAnnotation(Schema.class);

    // Only generate schema for annotated classes.
    if (schemaAnnotation == null) {
        return null;
    }

    return new SchemaResource(schemaAnnotation.id(), schemaAnnotation.name(), schemaAnnotation.description(),
            getAttributes(cls));
}

From source file:com.github.helenusdriver.driver.impl.RootClassInfoImpl.java

/**
 * Finds the class info for all type classes for this root element class.
 *
 * @author paouelle//  w w  w .  jav a2  s.  com
 *
 * @param <T> The type of POJO represented by this root element class
 *
 * @param  mgr the non-<code>null</code> statement manager
 * @param  clazz the root POJO class
 * @return the non-<code>null</code> map of class info for all types
 * @throws NullPointerException if <code>clazz</code> is <code>null</code>
 * @throws IllegalArgumentException if any of the type classes are invalid
 */
private static <T> Map<Class<? extends T>, TypeClassInfoImpl<? extends T>> findTypeInfos(
        StatementManagerImpl mgr, Class<T> clazz) {
    org.apache.commons.lang3.Validate.notNull(clazz, "invalid null root POJO class");
    final RootEntity re = clazz.getAnnotation(RootEntity.class);
    final int hsize = re.types().length * 3 / 2;
    final Map<Class<? extends T>, TypeClassInfoImpl<? extends T>> types = new HashMap<>(hsize);
    final Set<String> names = new HashSet<>(hsize);

    for (final Class<?> type : re.types()) {
        org.apache.commons.lang3.Validate.isTrue(clazz.isAssignableFrom(type),
                "type class '%s' must extends root element class: %s", type.getName(), clazz.getName());
        @SuppressWarnings("unchecked") // tested above!
        final TypeClassInfoImpl<? extends T> tcinfo = new TypeClassInfoImpl<>(mgr, clazz,
                (Class<? extends T>) type);

        org.apache.commons.lang3.Validate.isTrue(types.put(tcinfo.getObjectClass(), tcinfo) == null,
                "duplicate type element class '%s' defined for root element class '%s'", type.getSimpleName(),
                clazz.getSimpleName());
        org.apache.commons.lang3.Validate.isTrue(names.add(tcinfo.getType()),
                "duplicate type name '%s' defined by class '%s' for root element class '%s'", tcinfo.getType(),
                type.getSimpleName(), clazz.getSimpleName());
    }
    return types;
}

From source file:com.evolveum.midpoint.prism.marshaller.PrismBeanInspector.java

public static QName determineTypeForClassUncached(Class<?> beanClass) {
    XmlType xmlType = beanClass.getAnnotation(XmlType.class);
    if (xmlType == null) {
        return null;
    }/*ww w .j  a va2  s. c o  m*/

    String namespace = xmlType.namespace();
    if (BeanMarshaller.DEFAULT_PLACEHOLDER.equals(namespace)) {
        XmlSchema xmlSchema = beanClass.getPackage().getAnnotation(XmlSchema.class);
        namespace = xmlSchema.namespace();
    }
    if (StringUtils.isBlank(namespace) || BeanMarshaller.DEFAULT_PLACEHOLDER.equals(namespace)) {
        return null;
    }

    return new QName(namespace, xmlType.name());
}

From source file:com.zc.util.refelect.Reflector.java

/**
 *
 * ?annotationClass/*from  w ww  .  ja  v a2 s . c o m*/
 *
 * @param targetClass
 *            Class
 * @param annotationClass
 *            Class
 *
 * @return List
 */
public static <T extends Annotation> List<T> getAnnotations(Class targetClass, Class annotationClass) {

    List<T> result = new ArrayList<T>();
    Annotation annotation = targetClass.getAnnotation(annotationClass);
    if (annotation != null) {
        result.add((T) annotation);
    }
    Constructor[] constructors = targetClass.getDeclaredConstructors();
    // ?
    CollectionUtils.addAll(result, getAnnotations(constructors, annotationClass).iterator());

    Field[] fields = targetClass.getDeclaredFields();
    // ?
    CollectionUtils.addAll(result, getAnnotations(fields, annotationClass).iterator());

    Method[] methods = targetClass.getDeclaredMethods();
    // ?
    CollectionUtils.addAll(result, getAnnotations(methods, annotationClass).iterator());

    for (Class<?> superClass = targetClass.getSuperclass(); superClass == null
            || superClass == Object.class; superClass = superClass.getSuperclass()) {
        List<T> temp = (List<T>) getAnnotations(superClass, annotationClass);
        if (CollectionUtils.isNotEmpty(temp)) {
            CollectionUtils.addAll(result, temp.iterator());
        }
    }

    return result;
}

From source file:tools.xor.util.ClassUtil.java

public static tools.xor.AccessType getHibernateAccessType(Class<?> instanceClass) {
    org.hibernate.cfg.AccessType classDefinedAccessType;

    org.hibernate.cfg.AccessType hibernateDefinedAccessType = org.hibernate.cfg.AccessType.DEFAULT;
    org.hibernate.cfg.AccessType jpaDefinedAccessType = org.hibernate.cfg.AccessType.DEFAULT;

    org.hibernate.annotations.AccessType accessTypeAnno = instanceClass
            .getAnnotation(org.hibernate.annotations.AccessType.class);
    if (accessTypeAnno != null) {
        hibernateDefinedAccessType = org.hibernate.cfg.AccessType.getAccessStrategy(accessTypeAnno.value());
    }//from   www . j a v a2 s  .  c  o m

    Access accessAnno = instanceClass.getAnnotation(Access.class);
    if (accessAnno != null) {
        jpaDefinedAccessType = org.hibernate.cfg.AccessType.getAccessStrategy(accessAnno.value());
    }

    if (hibernateDefinedAccessType != org.hibernate.cfg.AccessType.DEFAULT
            && jpaDefinedAccessType != org.hibernate.cfg.AccessType.DEFAULT
            && hibernateDefinedAccessType != jpaDefinedAccessType) {
        throw new RuntimeException(
                "@PersistenceType and @Access specified with contradicting values. Use of @Access only is recommended. ");
    }

    if (hibernateDefinedAccessType != org.hibernate.cfg.AccessType.DEFAULT) {
        classDefinedAccessType = hibernateDefinedAccessType;
    } else {
        classDefinedAccessType = jpaDefinedAccessType;
    }
    return ClassUtil.getAccessStrategy(classDefinedAccessType);
}

From source file:com.netxforge.oss2.core.xml.JaxbUtils.java

private static Schema getValidatorFor(final Class<?> origClazz) {
    final Class<?> clazz = (Class<?>) (origClazz instanceof Class<?> ? origClazz : origClazz.getClass());
    LogUtils.tracef(clazz, "finding XSD for class %s", clazz);

    if (m_schemas.containsKey(clazz)) {
        return m_schemas.get(clazz);
    }/* ww  w. j a  v  a2 s  .co m*/

    final ValidateUsing schemaFileAnnotation = clazz.getAnnotation(ValidateUsing.class);
    if (schemaFileAnnotation == null || schemaFileAnnotation.value() == null) {
        return null;
    }

    final String schemaFileName = schemaFileAnnotation.value();
    InputStream schemaInputStream = null;
    try {
        final SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
        if (schemaInputStream == null) {
            final File schemaFile = new File(
                    System.getProperty("opennms.home") + "/share/xsds/" + schemaFileName);
            if (schemaFile.exists()) {
                LogUtils.tracef(clazz, "using file %s", schemaFile);
                schemaInputStream = new FileInputStream(schemaFile);
            }
            ;
        }
        if (schemaInputStream == null) {
            final File schemaFile = new File("target/xsds/" + schemaFileName);
            if (schemaFile.exists()) {
                LogUtils.tracef(clazz, "using file %s", schemaFile);
                schemaInputStream = new FileInputStream(schemaFile);
            }
            ;
        }
        if (schemaInputStream == null) {
            final URL schemaResource = Thread.currentThread().getContextClassLoader()
                    .getResource("xsds/" + schemaFileName);
            if (schemaResource == null) {
                LogUtils.debugf(clazz, "Unable to load resource xsds/%s from the classpath.", schemaFileName);
            } else {
                LogUtils.tracef(clazz, "using resource %s from classpath", schemaResource);
                schemaInputStream = schemaResource.openStream();
            }
        }
        if (schemaInputStream == null) {
            LogUtils.tracef(clazz, "Did not find a suitable XSD.  Skipping.");
            return null;
        }
        final Schema schema = factory.newSchema(new StreamSource(schemaInputStream));
        m_schemas.put(clazz, schema);
        return schema;
    } catch (final Throwable t) {
        LogUtils.warnf(clazz, t, "an error occurred while attempting to load %s for validation",
                schemaFileName);
        return null;
    } finally {
        IOUtils.closeQuietly(schemaInputStream);
    }
}

From source file:com.impetus.kundera.metadata.MetadataUtils.java

public static boolean isEmbeddedAtributeIndexable(Field embeddedField) {
    Class<?> embeddableClass = PropertyAccessorHelper.getGenericClass(embeddedField);

    IndexCollection indexCollection = embeddableClass.getAnnotation(IndexCollection.class);
    if (indexCollection != null && indexCollection.columns() != null) {
        return true;
    }/*ww  w  .j  av  a  2 s . c  o  m*/

    return false;
}

From source file:com.haulmont.cuba.core.config.ConfigUtil.java

public static SourceType getSourceType(Class<?> configInterface, Method method) {
    Source source = method.getAnnotation(Source.class);
    if (source == null) {
        Method getMethod = getGetMethod(configInterface, method);
        if (getMethod != null && !method.equals(getMethod)) {
            source = getMethod.getAnnotation(Source.class);
        }/*from w  w  w.j  a v a  2s.  c o  m*/
        if (source == null) {
            source = configInterface.getAnnotation(Source.class);
            if (source == null)
                return SourceType.DATABASE;
        }
    }
    return source.type();
}

From source file:com.impetus.kundera.metadata.MetadataUtils.java

public static boolean isColumnInEmbeddableIndexable(Field embeddedField, String columnFieldName) {
    Class<?> embeddableClass = PropertyAccessorHelper.getGenericClass(embeddedField);

    IndexCollection indexCollection = embeddableClass.getAnnotation(IndexCollection.class);
    if (indexCollection != null && indexCollection.columns() != null) {
        for (com.impetus.kundera.index.Index column : indexCollection.columns()) {
            if (columnFieldName != null && column != null && column.name() != null
                    && column.name().equals(columnFieldName)) {
                return true;
            }//from   w w  w .  j  av a2 s. co m
        }
    }

    return false;
}

From source file:org.jdbcluster.JDBClusterUtil.java

/** creates the map linking the DAO class to the cluster definition */
@SuppressWarnings("unchecked")
private static void fillDao2ClusterId() {
    ClusterTypeConfig config = ClusterTypeBase.getClusterTypeConfig();

    if (config == null) {
        throw new ConfigurationException("no clusterTypeConfig available");
    }/*from  w ww .j a  va 2 s.c  om*/

    dao2clusterId = new HashMap<Class<? extends Dao>, Set<String>>();

    List<String> ids = config.getClusterIDs();
    for (String id : ids) {
        Class<? extends Cluster> clazz = (Class<? extends Cluster>) createClass(config.getClusterClassName(id));
        DaoLink daoLink = clazz.getAnnotation(DaoLink.class);
        if (daoLink == null) {
            throw new ConfigurationException("cluster defined in configuration file without DaoLink: id [" + id
                    + "] and class [" + clazz + "]");
        }

        Class daoClazz = daoLink.dAOClass();
        Set<String> clusterIds = dao2clusterId.get(daoClazz);
        if (clusterIds == null) {
            clusterIds = new HashSet<String>();
        }
        clusterIds.add(id);
        dao2clusterId.put(daoClazz, clusterIds);
    }
}