According to the Java Language Specification (JLS) Chapter 9.6, annotations are "a special kind of interface type." In fact, it even says:
The direct superinterface of every annotation type is java.lang.annotation.Annotation
.
If we go to the documentation, we can see that java.lang.annotation.Annotation
is in fact an interface. I can think of two possible scenarios here which would involve casting off the top of my head. Note that this is not an exhaustive list. There are many different ways to design a type hierarchy.
Scenario 1
An interface can extend any other interface, so you could have an interface that extends an annotation. Then you would need to cast to the subinterface, like so:
public @interface MyAnnotation { }
public interface MyInterface extends MyAnnotation { }
public class MyClass implements MyInterface {
// You have to implement the abstract method:
@Override
public Class<? extends Annotation> annotationType() {
return MyAnnotation.class;
}
}
public class Driver {
public static void main(String[] args) {
// Implicit cast to super:
MyAnnotation annot = new MyClass();
// Now we have to explicitly cast back down to an interface:
MyInterface inter = (MyInterface) annot;
}
}
Scenario 2
It should be legal to cast an instance to one of its supertypes. Yes, it's not required, but it should be legal, so you could have something like the following code:
public @interface MyAnnotation { }
@MyAnnotation public class MyClass { }
public class Driver {
public static void main(String[] args) {
// Redundant, but legal cast:
Annotation annot = (Annotation) MyClass.class.getAnnotations()[0];
}
}
Limitations
Note that the JLS says:
By virtue of the AnnotationTypeDeclaration syntax, an annotation type declaration cannot be generic, and no extends clause is permitted.
This means that you cannot have an annotation be a subinterface of anything other than java.lang.annotation.Annotation
.
The JLS also says:
A consequence of the fact that an annotation type cannot explicitly declare a superclass or superinterface is that a subclass or subinterface of an annotation type is never itself an annotation type.
This means that the following code would be illegal:
public @interface MyAnnotation { }
public interface MyInterface extends MyAnnotation { }
// Illegal because MyInterface is not an annotation type:
@MyInterface public class MyClass { }
Your Scenario
You have two interfaces, org.junit.Test
and junit.framework.Test
. Yes, org.junit.Test
is an annotation, but all annotations are interfaces, as I've explained above. Now consider a class that implements both interfaces:
public class MyClass implements org.junit.Test, junit.framework.Test {
// You would have to implement all the methods here
}
Consider the following driver code, and note the legitimate, legal cast from org.junit.Test
to junit.framework.Test
:
public class Driver {
public static void main(String[] args) {
// Implicit cast, because MyClass is a subtype of org.junit.Test:
org.junit.Test test1 = new MyClass();
// Here we need an explicit cast, but it's still legal.
// test1 is still an instance of MyClass, so we can still cast it
// to junit.framework.test
junit.framework.Test test2 = (junit.framework.Test) test1;
}
}
As you can see above, you could create a class that is a subtype of both org.junit.Test
and junit.framework.Test
, so the compiler isn't going to complain if you cast between the two. If your class doesn't implement both interfaces, and you try to cast an instance to an interface it doesn't implement, you'll just get a runtime exception.
TestJunit3
class is crap. It mixes JUnit 3 classes (junit.framework.TestResult
,junit.framework.Test
) with JUnit 4 annotations (org.junit.Test
) which doesn't make sense. If you work with JUnit 3 your test classes extendjunit.framework.TestCase
and your test methods must have names starting withtest
. In JUnit 4 your test classes don't need to extend anything and you annotate your test methods with@org.junit.Test
.