0

I have a requirement to create a custom Annotation which when applied over a method checks and validates the input parameters (Primitive and non-primitive) against various checks. And if checks fail should return a error message directly.

While searching I have come across use of AbstractProcessor and ConstraintValidator when we create a custom annotation. Being new to creating custom annotation, I would like to understand how to go ahead implementing my problem statement.

user3398321
  • 73
  • 3
  • 7
  • 2
    An `AnnotationProcessor` is active at compile time and can, for example, generate new code. A `ConstraintValidator` is active at runtime and checks whether, for example, a parameter satisfies certain constraints. – Turing85 Mar 03 '19 at 14:18
  • A thing to note is that ConstraintValidator is part of the Validation API, while an AnnotationProcessor is shipped with the standard JDK. – LppEdd Mar 03 '19 at 14:19

2 Answers2

3

First of all, you need to understand that you're talking about two different things.

TL;DR: compile-time vs run-time - you need run-time


An Annotation processor is an abstract term to identify a process which runs during compile time, and which is able to scan the source files, the ones which have a .java extension.

The Annotation processor implementation might throw warnings, or even errors to stop the compilation task, based on arbitrary checks.
Example: "I noticed someone passed a null value to my method, it's a error! Stop everything!"

The starting point for implementing an Annotation processor is the AbstractProcessor base class, or the Processor interface (documentation).
You'd also need, unlike the ConstraintValidator implementation, to register it explicitly via a processor metadata file, which must be located under a standard directory inside the produced JAR artifact.

META-INF/services/javax.annotation.processing.Processor

What is needed to create an Annotation processor is already included in the standard JDK. You don't need external dependencies.


On the other hand, a ConstraintValidator identifies an interface which is shipped with the Validation API module, under the package javax.validation. There are different implementations of this API, but the most used one is Hibernate Validator (documentation).
The validation which this interface provides are verified at runtime.

Unlike the Annotation processor, the Validation API implementation must be provided manually, e.g.

<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.0.15.Final</version>
</dependency>

You wrote

when applied over a method checks and validates the input parameters (Primitive and non-primitive) against various checks

It seems you need to write run-time checks, which means the Validation API is the road to take.

LppEdd
  • 20,274
  • 11
  • 84
  • 139
  • 1
    Thanks @LppEdd for the detailed explanation. Was totally confused between the two and now its clear which one i should use. Thanks again – user3398321 Mar 03 '19 at 15:41
  • @user3398321 the Hibernate Validator documentation is really good. I recommend you to read it all (or a good part of it) before starting to write code. Another thing you could check out is Aspect Oriented Programming, specifically AspectJ. It will open you to a new world. – LppEdd Mar 03 '19 at 15:43
2

The use cases of AbstractProcessor and ConstraintValidator are quite different. Let's take a dive and explore both.


The AbstractProcessor is

a convenient superclass for most concrete annotation processors

So what are annotation processors and what is their use case? If you want to generate new source code at compile time, based on annotations, you can use annotation processors. They are provided as part of the compilation process and invoked through the Java compiler. An example of this is a static metamodel geneartors for JPA.


A ConstraintValidator can be used to ensure, at runtime, that attributes, parameters and alike satisfy certain constraint. One of the most popular constraints is @NotNull. At runtime, some piece of code, a constraint validator, checks that all fields/parameters annotated with @NotNull actually are not null. If you want to write your own constraint, you need to write an annotatation, as well as a processor to satisfy your constraint. This tutorial on dzone.com outlines the process of creating a custom annotation and corresponding ConstraintValidator.

Turing85
  • 18,217
  • 7
  • 33
  • 58
  • And I would that the one depends on the other but not the reverse. Indeed Constraint Validator implementation relies on annotation processors to process the annotations used by the client but nothing in the other way. https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#validator-annotation-processor – davidxxx Mar 03 '19 at 15:00
  • @davidxxx what you linked is an optional step. It's not mandatory. Hibernate Validator doesn't depend on annotation processing. They're completely separated. – LppEdd Mar 03 '19 at 15:03