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:org.easyj.orm.jpa.JPAEntityService.java

protected <T> String getNamedQuery(String queryName, Class<T> klazz) {
    NamedQueries queries = klazz.getAnnotation(NamedQueries.class);

    if (queries != null) {
        queryName = queryName.toLowerCase();
        for (NamedQuery named : queries.value()) {
            if (queryName.equals(named.name().toLowerCase())) {
                return named.query();
            }//from  w w  w  .ja  va 2s .  c om
        }
    }

    return null;
}

From source file:org.springdata.ehcache.mapping.BasicEhcachePersistentEntity.java

@SuppressWarnings("unchecked")
public BasicEhcachePersistentEntity(TypeInformation<T> typeInformation) {

    super(typeInformation);

    this.parser = new SpelExpressionParser();
    this.context = new StandardEvaluationContext();

    Class<T> rawType = typeInformation.getType();

    if (rawType.isAnnotationPresent(Entity.class)) {
        Entity annotation = rawType.getAnnotation(Entity.class);
        this.cacheName = StringUtils.hasText(annotation.cacheName()) ? annotation.cacheName() : null;
    } else {//from   w  w  w  . j av a2s  .  c om
        this.cacheName = null;
    }

    if (this.cacheName == null) {
        throw new BeanInitializationException(
                "entity class " + rawType + " does not specify cache name by Cache annotation");
    }

    if (DataSerializable.class.isAssignableFrom(rawType)) {
        serializer = new DataSerializer<T>();
        deserializer = new DataDeserializer<T>(rawType);
    } else {
        serializer = (Serializer<T>) new DefaultSerializer();
        deserializer = (Deserializer<T>) new DefaultDeserializer();
    }

}

From source file:com.github.lightdocs.ModelBuilder.java

@VisibleForTesting
String getRootResourceURIFor(Class<?> c) {
    return '/' + standardizePathAnnotationValue(c.getAnnotation(Path.class));
}

From source file:com.mmnaseri.dragonfly.events.CallbackResolver.java

@Override
public synchronized void postProcessMetadataContext(TableMetadataContext tableMetadataContext) {
    log.info("Looking up entity callbacks");
    final Collection<Class<?>> entityTypes = tableMetadataContext.getEntityTypes();
    eventHandlers.add(new EntityEventCallback());
    for (Class<?> entityType : entityTypes) {
        if (entityType.isAnnotationPresent(EntityListeners.class)) {
            final EntityListeners listeners = entityType.getAnnotation(EntityListeners.class);
            for (final Class type : listeners.value()) {
                log.info("Registering external callback: " + type);
                eventHandlers.add(new EntityEventCallback(entityType, type));
            }//from   www .  j a v a 2 s .com
        }
    }
    handlersFound = true;
    if (contextFound) {
        registerHandlers();
    }
}

From source file:com.esofthead.mycollab.common.interceptor.aspect.MonitorItemAspect.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();
    try {//from   w w  w .ja  va2 s.com
        Watchable watchableAnnotation = cls.getAnnotation(Watchable.class);
        if (watchableAnnotation != null) {
            int sAccountId = (Integer) PropertyUtils.getProperty(bean, "saccountid");
            int typeId = (Integer) PropertyUtils.getProperty(bean, "id");
            Integer extraTypeId = null;
            if (!"".equals(watchableAnnotation.extraTypeId())) {
                extraTypeId = (Integer) PropertyUtils.getProperty(bean, watchableAnnotation.extraTypeId());
            }

            MonitorItem monitorItem = new MonitorItem();
            monitorItem.setMonitorDate(new GregorianCalendar().getTime());
            monitorItem.setType(ClassInfoMap.getType(cls));
            monitorItem.setTypeid(typeId);
            monitorItem.setExtratypeid(extraTypeId);
            monitorItem.setUser(username);
            monitorItem.setSaccountid(sAccountId);

            monitorItemService.saveWithSession(monitorItem, username);
            LOG.debug("Save monitor item: " + BeanUtility.printBeanObj(monitorItem));

            if (!watchableAnnotation.userFieldName().equals("")) {
                String moreUser = (String) PropertyUtils.getProperty(bean, watchableAnnotation.userFieldName());
                if (moreUser != null && !moreUser.equals(username)) {
                    monitorItem.setId(null);
                    monitorItem.setUser(moreUser);
                    monitorItemService.saveWithSession(monitorItem, moreUser);
                }
            }
        }

        NotifyAgent notifyAgent = cls.getAnnotation(NotifyAgent.class);
        if (notifyAgent != null) {
            int sAccountId = (Integer) PropertyUtils.getProperty(bean, "saccountid");
            int typeId = (Integer) PropertyUtils.getProperty(bean, "id");
            RelayEmailNotificationWithBLOBs relayNotification = new RelayEmailNotificationWithBLOBs();
            relayNotification.setChangeby(username);
            relayNotification.setChangecomment("");
            relayNotification.setSaccountid(sAccountId);
            relayNotification.setType(ClassInfoMap.getType(cls));
            relayNotification.setAction(MonitorTypeConstants.CREATE_ACTION);
            relayNotification.setTypeid("" + typeId);
            relayNotification.setEmailhandlerbean(notifyAgent.value().getName());
            relayEmailNotificationService.saveWithSession(relayNotification, username);
            // Save notification item
        }
    } catch (Exception e) {
        LOG.error("Error when save relay email notification for save action of service " + cls.getName(), e);
    }
}

From source file:com.haulmont.cuba.web.sys.CubaUidlWriter.java

