Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you limit annotation target to be subclasses of a certain class?

Can you limit that a target of an annotation must be of a certain class?

I want to create a new validation Constraint to limit file types that are uploaded. The constraint annotation must only go on a MultipartFile property, not on String or anything like that. How do I limit this?

like image 942
f.khantsis Avatar asked Jul 20 '14 00:07

f.khantsis


2 Answers

Not at compile-time; the only restrictions available for annotation placement are by element type (method, class, etc.).

like image 68
chrylis -cautiouslyoptimistic- Avatar answered Oct 26 '22 23:10

chrylis -cautiouslyoptimistic-


Yes, this is possible (and was possible when the question was asked).

As a general rule, when working with annotations you need to use an annotation processor. You can write an annotation processor that issues errors whenever an annotation is written in a disallowed location.

If your question is whether this is possible with plain javac and no annotation processor, then the answer is "no".

like image 34
mernst Avatar answered Oct 26 '22 21:10

mernst