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.jroossien.boxx.nms.NMS.java

public <T> Object loadFromNMS(Class<T> dep) {
    if (!dep.isAnnotationPresent(NMSDependant.class))
        return null;
    NMSDependant nmsDependant = dep.getAnnotation(NMSDependant.class);
    Class<?> impl = null;//from  ww w  .ja v  a 2s.  c  o m
    try {
        impl = Class.forName(nmsDependant.implementationPath() + "." + dep.getSimpleName() + "_" + version);
        return impl.newInstance();
    } catch (ClassNotFoundException e) {
        Boxx.get().error("The current version is not supported: " + version + ".\n" + e.getMessage());
    } catch (InstantiationException | IllegalAccessException e) {
        e.printStackTrace();
    }
    return impl;
}

From source file:com.redhat.red.build.koji.http.httpclient4.HC4SyncObjectClient.java

private String getRequestMethod(Object obj) {
    final Class<?> type = ((obj instanceof Class<?>) ? (Class<?>) obj : obj.getClass());
    final Request req = type.getAnnotation(Request.class);
    if (req != null) {
        return req.method();
    }/*from  w ww .  j a  v  a 2  s  . c  om*/
    return null;
}

From source file:com.khs.sherpa.processor.RestfulRequestProcessor.java

public String getEndpoint(HttpServletRequest request) {
    Map<String, Object> map = applicationContext.getEndpointTypes();
    Collection<Method> methods = new HashSet<Method>();

    for (Entry<String, Object> entry : map.entrySet()) {
        Collection<Method> m = Reflections.getAllMethods(entry.getValue().getClass(),
                Predicates.and(ReflectionUtils.withAnnotation(Action.class),
                        SherpaPredicates.withActionMappingPattern(UrlUtil.getPath(request))));
        methods.addAll(m);/*  w  w  w . ja v a 2  s.  com*/
    }

    method = MethodUtil.validateHttpMethods(methods.toArray(new Method[] {}), request.getMethod());
    if (method != null) {
        Class<?> type = method.getDeclaringClass();
        if (type.isAnnotationPresent(Endpoint.class)) {
            if (StringUtils.isNotEmpty(type.getAnnotation(Endpoint.class).value())) {
                return type.getAnnotation(Endpoint.class).value();
            }
        }
        return type.getSimpleName();
    }
    throw new SherpaEndpointNotFoundException("no endpoint for url [" + UrlUtil.getPath(request) + "]");
}

From source file:org.dimitrovchi.conf.service.ServiceParameterUtils.java

static AnnotationParameters annotationParameters() {
    final Class<?>[] stack = ClassResolver.CLASS_RESOLVER.getClassContext();
    final Class<?> caller = stack[3];
    final List<Class<? extends Annotation>> interfaces = new ArrayList<>();
    Class<?> topCaller = null;
    for (int i = 3; i < stack.length && caller.isAssignableFrom(stack[i]); i++) {
        final Class<?> c = stack[i];
        topCaller = stack[i];/*from ww  w  . j a v  a 2  s. co m*/
        if (c.getTypeParameters().length != 0) {
            final TypeVariable<? extends Class<?>> var = c.getTypeParameters()[0];
            final List<Class<? extends Annotation>> bounds = new ArrayList<>(var.getBounds().length);
            for (final Type type : var.getBounds()) {
                if (type instanceof Class<?> && ((Class<?>) type).isAnnotation()) {
                    bounds.add((Class) type);
                }
            }
            if (bounds.size() > interfaces.size()) {
                interfaces.clear();
                interfaces.addAll(bounds);
            }
        }
    }
    final Map<Class<? extends Annotation>, List<Annotation>> annotationMap = new IdentityHashMap<>();
    for (int i = 3; i < stack.length && caller.isAssignableFrom(stack[i]); i++) {
        final Class<?> c = stack[i];
        for (final Class<? extends Annotation> itf : interfaces) {
            final Annotation annotation = c.getAnnotation(itf);
            if (annotation != null) {
                List<Annotation> annotationList = annotationMap.get(itf);
                if (annotationList == null) {
                    annotationMap.put(itf, annotationList = new ArrayList<>());
                }
                annotationList.add(0, annotation);
            }
        }
    }
    return new AnnotationParameters(topCaller, interfaces, annotationMap);
}

From source file:com.payu.ratel.register.ServiceRegisterPostProcessor.java

private Class<?> getPublishInterface(Object bean, String beanName) {
    Class<? extends Object> realBeanClazz = getRealBeanClass(bean, beanName);

    Publish publish = realBeanClazz.getAnnotation(Publish.class);

    if (publish == null) {
        return getFirstInterfaceOrDefined(bean, null);
    }/*from   ww  w .  jav  a 2 s  . c  om*/

    Class publishedInterface = publish.value();

    if (publishedInterface == Void.class) {
        return getFirstInterfaceOrDefined(bean, null);
    }

    return getFirstInterfaceOrDefined(bean, publishedInterface);
}

From source file:io.github.resilience4j.ratelimiter.autoconfigure.RateLimiterAspect.java

