Monday, December 8, 2008

java.lang.MemberOf

Wouldn't it be cool if there existed an annotation named java.lang.MemberOf that could tell the compiler that the marked interface is always implemented by a subclass of a described class.


package pkg.io;

@MemberOf(java.io.OutputStream)
public interface OutputStreamInterface {

public void write(int uint8)
throws java.io.IOException;

public void write(byte[] buf, int ofs, int len)
throws java.io.IOException;

public void flush()
throws java.io.IOException;

public void close()
throws java.io.IOException;
}


So that the name pkg.io.OutputStreamInterface could then be used in any way that the name java.io.OutputStream can be used?

Any implementor of an interface marked MemberOf would derive from the described class, in the example java.io.OutputStream.

Aside from creating a syntactic meta class, the compiler would need to inject CAST instructions in support of the MemberOf annotation.

No comments: