Generate a Constant expression from a function
For my Google Wave robot, on the onDocumentChanged event I want to apply a filter as follows:
@Capability(filter = FILTER)
@Override
public void onDocumentChanged(DocumentChangedEvent event) {
...
}
I want the filter to be generated the first time the robot is run, which I'm trying to do as follows:
private static final String FILTER = generateFilter();
private static final String generateFilter(){
...
}
However, it comp开发者_JAVA百科lains FILTER isn't a constant expression when used within @Capability.
generateFilter() will return the same string every time it is called, I'm only using it to create the string so that when I make changes, I don't need to worry about updating the filter.
Now I could be going about this all wrong, so wondered if anyone knew what I'm doing wrong, or knew a better way in which I could generate a constant expression from the function.
I'm unfamiliar with Google Wave, but a static initializer might be acceptable, as shown here and outlined below.
private static final String FILTER;
static { FILTER = "..."; }
Addendum: On closer scrutiny, this approach is not possible, as an annotation value must be (among other things) a constant expression.
The compiler needs the contant value in the annotation at compile time and your initialization will happen i think at the application initialization time.
You could probably do it like this:
private static final String FILTER = "YOUR STRING";
private static final String generateFilter() {
return FILTER;
}
That way if you need to change it and not worry you will go the the method and from there to the constant value :).
精彩评论