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.shigengyu.hyperion.cache.WorkflowStateCacheLoader.java

private static final StateOwner getStateOwner(Class<? extends WorkflowState> workflowStateClass) {

    // Try get annotation on class
    StateOwner stateOwner = workflowStateClass.getAnnotation(StateOwner.class);

    // Try get annotation on declaring class
    Class<?> declaringClass = workflowStateClass;
    while (stateOwner == null && (declaringClass = declaringClass.getDeclaringClass()) != null) {
        stateOwner = declaringClass.getAnnotation(StateOwner.class);
    }//from   w w w  .  j ava2  s.  co  m

    // Try get annotation on package
    if (stateOwner == null) {
        stateOwner = workflowStateClass.getPackage().getAnnotation(StateOwner.class);
    }

    return stateOwner;
}

From source file:com.weibo.api.motan.protocol.grpc.GrpcUtil.java

private static String getGrpcClassName(Class<?> clazz) {
    GrpcConfig config = clazz.getAnnotation(GrpcConfig.class);
    if (config == null || StringUtils.isBlank(config.grpc())) {
        throw new MotanFrameworkException("can not find grpc config in class " + clazz.getName());
    }//from  ww w .  j av  a 2 s  . c  o  m
    return config.grpc();
}

From source file:com.monarchapis.driver.spring.rest.ApiRequestHandlerInterceptor.java

/**
 * Looks for an annotation first on the method, then inspects the class.
 * /*from  ww w  .  ja v  a 2  s .  c o  m*/
 * @param method
 *            The method search for the desired annotation.
 * @param clazz
 *            THe class to search if the annotation is not present on the
 *            method.
 * @return the annotation if found, null otherwise.
 */
private static <T extends Annotation> T getAnnotation(Method method, Class<T> clazz) {
    T annotation = method.getAnnotation(clazz);

    if (annotation == null) {
        Class<?> declaringClass = method.getDeclaringClass();
        annotation = declaringClass.getAnnotation(clazz);

        while (annotation == null && declaringClass.getSuperclass() != null) {
            declaringClass = declaringClass.getSuperclass();
            annotation = declaringClass.getAnnotation(clazz);
        }
    }

    return annotation;
}

From source file:de.alpharogroup.io.annotations.ImportResourcesUtils.java

/**
 * Gets a map with ImportResource objects and the corresponding to the found class from the
 * given package Name. The search is made recursive. The key from an entry of the map is the
 * class where the ImportResource objects found and the value is an Array of the ImportResource
 * objects that contains in the class.//from  ww  w  .  java2  s  .com
 * 
 * @param packageName
 *            the package name
 * @return the import resources
 * @throws ClassNotFoundException
 *             the class not found exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static Map<Class<?>, ImportResource[]> getImportResources(final String packageName)
        throws ClassNotFoundException, IOException {
    final Map<Class<?>, ImportResource[]> resourcesMap = new LinkedHashMap<>();

    final Class<ImportResources> importResourcesClass = ImportResources.class;
    final Class<ImportResource> importResourceClass = ImportResource.class;
    final Set<Class<?>> importResourcesClasses = AnnotationUtils.getAllAnnotatedClasses(packageName,
            importResourcesClass);
    final Set<Class<?>> importResourceClasses = AnnotationUtils.getAllAnnotatedClasses(packageName,
            importResourceClass);
    importResourcesClasses.addAll(importResourceClasses);
    for (final Class<?> annotatedClass : importResourcesClasses) {
        final ImportResources importResources = annotatedClass.getAnnotation(ImportResources.class);
        ImportResource[] importResourcesArray = null;
        ImportResource[] importResourceArray = null;
        if (importResources != null) {
            importResourcesArray = importResources.resources();
        }

        final ImportResource importResource = annotatedClass.getAnnotation(ImportResource.class);
        if (importResource != null) {
            importResourceArray = new ImportResource[1];
            importResourceArray[0] = importResource;
        }
        final ImportResource[] array = (ImportResource[]) ArrayUtils.addAll(importResourceArray,
                importResourcesArray);
        Arrays.sort(array, new ImportResourceComparator());
        resourcesMap.put(annotatedClass, array);

    }
    return resourcesMap;
}

From source file:de.alpharogroup.io.annotations.ImportResourcesExtensions.java

/**
 * Gets a {@link Map} with {@link ImportResource} objects and the corresponding to the found
 * class from the given package Name. The search is made recursive. The key from an entry of the
 * map is the class where the {@link ImportResource} objects found and the value is an Array of
 * the {@link ImportResource} objects that contains in the class.
 *
 * @param packageName//from   w  w  w.  ja v  a  2s. c o m
 *            the package name
 * @return the import resources
 * @throws ClassNotFoundException
 *             occurs if a given class cannot be located by the specified class loader
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static Map<Class<?>, ImportResource[]> getImportResources(final String packageName)
        throws ClassNotFoundException, IOException {
    final Map<Class<?>, ImportResource[]> resourcesMap = new LinkedHashMap<>();

    final Class<ImportResources> importResourcesClass = ImportResources.class;
    final Class<ImportResource> importResourceClass = ImportResource.class;
    final Set<Class<?>> importResourcesClasses = AnnotationExtensions.getAllAnnotatedClasses(packageName,
            importResourcesClass);
    final Set<Class<?>> importResourceClasses = AnnotationExtensions.getAllAnnotatedClasses(packageName,
            importResourceClass);
    importResourcesClasses.addAll(importResourceClasses);
    for (final Class<?> annotatedClass : importResourcesClasses) {
        final ImportResources importResources = annotatedClass.getAnnotation(ImportResources.class);
        ImportResource[] importResourcesArray = null;
        ImportResource[] importResourceArray = null;
        if (importResources != null) {
            importResourcesArray = importResources.resources();
        }

        final ImportResource importResource = annotatedClass.getAnnotation(ImportResource.class);
        if (importResource != null) {
            importResourceArray = new ImportResource[1];
            importResourceArray[0] = importResource;
        }
        final ImportResource[] array = (ImportResource[]) ArrayUtils.addAll(importResourceArray,
                importResourcesArray);
        Arrays.sort(array, new ImportResourceComparator());
        resourcesMap.put(annotatedClass, array);

    }
    return resourcesMap;
}

From source file:com.github.srgg.yads.impl.context.communication.AbstractTransport.java

@edu.umd.cs.findbugs.annotations.SuppressWarnings("NP_NULL_ON_SOME_PATH")
private static byte getMessageCodeFor(final Message msg) {
    final List<Class<?>> interfaces = ClassUtils.getAllInterfaces(msg.getClass());

    Annotation mc = null;/*  ww  w . j ava 2 s  .  c o  m*/
    for (Class c : interfaces) {
        mc = c.getAnnotation(MessageCode.class);
        if (mc != null) {
            break;
        }
    }

    checkState(mc != null, "Can't get message code, message '%s' is not marked with @MessageCode",
            msg.getClass().getSimpleName());
    return (byte) ((MessageCode) mc).value().getNumber();
}

