1

I wrote a custom Java Annotation Processor that if applied to a class, generates a class with name of annotated class fields.

While executing, I can find name of annotated class (like "com.package.Person") with (DeclaredType)annotatedElement.asType() but Unable to get Class<com.package.Person> to find its fields.

I tried to load class with its full name with Class.forName(com.package.Person) that throws an exception ClassNotFoundException: com.package.Person

How can I get Class<Person>?

Homayoun Behzadian
  • 1,053
  • 9
  • 26
  • 2
    The classes do not (jet) exist (you are in the process of compiling them). Take a look into [`Types`](https://docs.oracle.com/en/java/javase/11/docs/api/java.compiler/javax/lang/model/util/Types.html). – Turing85 Jul 28 '19 at 07:11
  • But how lombok works? – Homayoun Behzadian Jul 28 '19 at 07:32
  • 1
    Lombok uses the annotation processor only "as a door in" and hijacks the AST. See [this question](https://stackoverflow.com/questions/6107197/how-does-lombok-work) for details. – Turing85 Jul 28 '19 at 07:33

2 Answers2

1

Finally, I find I can't get Class<?> but I can get fields by var elements = element.getEnclosedElements() that their kind is FIELD

Also getting superclass is possible by ((TypeElement) rootElement).getSuperclass()

Homayoun Behzadian
  • 1,053
  • 9
  • 26
0

May be Name is Wrong?

here's code. And it Work.

Class<?> testClass = Class.forName("PackageName.ClassName");
Object instance = testClass.newInstance();

com.package.Person -> package.Person. if not work, I'm worng. Sorry.

Super Shark
  • 376
  • 3
  • 12