Create annotation that includes FindBugs' and Lombok's #NonNull - java

I would like to use Lombok's #NonNull annotation to generate the null-checking code automatically for method parameters while also using FindBugs' #NonNull to use static analysis tools and generate appropriate warnings whenver the case applies.
As of now, I need to do the following:
public void doSomething (#lombok.NonNull #edu.umd.cs.findbugs.annotations.NonNull Object parameter)
{
// Do something
}
This is quite ugly, so I would like to avoid using this syntax. I read about nested annotations (here and here), but I can't seem to find a way to create my own custom annotation with both NonNull annotations as nested annotations. Am I trying to do something that cannot work?
Here's my latest attempt:
#Documented
#Retention (RetentionPolicy.CLASS)
#Target (value={ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE})
public #interface MyNonNull
{
public lombok.NonNull lombokNonNull () default #lombok.NonNull;
public edu.umd.cs.findbugs.annotations.NonNull findBugsNonNull () default #edu.umd.cs.findbugs.annotations.NonNull;
}

You cannot "merge" annotations with custom annotations, however you can use #ParametersAreNonnullByDefault on the class scope, which should allow edu.umd.cs.findbugs.annotations.NonNull to be inferred.

I think neither of your links will help you. You would need to extend from both with your custom annotation, yet you cannot even extend from one, as all annotations have an implicit extends clause for Annotation.

Related

Separate validation annotation and it's validator in separate modules

Situation: you have module with DTO objects used in your API, so that other project(s) can reuse then when sending requests. These DTO classes does have bean-validation annotations in them. And you would like to use your custom validations to validate DTO "arriving" via requests. The sender typically does not validate outgoing data, IIUC, and might not be interested in importing validators along with annotations.
Problem(?): bean-validation is defined in a way, where annotation defines who implements it (which is incorrect and it should be otherwise around imo), with possibility to specify empty array as annotation validator (seems like hack) and then pairing is done via manual hashmap manipulations instead of stuff like service loader etc.
How do you do this?
Would you split annotation and it's validator in separate modules?
How would you bind them together? I think it should be possible to use {} as validator and then use org.hibernate.validator.internal.metadata.core.ConstraintHelper#putValidatorDescriptors to bind them together, but I did not test it yet + maybe there is better way...
I agree that the annotation defining the validator does feel backwards. While not ideal, I've been able to work around this by separating my custom ConstraintValidator into an interface and implementation.
Example:
In api module define constraint and interface validator
#Constraint(validatedBy = MyConstraintValidator.class)
public #interface MyConstraint
{
}
public interface MyConstraintValidator
extends ConstraintValidator<MyConstraint, String>
{
}
In your service module define the implementation
public class MyConstraintValidatorImpl implements MyConstraintValidator
{
private FooService foo;
#Override
public boolean isValid( String value, ConstraintValidatorContext ctx)
{
// Implement your constraint logic
}
}
I we need to separate interface class and validator implementation into separate modules, it's possible. And even in a way, which I said in original question, that should be used. In API module you declare validation for example as:
#Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
#Retention(RUNTIME)
#Documented
#Constraint(validatedBy = {})
#SupportedValidationTarget(ValidationTarget.ANNOTATED_ELEMENT)
#ReportAsSingleViolation
public #interface AnyUuid {
//...
notice validatedBy = {}. The validator implementation looks like:
public class AnyUuidValidator implements ConstraintValidator<AnyUuid, Object> {
//...
and pairing can be setup using service loader(see javadoc if you don't know how that works). Put into file META-INF/services/javax.validation.ConstraintValidator FQDN of AnyUuidValidator shown above. And all other custom validators. And that should be it.
There is a bug I found with this. If I'm not mistaken. If you have DTO you cannot change (~annotate with constraints) and still want to validate them via bean validation, you can register validation definitions via xml file. If you are doing so and use service loader for pairing definition and implementation of custom validators, there is probably some bug and your custom validators won't be found. So verify this scenario before relying on service loader. But maybe I'm wrong, for me it was feasible to drop this validation trivially so I did to save some time and could ignore this.

can I use any annotation on custom annotation

As, I know we can apply annotation like #Target, #Retention, #Documented on custom annotation.
But recently I saw #Constraint applied on custom annotation.
Can We use any annotation on custom annotation? how it works?
Each annotation can only be written in certain places. Those places are determined by the #Target meta-annotation on the annotation's definition. For example, if an annotation is declared as
#Target(ElementType.ANNOTATION_TYPE)
public #interface MyAnnotation {
...
}
then #MyAnnotation may only be written on annotation declarations. You cannot write #MyAnnotation on a field or class declaration, or on a type use. An annotation that may be written on annotation declarations is called a meta-annotation.
You can learn more about annotations from the Java annotations tutorial.

#inherited also make #Overrided methods values inherited the annotation?

i'm working with a lot of interface inherited and annotations lately and I have a doubt about #Inherited annotation. I know that by default java annotations are not inherited for subclasses and methods. Using the #Inherited annotation we can make the annotation be inherited by the subclasses.
But it also work with the annotated values in the method constructors?
For example:
#Inherited
#StringDef({ID_REAR, ID_FRONT})
#Retention(RetentionPolicy.SOURCE)
public #interface CustomId {
}
public interface Setting {
void update(#CustomId String myCustomId);
}
public class CustomSetting implements Setting{
#Override public void update(String myCustomId) {
}
}
In this case, will the method update from CustomSetting implement the annotation#CustomId for his contructor (String myCustomId) ?
As Jesper answer above, the API docs is very clear about it: The #Inherit annotation doesn't work with anything other than a class:
Note that this meta-annotation type has no effect if the annotated type is used to annotate anything other than a class. Note also that this meta-annotation only causes annotations to be inherited from superclasses; annotations on implemented interfaces have no effect.

How #Target(ElementType.ANNOTATION_TYPE) works

Java annotations are marked with a #Target annotation to declare possible joinpoints which can be decorated by that annotation. Values TYPE, FIELD, METHOD, etc. of the ElementType enum are clear and simply understandable.
Question
WHY to use #Target(ANNOTATION_TYPE) value? What are the annotated annotations good for? What is their contribution? Give me an explanation of an idea how it works and why I should use it. Some already existing and well-known example of its usage would be great too.
You can use an annotated annotation to create a meta-annotation, for example consider this usage of #Transactional in Spring:
/**
* Shortcut and more descriptive "alias" for {#code #Transactional(propagation = Propagation.MANDATORY)}.
*/
#Target({ElementType.METHOD, ElementType.TYPE})
#Retention(RetentionPolicy.RUNTIME)
#Transactional(propagation = Propagation.MANDATORY)
public #interface RequiresExistingTransaction {
}
When you enable Spring to process the #Transactional annotation, it will look for classes and methods that carry #Transactional or any meta-annotation of it (an annotation that is annotated with #Transactional).
Anyway this was just one concrete example how one can make use of an annotated annotation. I guess it's mostly frameworks like Spring where it makes sense to use them.
Each annotation annotated by #Target(ElementType.ANNOTATION_TYPE) is called Meta-annotation. That means, you can define your own custom annotations that are an amalgamation of many annotations combined into one annotation to create composed annotations.
A good example from Android world is StringDef
Denotes that the annotated String element, represents a logical type and that its value should be one of the explicitly named constants.
#Retention(SOURCE)
#StringDef({POWER_SERVICE, WINDOW_SERVICE, LAYOUT_INFLATER_SERVICE})
public #interface ServicesName {}
public static final String POWER_SERVICE = "power";
public static final String WINDOW_SERVICE = "window";
public static final String LAYOUT_INFLATER_SERVICE = "layout_inflater";
Code inspector will treat #ServicesName and #WeekDays in the same way as #StringDef.
As a result we can create as much named StringDef's as we need and override set of constants. #Target(ElementType.ANNOTATION_TYPE) it is a tool that allows to extend the use of annotations.
Annotation is defined like a ordinary Java interface, but with an '#' preceding the interface keyword (i.e., #interface ). Annotations are used to provide supplemental information about a program. On the other hand, an interface can be defined as a container that stores the signatures of the methods to be implemented in the code segment.
WHY to use #Target(ANNOTATION_TYPE) value?
When there is need to apply an annotation to an another annotation. If you look at the source codes of the common Java annotations, you see often this code pattern:
#Target(ANNOTATION_TYPE)
public #interface TheAnnotation
{
...
}
For example,
#Documented
#Target({ ANNOTATION_TYPE })
#Retention(RUNTIME)
public #interface Constraint {
public Class<? extends ConstraintValidator<?, ?>>[] validatedBy();
}
What are the annotated annotations good for?
They are good or more precisely necessary if they are used to annotate other annotations.
What is their contribution?
They make possible to apply an annotation directly to an another annotation, that is a different thing than applying an annotation to a standard Java class or to method and so on.
Give me an explanation of an idea how it works and why I should use it.
For example, if you create a data model class and you may want that the program checks data validity. In that case, there might be need to create a new annotation and apply another annotations to this annotation. It is simple to add some data validity checks to this model by adding annotations to the class. For example, to check that some value is not null (#notNull) or email is valid (#ValidEmail) or length of a field is more than x characters (#Size). However, it is possible that there is not built in Java annotations for all purposes. For example, it is so if you liked to check if password and its matchingPassword are same. This is possible by creating the annotation class PasswordMatches:
#Target({TYPE})
#Retention(RUNTIME)
#Constraint(validatedBy = PasswordMatchesValidator.class)
#Documented
public #interface PasswordMatches {
String message() default "Passwords don't match";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
Note, there is line
#Constraint(validatedBy = PasswordMatchesValidator.class).
In other words, the annotation class Constraint, like the other annotations in this class also, must have ANNOTATION_TYPE as a value of target annotation.
Now the password equality check is easy to include to data model class simply by adding annotation #PasswordMatches:
#PasswordMatches
public class UserDto {
...
}
The PasswordMatchesValidator class could look like this:
public class PasswordMatchesValidator implements ConstraintValidator<PasswordMatches, Object> {
#Override
public void initialize(final PasswordMatches constraintAnnotation) {}
#Override
public boolean isValid(final Object obj, final ConstraintValidatorContext context) {
final UserDto user = (UserDto) obj;
return user.getPassword().equals(user.getMatchingPassword());
}
}
Some already existing and well-known example of its usage would be great too.
There is quite well-known example in item 4, but another known annotations which are applied frequently to custom annotations are #Retention, #Documented and #Target itself.
For example, if annotation looks like
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
public #interface SomeAnnotation {
String description() default "This is example for class annotation";
}
the compiler will complain in this situation
#SomeAnnotation
public class SomeClass {
#SomeAnnotation // here it's complaning
public void someMethod(){}
}
If you change
#Target(ElementType.TYPE)
to
#Target({ElementType.METHOD, ElementType.TYPE})
it won't complain anymore.
Annotation are basically additional metadata (information) that goes along with your code. It can be placed along side types (Classes, Interfaces), methods, and arguments.
It is often useful during compile time and runtime. Many popular APIs such as Java EE 5+, Spring, AspectJ leverage annotation for code clarity and consistency.
Using annotation often allows code to be more readable, more easily understood.
I'd recommend you read through the annotation chapter on Java tutorial
In the past metadata are often given as an xml file, and it's difficult for someone trying to understand the code if they have to lookup a different xml configuration file. The latest Java servlet API allows mapping of servlet simply by using annotation -- as opposed of web.xml mapping:
#WebServlet("/response")
public class ResponseServlet extends HttpServlet {
// servlet code here...
}

