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

The following examples show how to use org.springframework.core.annotation.AnnotatedElementUtils#getMergedAnnotation() . 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: ApiClientMethod.java    From onetwo with Apache License 2.0 6 votes vote down vote up
/****
 * 先从方法上查找注解,没有则从类上查找
 * @author weishao zeng
 * @param annoClass
 * @return

public <T extends Annotation> T findAnnotation(Class<T> annoClass) {
	T annoInst = AnnotatedElementUtils.getMergedAnnotation(getMethod(), annoClass);
	if (annoInst==null) {
		annoInst = AnnotatedElementUtils.getMergedAnnotation(getDeclaringClass(), annoClass);
	}
	return annoInst;
} */

private void initHandlers() {
	// 需要加@Inherited才能继承
	ResponseHandler resHandler = AnnotatedElementUtils.getMergedAnnotation(getMethod(), ResponseHandler.class);
	if (resHandler==null){
		resHandler = AnnotatedElementUtils.getMergedAnnotation(getDeclaringClass(), ResponseHandler.class);
	}
	if (resHandler!=null){
		Class<? extends CustomResponseHandler<?>> handlerClass = resHandler.value();
		if (handlerClass!=NullHandler.class) {
			CustomResponseHandler<?> customHandler = createAndInitComponent(resHandler.value());
			this.customResponseHandler = customHandler;
		}
		
		Class<? extends ApiErrorHandler> errorHandlerClass = resHandler.errorHandler();
		if (errorHandlerClass==DefaultErrorHandler.class) {
			this.apiErrorHandler = obtainDefaultApiErrorHandler();
		} else {
			this.apiErrorHandler = createAndInitComponent(errorHandlerClass);
		}
	} else {
		this.apiErrorHandler = obtainDefaultApiErrorHandler();
	}
}
 
Example 2
Source File: LineMessageHandlerSupport.java    From line-bot-sdk-java with Apache License 2.0 6 votes vote down vote up
private HandlerMethod getMethodHandlerMethodFunction(Object consumer, Method method) {
    final EventMapping mapping = AnnotatedElementUtils.getMergedAnnotation(method, EventMapping.class);
    if (mapping == null) {
        return null;
    }

    Preconditions.checkState(method.getParameterCount() == 1,
                             "Number of parameter should be 1. But {}",
                             (Object[]) method.getParameterTypes());
    // TODO: Support more than 1 argument. Like MVC's argument resolver?

    final Type type = method.getGenericParameterTypes()[0];

    final Predicate<Event> predicate = new EventPredicate(type);
    return new HandlerMethod(predicate, consumer, method, getPriority(mapping, type));
}
 
Example 3
Source File: TypeDescriptor.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Obtain the annotation of the specified {@code annotationType} that is on this type descriptor.
 * <p>As of Spring Framework 4.2, this method supports arbitrary levels of meta-annotations.
 * @param annotationType the annotation type
 * @return the annotation, or {@code null} if no such annotation exists on this type descriptor
 */
@Nullable
public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
	if (this.annotatedElement.isEmpty()) {
		// Shortcut: AnnotatedElementUtils would have to expect AnnotatedElement.getAnnotations()
		// to return a copy of the array, whereas we can do it more efficiently here.
		return null;
	}
	return AnnotatedElementUtils.getMergedAnnotation(this.annotatedElement, annotationType);
}
 
Example 4
Source File: TypeDescriptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtain the annotation of the specified {@code annotationType} that is on this type descriptor.
 * <p>As of Spring Framework 4.2, this method supports arbitrary levels of meta-annotations.
 * @param annotationType the annotation type
 * @return the annotation, or {@code null} if no such annotation exists on this type descriptor
 */
@SuppressWarnings("unchecked")
public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
	if (this.annotatedElement.isEmpty()) {
		// Shortcut: AnnotatedElementUtils would have to expect AnnotatedElement.getAnnotations()
		// to return a copy of the array, whereas we can do it more efficiently here.
		return null;
	}
	return AnnotatedElementUtils.getMergedAnnotation(this.annotatedElement, annotationType);
}
 
Example 5
Source File: SpringCacheAnnotationParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Provides the {@link DefaultCacheConfig} instance for the specified {@link Class}.
 * @param target the class-level to handle
 * @return the default config (never {@code null})
 */
