Example usage for java.lang Class getDeclaredAnnotations

List of usage examples for java.lang Class getDeclaredAnnotations

Introduction

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

Prototype

public Annotation[] getDeclaredAnnotations() 

Source Link

Usage

From source file:Main.java

public static void main(String[] unused) {
    try {/*  w  w  w .  ja  v a  2 s .  co  m*/
        Class d = Class.forName("java.util.Date");
        System.out.println(d.getDeclaredAnnotations().length);

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static String getValueByClassAnntation(Class clazz, String annClassName, String key) {
    Annotation[] annotations = clazz.getDeclaredAnnotations();
    return getValueInAnntationList(annotations, annClassName, key);
}

From source file:com.netflix.hystrix.contrib.javanica.utils.AopUtils.java

public static <T extends Annotation> Optional<T> getAnnotation(Class<?> type, Class<T> annotation) {
    Validate.notNull(annotation, "annotation cannot be null");
    Validate.notNull(type, "type cannot be null");
    for (Annotation ann : type.getDeclaredAnnotations()) {
        if (ann.annotationType().equals(annotation))
            return Optional.of((T) ann);
    }/*from  ww  w.  java2s.  c  o  m*/

    Class<?> superType = type.getSuperclass();
    if (superType != null && !superType.equals(Object.class)) {
        return getAnnotation(superType, annotation);
    }

    return Optional.absent();
}

From source file:com.helpinput.spring.BeanRegister.java

static String findScopeStr(Class<?> clz) {
    String result = null;/*  w  w  w  .j  a v  a 2 s.co  m*/
    if (clz != null && clz != Object.class) {
        Annotation[] annotations = clz.getDeclaredAnnotations();
        for (int i = annotations.length - 1; i >= 0; i--) {
            Annotation annotation = annotations[i];
            String simpleName = annotation.annotationType().getSimpleName();

            if (RequestScoped.equals(simpleName))
                return request_scope;
            else if (FlashScoped.equals(simpleName))
                return flash_scope;
            else if (ViewScoped.equals(simpleName))
                return view_scope;
            else if (SessionScoped.equals(simpleName))
                return session_scope;
            else if (ApplicatoinScoped.equals(simpleName))
                return application_scope;
            else if (Singleton.equals(simpleName))
                return singleton_scope;
            else if (Prototype.equals(simpleName))
                return prototype_scope;

            else if (annotation instanceof Scope) {
                Scope scopeAnn = (Scope) annotation;
                if (Utils.hasLength(scopeAnn.value()))
                    return scopeAnn.value();
            }
        }
        result = findScopeStr(clz.getSuperclass());
    }
    if (Utils.hasLength(result))
        return result;
    return singleton_scope;
}

From source file:org.nuxeo.apidoc.introspection.ServerInfo.java

protected static List<Class<?>> getSPI(Class<?> klass) {
    List<Class<?>> spi = new ArrayList<>();
    for (Field field : klass.getDeclaredFields()) {
        String cName = field.getType().getCanonicalName();
        if (cName.startsWith("org.nuxeo")) {
            // remove XObjects
            Class<?> fieldClass = field.getType();
            Annotation[] annotations = fieldClass.getDeclaredAnnotations();
            if (annotations.length == 0) {
                spi.add(fieldClass);//from   www.  j  a  va2s.  c  om
            }
        }
    }
    return spi;
}

From source file:unquietcode.tools.beanmachine.AnnotationUtils.java

/**
 * Determine whether an annotation for the specified <code>annotationType</code> is
 * declared locally on the supplied <code>clazz</code>. The supplied {@link Class}
 * may represent any type.//from   w  ww . ja v a  2  s  .c om
 * <p>Note: This method does <strong>not</strong> determine if the annotation is
 * {@link java.lang.annotation.Inherited inherited}. For greater clarity regarding inherited
 * annotations, consider using {@link #isAnnotationInherited(Class, Class)} instead.
 * @param annotationType the Class object corresponding to the annotation type
 * @param clazz the Class object corresponding to the class on which to check for the annotation
 * @return <code>true</code> if an annotation for the specified <code>annotationType</code>
 * is declared locally on the supplied <code>clazz</code>
 * @see Class#getDeclaredAnnotations()
 * @see #isAnnotationInherited(Class, Class)
 */
public static boolean isAnnotationDeclaredLocally(Class<? extends Annotation> annotationType, Class<?> clazz) {
    Assert.notNull(annotationType, "Annotation type must not be null");
    Assert.notNull(clazz, "Class must not be null");
    boolean declaredLocally = false;
    for (Annotation annotation : Arrays.asList(clazz.getDeclaredAnnotations())) {
        if (annotation.annotationType().equals(annotationType)) {
            declaredLocally = true;
            break;
        }
    }
    return declaredLocally;
}

From source file:com.github.valdr.thirdparty.spring.AnnotationUtils.java

/**
 * Determine whether an annotation for the specified {@code annotationType} is
 * declared locally on the supplied {@code clazz}. The supplied {@link Class}
 * may represent any type.//  www.  j a  v a2s.  c o m
 * <p>Note: This method does <strong>not</strong> determine if the annotation is
 * {@linkplain java.lang.annotation.Inherited inherited}. For greater clarity
 * regarding inherited annotations, consider using
 * {@link #isAnnotationInherited(Class, Class)} instead.
 * @param annotationType the Class object corresponding to the annotation type
 * @param clazz the Class object corresponding to the class on which to check for the annotation
 * @return {@code true} if an annotation for the specified {@code annotationType}
 * is declared locally on the supplied {@code clazz}
 * @see Class#getDeclaredAnnotations()
 * @see #isAnnotationInherited(Class, Class)
 */
public static boolean isAnnotationDeclaredLocally(Class<? extends Annotation> annotationType, Class<?> clazz) {
    boolean declaredLocally = false;
    for (Annotation annotation : clazz.getDeclaredAnnotations()) {
        if (annotation.annotationType().equals(annotationType)) {
            declaredLocally = true;
            break;
        }
    }
    return declaredLocally;
}

From source file:ei.ne.ke.cassandra.cql3.EntitySpecificationUtils.java

/**
 * Creates a column family suitable for use with the entity. We try to
 * extract the name of the CQL3 table from a {@link javax.persistence.Table}
 * annotation. If the annotation is absent or the "name" parameter is null,
 * we will use the "name" parameter of the {@link javax.persistence.Entity}
 * annotation. If that name is null, we will use the name of the class.
 *
 * @param entityClazz/*from   w ww  . j a  va  2  s.c  o  m*/
 * @return a column family object suitable for use with the given entity.
 */
public static <T> ColumnFamily<String, String> inferColumnFamily(Class<T> entityClazz) {
    Preconditions.checkNotNull(entityClazz);
    String tableName = null;
    for (Annotation annotation : entityClazz.getDeclaredAnnotations()) {
        Class<? extends Annotation> annotationType = annotation.annotationType();
        /*
         * @Table has the highest priority. Then follows @Entity. Lastly,
         * the name of the entity's class.
         */
        if (annotationType.equals(javax.persistence.Table.class)) {
            javax.persistence.Table table = ((javax.persistence.Table) annotation);
            if (table.name() != null) {
                tableName = table.name();
            }
        } else if (annotationType.equals(javax.persistence.Entity.class)) {
            javax.persistence.Entity entity = ((javax.persistence.Entity) annotation);
            if (tableName == null && entity.name() != null) {
                tableName = entity.name();
            }
        }
    }
    if (tableName == null) {
        tableName = entityClazz.getSimpleName();
    }
    tableName = normalizeCqlElementName(tableName);
    ColumnFamily<String, String> columnFamily = ColumnFamily.newColumnFamily(tableName, StringSerializer.get(),
            StringSerializer.get());
    return columnFamily;
}

From source file:ei.ne.ke.cassandra.cql3.EntitySpecificationUtils.java

/**
 * If exactly one field is annotated with @EmbeddedId or the class is
 * annotated with @IdClass, we consider the entity a compound entity (= the
 * table has a compound key).  Otherwise if exactly one field is annotated
 * with @Id, we consider the entity a simple entity (= the table has a
 * primary key)./*www.  j  a v  a 2  s .  c  om*/
 *
 * @param entityClazz
 * @return what type of entity the annotated class is (simple or compound).
 */
public static <T> SupportedEntityType inspectEntityClass(Class<T> entityClazz) {
    Preconditions.checkNotNull(entityClazz);
    boolean idClass = AnnotationUtils.isAnnotationDeclaredLocally(IdClass.class, entityClazz);
    Field id = findPrimaryKeyField(entityClazz);
    Field embeddedId = findCompoundKeyField(entityClazz);
    if (idClass) {
        if (embeddedId != null) {
            throw new IllegalStateException("@IdClass and @EmbeddedId are mutually exclusive.");
        }
        return SupportedEntityType.COMPOUND_IDCLASS;
    }
    if (id != null && embeddedId != null) {
        throw new IllegalStateException("@Id and @EmbeddedId are mutually exclusive");
    } else if (id == null && embeddedId == null) {
        throw new IllegalStateException("Entity needs at least one of @Id, @EmbeddedId, or @IdClass");
    } else if (id != null) {
        return SupportedEntityType.SIMPLE;
    } else if (embeddedId != null) {
        /*
         * If the field is annotated with @EmbeddedId, then the type of the
         * field must have been annotated with @Embeddable.
         */
        Class<?> embeddedType = embeddedId.getType();
        boolean annotatedWithEmbeddable = false;
        for (Annotation annotation : embeddedType.getDeclaredAnnotations()) {
            if (annotation.annotationType().equals(javax.persistence.Embeddable.class)) {
                annotatedWithEmbeddable = true;
                break;
            }
        }
        if (!annotatedWithEmbeddable) {
            throw new IllegalStateException("Embedded entity isn't annotated with @Embeddable.");
        }
        return SupportedEntityType.COMPOUND;
    } else {
        return SupportedEntityType.NONE;
    }
}

From source file:ch.aonyx.broker.ib.api.util.AnnotationUtils.java

/**
 * Determine whether an annotation for the specified <code>annotationType</code> is declared locally on the supplied
 * <code>clazz</code>. The supplied {@link Class} may represent any type.
 * <p>/*ww  w .  j  a  va  2  s. co m*/
 * Note: This method does <strong>not</strong> determine if the annotation is {@link java.lang.annotation.Inherited
 * inherited}. For greater clarity regarding inherited annotations, consider using
 * {@link #isAnnotationInherited(Class, Class)} instead.
 * 
 * @param annotationType
 *            the Class object corresponding to the annotation type
 * @param clazz
 *            the Class object corresponding to the class on which to check for the annotation
 * @return <code>true</code> if an annotation for the specified <code>annotationType</code> is declared locally on
 *         the supplied <code>clazz</code>
 * @see Class#getDeclaredAnnotations()
 * @see #isAnnotationInherited(Class, Class)
 */
public static boolean isAnnotationDeclaredLocally(final Class<? extends Annotation> annotationType,
        final Class<?> clazz) {
    Validate.notNull(annotationType, "Annotation type must not be null");
    Validate.notNull(clazz, "Class must not be null");
    boolean declaredLocally = false;
    for (final Annotation annotation : Arrays.asList(clazz.getDeclaredAnnotations())) {
        if (annotation.annotationType().equals(annotationType)) {
            declaredLocally = true;
            break;
        }
    }
    return declaredLocally;
}