In my library there is a concept of "user levels". I have provided several default levels but for various reasons want to give the user the option of using their own levels.
Currently this is implemented as
public interface AdminLevel {
public void name();
}
public enum StandardAdminLevels implements AdminLevel {
ADMIN,
ANONYMOUS
}
The problem is that the user is usually going to be passing their required user level in an annotation. Things I have tried and failed:
StandardAdminLevels.ADMIN.name()
- Fails with "attribute value must be constant"Is there any other way I can't think of to have extendable enums in annotations? I'm trying to stick with enums due to their clarity and basic protection against invalid values, but the only other way I can think of is String constants. The problem I have is that would require verification at every single point user levels are used, potentially even in client code
Any ideas?
One idea: every possible AdminLevel is its own class. Pass the Class
of that class to the annotation.
public final class Admin implements AdminLevel { ... }
public final class Anonymous implements AdminLevel { ... }
@Requires(Admin.class) public void protectedMethod() { ... }
@interface Requires {
Class<? extends AdminLevel> value();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With