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.nesscomputing.jersey.wadl.NessWadlAnnotator.java

@Override
public Method createMethod(AbstractResource r, AbstractResourceMethod m) {
    Method method = delegate.createMethod(r, m);

    Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<>();

    for (Annotation a : r.getResourceClass().getAnnotations()) {
        Class<? extends Annotation> type = a.annotationType();
        if (type.getAnnotation(WadlAnnotation.class) != null) {
            annotations.put(type, a);//from www .  j  a v a  2  s .  c o  m
        }
    }
    for (Annotation a : m.getMethod().getAnnotations()) {
        Class<? extends Annotation> type = a.annotationType();
        if (type.getAnnotation(WadlAnnotation.class) != null) {
            annotations.put(type, a);
        }
    }

    for (Annotation a : annotations.values()) {
        WadlAnnotation wadlAnnotation = checkNotNull(a.annotationType().getAnnotation(WadlAnnotation.class));
        Map<QName, String> attr = method.getOtherAttributes();
        String value = annotationStringValue(a);

        for (String xmlElement : wadlAnnotation.value()) {
            attr.put(new QName(NESSAPI_XML_NS, xmlElement, NESSAPI_XML_PREFIX), value);
        }
    }

    return method;
}

From source file:fm.pattern.tokamak.server.service.DataServiceImpl.java

private <T> String entity(Class<T> entity) {
    if (entity.isAnnotationPresent(Entity.class)) {
        return entity.getAnnotation(Entity.class).name();
    }/* w  ww  . j  a v  a 2s.  c  om*/
    throw new IllegalStateException(entity.getClass().getSimpleName()
            + " must have an @Entity annotation configured with the entity name.");
}

From source file:cn.loveapple.service.aop.MetaInfoAspect.java

/**
 * /*w  w  w  . j  av  a 2 s  .co  m*/
 * @param clz
 * @return
 */
protected String getKindName(Class<LoveappleModel> clz) {
    Model annotation = clz.getAnnotation(Model.class);
    if (StringUtils.isEmpty(annotation.kind())) {
        return clz.getName();
    }
    return annotation.kind();
}

From source file:com.esofthead.mycollab.common.interceptor.aspect.TraceableAspect.java

@AfterReturning("execution(public * com.esofthead.mycollab..service..*.saveWithSession(..)) && args(bean, username)")
public void traceSaveActivity(JoinPoint joinPoint, Object bean, String username) {

    Advised advised = (Advised) joinPoint.getThis();
    Class<?> cls = advised.getTargetSource().getTargetClass();

    Traceable traceableAnnotation = cls.getAnnotation(Traceable.class);
    if (traceableAnnotation != null) {
        try {//from  w w  w. j a  v a2 s.  c  o  m
            ActivityStreamWithBLOBs activity = constructActivity(cls, traceableAnnotation, bean, username,
                    ActivityStreamConstants.ACTION_CREATE);
            activityStreamService.save(activity);
        } catch (Exception e) {
            LOG.error("Error when save activity for save action of service " + cls.getName(), e);
        }
    }

}

From source file:com.haulmont.cuba.core.sys.SpringBeanLoader.java

public void updateContext(Collection<Class> classes) {
    if (beanFactory != null) {
        boolean needToRefreshRemotingContext = false;
        for (Class clazz : classes) {
            Service serviceAnnotation = (Service) clazz.getAnnotation(Service.class);
            ManagedBean managedBeanAnnotation = (ManagedBean) clazz.getAnnotation(ManagedBean.class);
            Component componentAnnotation = (Component) clazz.getAnnotation(Component.class);
            Controller controllerAnnotation = (Controller) clazz.getAnnotation(Controller.class);

            String beanName = null;
            if (serviceAnnotation != null) {
                beanName = serviceAnnotation.value();
            } else if (managedBeanAnnotation != null) {
                beanName = managedBeanAnnotation.value();
            } else if (componentAnnotation != null) {
                beanName = componentAnnotation.value();
            } else if (controllerAnnotation != null) {
                beanName = controllerAnnotation.value();
            }/*  w w  w. j a  v a 2s. co m*/

            if (StringUtils.isNotBlank(beanName)) {
                GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
                beanDefinition.setBeanClass(clazz);
                Scope scope = (Scope) clazz.getAnnotation(Scope.class);
                if (scope != null) {
                    beanDefinition.setScope(scope.value());
                }

                beanFactory.registerBeanDefinition(beanName, beanDefinition);
            }

            if (StringUtils.isNotBlank(beanName)) {
                needToRefreshRemotingContext = true;
            }
        }

        if (needToRefreshRemotingContext) {
            ApplicationContext remotingContext = RemotingContextHolder.getRemotingApplicationContext();
            if (remotingContext != null && remotingContext instanceof ConfigurableApplicationContext) {
                ((ConfigurableApplicationContext) remotingContext).refresh();
            }
        }
    }
}

From source file:jp.gr.java_conf.ka_ka_xyz.processor.JPA20AnnotationProcessor.java

