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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:38:51+00:00 2026-06-01T08:38:51+00:00

Suppose I have this annotation @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Name { String value(); }

  • 0

Suppose I have this annotation

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface  Name 
{
    String value();
}

This is going to be used as follows

@Name("name1")
public static Foo foo = new Foo();

I have multiples of these across my project source files. Is there an fairly simple way to search and collect all those “foo”s that’re preceded by @Name?
In other words, I’d like to write a method that would return a Set<Foo> containing these.

Thanks!!!

  • 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-01T08:38:53+00:00Added an answer on June 1, 2026 at 8:38 am

    I am not really familiar with the classpath scanners that others are suggesting. They seem like a robust – if not ideal – solution.

    If you have control over the source, then you could use annotation processing.

    Create an annotation processor that will create a class – MapClass with a static member Map<String,Foo>. Every time the annotation processor encounters the @Name annotation, it adds that to the source code of MapClass. When it finishes processing the annotations, it will have the same effect as if you hard coded the map.

    Annotation processing happens during compile time. If some of the classes in your project are not compiled by you. For example, if someone else compiles some classes and gives a jar to you, then it won’t work as easily. But if all the classes are compiled by you then it should not be a problem.

    To create an annotation processor, extend AbstractProcessor. You will want to annotate your class with a @ SupportedAnnotationTypes ( "Name" ) annotation (make sure name is the fully qualified name of your annotation.

    Override the process method. process has two parameters: annotations and roundEnv. annotations is just the set of annotations that this particular processor supports – in your case it should be (Name). roundEnv is a useful utility class.

    Iterate through the one annotation in annotations. Use roundEnv to getElementsAnnotatedWith. This should give you the set of all elements that carry the @Name annotation.

    AbstractProcessor has another utility member – processingEnv. Use its getFiler method to createSourceFile.

    Then you have to modify your compilation a little bit. You must compile your processor separately and before the other classes. After the processor is compiled and you are compiling the other classes you must tell the compiler about your processor. If you are using the command line you would add -processorpath /path/to/processor/class[es] and -processor qualified.name.of.processor.

    The advantages of this approach over the class path scanner is that everything happens at compile time. So for example, if you accidentally add a @Name annotation to a Bar element, then you can have the processor throw a compile time error (if you wish the processor can ignore it). Then you can fix it before the product ships. With a class path scanner, any error thrown is a run time error – which the user will see.

    The disadvantage of this approach is also that everything happens at compile time. This makes it harder to dynamically add classes to the project.

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

Sidebar

Related Questions

Suppose I have this annotation class @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MethodXY { public int
Suppose I have this interface public interface IFoo { ///<summary> /// Foo method ///</summary>
Suppose I have a function public int doSomething(@QueryParam(id) String name, int x){ .... }
Suppose I have this: @Transactional(rollbackFor = NotificationException.class) public interface PersonManagerService { public void addPerson(Person
Suppose I have this Windows wchar_t string: L\x4f60\x597d and L\x00e4\x00a0\x597d and would like to
Suppose I have this code: String encoding = UTF-16; String text = [Hello StackOverflow];
Suppose you have a generic interface and an implementation: public interface MyInterface<T> { void
Suppose I have this structures in c++ : class A{ public: B b; }
Suppose I have this code... foo.h @interface Foo : NSObject { NSString *aString; //
This question relates to Django Aggregation/Annotation in 1.1. Suppose I have a simple model

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.