Example usage for com.google.gwt.core.ext.typeinfo JParameterizedType findMethod

List of usage examples for com.google.gwt.core.ext.typeinfo JParameterizedType findMethod

Introduction

In this page you can find the example usage for com.google.gwt.core.ext.typeinfo JParameterizedType findMethod.

Prototype

JMethod findMethod(String name, JType[] paramTypes);

Source Link

Usage

From source file:com.mvp4g.rebind.config.Mvp4gConfiguration.java

License:Apache License

/**
 * Checks that all event handler names correspond to a configured mvp4g element. Verify that
 * these elements are valid. Remove the ones that don't handle events or aren't associated with
 * the start view.</p>// w  ww  .j  a  va2  s . c o m
 *
 * @throws InvalidMvp4gConfigurationException
 */
void validateEventHandlers() throws InvalidMvp4gConfigurationException {

    Map<String, List<EventElement>> presenterAndEventHandlerMap = new HashMap<String, List<EventElement>>();
    Map<String, List<EventElement>> activateMap = new HashMap<String, List<EventElement>>();
    Map<String, List<EventElement>> deactivateMap = new HashMap<String, List<EventElement>>();
    Map<String, List<EventElement>> generateMap = new HashMap<String, List<EventElement>>();
    // binds map validates in the same rules as presenters and event
    // handlers and added to the same map
    Map<JClassType, List<EventElement>> broadcastMap = new HashMap<JClassType, List<EventElement>>();

    // Add presenter that handles event
    List<EventElement> eventList;
    List<String> activates, deactivates, handlers, binds, generates;
    String broadcast;
    JClassType type;
    for (EventElement event : events) {
        handlers = event.getHandlers();
        activates = event.getActivate();
        deactivates = event.getDeactivate();
        broadcast = event.getBroadcastTo();
        generates = event.getGenerate();
        binds = event.getBinds();
        if (handlers != null) {
            for (String handler : handlers) {
                // checking that we don't have the same handler in the binds
                // and in the handlers annotation
                if (binds != null && binds.contains(handler)) {
                    throw new InvalidMvp4gConfigurationException(
                            String.format(BIND_AND_HANDLER_FOR_SAME_EVENT, event.getType(), handler, handler));
                }
                eventList = presenterAndEventHandlerMap.get(handler);
                if (eventList == null) {
                    eventList = new ArrayList<EventElement>();
                    presenterAndEventHandlerMap.put(handler, eventList);
                }
                eventList.add(event);
            }
        }
        if (activates != null) {

            for (String activate : activates) {
                eventList = presenterAndEventHandlerMap.get(activate);
                if ((deactivates != null) && (deactivates.contains(activate))) {
                    throw new InvalidMvp4gConfigurationException(
                            String.format(ACTIVATE_DEACTIVATE_SAME_TIME, event, activate));
                }

                if (eventList == null) {
                    eventList = new ArrayList<EventElement>();
                    activateMap.put(activate, eventList);
                }
                eventList.add(event);
            }
        }
        if (deactivates != null) {
            for (String deactivate : deactivates) {
                eventList = presenterAndEventHandlerMap.get(deactivate);
                if (eventList == null) {
                    eventList = new ArrayList<EventElement>();
                    deactivateMap.put(deactivate, eventList);
                }
                eventList.add(event);
            }
        }
        if (broadcast != null) {
            type = getType(event, broadcast);
            eventList = broadcastMap.get(type);
            if (eventList == null) {
                eventList = new ArrayList<EventElement>();
                broadcastMap.put(type, eventList);
            }
            eventList.add(event);
        }
        if (generates != null) {
            for (String generate : generates) {
                eventList = generateMap.get(generate);
                if (eventList == null) {
                    eventList = new ArrayList<EventElement>();
                    generateMap.put(generate, eventList);
                }
                eventList.add(event);
            }
        }
        // we are handling two exceptions with binds, if the event is
        // passive and it has binds elements
        if (event.isPassive() && (binds != null) && (binds.size() > 0)) {
            throw new InvalidMvp4gConfigurationException(
                    String.format(BIND_FOR_PASSIVE_EVENT, event.getName()));
        }
        // create list of binds to ensure that presenter will not be deleted
        // later
        if (binds != null) {
            for (String bind : binds) {
                eventList = presenterAndEventHandlerMap.get(bind);
                if (eventList == null) {
                    eventList = new ArrayList<EventElement>();
                    presenterAndEventHandlerMap.put(bind, eventList);
                }
                eventList.add(event);
            }
        }
    }

    boolean hasStartView = start.hasPresenter();
    String startPresenter = start.getPresenter();
    JGenericType presenterGenType = getType(null, PresenterInterface.class.getCanonicalName()).isGenericType();
    JGenericType eventHandlerGenType = getType(null, EventHandlerInterface.class.getCanonicalName())
            .isGenericType();
    JGenericType reverseViewGenType = getType(null, ReverseViewInterface.class.getCanonicalName())
            .isGenericType();
    JClassType eventBusType = getType(null, eventBus.getInterfaceClassName());
    JType[] noParam = new JType[0];
    JClassType presenterType, viewParam, presenterParam, viewType;
    ViewElement view = null;
    JParameterizedType genPresenter, genView;
    boolean hasPossibleBroadcast = (broadcastMap.size() > 0);
    Set<PresenterElement> toRemove = new HashSet<PresenterElement>();
    String viewName, name;
    boolean toKeep, notDirectHandler;
    List<EventElement> eventDeactivateList, eventActivateList, eventGenerateList;
    for (PresenterElement presenter : presenters) {
        name = presenter.getName();
        eventList = presenterAndEventHandlerMap.remove(name);// binds presenters are checking as handlers
        eventDeactivateList = deactivateMap.remove(name);
        eventActivateList = activateMap.remove(name);
        eventGenerateList = generateMap.remove(name);
        viewName = presenter.getView();

        toKeep = (eventList != null) || (hasStartView && name.equals(startPresenter))
                || (eventGenerateList != null);
        notDirectHandler = !toKeep && (presenter.isMultiple() || hasPossibleBroadcast);
        if (toKeep || notDirectHandler) {
            toKeep = controlEventBus(presenter, eventHandlerGenType, eventBusType, toKeep);
            if (toKeep) {

                presenterType = getType(presenter, presenter.getClassName());

                toKeep = findPossibleBroadcast(broadcastMap, presenter, presenterType) || !notDirectHandler
                        || presenter.isMultiple();

                if (toKeep) {
                    genPresenter = presenterType.asParameterizationOf(presenterGenType);
                    if (genPresenter == null) {
                        throw new InvalidClassException(presenter, PresenterInterface.class.getCanonicalName());
                    }

                    viewParam = (JClassType) genPresenter.findMethod("getView", noParam).getReturnType();

                    // Control if view injected to the event bus is
                    // compatible with
                    // presenter view type
                    view = getElement(viewName, views, presenter);
                    viewType = getType(view, view.getClassName());
                    if (!viewType.isAssignableTo(viewParam)) {
                        throw new InvalidTypeException(presenter, "View", view.getClassName(),
                                viewParam.getQualifiedSourceName());
                    }

                    if (viewType.isAssignableTo(reverseViewGenType)) {
                        genView = viewType.asParameterizationOf(reverseViewGenType);
                        presenterParam = (JClassType) genView.findMethod("getPresenter", noParam)
                                .getReturnType();
                        if (!presenterType.isAssignableTo(presenterParam)) {
                            throw new InvalidTypeException(view, "Presenter", presenter.getClassName(),
                                    presenterParam.getQualifiedSourceName());
                        }
                        presenter.setInverseView(Boolean.TRUE.toString());

                    }

                    if (!presenter.isMultiple() && !presenter.isAsync()) {
                        view.setInstantiateAtStart(true);
                    }
                }
            }

        }

        if (!toKeep) {
            removeFromActivateDeactivate(eventActivateList, eventDeactivateList, presenter);
            // this object is not used, you can remove it
            toRemove.add(presenter);
        }

        if ((eventGenerateList != null) && (!presenter.isMultiple())) {
            throw new InvalidMvp4gConfigurationException(String.format(GENERATE_NOT_MULTIPLE,
                    eventGenerateList.get(0).getType(), presenter.getName()));
        }
    }

    Set<EventHandlerElement> toRemoveEventHandlers = new HashSet<EventHandlerElement>();
    for (EventHandlerElement eventHandler : eventHandlers) {
        name = eventHandler.getName();
        eventList = presenterAndEventHandlerMap.remove(name);
        eventDeactivateList = deactivateMap.remove(name);
        eventActivateList = activateMap.remove(name);
        eventGenerateList = generateMap.remove(name);

        toKeep = (eventList != null) || (eventGenerateList != null);
        notDirectHandler = !toKeep && (eventHandler.isMultiple() || hasPossibleBroadcast);
        if (toKeep || notDirectHandler) {
            toKeep = controlEventBus(eventHandler, eventHandlerGenType, eventBusType, toKeep);
            if (toKeep) {
                toKeep = findPossibleBroadcast(broadcastMap, eventHandler,
                        getType(eventHandler, eventHandler.getClassName())) || !notDirectHandler
                        || eventHandler.isMultiple();
            }
        }

        if (!toKeep) {
            removeFromActivateDeactivate(eventActivateList, eventDeactivateList, eventHandler);
            // this object is not used, you can remove it
            toRemoveEventHandlers.add(eventHandler);
        }

        if ((eventGenerateList != null) && (!eventHandler.isMultiple())) {
            throw new InvalidMvp4gConfigurationException(String.format(GENERATE_NOT_MULTIPLE,
                    eventGenerateList.get(0).getType(), eventHandler.getName()));
        }
    }

    // Missing handlers
    if (!presenterAndEventHandlerMap.isEmpty()) {
        String it = presenterAndEventHandlerMap.keySet().iterator().next();
        throw new UnknownConfigurationElementException(presenterAndEventHandlerMap.get(it).get(0), it);
    }
    if (!activateMap.isEmpty()) {
        String it = activateMap.keySet().iterator().next();
        throw new UnknownConfigurationElementException(activateMap.get(it).get(0), it);
    }

    if (!deactivateMap.isEmpty()) {
        String it = deactivateMap.keySet().iterator().next();
        throw new UnknownConfigurationElementException(deactivateMap.get(it).get(0), it);
    }
    if (!generateMap.isEmpty()) {
        String it = generateMap.keySet().iterator().next();
        throw new UnknownConfigurationElementException(generateMap.get(it).get(0), it);
    }

    removeUselessElements(presenters, toRemove);
    removeUselessElements(eventHandlers, toRemoveEventHandlers);
}

