List of usage examples for org.springframework.core.annotation AnnotationUtils findAnnotation
@Nullable public static <A extends Annotation> A findAnnotation(Class<?> clazz, @Nullable Class<A> annotationType)
From source file:org.springframework.web.method.ControllerAdviceBean.java
/** * Checks whether the given bean type should be assisted by this * {@code @ControllerAdvice} instance.//from w w w . j a v a2 s .c om * @param beanType the type of the bean to check * @see org.springframework.web.bind.annotation.ControllerAdvice * @since 4.0 */ public boolean isApplicableToBeanType(Class<?> beanType) { if (!hasSelectors()) { return true; } else if (beanType != null) { for (Class<?> clazz : this.assignableTypes) { if (ClassUtils.isAssignable(clazz, beanType)) { return true; } } for (Class<? extends Annotation> annotationClass : this.annotations) { if (AnnotationUtils.findAnnotation(beanType, annotationClass) != null) { return true; } } String packageName = beanType.getPackage().getName(); for (Package basePackage : this.basePackages) { if (packageName.startsWith(basePackage.getName())) { return true; } } } return false; }
From source file:org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.java
@Override public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { Class<?> clazz = ClassUtils.getUserClass(handler); Boolean annotatedWithSessionAttributes = this.sessionAnnotatedClassesCache.get(clazz); if (annotatedWithSessionAttributes == null) { annotatedWithSessionAttributes = (AnnotationUtils.findAnnotation(clazz, SessionAttributes.class) != null); this.sessionAnnotatedClassesCache.put(clazz, annotatedWithSessionAttributes); }//from ww w . ja v a 2 s. c om if (annotatedWithSessionAttributes) { // Always prevent caching in case of session attribute management. checkAndPrepare(request, response, this.cacheSecondsForSessionAttributeHandlers, true); // Prepare cached set of session attributes names. } else { // Uses configured default cacheSeconds setting. checkAndPrepare(request, response, true); } // Execute invokeHandlerMethod in synchronized block if required. if (this.synchronizeOnSession) { HttpSession session = request.getSession(false); if (session != null) { Object mutex = WebUtils.getSessionMutex(session); synchronized (mutex) { return invokeHandlerMethod(request, response, handler); } } } return invokeHandlerMethod(request, response, handler); }
From source file:org.springframework.web.servlet.mvc.annotation.MyAnnotationMethodHandlerAdapter.java
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { Class<?> clazz = ClassUtils.getUserClass(handler); Boolean annotatedWithSessionAttributes = this.sessionAnnotatedClassesCache.get(clazz); if (annotatedWithSessionAttributes == null) { annotatedWithSessionAttributes = (AnnotationUtils.findAnnotation(clazz, SessionAttributes.class) != null); this.sessionAnnotatedClassesCache.put(clazz, annotatedWithSessionAttributes); }//from w w w . j a va 2 s . c o m if (annotatedWithSessionAttributes) { // Always prevent caching in case of session attribute management. checkAndPrepare(request, response, this.cacheSecondsForSessionAttributeHandlers, true); // Prepare cached set of session attributes names. } else { // Uses configured default cacheSeconds setting. checkAndPrepare(request, response, true); } // Execute invokeHandlerMethod in synchronized block if required. if (this.synchronizeOnSession) { HttpSession session = request.getSession(false); if (session != null) { Object mutex = WebUtils.getSessionMutex(session); synchronized (mutex) { return invokeHandlerMethod(request, response, handler); } } } return invokeHandlerMethod(request, response, handler); }
From source file:org.springframework.web.servlet.mvc.support.MvcUrlUtils.java
/** * Extract the type-level URL mapping or return an empty String. If multiple mappings * are found, the first one is used./*w w w .jav a 2 s . c o m*/ */ public static String getTypeLevelMapping(Class<?> controllerType) { Assert.notNull(controllerType, "'controllerType' must not be null"); RequestMapping annot = AnnotationUtils.findAnnotation(controllerType, RequestMapping.class); if ((annot == null) || ObjectUtils.isEmpty(annot.value())) { return "/"; } if (annot.value().length > 1) { logger.warn("Multiple class level mappings on " + controllerType.getName() + ", using the first one"); } return annot.value()[0]; }
From source file:org.springframework.web.servlet.mvc.support.MvcUrlUtils.java
/** * Extract the mapping from the given controller method, including both type and * method-level mappings. If multiple mappings are found, the first one is used. *//* w ww . java2 s . co m*/ public static String getMethodMapping(Method method) { RequestMapping methodAnnot = AnnotationUtils.findAnnotation(method, RequestMapping.class); Assert.notNull(methodAnnot, "No mappings on " + method.toGenericString()); PatternsRequestCondition condition = new PatternsRequestCondition(methodAnnot.value()); RequestMapping typeAnnot = AnnotationUtils.findAnnotation(method.getDeclaringClass(), RequestMapping.class); if (typeAnnot != null) { condition = new PatternsRequestCondition(typeAnnot.value()).combine(condition); } Set<String> patterns = condition.getPatterns(); if (patterns.size() > 1) { logger.warn("Multiple mappings on " + method.toGenericString() + ", using the first one"); } return (patterns.size() == 0) ? "/" : patterns.iterator().next(); }
From source file:org.springframework.web.socket.server.standard.SpringConfigurator.java
@SuppressWarnings("unchecked") @Override/*from w ww . j a v a 2 s . c o m*/ public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException { WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); if (wac == null) { String message = "Failed to find the root WebApplicationContext. Was ContextLoaderListener not used?"; logger.error(message); throw new IllegalStateException(message); } String beanName = ClassUtils.getShortNameAsProperty(endpointClass); if (wac.containsBean(beanName)) { T endpoint = wac.getBean(beanName, endpointClass); if (logger.isTraceEnabled()) { logger.trace("Using @ServerEndpoint singleton " + endpoint); } return endpoint; } Component ann = AnnotationUtils.findAnnotation(endpointClass, Component.class); if (ann != null && wac.containsBean(ann.value())) { T endpoint = wac.getBean(ann.value(), endpointClass); if (logger.isTraceEnabled()) { logger.trace("Using @ServerEndpoint singleton " + endpoint); } return endpoint; } beanName = getBeanNameByType(wac, endpointClass); if (beanName != null) { return (T) wac.getBean(beanName); } if (logger.isTraceEnabled()) { logger.trace("Creating new @ServerEndpoint instance of type " + endpointClass); } return wac.getAutowireCapableBeanFactory().createBean(endpointClass); }
From source file:org.springframework.yarn.config.annotation.SpringYarnConfigurerAdapter.java
@Override public final void init(SpringYarnConfigBuilder builder) throws Exception { builder.setSharedObject(YarnConfigBuilder.class, getConfigBuilder()); builder.setSharedObject(YarnResourceLocalizerBuilder.class, getLocalizerBuilder()); builder.setSharedObject(YarnEnvironmentBuilder.class, getEnvironmentBuilder()); EnableYarn annotation = AnnotationUtils.findAnnotation(getClass(), EnableYarn.class); Enable enable = annotation.enable(); if (log.isDebugEnabled()) { log.debug("Enabling builder for " + enable); }/*from ww w .j a v a 2 s . co m*/ if (enable == Enable.CLIENT) { builder.setSharedObject(YarnClientBuilder.class, getClientBuilder()); } else if (enable == Enable.APPMASTER) { builder.setSharedObject(YarnAppmasterBuilder.class, getAppmasterBuilder()); } else if (enable == Enable.CONTAINER) { builder.setSharedObject(YarnContainerBuilder.class, getContainerBuilder()); } }
From source file:org.squashtest.tm.service.security.acls.domain.InheritableAclsObjectIdentityRetrievalStrategy.java
/** * If the domain object is annotated with {@link InheritsAcls}, will return an {@link ObjectIdentity} according to * the annotation rules. Otherwise, simple delegates the retrieval. *//*from w ww .j a v a 2 s .co m*/ @Override public ObjectIdentity getObjectIdentity(Object domainObject) { InheritsAcls inherits = AnnotationUtils.findAnnotation(domainObject.getClass(), InheritsAcls.class); Object identityHolder; if (inherits == null) { identityHolder = domainObject; LOGGER.trace("Will use domain object for OID retrieval of {}", identityHolder); } else { identityHolder = findAclHolder(domainObject, inherits); LOGGER.trace("Will use constrained object {} for OID retrieval of {}", identityHolder, domainObject); } return delegate.getObjectIdentity(identityHolder); }
From source file:org.tdar.core.service.ReflectionService.java
/** * Check whether the identified Method or Action has the annotation * /*from w w w .ja v a2 s .c o m*/ * @param invocation * @param annotationClass * @return * @throws SecurityException * @throws NoSuchMethodException */ public static boolean methodOrActionContainsAnnotation(ActionInvocation invocation, Class<? extends Annotation> annotationClass) throws SecurityException, NoSuchMethodException { Object action = invocation.getAction(); ActionProxy proxy = invocation.getProxy(); String methodName = proxy.getMethod(); Method method = null; if (methodName == null) { methodName = EXECUTE; } String key = annotationClass.getCanonicalName() + "|" + action.getClass().getCanonicalName() + "$" + methodName; Boolean found = annotationLookupCache.get(key); staticLogger.trace("key: {}, found: {}", key, found); if (found != null) { return found; } found = Boolean.FALSE; if (action != null) { method = action.getClass().getMethod(methodName); } if (method != null) { Object class_ = AnnotationUtils.findAnnotation(method.getDeclaringClass(), annotationClass); Object method_ = AnnotationUtils.findAnnotation(method, annotationClass); found = ((class_ != null) || (method_ != null)); } Annotation parentClassAnnotation = AnnotationUtils.findAnnotation(action.getClass(), annotationClass); if (parentClassAnnotation != null) { found = true; } annotationLookupCache.put(key, found); return found; }
From source file:org.tdar.core.service.ReflectionService.java
/** * For the specified Method, return the annotation of the identified Class. * /* w w w. ja va2 s . c o m*/ * @param method * @param annotationClass * @return */ public static <C extends Annotation> C getAnnotationFromMethodOrClass(Method method, Class<C> annotationClass) { C method_ = AnnotationUtils.findAnnotation(method, annotationClass); if (method_ != null) { return method_; } C class_ = AnnotationUtils.findAnnotation(method.getDeclaringClass(), annotationClass); if (class_ != null) { return class_; } return null; }