Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am not really familiar with the classpath scanners that others are suggesting. They seem like a robust - if not ideal - solution.</p> <p>If you have control over the source, then you could use annotation processing.</p> <p>Create an annotation processor that will create a class - <code>MapClass</code> with a static member <code>Map&lt;String,Foo&gt;</code>. Every time the annotation processor encounters the @Name annotation, it adds that to the source code of <code>MapClass</code>. When it finishes processing the annotations, it will have the same effect as if you hard coded the map.</p> <p>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.</p> <p>To create an annotation processor, extend <a href="http://docs.oracle.com/javase/7/docs/api/javax/annotation/processing/AbstractProcessor.html" rel="nofollow"><code>AbstractProcessor</code></a>. You will want to annotate your class with a <a href="http://docs.oracle.com/javase/7/docs/api/javax/annotation/processing/SupportedAnnotationTypes.html" rel="nofollow"><code>@ SupportedAnnotationTypes ( "Name" )</code></a> annotation (make sure name is the fully qualified name of your annotation.</p> <p>Override the <a href="http://docs.oracle.com/javase/7/docs/api/javax/annotation/processing/AbstractProcessor.html#process%28java.util.Set,%20javax.annotation.processing.RoundEnvironment%29" rel="nofollow"><code>process</code></a> method. <code>process</code> has two parameters: <code>annotations</code> and <code>roundEnv</code>. <code>annotations</code> is just the set of annotations that this particular processor supports - in your case it should be (Name). <code>roundEnv</code> is a useful utility class.</p> <p>Iterate through the one annotation in <code>annotations</code>. Use <code>roundEnv</code> to <a href="http://docs.oracle.com/javase/7/docs/api/javax/annotation/processing/RoundEnvironment.html#getElementsAnnotatedWith%28javax.lang.model.element.TypeElement%29" rel="nofollow"><code>getElementsAnnotatedWith</code></a>. This should give you the set of all elements that carry the <code>@Name</code> annotation.</p> <p><code>AbstractProcessor</code> has another utility member - <a href="http://docs.oracle.com/javase/7/docs/api/javax/annotation/processing/AbstractProcessor.html#processingEnv" rel="nofollow"><code>processingEnv</code></a>. Use its <a href="http://docs.oracle.com/javase/7/docs/api/javax/annotation/processing/ProcessingEnvironment.html#getFiler%28%29" rel="nofollow"><code>getFiler</code></a> method to <a href="http://docs.oracle.com/javase/7/docs/api/javax/annotation/processing/Filer.html#createSourceFile%28java.lang.CharSequence,%20javax.lang.model.element.Element...%29" rel="nofollow"><code>createSourceFile</code></a>.</p> <p>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 <code>-processorpath /path/to/processor/class[es]</code> and <code>-processor qualified.name.of.processor</code>.</p> <p>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 <code>@Name</code> annotation to a <code>Bar</code> 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.</p> <p>The disadvantage of this approach is also that everything happens at compile time. This makes it harder to dynamically add classes to the project.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload