Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How can I scan a classpath to find all classes of a particular type?

0
Posted

How can I scan a classpath to find all classes of a particular type?

0

The process is simpler than you might think. You will use the Javac API to analyze files. So first, you’ll want to get the classpath – for example ClassPath path = project.getLookup().lookup( ClassPathProvider.class).findClassPath(someFile, ClassPath.SOURCE); (or if you can directly access the project, get the classpath some other way). Then you’ll recursively iterate all of the children of the FileObjects that are the classpath roots. For each one with the extension .java, you will call JavaSource src = JavaSource.forFileObject(fo); to get the JavaSource for that FileObject. The JavaSource is something you can pass a task to, and it will run the compiler against it and you can write a visitor or scanner that visits every element of the compilation tree. So you will implement CancellableTask to create a task that can analyze a source file. The first thing that task will do is ask the compiler to completely resolve all of the types (this can be slow): compiler.toP

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123