java自定义注解

本文介绍了一种使用Java注解来记录方法元数据的方法,并提供了一个实用工具类用于解析这些注解信息。通过@MethodInfo注解,可以在不修改业务代码的情况下为方法添加作者、日期、版本及备注等信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注解MethodInfo

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
@Documented
@Target(ElementType.METHOD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodInfo {
String author() default "";
    String date();
    int revision() default 1;
    String comments();
}
//工具类

import java.lang.reflect.Method;
 
import org.apache.commons.lang.StringUtils;
 
import com.huawei.common.cus.MethodInfo;
 
public class ArdoCusUtil {
public static void main(String[] args) {
//methodTools("com.ardo.dao");
    }
public static String methodTools(String classpath) {
String author = "", comments = "", date = "", result = "";
StringBuffer sb = new StringBuffer();
try {
int lastone = classpath.lastIndexOf(".");
String meClass = classpath.substring(0, lastone);
String meName = classpath.substring((lastone+1),classpath.length());
//System.out.println("meClass="+meClass+" meName="+meName);
            for (Method method : ArdoCusUtil.class
                    .getClassLoader()
                    .loadClass((meClass))	//类包路径
                    .getMethods()) {
            if(meName.equals(method.getName())) {
            if (method.isAnnotationPresent(MethodInfo.class)) {
                        try {
//                            for (Annotation anno : method.getDeclaredAnnotations()) {
//                                System.out.println("Annotation in Method '"  + method + "' : " + anno);
//                            }
                            MethodInfo methodAnno = method.getAnnotation(MethodInfo.class);
                            author = methodAnno.author();
                            comments = methodAnno.comments();
                            date = methodAnno.date();
                           
                            sb.append("[");
                            if(StringUtils.isNotEmpty(author)) {
                            sb.append(" "+author+" ");
                            }
                            if(StringUtils.isNotEmpty(comments)) {
                            sb.append(" "+comments+" ");
                            }
                            if(StringUtils.isNotEmpty(date)) {
                            sb.append(" "+date+" ");
                            }
                            sb.append("]");
                            result = sb.toString();
    
                        } catch (Throwable ex) {
                            ex.printStackTrace();
                        }
                    }
            }
               
            }
        } catch (SecurityException | ClassNotFoundException e) {
            e.printStackTrace();
        }
return result;
}
}
Dao:

@MethodInfo(author = "duzhw", comments = "新增用户角色方法", date = "2017-01-11", revision = 1)
public int saveUserRole (@Param("userId")String userId, @Param("roleId")int roleId);




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值