android使用的版本
apply plugin: 'com.android.application'
//apply plugin: 'kotlin-android'
apply plugin: 'com.jakewharton.butterknife'
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
// compileSdkVersion 28
// buildToolsVersion "29.0.2"
/**
* 读取类路径下的所有类
*
* @param packageName 包名
* @param Context 上下文传MainActive
* @return List
*/
private void reader(String packageName, Context context) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
String packageCodePath =context.getPackageCodePath();
Set dexFiles = applicationDexFile(packageCodePath);
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
for (DexFile dexFile : dexFiles) {
if (dexFile == null) continue;
Enumeration entries = dexFile.entries();
while (entries.hasMoreElements()) {
String currentClassPath = entries.nextElement();
if (currentClassPath == null || currentClassPath.isEmpty() || currentClassPath.indexOf(packageName) != 0)
continue;
Class> clazz = Class.forName(currentClassPath, true, classLoader);
if (clazz == null) continue;
String className = clazz.getName();
//初始化controller
if (clazz.isAnnotationPresent(Controller.class)) {
Controller controller = clazz.getAnnotation(Controller.class);
String beanName = controller.value();
if ("".equals(beanName)) {
beanName = clazz.getName();
}
if (mapping.containsKey(beanName)) {
throw new RuntimeException("bean重名请检查:" + className);
}
mapping.put(beanName, clazz.newInstance());
String baseUrl = "";
//获取controller的前缀
if (clazz.isAnnotationPresent(RequestMapping.class)) {
RequestMapping requestMapping = clazz.getAnnotation(RequestMapping.class);
baseUrl = requestMapping.value();
}
//获取处理方法
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (!method.isAnnotationPresent(RequestMapping.class)) {
continue;
}
RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
String methodPath = requestMapping.value();
String url = String.format("%s/%s", baseUrl, methodPath).replaceAll("/+", "/");
if (mapping.containsKey(url)) {
throw new RuntimeException("url重名请检查:" + className + "url:" + url);
}
mapping.put(url, method);
}
} else if (clazz.isAnnotationPresent(Service.class)) {
Service service = clazz.getAnnotation(Service.class);
String beanName = service.value();
if ("".equals(beanName)) {
beanName = clazz.getName();
}
Object o = clazz.newInstance();
if (mapping.containsKey(beanName)) {
throw new RuntimeException("bean重名请检查:" + className);
}
mapping.put(beanName, o);
for (Class> aClass : clazz.getInterfaces()) {
mapping.put(aClass.getName(), o);
}
}
}
}
}