private RateLimiter getRateLimiterAnnotation(ProceedingJoinPoint proceedingJoinPoint) {
    RateLimiter rateLimiter = null;/*from w ww .  j a v  a 2  s .c o  m*/
    Class<?> targetClass = proceedingJoinPoint.getTarget().getClass();
    if (targetClass.isAnnotationPresent(RateLimiter.class)) {
        rateLimiter = targetClass.getAnnotation(RateLimiter.class);
        if (rateLimiter == null) {
            rateLimiter = targetClass.getDeclaredAnnotation(RateLimiter.class);
        }
        if (rateLimiter == null) {
            logger.debug("TargetClass has no declared annotation 'RateLimiter'");
        }
    }
    return rateLimiter;
}

From source file:fr.exanpe.t5.lib.internal.authorize.AuthorizePageFilter.java

/**
 * Return the {@link Authorize} annotation if found
 * //  ww  w.  ja  v a  2 s. co  m
 * @param clazz the class to look for
 * @return the annotation, or null
 */
private Authorize process(Class<?> clazz) {
    if (!clazz.isAnnotationPresent(Authorize.class)) {
        return null;
    }

    return clazz.getAnnotation(Authorize.class);
}

From source file:com.ebay.jetstream.management.HtmlResourceFormatter.java

@Override
protected final void formatBean(Object bean) throws Exception {
    Class<?> bclass = bean.getClass();
    ManagedResource mr = bclass.getAnnotation(ManagedResource.class);
    String help = mr == null ? null : mr.description();
    formatBean(false, bclass, help);/*  w ww  .  j  a  va 2s  .c  om*/
    boolean section = false;
    for (PropertyDescriptor pd : Introspector.getBeanInfo(bclass).getPropertyDescriptors()) {
        Method getter = pd.getReadMethod();
        if (!XMLSerializationManager.isHidden(getter)) {
            if (!section) {
                section = true;
                formatSection(false, "Properties");
            }
            formatProperty(bean, pd);
        }
    }
    if (section) {
        formatSection(true, "Properties");
    }
    section = false;
    for (Method method : bean.getClass().getMethods()) {
        ManagedOperation mo = method.getAnnotation(ManagedOperation.class);
        if (mo != null && method.getParameterTypes().length == 0) {
            help = mo.description();
            if (!section) {
                section = true;
                formatSection(false, "Operations");
            }
            formatOperation(method);
        }
        if (section) {
            formatSection(true, "Operations");
        }
    }
}

From source file:org.owasp.webgoat.application.WebGoatServletListener.java

private void loadServlets(ServletContextEvent sce) {
    final ServletContext servletContext = sce.getServletContext();
    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(
            false);/*from w  w  w.  j  ava2 s .  c o m*/
    provider.addIncludeFilter(new AnnotationTypeFilter(LessonServletMapping.class));
    Set<BeanDefinition> candidateComponents = provider.findCandidateComponents("org.owasp.webgoat");
    try {
        for (BeanDefinition beanDefinition : candidateComponents) {
            Class controllerClass = Class.forName(beanDefinition.getBeanClassName());
            LessonServletMapping pathAnnotation = (LessonServletMapping) controllerClass
                    .getAnnotation(LessonServletMapping.class);
            final ServletRegistration.Dynamic dynamic = servletContext
                    .addServlet(controllerClass.getSimpleName(), controllerClass);
            dynamic.addMapping(pathAnnotation.path());
        }
    } catch (Exception e) {
        logger.error("Error", e);
    }
}

From source file:it.geosolutions.geobatch.annotations.ActionServicePostProcessor.java

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

    if (bean.getClass().equals(AliasRegistry.class)) {
        AliasRegistry aliasRegistry = (AliasRegistry) bean;
        ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(
                true);/*from w  w w. j a  v  a 2  s .com*/
        scanner.addIncludeFilter(new AnnotationTypeFilter(Action.class));
        for (BeanDefinition bd : scanner.findCandidateComponents("it.geosolutions")) {
            try {
                Class actionClass = Class.forName(bd.getBeanClassName());
                Action annotation = (Action) actionClass.getAnnotation(Action.class);
                if (annotation != null) {
                    Class<? extends ActionConfiguration> configurationClass = annotation.configurationClass();

                    String alias = configurationClass.getSimpleName();
                    if (annotation.configurationAlias() != null && !annotation.configurationAlias().isEmpty()) {
                        alias = annotation.configurationAlias();
                    }
                    aliasRegistry.putAlias(alias, configurationClass);

                    if (annotation.aliases() != null) {
                        for (Class a : annotation.aliases()) {
                            if (NullType.class == a)
                                continue;
                            aliasRegistry.putAlias(a.getSimpleName(), a);
                        }
                    }

                    if (annotation.implicitCollections() != null) {
                        for (String ic : annotation.implicitCollections()) {
                            if (ic == null || ic.isEmpty())
                                continue;
                            aliasRegistry.putImplicitCollection(ic, configurationClass);
                        }
                    }

                    GenericActionService asr = new GenericActionService(
                            annotation.configurationClass().getSimpleName(), actionClass);
                    asr.setApplicationContext(applicationContext);
                    actionList.add(asr);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    return bean;
}