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

旧城冷巷雨未停 提交于 2020-04-10 07:37:05

问题


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?


回答1:


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




回答2:


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".



来源:https://stackoverflow.com/questions/24846206/can-you-limit-annotation-target-to-be-subclasses-of-a-certain-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!