DefaultCacheConfig getDefaultCacheConfig(Class<?> target) {
	CacheConfig annotation = AnnotatedElementUtils.getMergedAnnotation(target, CacheConfig.class);
	if (annotation != null) {
		return new DefaultCacheConfig(annotation.cacheNames(), annotation.keyGenerator(),
				annotation.cacheManager(), annotation.cacheResolver());
	}
	return new DefaultCacheConfig();
}
 
Example 6
Source File: SupportedMediaTypeExtractor.java    From onetwo with Apache License 2.0 5 votes vote down vote up
@Override
public List<MediaType> load(Type type) throws Exception {
	if(!Class.class.isInstance(type)){
		return Collections.emptyList();
	}
	Class<?> clazz = (Class<?>) type;
	List<MediaType> mediaTypes = Collections.emptyList();
	SupportedMediaType mediaTypeAnnotation = AnnotatedElementUtils.getMergedAnnotation(clazz, SupportedMediaType.class);
	if(mediaTypeAnnotation!=null){
		String[] mediaTypeStrs = mediaTypeAnnotation.value();
		mediaTypeStrs = mediaTypeStrs==null?LangUtils.EMPTY_STRING_ARRAY:mediaTypeStrs;
		mediaTypes = MediaType.parseMediaTypes(Arrays.asList(mediaTypeStrs));
	}
	return mediaTypes;
}
 
Example 7
Source File: RequestMappingResolver.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
public static RequestMappingResolveResult resolveOwnPath(Method method) {
    RequestMethod requestMethod =null;
    String path =null;
    
    ApiRequestMappingAutoWithMethodName apiRequestMappingAutoWithMethodName = AnnotationUtil.getAnnotation(method,  ApiRequestMappingAutoWithMethodName.class);
    
    RequestMapping requestMapping = AnnotatedElementUtils.getMergedAnnotation(method, RequestMapping.class);
    String requestMappingValue = getRequestMappingValue(requestMapping);
    
    //判断requestMapping是否在method上
    boolean requestMappingOnMethod =method.isAnnotationPresent(RequestMapping.class);
    
    if (!requestMappingOnMethod && apiRequestMappingAutoWithMethodName != null && StringUtil.isBlank(requestMappingValue)) {
        String methodName = method.getName();
        path =StringUtil.startWithSlash(methodName);
        requestMethod = apiRequestMappingAutoWithMethodName.method();
    } else if (StringUtil.isNotBlank(requestMappingValue)) {
        RequestMethod[] requestMethods = requestMapping.method();
        requestMethod =  CollectionUtil.getFirst(requestMethods);
        path =requestMappingValue;
    }
    
    if (requestMethod == null && requestMapping!=null) {
        RequestMethod[] netMethods = requestMapping.method();
        requestMethod =  CollectionUtil.getFirst(netMethods);
    }
    
    return new RequestMappingResolveResult(path, requestMethod);
}
 
Example 8
Source File: AbstractMethodResolver.java    From onetwo with Apache License 2.0 5 votes vote down vote up
/****
 * 先从方法上查找注解,没有则从类上查找
 * @author weishao zeng
 * @param annoClass
 * @return
 */
public <A extends Annotation> A findAnnotation(Class<A> annoClass) {
	A annoInst = AnnotatedElementUtils.getMergedAnnotation(getMethod(), annoClass);
	if (annoInst==null) {
		annoInst = AnnotatedElementUtils.getMergedAnnotation(getDeclaringClass(), annoClass);
	}
	return annoInst;
}
 
Example 9
Source File: AnnotationUtil.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Internal get annoation.
 *
 * @param <A> the generic type
 * @param annotatedElement the annotated element
 * @param annotationType the annotation type
 * @return the a
 */
private static <A extends Annotation> A internalGetAnnoation(AnnotatedElement annotatedElement, Class<A> annotationType) {
    //用spring中的方法找annotation
    A annotation = AnnotatedElementUtils.getMergedAnnotation(annotatedElement, annotationType);
    if (annotation != null) {
        return annotation;
    }

    if (annotatedElement instanceof Method) {
        Method method = (Method) annotatedElement;
        String methodName = method.getName();
        Class<?> returnType = method.getReturnType();
        Class<?>[] parameterTypes = method.getParameterTypes();

        Class<?> superclass = method.getDeclaringClass().getSuperclass();
        if (superclass == null || superclass.isAssignableFrom(Object.class)) {
            return null;
        }
        //从父方法中找annotation
        Method[] methods = superclass.getMethods();
        for (Method mtd : methods) {
            if (!ReflectionUtil.methodIsInherited(methodName, returnType, parameterTypes, mtd)) {
                continue;
            }
            return internalGetAnnoation(mtd, annotationType);
        }
    }

    return annotation;
}
 
