注解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);