我正在尝试创建一个自定义Gradle插件,该插件可基于注释读取所有类。我的urlclassloader正在运行,它基于插件提供的基本包,但是当我使用getMethods()函数时,会收到ClassNotFoundException。
这是因为某些方法具有第三方依赖项中的参数。有没有办法使用使用插件的项目的所有依赖项?我想使用objectmapper反序列化它们。
我的代码:
@TaskAction
public void generateRaml() throws MalformedURLException, ClassNotFoundException {
SourceSetContainer ssc = getProject().getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
File classesDir = ssc.getByName("main").getOutput().getClassesDirs().getSingleFile();
URL[] urls = new URL[] { classesDir.toURI().toURL() };
ClassLoader classLoader = new URLClassLoader(urls, null);
ResourceLoader resourceLoader = new DefaultResourceLoader(classLoader);
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
provider.setResourceLoader(resourceLoader);
provider.addIncludeFilter(new AnnotationTypeFilter(Controller.class));
provider.addIncludeFilter(new AnnotationTypeFilter(RestController.class));
Set<BeanDefinition> components = provider.findCandidateComponents(getBasePackage());
System.out.println("Found " + components.size() + " components");
for (BeanDefinition component : components) {
System.out.println("Component " + component.getBeanClassName());
Class<?> componentClass = Class.forName(component.getBeanClassName(), false, classLoader);
System.out.println("Class: " + componentClass.getName());
Method[] methods = componentClass.getDeclaredMethods();
System.out.println("Methods: " + methods.length);
}
当我使用“ componentClass.getDeclaredMethods()”时,插件会失败