From source file:com.mvp4g.rebind.config.Mvp4gConfiguration.java

License:Apache License

/**
 * Control if the event bus of the handler is compatible with the module event bus.
 * <p/>//from w ww .  jav a  2  s . co  m
 * If the handler event bus is not compatible and is not multiple, then throw an exception.
 * <p/>
 * If the handler is multiple, then it's an handler of another module, just ignore it by
 * returning false
 *
 * @param eventHandler
 * @param eventHandlerGenType
 * @param eventBusType
 * @return true if no error, false if the presenter has to be ignored.
 * @throws InvalidClassException  throws if the handler event bus is not compatible
 * @throws InvalidTypeException   throws if the handler event bus is not compatible
 * @throws NotFoundClassException throws if the handler event bus is not compatible
 */
private boolean controlEventBus(EventHandlerElement eventHandler, JGenericType eventHandlerGenType,
        JClassType eventBusType, boolean directHandler)
        throws InvalidClassException, InvalidTypeException, NotFoundClassException {

    JClassType eventHandlerType = getType(eventHandler, eventHandler.getClassName());
    JParameterizedType genEventHandler = eventHandlerType.asParameterizationOf(eventHandlerGenType);
    if (genEventHandler == null) {
        if (directHandler) {
            throw new InvalidClassException(eventHandler, EventHandlerInterface.class.getCanonicalName());
        } else {
            return false;
        }
    }

    JClassType eventBusParam = (JClassType) genEventHandler.findMethod("getEventBus", new JType[0])
            .getReturnType();

    // Control if presenter event bus is compatible with module
    // event bus
    if (!eventBusType.isAssignableTo(eventBusParam)) {
        if (directHandler) {
            throw new InvalidTypeException(eventHandler, "Event Bus", eventBus.getInterfaceClassName(),
                    eventBusParam.getQualifiedSourceName());
        } else {
            return false;
        }
    }

    return true;
}