Example usage for java.lang Class getAnnotations

List of usage examples for java.lang Class getAnnotations

Introduction

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

Prototype

public Annotation[] getAnnotations() 

Source Link

Usage

From source file:com.sugaronrest.restapicalls.ModuleInfo.java

/**
 * Gets the module model info object.//from w  ww .  j a  va 2  s .  c o  m
 *
 *  @param moduleName The module model name.
 *  @return The model info object.
 */
private static ModuleInfo readByName(String moduleName) throws Exception {
    ModuleInfo moduleInfo = new ModuleInfo();
    Class moduleClass = getClassFromName(moduleName);
    String jsonModuleName = null;

    if (moduleClass == null) {
        moduleInfo.name = moduleName;
        return moduleInfo;
    }

    Annotation[] moduleAnnotations = moduleClass.getAnnotations();
    for (Annotation annotation : moduleAnnotations) {
        if (annotation instanceof Module) {
            Module module = (Module) annotation;
            moduleName = module.name();
            jsonModuleName = module.tablename();
        }
    }

    if ((moduleName == null) || (jsonModuleName == null)) {
        return null;
    }

    moduleInfo.name = moduleName;
    moduleInfo.jsonName = jsonModuleName;
    moduleInfo.type = moduleClass;

    List<ModuleProperty> modelProperties = getFieldAnnotations(moduleClass);
    moduleInfo.modelProperties = modelProperties;

    return moduleInfo;
}

From source file:io.fabric8.forge.rest.dto.UICommands.java

protected static boolean isJsonObject(Object value) {
    if (value != null) {
        Class<?> aClass = value.getClass();
        while (aClass != null && !aClass.equals(Object.class)) {
            Annotation[] annotations = aClass.getAnnotations();
            if (annotations != null) {
                for (Annotation annotation : annotations) {
                    String annotationClassName = annotation.getClass().getName();
                    if (annotationClassName != null
                            && annotationClassName.startsWith("com.fasterxml.jackson.")) {
                        // lets assume its a JSON DTO!
                        return true;
                    } else {
                        String text = annotation.toString();
                        // because of the Forge proxying we can't just use the actual class here...
                        if (text.indexOf("com.fasterxml.jackson.") >= 0) {
                            return true;
                        }//from   w w w . ja  v a 2  s . co  m
                    }
                }
            }
            aClass = aClass.getSuperclass();
        }
    }
    return false;
}

From source file:org.seedstack.seed.core.utils.BaseClassSpecifications.java

/**
 * Checks if the given class is annotated or meta annotated with the given annotation.
 *
 * @param aClass          the class to check
 * @param annotationClass the requested annotation
 * @return true if the class if annotated or meta annotated, false otherwise
 *//*w ww. ja v a  2s  . c  o m*/
public static boolean hasAnnotationDeep(Class<?> aClass, Class<? extends Annotation> annotationClass) {
    if (aClass.equals(annotationClass)) {
        return true;
    }

    for (Annotation anno : aClass.getAnnotations()) {
        Class<? extends Annotation> annoClass = anno.annotationType();
        if (!annoClass.getPackage().getName().startsWith("java.lang")
                && hasAnnotationDeep(annoClass, annotationClass)) {
            return true;
        }
    }

    return false;
}

From source file:org.seedstack.seed.core.utils.SeedReflectionUtils.java

/**
 * Finds a annotation meta-annotated on the given annotated element.
 * <p>//from  w w  w .  ja v  a2  s . c  o  m
 * If the annotated element is a class, the method will go through all its parent class and interface to find an
 * annotation meta annotated.
 *
 * @param <T> the annotation type to retrieve
 * @param annotatedElementClass the annotated element to reach
 * @param metaAnnotationToFind  the meta annotation to find
 * @return the annotation meta annotated if exist, null otherwise
 */
public static <T extends Annotation> Annotation getAnnotationMetaAnnotatedFromAncestor(
        Class<?> annotatedElementClass, Class<T> metaAnnotationToFind) {
    Annotation[] annotations = annotatedElementClass.getAnnotations();
    if (annotations != null) {
        for (Annotation annotation : annotations) {
            Annotation metaAnnotation = SeedReflectionUtils
                    .getMetaAnnotationFromAncestors(annotation.getClass(), metaAnnotationToFind);
            if (metaAnnotation != null) {
                return annotation;
            }
        }
    }
    return null;
}

From source file:org.seedstack.seed.core.utils.SeedReflectionUtils.java

/**
 * Check if the class clazz has the annotation annotationClass up in the hierarchy.
 *
 * @param clazz           The class to search from.
 * @param annotationClass The annotation class to search.
 * @return true if annotation is present, false otherwise.
 *//*from   ww w  . j a  v a2 s  .  c om*/
public static boolean hasAnnotationDeep(Class<?> clazz, Class<? extends Annotation> annotationClass) {

    if (clazz.equals(annotationClass)) {
        return true;
    }

    for (Annotation anno : clazz.getAnnotations()) {
        Class<? extends Annotation> annoClass = anno.annotationType();
        if (!annoClass.getPackage().getName().startsWith(JAVA_LANG)
                && hasAnnotationDeep(annoClass, annotationClass)) {
            return true;
        }
    }

    return false;
}

From source file:com.astamuse.asta4d.data.InjectUtil.java

