1

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!

Romulus Urakagi Ts'ai
  • 3,699
  • 10
  • 42
  • 68
  • `getContext` expects an argument of type `ActionHandler`. Is `Foo` an `ActionHandler`? Is `Foo` what you should be passing in or what you should be getting out? – Sotirios Delimanolis Jul 28 '14 at 03:12
  • I don't understand what your code has to do with annotations beside the fact that there are annotated classes. And if you get exceptions you have to include the logcat in your question. Without it we can just guess what the problem might be. – Xaver Kapeller Jul 28 '14 at 03:26
  • `Foo` is an `ActionHandler`, and I want to use annotations to prevent regular interface's implementing. – Romulus Urakagi Ts'ai Jul 28 '14 at 03:49
  • Check this for getContext : http://stackoverflow.com/questions/10641144/difference-between-getcontext-getapplicationcontext-getbasecontext-and – VVB Jul 28 '14 at 05:35

0 Answers0