1 year ago

#353528

test-img

Ray

How do I get the directory of the 'input' source files in an annotation processor?

I want to get the directories of the source files which are getting compiled after annotation processing while doing the annotation processing without relying on directory/build tool conventions.

public class MyProcessor extends AbstractProcessor {

  @Override
  public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    // Get the directories with source files to be compiled here
    // For example, I should be able to find all classes which I obtain via the following in the directories I want:
    roundEnv.getElementsAnnotatedWith(MyAnnotation.class)
  }
}

I have tried:

  1. A relative path which I got with Paths.get("."). But not only would I still have to rely on directory/build tool conventions to get to the source directories, it also doesn't work when the build is started from anywhere else but the project root.
  2. Checking all properties in System.getProperties() and System.getenv() to see if there is anything useful being set.
  3. Checked javax.lang.model.element.TypeElement, javax.lang.model.type.TypeMirror and the corresponding utility methods to see if I can get an instance's location (Did I miss something?)
  4. Checked various combinations of StandardJavaFileManager's methods, some of which suggest to return what I want, but returning either null/empty collections or throwing exceptions for module related parameters. Example: standardJavaFileManager.list(StandardLocation.SOURCE_PATH, "", Set.of(Kind.SOURCE), true).

// edit

I'm trying to create a functionality where classes can be picked up based on super-types. For that, I created an Annotation where I can specify these super-types. I now need to scan all source files to check for classes that are sub-types of X, for which I am using a library. Said library needs to be pointed to a directory containing the classes to scan, which is why I need the source folder.

java

annotations

annotation-processing

0 Answers

Your Answer

Accepted video resources