Skip to content
Advertisement

Make method accept only parameter with particular annotation

I have a method

JavaScript

And I have a class which holds field

JavaScript

And I have main class, where I call that method and pass him that:

JavaScript

Problem is that the method accepts all the 3 parameters, (no warnings, errors, nothing) however only roomDao is annotated. Annotation itself:

JavaScript

How to make the restriction, so that injectConfiguration(@Configurable Object bean) would accept only field (or class instance) annotated with Configurable ?

Advertisement

Answer

You can accomplish this by using an annotation processor. An example of such a tool is the Checker Framework. It enables you to write type annotations in your program, then it type-checks the type annotations at compile time. It issues a warning if the type annotations in your program are not consistent with one another.

The easiest way for you to implement the checking would be to use the Subtyping Checker.

Here is an example from its manual:

JavaScript

When you invoke javac using a couple extra command-line arguments, javac issues an error for the second invocation of sendOverInternet but not the first one:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement