I'm trying to use annotated class as parameters like below:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ContextHolder {
}
@ContextHolder
class Foo extends Context {
}
// In some other place
protected Context getContext(ActionHandler handler) {
if (handler.getClass().isAssignableFrom(Context.class)) {
return (Context) handler;
}
for (Method m : handler.getClass().getDeclaredMethods()) {
if (m.getReturnType().isAssignableFrom(Context.class)) {
try {
return (Context) m.invoke(handler);
} catch (IllegalAccessException e) {
ALog.w("", e);
} catch (IllegalArgumentException e) {
ALog.w("", e);
} catch (InvocationTargetException e) {
ALog.w("", e);
}
break;
}
}
ALog.e("Can't find Context in passed ActionHandler");
return null;
}
Foo foo = ...;
getContext(foo?)
The problem is I don't know how to call getContext()
. Simply passing foo results compile error.
Any hint will be appreciated. Thanks!