android 提取方法,Android内获取class方法

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

}

}

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值