@SuppressWarnings("deprecation")
@Override/*from w w  w .j a v a 2  s.c  om*/
protected void handleAdditionalDependencies(List<Class<? extends ClientConnector>> newConnectorTypes,
        List<String> scriptDependencies, List<String> styleDependencies) {
    LegacyCommunicationManager manager = VaadinSession.getCurrent().getCommunicationManager();

    for (Class<? extends ClientConnector> connector : newConnectorTypes) {
        WebJarResource webJarResource = connector.getAnnotation(WebJarResource.class);
        if (webJarResource == null)
            continue;

        for (String uri : webJarResource.value()) {
            String resourceUri = processResourceUri(uri);
            String resourcePath = getResourceActualPath(resourceUri);

            resourcePath = resourcePath.replace(META_INF_PREFIX, VAADIN_PREFIX);

            if (resourcePath.endsWith(JAVASCRIPT_EXTENSION)) {
                scriptDependencies.add(manager.registerDependency(resourcePath, connector));
            }

            if (resourcePath.endsWith(CSS_EXTENSION)) {
                styleDependencies.add(manager.registerDependency(resourcePath, connector));
            }
        }
    }
}

From source file:cf.spring.servicebroker.AnnotationCatalogAccessorProvider.java

protected CatalogAccessor initializeAccessor() {
    final List<BrokerServiceAccessor> serviceAccessors = new ArrayList<>();

    final String[] serviceBrokers = context.getBeanNamesForAnnotation(ServiceBroker.class);
    for (String serviceBrokerName : serviceBrokers) {
        final Class<?> clazz = getBeanClass(serviceBrokerName);

        final ServiceBroker serviceBroker = clazz.getAnnotation(ServiceBroker.class);

        for (Service service : serviceBroker.value()) {
            final CatalogService catalogService = buildCatalogService(service);

            serviceAccessors.add(getMethodAccessor(serviceBrokerName, catalogService));
        }//from w  w w  .  ja  va  2 s.  c  om
    }

    return new CatalogAccessor(serviceAccessors);
}

From source file:grails.plugin.springsecurity.acl.access.method.SecuredAnnotationSecurityMetadataSource.java

/**
 * {@inheritDoc}// w  ww .j  ava2 s.co  m
 * @see org.springframework.security.access.method.AbstractFallbackMethodSecurityMetadataSource#findAttributes(
 *    java.lang.Class)
 */
@Override
protected Collection<ConfigAttribute> findAttributes(final Class<?> clazz) {

    Class<?> actualClass = ProxyUtils.unproxy(clazz);

    if (!isService(actualClass)) {
        return null;
    }

    return processAnnotation(actualClass.getAnnotation(Secured.class));
}

From source file:com.fantasy.stataggregator.entities.dao.AbstractRepository.java

public List<T> findByNamedQuery(Class<T> entity, String name, Map<String, ? extends Object> parameters) {
    NamedQueries nq = entity.getAnnotation(NamedQueries.class);
    NamedQuery[] nqs = nq.value();//  w  ww  .j  a  v a 2 s.  c o m
    String queryName = null;

    for (NamedQuery qn : nqs) {
        String qname = qn.name();
        if (qname.contains(name)) {
            queryName = qname;
            break;
        }
    }
    List<T> results = new ArrayList(1);
    if (Objects.nonNull(queryName)) {
        TypedQuery<T> query = em.createNamedQuery(queryName, entityClass);
        if (Objects.nonNull(parameters) && !parameters.isEmpty()) {
            parameters.entrySet().stream().forEach((entry) -> {
                query.setParameter((String) entry.getKey(), entry.getValue());
            });
        }
        results = query.getResultList();
    }
    return results;
}

From source file:com.taobao.itest.core.TestContextManager.java

private TestListener[] retrieveTestListeners(Class<?> clazz) {
    Class<TestListeners> annotationType = TestListeners.class;
    @SuppressWarnings("rawtypes")
    List<Class> classesAnnotationDeclared = AnnotationUtil.findClassesAnnotationDeclaredWith(clazz,
            annotationType);/*www.  j  ava  2s . c o m*/
    List<Class<? extends TestListener>> classesList = new ArrayList<Class<? extends TestListener>>();
    for (Class<?> classAnnotationDeclared : classesAnnotationDeclared) {
        TestListeners testListeners = classAnnotationDeclared.getAnnotation(annotationType);
        Class<? extends TestListener>[] valueListenerClasses = testListeners.value();
        Class<? extends TestListener>[] listenerClasses = testListeners.listeners();
        if (!ArrayUtils.isEmpty(valueListenerClasses) && !ArrayUtils.isEmpty(listenerClasses)) {
            String msg = String.format(
                    "Test class [%s] has been configured with @TestListeners' 'value' [%s] and 'listeners' [%s] attributes. Use one or the other, but not both.",
                    classAnnotationDeclared, ArrayUtils.toString(valueListenerClasses),
                    ArrayUtils.toString(listenerClasses));
            throw new RuntimeException(msg);
        } else if (!ArrayUtils.isEmpty(valueListenerClasses)) {
            listenerClasses = valueListenerClasses;
        }

        if (listenerClasses != null) {
            classesList.addAll(0, Arrays.<Class<? extends TestListener>>asList(listenerClasses));
        }
        if (!testListeners.inheritListeners()) {
            break;
        }
    }

    List<TestListener> listeners = new ArrayList<TestListener>(classesList.size());
    for (Class<? extends TestListener> listenerClass : classesList) {
        listeners.add((TestListener) BeanUtils.instantiateClass(listenerClass));
    }
    return listeners.toArray(new TestListener[listeners.size()]);
}