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:moe.encode.airblock.commands.core.components.ComponentBag.java

/**
 * Returns the correct method for the implementation.
 *
 * @param cls     The class which method should be searched.
 * @param method  The method that should be found.
 * @param handle  The handle that is affected by the holder.
 * @return The method or {@code null} if the method was not found.
 *///from   w  w w  .j a v a2  s. c o  m
@SuppressWarnings("unchecked")
Method getMethod(Class<?> cls, Method method, Handle<?> handle) {
    Class<?> current = cls;
    while (current.getAnnotation(Components.class) != null) {
        Class<?> handleCls = handle.getClass();
        while (Handle.class.isAssignableFrom(handleCls)) {
            try {
                return current.getDeclaredMethod(method.getName(),
                        ArrayUtils.add(method.getParameterTypes(), 0, handleCls));
            } catch (NoSuchMethodException ignored) {
                handleCls = handleCls.getSuperclass();
            }
        }
        current = current.getSuperclass();
    }
    return null;
}

From source file:com.zhaimi.message.event.DataEventMessageDispatcher.java

/**
 * @param listener/*from   w ww.  j ava  2 s .c  o  m*/
 */
private void triggerDataEventMethod(Object listener, MsgBean<String, String> msgBean) {

    Class<?> clazz = listener.getClass();
    DataListener dataListener = clazz.getAnnotation(DataListener.class);
    String dataObject = dataListener.dataObject();

    triggerDataEvent(listener, dataObject, msgBean);
}

From source file:de.openknowledge.jaxrs.versioning.conversion.InterversionConverter.java

public <T> T convertToLowerVersion(String targetVersion, Object source) {
    Class<?> sourceType = source.getClass();
    SupportedVersion supportedVersion = sourceType.getAnnotation(SupportedVersion.class);
    if (supportedVersion == null) {
        throw new IllegalVersionException(targetVersion);
    }/*from w  ww .  j a v  a2  s  .  c om*/
    if (targetVersion.equals(supportedVersion.version())) {
        return (T) source;
    }
    if (supportedVersion.previous() == Object.class) {
        throw new IllegalVersionException(targetVersion);
    }
    return convertToLowerVersion(targetVersion,
            map(source, supportedVersion.previous(), new DefaultVersionContext()));
}

From source file:com.taobao.itest.listener.ITestSpringContextListener.java

private String[] retrieveLocations(Class<?> clazz) {
    Class<ITestSpringContext> annotationType = ITestSpringContext.class;
    @SuppressWarnings("rawtypes")
    List<Class> classesAnnotationDeclared = AnnotationUtil.findClassesAnnotationDeclaredWith(clazz,
            annotationType);//from w w  w.  ja  va  2 s. c o  m
    List<String> locationsList = new ArrayList<String>();
    for (Class<?> classAnnotationDeclared : classesAnnotationDeclared) {
        ITestSpringContext iTestSpringContext = classAnnotationDeclared.getAnnotation(annotationType);
        String[] value = iTestSpringContext.value();
        String[] locations = iTestSpringContext.locations();
        if (!ArrayUtils.isEmpty(value) && !ArrayUtils.isEmpty(locations)) {
            String msg = String.format(
                    "Test class [%s] has been configured with @ITestSpringContext' 'value' [%s] and 'locations' [%s] attributes. Use one or the other, but not both.",
                    classAnnotationDeclared, ArrayUtils.toString(value), ArrayUtils.toString(locations));
            throw new RuntimeException(msg);
        } else if (!ArrayUtils.isEmpty(value)) {
            locations = value;
        }

        if (locations != null) {
            locationsList.addAll(0, Arrays.<String>asList(locations));
        }
        if (!iTestSpringContext.inheritLocations()) {
            break;
        }
    }
    return locationsList.toArray(new String[locationsList.size()]);
}

From source file:com.ocs.dynamo.ui.auth.DefaultPermissionCheckerImpl.java

@PostConstruct
public void postConstruct() {

    // scan the class path for all classes annotated with @SpringView
    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(
            true);//from w w w. jav  a2s . c  o m
    provider.addIncludeFilter(new AnnotationTypeFilter(SpringView.class));

    Set<BeanDefinition> views = provider.findCandidateComponents(basePackage);
    for (BeanDefinition d : views) {
        try {
            Class<?> clazz = Class.forName(d.getBeanClassName());

            SpringView view = clazz.getAnnotation(SpringView.class);

            // store the permissions both under the bean name and the view
            // name - unfortunately these
            // don't always have to match but there is no way to tell this
            // to the authentication framework!
            Authorized auth = clazz.getAnnotation(Authorized.class);
            if (auth != null && auth.roles().length > 0) {
                int p = d.getBeanClassName().lastIndexOf(".");
                permissions.put(d.getBeanClassName().substring(p + 1), Arrays.asList(auth.roles()));
                editOnly.put(d.getBeanClassName().substring(p + 1), auth.editOnly());

                permissions.put(view.name(), Arrays.asList(auth.roles()));
                editOnly.put(view.name(), auth.editOnly());
            }
        } catch (ClassNotFoundException e) {
            LOG.error(e.getMessage(), e);
        }
    }
}