From source file:com.liferay.apio.architect.internal.annotation.representor.processor.TypeProcessor.java

private static ParsedType _processType(Class<?> typeClass, boolean nested) {
    Type type = typeClass.getAnnotation(Type.class);

    Builder builder = new Builder(type, typeClass);

    if (!nested) {
        Method idMethod = Try.fromFallible(() -> getMethodsListWithAnnotation(typeClass, Id.class))
                .filter(methods -> !methods.isEmpty()).map(methods -> methods.get(0)).orElse(null);

        builder.idMethod(idMethod);//from   w ww .  j a v a2  s .com
    }

    List<Method> methods = getMethodsListWithAnnotation(typeClass, Field.class);

    methods.forEach(method -> _processMethod(builder, method));

    return builder.build();
}

From source file:cherry.example.web.util.ViewNameUtil.java

public static String fromMethodCall(Object invocationInfo) {

    MethodInvocationInfo info = (MethodInvocationInfo) invocationInfo;
    Method method = info.getControllerMethod();
    Class<?> type = method.getDeclaringClass();

    UriComponentsBuilder ucb = UriComponentsBuilder.newInstance();
    String typePath = getMappedPath(type.getAnnotation(RequestMapping.class));
    if (StringUtils.isNotEmpty(typePath)) {
        ucb.path(typePath);/*w w  w . j  a  v  a  2 s  .  c o m*/
    }

    String methodPath = getMappedPath(method.getAnnotation(RequestMapping.class));
    if (StringUtils.isNotEmpty(methodPath)) {
        ucb.pathSegment(methodPath);
    }

    String path = ucb.build().getPath();
    if (path.startsWith("/")) {
        return path.substring(1);
    }
    return path;
}

From source file:cc.redpen.validator.ValidatorFactory.java

static void registerValidator(Class<? extends Validator> clazz) {
    boolean deprecated = clazz.getAnnotation(Deprecated.class) == null ? false : true;
    if (deprecated) {
        LOG.warn(clazz.getName() + " is deprecated");
    }//from   w  w w.  j a  v a 2 s .  c  o m
    validators.put(clazz.getSimpleName().replace("Validator", ""), createValidator(clazz));
}

From source file:ar.com.zauber.commons.repository.BaseEntity.java

/**
 * @param theClass/*from   w  w w .jav  a 2s.c om*/
 * @return
 */
private static Set<Field> getIdentityFields(final Class<?> theClass) {
    Set<Field> fields = new HashSet<Field>();
    if (theClass.getAnnotation(IdentityProperties.class) != null) {
        String[] fieldNamesArray = theClass.getClass().getAnnotation(IdentityProperties.class).fieldNames();
        for (int i = 0; i < fieldNamesArray.length; i++) {
            try {
                fields.add((theClass.getField(fieldNamesArray[i])));
            } catch (final SecurityException e) {
                throw new IllegalStateException(e);
            } catch (final NoSuchFieldException e) {
                throw new IllegalStateException(e);
            }

        }
    } else {
        Field[] fieldsArray = theClass.getFields();
        for (int i = 0; i < fieldsArray.length; i++) {
            if (fieldsArray[i].getAnnotation(IdentityProperty.class) != null) {
                fields.add(fieldsArray[i]);
            }
        }
        if (!theClass.getSuperclass().equals(Object.class)) {
            fields.addAll(getIdentityFields(theClass.getSuperclass()));
        }
    }
    return fields;
}