标题Servlet实现傻瓜式mvc(三)
获取特定包内注解方法的注解值
import com.barcode.lrms.scanner.Annotation.BIData;
import java.lang.reflect.Method;
import java.util.List;
/**
* <b>功能:</b>获取特定包内注解方法的注解值
*
* @author Rock_yan
* @date 2020-12-10 17:44
* @Version 1.0
*/
public class AnnotationUtil {
public static Object validAnnotation(List<Class> clsList,String uri,String json) {
Object ret = new Object();
if (clsList != null && clsList.size() > 0) {
for (Class cls : clsList) {
//获取类中的所有的方法
Method[] methods = cls.getDeclaredMethods();
if (methods != null && methods.length > 0) {
for (Method method : methods) {
BIData peopleAnnotion = (BIData) method.getAnnotation( BIData.class );
if (peopleAnnotion != null && uri.equals( peopleAnnotion.ServletURL() )){
try {
Object obj = cls.newInstance();
// 获取参数类型..后续补充调用有参函数。
//TODO
Class[] paramTypes = method.getParameterTypes();
/*for (Class class2 : paramTypes) {
System.out.print(class2.getName() + ",");
}*/
/**
* 添加一段代码,暂时处理一下post请求。
*/
if((!json.equals( "" )&& !json.equals( "{}" )) || paramTypes.length>0){
ret = method.invoke(obj,json);
}else{
ret = method.invoke(obj);
}
}catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch(Exception exp) {
System.out.println("运行validAnnotation()方法异常!"+ exp);
exp.printStackTrace();
}
//可以做权限验证
// System.out.println( "请求:"+uri);
}
}
}
}
}
return ret;
}
}