Example 10
Source File: SpringRequestHeaderParameterParser.java    From RestDoc with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isRequired(Parameter parameter, Type actualParamType) {
    var requestHeaderAnno = AnnotatedElementUtils.getMergedAnnotation(parameter, RequestHeader.class);
    if (requestHeaderAnno != null)
    {
        return requestHeaderAnno.required();
    }
    return true;
}
 
Example 11
Source File: JobBeanIntrospector.java    From haven-platform with Apache License 2.0 5 votes vote down vote up
private static boolean processDeps(List<Dependency> deps, AnnotatedElement ao) {
    if(!(AnnotatedElementUtils.isAnnotated(ao, Autowired.class))) {
        return false;
    }
    Qualifier qualifier = AnnotatedElementUtils.getMergedAnnotation(ao, Qualifier.class);
    String name = qualifier == null ? null : qualifier.value();
    Class<?> type = (ao instanceof Field)? ((Field)ao).getType() : ((Method)ao).getReturnType();
    deps.add(new Dependency(name, type));
    return true;
}
 
Example 12
Source File: SpringResponseBodyReturnParser.java    From RestDoc with Apache License 2.0 5 votes vote down vote up
@Override
protected int parseStatusCode(Method method) {
    ResponseStatus responseStatusAnno = AnnotatedElementUtils.getMergedAnnotation(method, ResponseStatus.class);
    if (responseStatusAnno != null)
    {
        responseStatusAnno = AnnotatedElementUtils.getMergedAnnotation(method.getDeclaringClass(), ResponseStatus.class);
    }
    if (responseStatusAnno != null)
    {
        return responseStatusAnno.code() != null ? responseStatusAnno.code().value() : responseStatusAnno.value().value();
    }
    return 200;
}
 
Example 13
Source File: SpringPathVariableParameterParser.java    From RestDoc with Apache License 2.0 5 votes vote down vote up
@Override
protected String getParameterName(Parameter parameter) {
    var paramName = super.getParameterName(parameter);
    var requestParamAnno = AnnotatedElementUtils.getMergedAnnotation(parameter, PathVariable.class);
    if (requestParamAnno != null && !StringUtils.isEmpty(requestParamAnno.name()))
    {
        return requestParamAnno.name();
    }
    return paramName;
}
 
Example 14
Source File: SpringRequestBodyParameterParser.java    From RestDoc with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isRequired(Parameter parameter, Type actualParamType) {
    var requestBodyAnno = AnnotatedElementUtils.getMergedAnnotation(parameter, RequestBody.class);
    if (requestBodyAnno != null)
    {
        return requestBodyAnno.required();
    }
    return true;
}
 
Example 15
Source File: SpringMultipartParameterParser.java    From RestDoc with Apache License 2.0 5 votes vote down vote up
@Override
protected String getParameterName(Parameter parameter) {
    var paramName = super.getParameterName(parameter);

    var requestParamAnno = AnnotatedElementUtils.getMergedAnnotation(parameter, RequestParam.class);
    if (requestParamAnno != null)
    {
        return requestParamAnno.name();
    }
    return paramName;
}
 
Example 16
Source File: SpringMultipartParameterParser.java    From RestDoc with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isRequired(Parameter parameter, Type actualParamType) {
    var requestParamAnno = AnnotatedElementUtils.getMergedAnnotation(parameter, RequestParam.class);
    if (requestParamAnno != null)
    {
        return requestParamAnno.required();
    }
    return true;
}
 
Example 17
Source File: SpringRequestParamParameterParser.java    From RestDoc with Apache License 2.0 5 votes vote down vote up
@Override
protected String getParameterName(Parameter parameter) {
    var paramName = super.getParameterName(parameter);

    var requestParamAnno = AnnotatedElementUtils.getMergedAnnotation(parameter, RequestParam.class);
    if (requestParamAnno != null && !StringUtils.isEmpty(requestParamAnno.name()))
    {
        return requestParamAnno.name();
    }
    return paramName;
}
 
Example 18
Source File: SpringRequestParamParameterParser.java    From RestDoc with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isRequired(Parameter parameter, Type actualParamType) {
    var requestParamAnno = AnnotatedElementUtils.getMergedAnnotation(parameter, RequestParam.class);
    if (requestParamAnno != null)
    {
        return requestParamAnno.required();
    }
    return true;
}
 
