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 8661379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:29:01+00:00 2026-06-12T16:29:01+00:00

I am trying to write an annotation processor using java. This annotation processor needs

  • 0

I am trying to write an annotation processor using java. This annotation processor needs to identify annotated nested classes within an annotated class as seen below. I will process the annotated classes first then process their inner annotations. This is performed at compile time and I will have no existing knowledge of the class being processed. It is possible to have multiple nested classes within Foo. How do I process the annotations of all these nested classes.

@MyAnnotation(value="Something important")
public class Foo
{
    private Integer A;

    @MyMethodAnnotation(value="Something Else")
    public Integer getA() { return this.A; }

    @MyAnnotation(value="Something really important")
    private class Bar
    {
        private Integer B;

        @MyMethodAnnotation(value="Something Else that is very Important")
        public Integer getB() { return this.B }     
    }
}

How can I get access to the nested Bar class, it’s annotation ‘MyAnnotation’ and its ‘MyMethodAnnotation’ during processing? The following code only prints out information about class Foo. How can I process information about Bar?

for (Element element : env.getElementsAnnotatedWith(MyAnnotation.class)) {
    if ( element.getKind().equals(ElementKind.CLASS) )
    {
        System.out.println(element.getKind().name() + " " + element.getSimpleName() );
        processInnerClassElement(element);
    }
    else
    {
        System.out.println(element.getKind().name() + " " + element.getSimpleName() );
    }    
}

...


private void processInnerClassElement(Element element)
{
    for (Element e : element.getEnclosedElements() )
    {
        if ( e.getKind().equals(ElementKind.CLASS) )
        {
            System.out.println(e.getKind().name() + " " + e.getSimpleName() );
            processInnerClassElement(e);
        }
        else
        {
            System.out.println(e.getKind().name() + " " + e.getSimpleName()  );
        }
    }
}
  • 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-06-12T16:29:03+00:00Added an answer on June 12, 2026 at 4:29 pm

    I guess it depends how these annotation relate to each other.

    You could simply declare all the annotations in @SupportedAnnotationTypes and have several blocks in the process-method like:

    for (Element element : roundEnv.getElementsAnnotatedWith(MyAnnotation.class)) {
        MyAnnotation myAnnotation = element.getAnnotation(MyAnnotation.class);
        if (myAnnotation != null) {
            doSomething(myAnnotation, element);
        }
    }
    
    for (Element element : roundEnv.getElementsAnnotatedWith(MyMethodAnnotation.class)) {
        MyMethodAnnotation myMethodAnnotation = element.getAnnotation(MyMethodAnnotation.class);
        if (myMethodAnnotation != null) {
            doSomething(myMethodAnnotation, element);
        }
    }
    

    Otherwise you might be able to use element.getEnclosedElements() and element.getEnclosingElement() to achieve what you want.

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

Sidebar

Related Questions

I'm currently trying to write an apt annotation processor. I can however not find
Trying to write app for service technicians that will display open service calls within
I'm trying to write a web service for the java.util.logging api. So I wrote
I'm trying to write a Java program such that after calling a methodA() ,
I am trying ot write an integration test for one of my controller classes
I'm trying to write this method in my controller: @ResponseBody @RequestMapping(value = {/getTeams}, method
We've been trying write unit tests for a worker class written in C#, which
I'm trying write a java program to send live microphone data over UDP, then
I am trying to write a RESTFul Webservice using Jersey. package sample.hello.resources; @Path(/hello) public
I am trying to unmarshall data from an XML object in Java using SimpleXML.

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.