JSR 308 proposes to add type annotations to Java. After its ratification, programmers will be able to add an annotation wherever a Java type is currently allowed. That includes not only method/field/local/parameter decorations, but also constructor calls, type casts, and most curiously instanceof checks. The Checker Framework uses JSR 308 to implement type qualifiers like @NonNull
on object types, or @Regex
on strings.
Now all what Checkers does is to statically analyze your code. That's all compile time checks. That's fine. But what I want to have is a mechanism that can do checks at runtime. You can declare:
@Regex String p1 = "[a-z]+";
@Regex String p1 = "[a-z)+"; // compile time error from annotation processor
I can also write:
if (x instanceof @Regex String) ...
but that is no different from x instanceof String
, no runtime check is performed.
I need a compile time annotation processor or runtime bytecode manipulator that lets me run arbitrary code on instanceof
checks and return a boolean. Is this possible with Java?