public JPA20AnnotationProcessor(Class<?> clazz) {
    if (clazz != null) {
        Annotation classAnnotation = clazz.getAnnotation(Access.class);
        Access access = null;//from  w  w  w.  j  ava  2s .c o  m
        if (classAnnotation instanceof Access) {
            access = (Access) classAnnotation;
        }
        boolean isFieldAccess = (access == null || AccessType.FIELD == access.value());
        Field[] fields = clazz.getDeclaredFields();
        if (isFieldAccess) {
            for (Field field : fields) {
                registerFieldAnnotation(field);
            }
        } else {
            for (Field field : fields) {
                registerMethodAnnotation(field, clazz);
            }
        }
    }
}

From source file:org.xaloon.wicket.component.application.VirtualPageFactory.java

private <C extends Page> Page newPageInternal(Class<C> pageClass, PageParameters parameters) {
    MountPanel mp = pageClass.getAnnotation(MountPanel.class);
    if (mp != null) {
        try {//  www  . j  av a 2s  . c o  m

            Panel content = newInstance(mp, parameters);
            Class<? extends Page> layoutPageClass = AbstractWebApplication.get().getLayoutPageClass();
            if (layoutPageClass != null) {
                Page page = createPage(layoutPageClass, parameters);
                page.addOrReplace(content);
                injectAdditionalInfo(page, pageClass);

                return page;
            }
        } catch (Exception e) {
            log.error(e);
            e.printStackTrace();
        }
    }
    return createPage(pageClass, parameters);
}

From source file:info.mikaelsvensson.devtools.analysis.shared.CommandLineUtil.java

public List<Option> getOptions(Object owner) throws IllegalAccessException {
    List<Option> options = new ArrayList<Option>();
    Class<?> cls = owner.getClass();
    do {/* www .j  a  v a  2  s.c  om*/
        CliOptions cliOptions = cls.getAnnotation(CliOptions.class);
        if (cliOptions != null) {
            for (CliOptionConfig config : cliOptions.opts()) {

                if (config != null) {
                    Option option = new Option(config.name(), config.description());
                    if (config.longName().length() > 0) {
                        option.setLongOpt(config.longName());
                    }
                    if (config.numArgs() == OPTIONAL) {
                        option.setOptionalArg(true);
                    } else {
                        option.setArgs(config.numArgs());
                    }
                    option.setArgName(config.argsDescription());
                    option.setRequired(config.required());
                    option.setValueSeparator(config.separator());
                    options.add(option);
                }
            }
        }
    } while ((cls = cls.getSuperclass()) != null);
    return options;
}

From source file:org.elasticsoftware.elasticactors.runtime.PluggableMessageHandlersScanner.java

@PostConstruct
public void init() {
    String[] basePackages = ScannerHelper.findBasePackagesOnClasspath(applicationContext.getClassLoader());
    ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();

    for (String basePackage : basePackages) {
        configurationBuilder.addUrls(ClasspathHelper.forPackage(basePackage));
    }/*from   w w  w  .  j av a2 s  . co m*/

    Reflections reflections = new Reflections(configurationBuilder);

    Set<Class<?>> handlerClasses = reflections.getTypesAnnotatedWith(PluggableMessageHandlers.class);

    for (Class<?> handlerClass : handlerClasses) {
        PluggableMessageHandlers handlerAnnotation = handlerClass.getAnnotation(PluggableMessageHandlers.class);
        registry.put(handlerAnnotation.value(), handlerClass);
    }

    Set<Class<? extends ActorLifecycleListener>> listenerClasses = reflections
            .getSubTypesOf(ActorLifecycleListener.class);
    for (Class<? extends ActorLifecycleListener> listenerClass : listenerClasses) {
        try {
            ActorLifecycleListener lifeCycleListener = listenerClass.newInstance();
            // ensure that the lifeCycle listener handles the correct state class
            lifecycleListeners.put(lifeCycleListener.getActorClass(), lifeCycleListener);
        } catch (Exception e) {
            logger.error("Exception while instantiating ActorLifeCycleListener", e);
        }
    }
}

From source file:com.excilys.spring.mom.annotation.MOMAnnotationProcessing.java

@Override
public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
    final Class<?> clazz = bean.getClass();
    MOMController classAnnotation = clazz.getAnnotation(MOMController.class);

    // If the bean is annotated with @MOMController
    if (classAnnotation != null) {
        LOGGER.debug("Found @MOMController annotated class : {}", clazz);

        ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() {
            public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
                MOMMapping methodAnnotation = method.getAnnotation(MOMMapping.class);

                // If the method is annotated with @MOMMapping
                if (methodAnnotation != null) {
                    String topic = resolveProperty(methodAnnotation.topic());
                    MOMMappingConsum consum = methodAnnotation.consumes();

                    LOGGER.debug("Configuring @MOMMapping({}) method {}", consum, method);

                    try {
                        momClient.subscribe(topic, new MOMMethodHandler(method, bean, consum));
                    } catch (NotConnectedException e) {
                        LOGGER.error("Can't subscribe to topic {}", topic, e);
                    } catch (SocketException e) {
                        LOGGER.error("Can't subscribe to topic {}", topic, e);
                    }/*from   w  w  w .  ja va 2 s . c  om*/
                }
            }
        });
    }

    return bean;
}