Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should use the Java annotation coupled with the javac annotation processor, as it's the most efficient solution. It's however a bit more complicated than the usual annotation paradigm.</p> <p><a href="http://docs.oracle.com/javase/8/docs/technotes/tools/windows/javac.html#sthref66" rel="nofollow">This link</a> shows you how you can implement an annotation processor that will be used at the compile time.</p> <p>If I reuse your example, I'd go this way:</p> <pre><code>@Target(ElementType.TYPE) @Retention(RetentionType.SOURCE) @interface MetaData { String table(); } abstract class Entity {} @MetaData(table="a") class A extends Entity {} @MetaData(table="b") class B extends Entity {} class EntityGetter { public &lt;E extends Entity&gt; E getEntity(Class&lt;E&gt; type) { MetaData metaData = type.getAnnotation(MetaData.class); if (metaData == null) { throw new Error("Should have been compiled with the preprocessor."); // Yes, do throw an Error. It's a compile-time error, not a simple exceptional condition. } String table = metaData.table(); // do whatever you need. } } </code></pre> <p>In your annotation processing, you then should check whether the annotation is set, whether the values are correct, and make the compilation fail.</p> <p>The complete documentation is available in the documentation for the <a href="http://docs.oracle.com/javase/8/docs/api/javax/annotation/processing/package-summary.html" rel="nofollow">package <code>javax.annotation.processing</code></a>.</p> <p>Also, a few tutorials are available on the Internet if you search for "java annotation processing".</p> <p>I will not go deeper in the subject as I never used the technology myself before.</p>
 

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