Hello.
I want to do a Gradle task on Java that will look for classes marked an annotation in my Java application and process those classes. This my task method:
@TaskAction
public void greet() throws {
Reflections reflections = new Reflections(“ru.mycomp”);
for (Class<?> clazz : reflections.getTypesAnnotatedWith(MisEntity2.class)) {
System.out.println(clazz.toString());
}
}
But the code find the clasess in Gradle project, not in my application.
Any ideas…
Thanks.
It don’t work. You advised “The classpath for tasks was defined by the buildscript { classpath { … } }.”. I have in this section reference on gradle plugin. Perhaps this is problem?
You’ll obviously need reflections on the buildscript.depencies.classpath (I’m not sure if this task is from buildSrc or a plugin). You won’t need the scanned classes on the buildscript classpath because we are using UrlClassloader to avoid a chicken-or-egg scenario.
You might want to use the following so reflections doesn’t scan the buildscript classloader
new UrlClassloader(urls, null)
You can check that the classloader is configured properly by attempting:
classLoader.loadClass("foo.bar.MyClass")
If that’s able to load one of the classes you are scanning for, I think you’re best to head over to the reflections forum as this is no longer a gradle question
Hi I am new to gradle.
I intend to use reflection as part of the gradle task to read all the classes in one of the packages in my multi-module repo. Need some help as I end up getting the following error:
Execution failed for task ‘:hello’.
No signature of method: java.io.File.toUri() is applicable for argument types: () values:
Possible solutions: toURI(), toURL(), mkdir(), toString(), toPath(), toString()
######### Task Snippet
class AutogenTask extends DefaultTask { @TaskAction
def greet() {
URL[] urls;
List<URL> listOfURL = new ArrayList<>();
SourceSetContainer ssc = getProject().getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
File classesDir = ssc.getByName("dir_name").getOutput().getClassesDir();
listOfURL.add(classesDir.toUri().toUrl());
urls = listOfURL.toArray(new URL[0]);
ClassLoader classLoader = new java.net.URLClassLoader(urls,null);
Reflections reflections = new Reflections("com.autogen.sample.common", classLoader);
Set<Class<? extends Object>> allClasses =
reflections.getSubTypesOf(Object.class)
println "I am here2"
for(Class c:allClasses) {
println "I am here3"
println "Derived Class : " + c.getName()
}
This is not throwing errors but it is also not getting the class names as it is intended here…
All the .java classes are under : com.autogenerate.data.common
Code Snippet
class AutogenTask extends DefaultTask { @TaskAction
def greet() {
List listOfURL = new ArrayList<>();
File classesDir = project.file(“auto/src/generatedData/java/com/autogenerate/data/common”)
listOfURL.add(classesDir.toURI().toURL());
URL[] urls = listOfURL.toArray(new URL[0]);
ClassLoader classLoader = new URLClassLoader(urls);
Reflections reflections = new Reflections("com.autogenerate.data.common", classLoader);
Set<Class<? extends Object>> allClasses =
reflections.getSubTypesOf(Object.class)
println "I am here2"
for(Class c:allClasses) {
println "I am here3"
println "Derived Class : " + c.getName()
}
}
}
Even if I go the route of SourceSetGenerator, I am still not able to grab the classes, where the package is in : auto ->java->com.autogenerate.data.common
class AutogenTask extends DefaultTask { @TaskAction
def greet() {
SourceSetContainer ssc = getProject().findProject(“urns”).getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
File dir = ssc.getByName(“auto”).getOutput().getClassesDir();
List listOfURL = new ArrayList<>();
listOfURL.add(dir.toURI().toURL());
URL[] urls = listOfURL.toArray(new URL[0]);
ClassLoader classLoader = new URLClassLoader(urls);
Reflections reflections = new Reflections("com.autogenerate.data.common", classLoader);
Set<Class<? extends Object>> allClasses =
reflections.getSubTypesOf(Object.class)
for(Class c:allClasses) {
println "Class Name : " + c.getName()
}
I don’t find unit testing Gradle plugins very helpful since it’s easy to get a false positive. So I usually only provide integration tests with TestKit. Since gradle scripts are groovy, you can put assert statements in your test gradle scripts which I find convenient.