Multiple annotations of the same type on one element?

I'm attempting to slap two or more annotations of the same type on a single element, in this case, a method. Here's the approximate code that I'm working with:
public class Dupe {
public #interface Foo {
String bar();
}
#Foo(bar="one")
#Foo(bar="two")
public void haha() {}
}
When compiling the above, javac complains about a duplicate annotation:
max#upsight:~/work/daybreak$ javac Dupe.java
Dupe.java:5: duplicate annotation
Is it simply not possible to repeat annotations like this? Pedantically speaking, aren't the two instances of #Foo above different due to their contents being different?
If the above isn't possible, what are some potential workarounds?
UPDATE: I've been asked to describe my use case. Here goes.
I'm building a syntax sugarish mechanism to "map" POJOs to document stores such as MongoDB. I want to allow indexes to be specified as annotations on the getters or setters. Here's a contrived example:
public class Employee {
private List<Project> projects;
#Index(expr = "project.client_id")
#Index(expr = "project.start_date")
public List<Project> getProjects() { return projects; }
}
Obviously, I want to be able to quickly find instances of Employee by various properties of Project. I can either specify #Index twice with different expr() values, or take the approach specified in the accepted answer. Even though Hibernate does this and it's not considered a hack, I think it still makes sense to at least allow having multiple annotations of the same type on a single element.
Note: This answer is partially outdated since Java 8 introduced the #Repeatable annotation (see answer by #mernst). The need for a #Foos container annotation and dedicated handling still remain though.
Two or more annotations of same type aren't allowed. However, you could do something like this:
public #interface Foos {
Foo[] value();
}
// pre Java 8
#Foos({#Foo(bar="one"), #Foo(bar="two")})
public void haha() {}
// post Java 8 with #Repeatable(Foos.class) on #Foo
#Foo(bar="one") #Foo(bar="two")
public void haha() {}
You'll need dedicated handling of Foos annotation in code though.
Repeating annotations in Java 8
In Java 8 (released in March 2014), it is possible to write repeated/duplicate annotations.
See tutorial, Repeating Annotations.
See specification, JEP 120: Repeating Annotations.
Apart from the other ways mentioned, there is one more less verbose way in Java8:
#Target(ElementType.TYPE)
#Repeatable(FooContainer.class)
#Retention(RetentionPolicy.RUNTIME)
#interface Foo {
String value();
}
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
#interface FooContainer {
Foo[] value();
}
#Foo("1") #Foo("2") #Foo("3")
class Example{
}
Example by default gets, FooContainer as an Annotation
Arrays.stream(Example.class.getDeclaredAnnotations()).forEach(System.out::println);
System.out.println(Example.class.getAnnotation(FooContainer.class));
Both the above print:
#com.FooContainer(value=[#com.Foo(value=1), #com.Foo(value=2),
#com.Foo(value=3)])
#com.FooContainer(value=[#com.Foo(value=1), #com.Foo(value=2), #com.Foo(value=3)])
http://docs.oracle.com/javase/tutorial/java/annotations/repeating.html
Starting from Java8 you can describe repeatable annotations:
#Repeatable(FooValues.class)
public #interface Foo {
String bar();
}
public #interface FooValues {
Foo[] value();
}
Note, value is required field for values list.
Now you can use annotations repeating them instead of filling the array:
#Foo(bar="one")
#Foo(bar="two")
public void haha() {}
As said by sfussenegger, this isn't possible.
The usual solution is to build an "multiple" annotation, that handles an array of the previous annotation. It is typically named the same, with an 's' suffix.
By the way, this is very used in big public projects (Hibernate for example), so it shouldn't be considered as a hack, but rather a correct solution for this need.
Depending on your needs, it could be better to allow your earlier annotation to handle multiple values.
Example:
public #interface Foo {
String[] bars();
}
combining the other answers into the simplest form ... an annotation with a simple list of values ...
#Foos({"one","two"})
private String awk;
//...
public #interface Foos{
String[] value();
}
If you have only 1 parameter "bar" you can name it as "value". In this case you wont have to write the parameter name at all when you use it like this:
#Foos({#Foo("one"), #Foo("two")})
public void haha() {}
a bit shorter and neater, imho..
In the current version of Java, I was able to resolve this issue with the following annotation:
#Foo({"one", "two"})

Categories