Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6619065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:55:08+00:00 2026-05-25T20:55:08+00:00

An annotation processor will provide you with two TypeElement , one for the annotated

  • 0

An annotation processor will provide you with two TypeElement, one for the annotated object and one the annotation itself.

Let’s assume one needs to know the Class type of the annotated object during annotation processing to generate code. One can extract the qualified name with TypeElement.getQualifiedName(). However, any attempt to use Class.forName(fullyQualifiedName) throws a ClassNotFoundException.

It probably means the class is not on the path of the annotation processing code. Is it ever possible to retrieve such a class during annotation processing without having to put all annotated code in the classpath of the processing library?

A workaround is to generate something like:

Class c = Class.forName("thefullyqualifiedname");

and use c in the generated code instead, but it is not clean.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:55:08+00:00Added an answer on May 25, 2026 at 8:55 pm

    The best workaround I could come up with is the following:

        JClass annotatedType = jcmSource.ref(Class.class);
    
        JInvocation m =  annotatedType.staticInvoke("forName");
        m.arg(fa.getAnnotated().toString());
    
        JFieldVar field = sourceClass.field(
                JMod.PRIVATE | JMod.STATIC, annotatedType, "c");
    
        JBlock staticInit = sourceClass.init();
    
        JTryBlock tb = staticInit._try();
        JBlock jbtb = tb.body();
            jbtb.assign(field, m);
        tb._catch(jcmSource.ref(ClassNotFoundException.class));
    

    which produces:

    private static Class c;
    
    static {
        try {
            c = Class.forName("net.codegentest.CodeGenTest");
        } catch (ClassNotFoundException _x) {
        }
    }
    

    It does not give explicit access to the class type of the annotated class, but at least, it can be generated in the code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an own annotation processor (let's call it MyProcessor) and a project (let's
I have a Java Annotation Processor that extends from AbstractProcessor . I have two
I have a maven project generating a new class from an annotation processor during
When obtaining a TypeElement in an annotation processor, you can ask for its super
what is annotation class. what is the use of it in java/android. In iphone
If a class defined an annotation, is it somehow possible to force its subclass
I'm trying to create an annotation for a local variable. I know that I
In my Eclipse project I'm using a third-party annotation processor, Hibernate Metamodel Generator to
I have a custom annotation and it's processor & processorFactory. How do I configure
I'm trying to move a build which generates sources using an annotation processor to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.