Example 19
Source File: SpringRequestHeaderParameterParser.java    From RestDoc with Apache License 2.0 5 votes vote down vote up
@Override
protected String getParameterName(Parameter parameter) {
    var paramName = super.getParameterName(parameter);

    var requestParamAnno = AnnotatedElementUtils.getMergedAnnotation(parameter, RequestHeader.class);
    if (requestParamAnno != null && !StringUtils.isEmpty(requestParamAnno.name()))
    {
        return requestParamAnno.name();
    }
    return paramName;
}
 
Example 20
Source File: MethodWrap.java    From stategen with GNU Affero General Public License v3.0 4 votes vote down vote up
private void genState() {
    //todo 如查泛型没定义,应该报一个异常
    Class<?> returnClz = returnWrap.getIsGeneric() ? returnWrap.getGeneric().getClazz() : returnWrap.getClazz();
    boolean stateFieldAdded = false;

    DataOpt dataOpt = DataOpt.APPEND_OR_UPDATE;
    Set<String> areaExtraProps = null;
    Set<String> stateExtraProps = null;
    Boolean init = false;
    Boolean isSetted = false;
    Boolean genEffect = true;
    Boolean genReducer = true;
    Boolean initCheck = true;
    BaseWrap areaTemp = null;
    Boolean genRefresh = false;

    StateWrap stateWrap = new StateWrap();
    this.setState(stateWrap);
    Set<AreaExtraProp> areaExtraPropAnnos = AnnotatedElementUtils.getMergedRepeatableAnnotations(methodFun, AreaExtraProp.class);
    areaExtraProps = CollectionUtil.toSet(areaExtraPropAnnos, AreaExtraProp::value);

    Set<StateExtraProp> stateExtraPropAnnos = AnnotatedElementUtils.getMergedRepeatableAnnotations(methodFun, StateExtraProp.class);
    stateExtraProps = CollectionUtil.toSet(stateExtraPropAnnos, StateExtraProp::value);

    genEffect = AnnotationUtil.getAnnotationValueFormMembers(GenEffect.class, GenEffect::value, false, methodFun);
    genReducer = AnnotationUtil.getAnnotationValueFormMembers(GenReducer.class, GenReducer::value, false, methodFun);
    genRefresh = AnnotationUtil.getAnnotationValueFormMembers(GenRefresh.class, GenRefresh::value, false, methodFun);

    State stateAnno = AnnotatedElementUtils.getMergedAnnotation(methodFun, State.class);
    if (stateAnno != null) {
        init = stateAnno.init();
        dataOpt = stateAnno.dataOpt();
        isSetted = true;
        initCheck = stateAnno.initCheck();
        Class<?> stateAreaClass = stateAnno.area();
        if (stateAreaClass != Object.class) {
            if (stateAreaClass != returnClz) {
                areaTemp = GenContext.wrapContainer.add(stateAreaClass, false);
                stateFieldAdded = true;
            }
        }
    }

    if (stateAnno!=null && !stateFieldAdded && area == null) {
        if (returnClz != Object.class && GenContext.wrapContainer.checkIsOrgSimpleOrEnum(returnClz) && ((ApiWrap) apiWrap).getGenModel()) {
            String errMessage = "未指定area,如 @State(area=User.class),只有返回值是非基本类型、SimpleResponse、Object可以不指定area"
                                + ReflectionUtil.getJavaConsoleLink(methodFun);
            AssertUtil.throwException(errMessage);
        }
        areaTemp = GenContext.wrapContainer.add(returnClz, false);
    }

    if (areaTemp != null && !SimpleResponse.class.isAssignableFrom(areaTemp.getClazz())) {
        apiWrap.addArea(areaTemp);
        this.area = areaTemp;
    }

    if (areaTemp == null) {

    }

    stateWrap.setInit(init);
    stateWrap.setAreaExtraProps(areaExtraProps);
    stateWrap.setStateExtraProps(stateExtraProps);
    stateWrap.setDataOpt(dataOpt);
    stateWrap.setIsSetted(isSetted);
    stateWrap.setGenEffect(genEffect);
    stateWrap.setInitCheck(initCheck);
    stateWrap.setGenRefresh(genRefresh);
    stateWrap.setGenReducer(genReducer);
}