Reputation: 10961
I'm using Elements.getElementValuesWithDefaults to retrieve annotationvalues. Key of the returned Map is something extending ExecutableElement. I can iterate over the entrySet and check the name of each key but this leads to ugly if-else-cascades.
Map<? extends ExecutableElement, ? extends AnnotationValue> annotationValues =
elementUtils.getElementValuesWithDefaults(annotationMirror);
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : annotationValues.entrySet()) {
if (entry.getKey().getSimpleName().contentEquals("method1")) {
// do something with value of method1
} else if (entry.getKey().getSimpleName().contentEquals("method2")) {
// do something with value of method2
} else if (entry.getKey().getSimpleName().contentEquals("method3")) {
// do something with value of method3
}
}
I did not find an easy way to create an ExecutableElement as key to get the value. Anyone knows one?
Upvotes: 0
Views: 214
Reputation: 27984
You could use a map of runnables.
Map<String, Runnable> map = new HashMap();
map.put("method1", new Runnable() { ... });
map.put("method2", new Runnable() { ... });
map.put("method3", new Runnable() { ... });
for (Map.Entry entry : ...) {
map.get(entry.getKey().getSimpleName()).run();
}
I'm guessing you want to pass the entry into the method so you might want a custom interface instead of Runnable.
Upvotes: 1