I’m rolling my own IAuthorizationStrategy for Wicket 1.5.x I’ve setup type annotation for pages to use with isInstantiationAuthorized(). It works well and I’d like to use annotations for isActionAuthorized() as well. Ideally I’d like to be able annotate local variables and then check the annotations in my AuthStrategy. From what I’ve read Local variable Annotation doesn’t work that way.
Is there any kind of known work around, maybe some sort of Compile time annotation processing to turn an annotated local variable into an “anonymous” subclass with the annotation as a type annotation?
For the record, the annotation I’m trying to use looks like this:
@Retention(RetentionPolicy.Runtime)
@Target(ElementType.Type, ElementType.LOCAL_VARIABLE)
public @interface AdminOnly
{
int isVisible() default 0;
int isEnabled() default 1;
}
UPDATE
So based on @Xavi López’es answer what I was hoping to do isn’t exactly possible.
Annotated LocalVariables should be available at compile time though. Is there some way maybe I could use them as a shortcut for boiler-plating the meta-data code examples that are available in Wicket Examples or the excellent Apache Wicket Cookbook?
I’ve struggled with a similar issue some time ago with Wicket 1.3.x, and didn’t find any way to achieve this with annotations. Annotations on local variables can’t be retained at run-time, as explained in the JLS (9.6.3.2. @Retention):
In this related question: How can I create an annotation processor that processes a Local Variable? they talked about LAPT-javac, a patched
javacversion to allow this. On their site there’s a link to the Type Annotations Specification (JSR 308), which will hopefully address this subject (JDK 8 ?).I ended up defining a plain old interface with a related functionality code:
The main problem with this approach is that it’s not possible to make instant anonymous subclasses of a specific class implement other interfaces (such as
Component c = new TextField() implements AdminOnly { }) , but you can always defineComponentextensions that just implementRestrictedComponentin a class:Finally, I ended up implementing a
RestrictedContainerthat just subclassedWebMarkupContainerand put every secured component inside one, modelling it with a<wicket:container>in the markup.And then in the Authorization Strategy checked for
component instanceof RestrictedComponentand returnedtrueorfalsedepending on user permissions on the associated function.