Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Patch for AbstractView #20

Open
pgaschuetz opened this issue Feb 3, 2012 · 1 comment
Open

Patch for AbstractView #20

pgaschuetz opened this issue Feb 3, 2012 · 1 comment

Comments

@pgaschuetz
Copy link

Hi,

here's a replacement for AbstractView.createPresenter(), which uses reflection to determine and create the Presenter

@Override
public P createPresenter() {

    ParameterizedType thisType = (ParameterizedType) getClass().getGenericSuperclass();

    @SuppressWarnings("unchecked")
    Class<P> presenterClazz = (Class<P>) thisType.getActualTypeArguments()[1];

    try {
        Constructor<P> ctor = presenterClazz.getConstructor();
        return ctor.newInstance();
    } catch (NoSuchMethodException e) {
        throw new UnsupportedOperationException(
                "No default constructor for Presenter '" + presenterClazz.getSimpleName() + "' available.");
    }
    catch (Exception e) {
        throw new RuntimeException(e);
    }
}
@pgaschuetz
Copy link
Author

Sorry, here's a version that calls the correct constructor

@Override
@SuppressWarnings("unchecked")
public P createPresenter() {

    ParameterizedType thisType = (ParameterizedType) getClass().getGenericSuperclass();

    Class<V> viewClazz = (Class<V>) thisType.getActualTypeArguments()[0];
    Class<P> presenterClazz = (Class<P>) thisType.getActualTypeArguments()[1];

    try {
        Constructor<?> ctor = presenterClazz.getConstructor(viewClazz);
        return (P) ctor.newInstance(this);
    } catch (NoSuchMethodException e) {
        throw new UnsupportedOperationException(
                "No default constructor for Presenter " + presenterClazz.getSimpleName() + " available.");
    }
    catch (Exception e) {
        throw new RuntimeException(e);
    }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant