All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.livk.commons.util.AnnotationUtils Maven / Gradle / Ivy

There is a newer version: 1.3.3
Show newest version
/*
 * Copyright 2021-2024 spring-boot-extension the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.livk.commons.util;

import lombok.experimental.UtilityClass;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Method;

/**
 * 

* Annotation工具类 *

* * @author livk */ @UtilityClass public class AnnotationUtils extends org.springframework.core.annotation.AnnotationUtils { /** * 获取方法上或者类路径上的注解,方法级别优先,类路径允许复合注解 * @param annotation泛型 * @param methodParameter parameter * @param annotationClass annotation * @return annotation annotation element */ public A getAnnotationElement(MethodParameter methodParameter, Class annotationClass) { A annotation = getAnnotation(methodParameter.getAnnotatedElement(), annotationClass); if (annotation == null) { Class containingClass = methodParameter.getContainingClass(); annotation = getAnnotation(containingClass, annotationClass); } return annotation; } /** * 获取方法上或者类路径上的注解,方法级别优先,类路径允许复合注解 * @param annotation泛型 * @param method method * @param annotationClass annotation * @return annotation annotation element */ public A getAnnotationElement(Method method, Class annotationClass) { A annotation = getAnnotation(method, annotationClass); if (annotation == null) { annotation = getAnnotation(method.getDeclaringClass(), annotationClass); } return annotation; } /** * 判断方法上或者类路径上是否包含注解,类路径允许复合注解 * @param annotation泛型 * @param methodParameter parameter * @param annotationClass annotation * @return bool boolean */ public boolean hasAnnotationElement(MethodParameter methodParameter, Class annotationClass) { Class containingClass = methodParameter.getContainingClass(); return (AnnotatedElementUtils.hasAnnotation(containingClass, annotationClass) || AnnotatedElementUtils.hasAnnotation(methodParameter.getAnnotatedElement(), annotationClass)); } /** * 判断方法上或者类路径上是否包含注解,类路径允许复合注解 * @param annotation泛型 * @param method method * @param annotationClass annotation * @return bool boolean */ public boolean hasAnnotationElement(Method method, Class annotationClass) { return AnnotatedElementUtils.hasAnnotation(method, annotationClass) || AnnotatedElementUtils.hasAnnotation(method.getDeclaringClass(), annotationClass); } /** * 构建AnnotationAttributes * @param metadata the metadata * @param annotationClassName the annotation class name * @return the annotation attributes */ public AnnotationAttributes attributesFor(AnnotatedTypeMetadata metadata, String annotationClassName) { return AnnotationAttributes.fromMap(metadata.getAnnotationAttributes(annotationClassName)); } /** * 构建AnnotationAttributes * @param 注解类型 * @param metadata the metadata * @param annotationClass the annotation class * @return the annotation attributes */ public AnnotationAttributes attributesFor(AnnotatedTypeMetadata metadata, Class annotationClass) { return attributesFor(metadata, annotationClass.getName()); } /** * 根据key获取AnnotationAttributes数据,并转成枚举数组 * @param attributes AnnotationAttributes * @param key key * @param 枚举类型 * @return E[] */ @SuppressWarnings("unchecked") public > E[] getValue(AnnotationAttributes attributes, String key) { Object value = attributes.get(key); if (!(value instanceof Enum[]) && Enum[].class.getComponentType().isInstance(value)) { Object array = Array.newInstance(Enum[].class.getComponentType(), 1); Array.set(array, 0, value); value = array; } return (E[]) value; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy