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>?

3
  • 2
    The classes do not (jet) exist (you are in the process of compiling them). Take a look into Types. Commented Jul 28, 2019 at 7:11
  • But how lombok works? Commented Jul 28, 2019 at 7:32
  • 1
    Lombok uses the annotation processor only "as a door in" and hijacks the AST. See this question for details. Commented Jul 28, 2019 at 7:33

2 Answers 2

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()

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

This will not work. The class does not jet exist since you are in the process of compiling.
I wrote in question that Class.forName() throws exception

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.