Java Code Examples for org.springframework.core.annotation.AnnotatedElementUtils#hasAnnotation()

The following examples show how to use org.springframework.core.annotation.AnnotatedElementUtils#hasAnnotation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpringMethodFilter.java    From RestDoc with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isSupport(Method method) {
    if (method.isSynthetic() || method.isBridge())
        return false;



    // 如果方法和类上都没有ResponseBody,返回false
    if (!AnnotatedElementUtils.hasAnnotation(method, ResponseBody.class) &&
        !AnnotatedElementUtils.hasAnnotation(method.getDeclaringClass(), ResponseBody.class))
    {
            return false;
    }
    var annotations = method.getAnnotations();
    for (var annotation : annotations) {
        var annotationType = annotation.annotationType();

        if (_classes.contains(annotationType))
            return true;
    }
    return false;
}
 
Example 2
Source File: AutoRequestBodyProcessor.java    From summerframework with Apache License 2.0 6 votes vote down vote up
@Override
protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) {
    if (AnnotatedElementUtils.hasAnnotation(parameter.getContainingClass(), ApiController.class)) {
        binder.validate();
    } else {
        Annotation[] annotations = parameter.getParameterAnnotations();
        for (Annotation ann : annotations) {
            Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
            if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
                Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
                Object[] validationHints = (hints instanceof Object[] ? (Object[])hints : new Object[] {hints});
                binder.validate(validationHints);
                break;
            }
        }
    }
}
 
Example 3
Source File: FixFeignClientsHandlerMapping.java    From onetwo with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isHandler(Class<?> beanType) {
	//避免springmvc 把抽象出来的带有RequestMapping注解的client接口扫描到controller注册
	boolean isFeignClient = AnnotatedElementUtils.hasAnnotation(beanType, FeignClient.class);
	if(BootCloudUtils.isNetflixFeignClientPresent() && isFeignClient){
		if(log.isInfoEnabled()){
			log.info("ignore FeignClient: {}", beanType);
		}
		return false;
	}
	return super.isHandler(beanType);
}
 
Example 4
Source File: AutoRequestBodyProcessor.java    From summerframework with Apache License 2.0 5 votes vote down vote up
@Override
public boolean supportsParameter(MethodParameter parameter) {
    if (AnnotatedElementUtils.hasAnnotation(parameter.getContainingClass(), ApiController.class)){
        ApiController ann = AnnotationUtils.findAnnotation(parameter.getContainingClass(), ApiController.class);
        if(ann.requestBody()&&!hasSpringAnnotation(parameter)){
            return true;
        }
    }
    return super.supportsParameter(parameter);
}
 
Example 5
Source File: WebTestContextBootstrapper.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Returns {@link WebDelegatingSmartContextLoader} if the supplied class is
 * annotated with {@link WebAppConfiguration @WebAppConfiguration} and
 * otherwise delegates to the superclass.
 */
@Override
protected Class<? extends ContextLoader> getDefaultContextLoaderClass(Class<?> testClass) {
	if (AnnotatedElementUtils.hasAnnotation(testClass, WebAppConfiguration.class)) {
		return WebDelegatingSmartContextLoader.class;
	}
	else {
		return super.getDefaultContextLoaderClass(testClass);
	}
}
 
Example 6
Source File: WxMappingHandlerMapping.java    From FastBootWeixin with Apache License 2.0 5 votes vote down vote up
@Override
protected HandlerMethod createHandlerMethod(Object handler, Method method) {
    if (handler instanceof String) {
        String beanName = (String) handler;
        handler = this.getApplicationContext().getAutowireCapableBeanFactory().getBean(beanName);
    }
    if (AnnotatedElementUtils.hasAnnotation(method, WxAsyncMessage.class) || AnnotatedElementUtils.hasAnnotation(handler.getClass(), WxAsyncMessage.class)) {
        return new HandlerMethod(wxAsyncHandlerFactory.createHandler(handler), method);
    } else {
        return new HandlerMethod(handler, method);
    }
}
 
Example 7
Source File: SimpAnnotationMethodMessageHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected boolean isHandler(Class<?> beanType) {
	return AnnotatedElementUtils.hasAnnotation(beanType, Controller.class);
}
 
Example 8
Source File: RequestMappingHandlerMapping.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * {@inheritDoc}
 * Expects a handler to have a type-level @{@link Controller} annotation.
 */
@Override
protected boolean isHandler(Class<?> beanType) {
	return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
			AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
}
 
Example 9
Source File: BeanAnnotationHelper.java    From java-technology-stack with MIT License 4 votes vote down vote up
public static boolean isBeanAnnotated(Method method) {
	return AnnotatedElementUtils.hasAnnotation(method, Bean.class);
}
 
Example 10
Source File: TransactionalEventListenerFactory.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public boolean supportsMethod(Method method) {
	return AnnotatedElementUtils.hasAnnotation(method, TransactionalEventListener.class);
}
 
Example 11
Source File: RequestMappingHandlerMapping.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * {@inheritDoc}
 * Expects a handler to have a type-level @{@link Controller} annotation.
 */
@Override
protected boolean isHandler(Class<?> beanType) {
	return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
			AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
}
 