From source file:com.dotcallservice.manager.ServiceLocator.java

@SuppressWarnings("unchecked")
public <T> T getService(Class<T> service) {
    if (servletConfig == null) {
        throw new IllegalStateException("ServiceManager has not been initialized !");
    }// w  ww .  j a v a2s  .c  o m

    Service daoService = service.getAnnotation(Service.class);
    return (T) getStringBean(daoService.serviceName());
}

From source file:org.jacpfx.vertx.spring.SpringVerticleFactory.java

private Verticle createSpringVerticle(final Class<?> currentVerticleClass, ClassLoader classLoader) {
    final SpringVerticle annotation = currentVerticleClass.getAnnotation(SpringVerticle.class);
    final Class<?> springConfigClass = annotation.springConfig();

    // Create the parent context  
    final GenericApplicationContext genericApplicationContext = new GenericApplicationContext();
    genericApplicationContext.setClassLoader(classLoader);
    if (parentContext != null) {
        genericApplicationContext.setParent(parentContext);
    }//from ww w  . j a  va  2s .com
    genericApplicationContext.refresh();
    genericApplicationContext.start();

    // 1. Create a new context for each verticle and use the specified spring configuration class if possible
    AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
    annotationConfigApplicationContext.setParent(genericApplicationContext);
    annotationConfigApplicationContext.register(SpringContextConfiguration.class, springConfigClass);

    // 2. Register a bean definition for this verticle
    annotationConfigApplicationContext.registerBeanDefinition(currentVerticleClass.getSimpleName(),
            new VerticleBeanDefinition(currentVerticleClass));

    // 3. Add a bean factory post processor to avoid configuration issues
    annotationConfigApplicationContext
            .addBeanFactoryPostProcessor(new SpringSingleVerticleConfiguration(currentVerticleClass));
    annotationConfigApplicationContext.refresh();
    annotationConfigApplicationContext.start();
    annotationConfigApplicationContext.registerShutdownHook();

    // 5. Return the verticle by fetching the bean from the context
    return (Verticle) annotationConfigApplicationContext.getBeanFactory()
            .getBean(currentVerticleClass.getSimpleName());
}

From source file:org.wte4j.impl.format.FormatterRegistry.java

void registerNamedFormatter(Class<? extends Formatter> someClass) {
    FormatterName annotatedName = someClass.getAnnotation(FormatterName.class);
    String name = someClass.getSimpleName();
    if (annotatedName != null) {
        name = annotatedName.value();/*  www  . jav  a  2s. co  m*/
    }
    if (namedFormatters.containsKey(name)) {
        logger.warn("replace formatter {} with class {}", name, someClass.getName());
    }
    namedFormatters.put(name, (Class<? extends Formatter>) someClass);
    logger.info("registered formatter {} with name {}", someClass.getName(), name);
}

From source file:com.nabla.wapp.server.database.UpdateStatement.java

@SuppressWarnings("unchecked")
private IRecordTable commonConstructor(final Class clazz) {
    if (clazz == null)
        return null;
    final IRecordTable t = (IRecordTable) clazz.getAnnotation(IRecordTable.class);
    for (Field field : clazz.getDeclaredFields()) {
        final IRecordField definition = field.getAnnotation(IRecordField.class);
        if (definition == null)
            continue;
        if (definition.id())
            recordId = createParameter(field);
        else {/*w  w  w.j  a va2s  .  co  m*/
            if (definition.unique())
                uniqueFieldName = field.getName();
            parameters.add(createParameter(field));
        }
    }
    final IRecordTable tt = commonConstructor(clazz.getSuperclass());
    return (t == null) ? tt : t;
}

From source file:org.wte4j.impl.format.FormatterRegistry.java

private void registerDefaultFormatter(Class<? extends Formatter> someClass) {
    DefaultFormatter defaultAnnnotation = someClass.getAnnotation(DefaultFormatter.class);
    if (defaultAnnnotation != null) {
        checkDefaultFormatterClass(someClass);
        for (Class<?> type : defaultAnnnotation.value()) {
            registerDefaultFormatter(type, someClass);
        }//from w w w .j av a 2 s .  c o  m
    }
}