public final static Object retrieveContextDataSetInstance(Class cls, String searchName, String searchScope)
        throws DataOperationException {
    try {/*from  ww w .  j ava 2  s  . co m*/
        ContextDataSet cds = ConvertableAnnotationRetriever.retrieveAnnotation(ContextDataSet.class,
                cls.getAnnotations());
        if (cds == null) {
            throw new NullPointerException(
                    "Could not find any ContextDataSet convertable annotation on given class:" + cls.getName());
        }
        TargetInfo info = new TargetInfo();
        info.contextDataSetFactory = cds.factory().newInstance();
        info.isContextDataSetSingletonInContext = cds.singletonInContext();
        info.defaultValue = null;
        info.name = searchName;
        info.scope = searchScope;
        info.type = cls;
        info.typeUnMatch = TypeUnMacthPolicy.EXCEPTION;
        ContextDataHolder result = findValueForTarget(info, null);
        if (result == null) {
            return null;
        } else {
            return result.getValue();
        }
    } catch (IllegalAccessException | InstantiationException e) {
        throw new DataOperationException(e.getMessage(), e);
    }
}

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

/**
 * Find annotations./*from  w  w  w  .j ava  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:org.entando.entando.aps.system.services.api.UnmarshalUtils.java

public static Object unmarshal(ApplicationContext applicationContext, Class expectedType,
        InputStream bodyStream, MediaType contentType) throws Throwable {
    Object bodyObject = null;//from w ww  .  j a  va  2 s  . c  om
    try {
        String body = IOUtils.toString(bodyStream, "UTF-8");
        InputStream stream = new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8));
        if (MediaType.APPLICATION_JSON_TYPE.equals(contentType)) {
            JSONProvider jsonProvider = (null != applicationContext)
                    ? (JSONProvider) applicationContext.getBean("jsonProvider")
                    : new JSONProvider();
            bodyObject = jsonProvider.readFrom(expectedType, expectedType.getGenericSuperclass(),
                    expectedType.getAnnotations(), contentType, null, stream/*bodyStream*/);
        } else {
            JAXBContext context = JAXBContext.newInstance(expectedType);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            bodyObject = (Object) unmarshaller.unmarshal(stream/*bodyStream*/);
        }
    } catch (Throwable t) {
        _logger.error("Error unmarshalling request body", t);
        throw new ApsSystemException("Error unmarshalling request body", t);
    }
    return bodyObject;
}

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

/**
 * Find a single {@link java.lang.annotation.Annotation} of <code>annotationType</code> from the supplied {@link Class},
 * traversing its interfaces and superclasses if no annotation can be found on the given class itself.
 * <p>This method explicitly handles class-level annotations which are not declared as
 * {@link java.lang.annotation.Inherited inherited} <i>as well as annotations on interfaces</i>.
 * <p>The algorithm operates as follows: Searches for an annotation on the given class and returns
 * it if found. Else searches all interfaces that the given class declares, returning the annotation
 * from the first matching candidate, if any. Else proceeds with introspection of the superclass
 * of the given class, checking the superclass itself; if no annotation found there, proceeds
 * with the interfaces that the superclass declares. Recursing up through the entire superclass
 * hierarchy if no match is found./*from  w  ww  .ja va 2s  . co m*/
 * @param clazz the class to look for annotations on
 * @param annotationType the annotation class to look for
 * @return the annotation found, or <code>null</code> if none found
 */
public static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType) {
    Assert.notNull(clazz, "Class must not be null");
    A annotation = clazz.getAnnotation(annotationType);
    if (annotation != null) {
        return annotation;
    }
    for (Class<?> ifc : clazz.getInterfaces()) {
        annotation = findAnnotation(ifc, annotationType);
        if (annotation != null) {
            return annotation;
        }
    }
    if (!Annotation.class.isAssignableFrom(clazz)) {
        for (Annotation ann : clazz.getAnnotations()) {
            annotation = findAnnotation(ann.annotationType(), annotationType);
            if (annotation != null) {
                return annotation;
            }
        }
    }
    Class<?> superClass = clazz.getSuperclass();
    if (superClass == null || superClass == Object.class) {
        return null;
    }
    return findAnnotation(superClass, annotationType);
}

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

/**
 * Find a single {@link java.lang.annotation.Annotation} of {@code annotationType} from the supplied {@link Class},
 * traversing its interfaces and superclasses if no annotation can be found on the given class itself.
 * <p>This method explicitly handles class-level annotations which are not declared as
 * {@link java.lang.annotation.Inherited inherited} <i>as well as annotations on interfaces</i>.
 * <p>The algorithm operates as follows: Searches for an annotation on the given class and returns
 * it if found. Else searches all interfaces that the given class declares, returning the annotation
 * from the first matching candidate, if any. Else proceeds with introspection of the superclass
 * of the given class, checking the superclass itself; if no annotation found there, proceeds
 * with the interfaces that the superclass declares. Recursing up through the entire superclass
 * hierarchy if no match is found.//from  ww w.j  a  va 2s .com
 * @param clazz the class to look for annotations on
 * @param annotationType the annotation class to look for
 * @return the annotation found, or {@code null} if none found
 */
public static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType) {
    A annotation = clazz.getAnnotation(annotationType);
    if (annotation != null) {
        return annotation;
    }
    for (Class<?> ifc : clazz.getInterfaces()) {
        annotation = findAnnotation(ifc, annotationType);
        if (annotation != null) {
            return annotation;
        }
    }
    if (!Annotation.class.isAssignableFrom(clazz)) {
        for (Annotation ann : clazz.getAnnotations()) {
            annotation = findAnnotation(ann.annotationType(), annotationType);
            if (annotation != null) {
                return annotation;
            }
        }
    }
    Class<?> superClass = clazz.getSuperclass();
    if (superClass == null || superClass.equals(Object.class)) {
        return null;
    }
    return findAnnotation(superClass, annotationType);
}