Example 12
Source File: RequestMappingHandlerMapping.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>Expects a handler to have either a type-level @{@link Controller}
 * annotation or a type-level @{@link RequestMapping} annotation.
 */
@Override
protected boolean isHandler(Class<?> beanType) {
	return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
			AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
}
 
Example 13
Source File: TransactionalEventListenerFactory.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public boolean supportsMethod(Method method) {
	return AnnotatedElementUtils.hasAnnotation(method, TransactionalEventListener.class);
}
 
Example 14
Source File: RequestMappingHandlerMapping.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>Expects a handler to have either a type-level @{@link Controller}
 * annotation or a type-level @{@link RequestMapping} annotation.
 */
@Override
protected boolean isHandler(Class<?> beanType) {
	return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
			AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
}
 
Example 15
Source File: RequestResponseBodyMethodProcessor.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public boolean supportsReturnType(MethodParameter returnType) {
	return (AnnotatedElementUtils.hasAnnotation(returnType.getContainingClass(), ResponseBody.class) ||
			returnType.hasMethodAnnotation(ResponseBody.class));
}
 
Example 16
Source File: ParameterResolutionDelegate.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Determine if the supplied {@link Parameter} can <em>potentially</em> be
 * autowired from an {@link AutowireCapableBeanFactory}.
 * <p>Returns {@code true} if the supplied parameter is annotated or
 * meta-annotated with {@link Autowired @Autowired},
 * {@link Qualifier @Qualifier}, or {@link Value @Value}.
 * <p>Note that {@link #resolveDependency} may still be able to resolve the
 * dependency for the supplied parameter even if this method returns {@code false}.
 * @param parameter the parameter whose dependency should be autowired
 * (must not be {@code null})
 * @param parameterIndex the index of the parameter in the constructor or method
 * that declares the parameter
 * @see #resolveDependency
 */
public static boolean isAutowirable(Parameter parameter, int parameterIndex) {
	Assert.notNull(parameter, "Parameter must not be null");
	AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex);
	return (AnnotatedElementUtils.hasAnnotation(annotatedParameter, Autowired.class) ||
			AnnotatedElementUtils.hasAnnotation(annotatedParameter, Qualifier.class) ||
			AnnotatedElementUtils.hasAnnotation(annotatedParameter, Value.class));
}
 
Example 17
Source File: ParameterAutowireUtils.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Determine if the supplied {@link Parameter} can potentially be
 * autowired from an {@link ApplicationContext}.
 * <p>Returns {@code true} if the supplied parameter is of type
 * {@link ApplicationContext} (or a sub-type thereof) or is annotated or
 * meta-annotated with {@link Autowired @Autowired},
 * {@link Qualifier @Qualifier}, or {@link Value @Value}.
 * @param parameter the parameter whose dependency should be autowired
 * @param parameterIndex the index of the parameter
 * @see #resolveDependency
 */
static boolean isAutowirable(Parameter parameter, int parameterIndex) {
	if (ApplicationContext.class.isAssignableFrom(parameter.getType())) {
		return true;
	}
	AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex);
	return (AnnotatedElementUtils.hasAnnotation(annotatedParameter, Autowired.class) ||
			AnnotatedElementUtils.hasAnnotation(annotatedParameter, Qualifier.class) ||
			AnnotatedElementUtils.hasAnnotation(annotatedParameter, Value.class));
}
 
Example 18
Source File: SpringExtension.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Determine if the value for the {@link Parameter} in the supplied {@link ParameterContext}
 * should be autowired from the test's {@link ApplicationContext}.
 * <p>Returns {@code true} if the parameter is declared in a {@link Constructor}
 * that is annotated with {@link Autowired @Autowired} and otherwise delegates to
 * {@link ParameterAutowireUtils#isAutowirable}.
 * <p><strong>WARNING</strong>: If the parameter is declared in a {@code Constructor}
 * that is annotated with {@code @Autowired}, Spring will assume the responsibility
 * for resolving all parameters in the constructor. Consequently, no other registered
 * {@link ParameterResolver} will be able to resolve parameters.
 * @see #resolveParameter
 * @see ParameterAutowireUtils#isAutowirable
 */
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
	Parameter parameter = parameterContext.getParameter();
	int index = parameterContext.getIndex();
	Executable executable = parameter.getDeclaringExecutable();
	return (executable instanceof Constructor &&
			AnnotatedElementUtils.hasAnnotation(executable, Autowired.class)) ||
			ParameterAutowireUtils.isAutowirable(parameter, index);
}
 
Example 19
Source File: HandlerMethod.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Return whether the parameter is declared with the given annotation type.
 * @param annotationType the annotation type to look for
 * @since 4.3
 * @see AnnotatedElementUtils#hasAnnotation
 */
public <A extends Annotation> boolean hasMethodAnnotation(Class<A> annotationType) {
	return AnnotatedElementUtils.hasAnnotation(this.method, annotationType);
}
 
Example 20
Source File: ApiVersionHandlerMapping.java    From summerframework with Apache License 2.0 2 votes vote down vote up
/**
 * 只注册Controller,不注册feign接口
 * @param beanType
 * @return
 */
@Override
protected boolean isHandler(Class<?> beanType) {
    return AnnotatedElementUtils.hasAnnotation(beanType, Controller.class);
}