Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mutually exclusive annotations

Tags:

Is it possible to mark two annotations as being mutually exclusive of each other?

I've got a custom JUnit runner and I want to make sure that if a test is marked with my annotation @Custom marking it with @Test will throw (preferably) a compile error or (less preferably) a runtime error.

The reason I want this is that @Custom is basically @Test but with some extra pre-processing before it runs the test. And, the way I've coded it, for each custom tag (yes, there's more than one), the test will be run as many times with the corresponding pre-processing each time. So, having it run for @Test as well doesn't make sense since that test is meant to have some pre-processing.

And, yes, I want to support both @Custom and @Test in my framework (although, not for the same test method).

like image 598
noob Avatar asked Feb 20 '12 17:02

noob


People also ask

What is annotation and its types?

An annotated bibliography describes the field of research on a topic and should include sources that reflect the range of approaches to the subject. There are four main types of annotations. Descriptive. A descriptive (also called an indicative) annotation gives a brief overview or summary of the text.

What are marker annotations?

What is a Marker Annotation? Marker annotations are special annotations in Java that do not contain any members or data. As marker interfaces do not contain any members, just declaring the annotation in your code is sufficient for it to influence the output in whatever terms you want.

What is Java Lang annotation?

Java Annotation is a tag that represents the metadata i.e. attached with class, interface, methods or fields to indicate some additional information which can be used by java compiler and JVM.


2 Answers

There isn't really any way to put limitations on what annotations you can put on a Java element. So, for example, you could mark a field as both @Required and @Optional, even though this would be completely nonsensical.

You would have to do the check at runtime by looking at what annotations are present, or by looking for the specific one you are interested in, and then throwing an exception (which would be caught by a unit test).

There might be a better solution to your specific problem. If you want to treat certain test cases specially, you might want to implement your own test runner and use the @RunWith annotation to invoke it. Then, you could ensure your @Custom methods get the prerequisite actions.

like image 92
BoffinBrain Avatar answered Oct 12 '22 00:10

BoffinBrain


You could utilize the Java Annotation Processing to do this for you (by calling a post-compile operation that in turn fails the compilation task), but it is needlessly complicated. (If you really want to, tell me and I dig out that code for you). More easily it would be to have a small class that you call manually that collects the information.

So more precise: do you need it for you (yourself and only you) as convenience or do you need it for your huge project as an automated solution (and are willing to put some effort in it?)

like image 28
Angelo Fuchs Avatar answered Oct 12 '22 00:10

Angelo Fuchs