1

Can you help me understand how/why is the typecast at (1) is working ? I could not find anything indicating that Java annotation types can be typecast to an Interface type.

org.junit.Test //this is an annotation
junit.framework.Test //this is an interface

Its just that I don't see a relation between org.junit.Test and junit.framework.Test

import org.junit.Test;
import junit.framework.AssertionFailedError;
import junit.framework.TestResult;
public class TestJunit3 extends TestResult {
   // add the error
   public synchronized void addError(Test test, Throwable t) {
      super.addError((junit.framework.Test) test, t); //how does this typecast work (1)
   }
   // add the failure
   public synchronized void addFailure(Test test, AssertionFailedError t) {
      super.addFailure((junit.framework.Test) test, t);
   }
   @Test
   public void testAdd() {
      // add any test
   }
   // Marks that the test run should stop.
   public synchronized void stop() {
      //stop the test here
   }
}
1
  • 1
    IMHO this 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 extend junit.framework.TestCase and your test methods must have names starting with test. In JUnit 4 your test classes don't need to extend anything and you annotate your test methods with @org.junit.Test. Commented Mar 27, 2021 at 21:35

1 Answer 1

5

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.

3
  • Thanks. Its just that I don't see a relation between org.junit.Test and junit.framework.Test Commented Mar 27, 2021 at 20:52
  • both are interfaces (even if one is a specialized one), so a class can implement both (not to speak about java.lang.reflect.Proxys)
    – user15244370
    Commented Mar 27, 2021 at 21:16
  • 1
    @AnkurAgarwal I've added a section on your specific scenario. user15244370 is correct; you could have a class that implements both org.junit.Test and junit.framework.Test, so the compiler isn't going to complain if you cast between the two. Commented Mar 28, 2021 at 1:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.