Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can build an Annotation processor for that. Annotation Processors are compiler plugins that get run at compile time. Their errors show up as compiler errors, and may even halt the build.</p> <p>Here is a sample code (I didn't run it though):</p> <pre><code>@SupportedAnnotationTypes("*") // needed to run on all classes being compiled @SupportedSourceVersion(SourceVersion.RELEASE_6) public class DefaultConstructor extends AbstractProcessor { @Override public boolean process(Set&lt;? extends TypeElement&gt; annotations, RoundEnvironment roundEnv) { for (TypeElement type : ElementFilter.typesIn(roundEnv.getRootElements())) { if (requiresDefaultConstructor(type)) checkForDefaultConstructor(type); } return false; } private void checkForDefaultConstructor(TypeElement type) { for (ExecutableElement cons : ElementFilter.constructorsIn(type.getEnclosedElements())) { if (cons.getParameters().isEmpty()) return; } // Couldn't find any default constructor here processingEnv.getMessager().printMessage( Diagnostic.Kind.ERROR, "type is missing a default constructor", type); } private boolean requiresDefaultConstructor(TypeElement type) { // sample: require any JPA Entity to have a default constructor return type.getAnnotation(Entity.class)) != null || type.getQualifiedName().toString().contains("POJO"); } } </code></pre> <p>The annotation processor becomes even easier if you introduce an annotation (e.g. RequiresDefaultAnnotation).</p> <p><strong>Declaring the requirement of having a default qualifier</strong></p> <p>::I am also assuming that the OP asking for a mechanism that prevents accidental errors for developers, especially written by someone else.::</p> <p>There has to be a mechanism to declare which classes require a default processor. Hopefully, you already have a criteria for that, whether it is a pattern in the name, pattern in the qualifier, a possible annotation, and/or a base type. In the sample I provided above, you can specify the criteria in the method <code>requiresDefaultConstructor()</code>. Here is a sample of how it can be done:</p> <ol> <li><p>Based on a name pattern. <code>TypeElement</code> provide access to the fully qualified name and package name.</p> <pre><code>return type.getQualifiedName().toString().contains("POJO"); </code></pre></li> <li><p>Based on an annotation present on the type declaration. For example, all Java Bean Entity classes should have a non-default constructors</p> <pre><code>return type.getAnnotation(Entity.class) != null; </code></pre></li> <li><p>Based on a abstract class or interface.</p> <pre><code>TypeElement basetype = processingEnv.getElements().getTypeElement("com.notnoop.mybase"); return processingEnv.getTypes().isSubtype(type.asType(), basetype.asType()); </code></pre></li> <li><p>[Recommended Approach]: If you are using the basetype interface, I recommend mixing the annotation approach with the base type interface. You can declare an annotation, e.g. <code>MyPlain</code>, along with the meta annotation: <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/annotation/Inherited.html" rel="noreferrer"><code>@Inherited</code></a>. Then you can annotate the base type with that annotation, then all subclasses would inherit the annotation as well. Then your method would just be</p> <pre><code>return type.getAnnotation(MyPlain.class) != null; </code></pre> <p>This is better because it's a bit more configurable, if the pattern is indeed based on type hierarchy, and you own the root class.</p></li> </ol> <p>As mentioned earlier, just because it is called "annotation processing", it does mean that you have to use annotations! Which approach in the list you want to follow depends on your context. Basically, the point is that whatever logic you would want to configure in your deployment enforcement tools, that logic goes in <code>requiresDefaultConstructor</code>.</p> <p><strong>Classes the processor will run on</strong></p> <p>Annotation Processors invocation on any given class depends on <code>SupportedAnnotationTypes</code>. If the <code>SupportedAnnotationTypes</code> meta-annotation specifies a concrete annotation, then the processor will only run on those classes that contain such annotation.</p> <p>If <code>SupportedAnnotationTypes</code> is <code>"*"</code> though, then the processor will be invoked on all classes, annotated or not! Check out the [Javadoc](<a href="http://java.sun.com/javase/6/docs/api/javax/annotation/processing/Processor.html#getSupportedAnnotationTypes())" rel="noreferrer">http://java.sun.com/javase/6/docs/api/javax/annotation/processing/Processor.html#getSupportedAnnotationTypes())</a>, which states:</p> <blockquote> <p>Finally, <code>"*"</code> by itself represents the set of all annotation types, including the empty set. Note that a processor should not claim <code>"*"</code> unless it is actually processing all files; claiming unnecessary annotations may cause a performance slowdown in some environments.</p> </blockquote> <p>Please note how <code>false</code> is returned to ensure that the processor doesn't claim all annotations.</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