Search in sources :

Example 11 with Mvp4gAnnotationException

use of com.mvp4g.rebind.exception.loader.Mvp4gAnnotationException in project mvp4g by mvp4g.

the class EventsAnnotationsLoaderTest method testStartNoView.

@Test(expected = Mvp4gAnnotationException.class)
public void testStartNoView() throws Mvp4gAnnotationException {
    try {
        List<JClassType> annotedClasses = new ArrayList<JClassType>();
        JClassType type = oracle.addClass(Events.SimpleEventBus.class);
        annotedClasses.add(type);
        loader.load(annotedClasses, configuration);
    } catch (Mvp4gAnnotationException e) {
        assertTrue(e.getMessage().contains("There is no instance of"));
        throw e;
    }
}
Also used : JClassType(com.google.gwt.core.ext.typeinfo.JClassType) Events(com.mvp4g.rebind.test_tools.annotation.Events) Mvp4gAnnotationException(com.mvp4g.rebind.exception.loader.Mvp4gAnnotationException) Test(org.junit.Test)

Example 12 with Mvp4gAnnotationException

use of com.mvp4g.rebind.exception.loader.Mvp4gAnnotationException in project mvp4g by mvp4g.

the class EventsAnnotationsLoaderTest method testDoubleForward.

@Test(expected = Mvp4gAnnotationException.class)
public void testDoubleForward() throws Mvp4gAnnotationException {
    try {
        List<JClassType> annotedClasses = new ArrayList<JClassType>();
        annotedClasses.add(oracle.addClass(PresenterWithName.class));
        new PresenterAnnotationsLoader().load(annotedClasses, configuration);
        annotedClasses.clear();
        JClassType type = oracle.addClass(Events.EventBusDoubleForward.class);
        annotedClasses.add(type);
        loader.load(annotedClasses, configuration);
    } catch (Mvp4gAnnotationException e) {
        assertTrue(e.getMessage().contains("Duplicate value for Forward event. It is already defined by another method."));
        throw e;
    }
}
Also used : JClassType(com.google.gwt.core.ext.typeinfo.JClassType) PresenterWithName(com.mvp4g.rebind.test_tools.annotation.presenters.PresenterWithName) Events(com.mvp4g.rebind.test_tools.annotation.Events) Mvp4gAnnotationException(com.mvp4g.rebind.exception.loader.Mvp4gAnnotationException) Test(org.junit.Test)

Example 13 with Mvp4gAnnotationException

use of com.mvp4g.rebind.exception.loader.Mvp4gAnnotationException in project mvp4g by mvp4g.

the class Mvp4gAnnotationsWithServiceLoader method loadElement.

/*
   * (non-Javadoc)
   *
   * @see com.mvp4g.rebind.config.loader.annotation.Mvp4gAnnotationsLoader#loadElement
   * (com.google.gwt .core.ext.typeinfo.JClassType, java.lang.annotation.Annotation,
   * com.mvp4g.rebind.config.Mvp4gConfiguration)
   */
@Override
protected void loadElement(JClassType c, T annotation, Mvp4gConfiguration configuration) throws Mvp4gAnnotationException {
    Mvp4gWithServicesElement element = loadElementWithServices(c, annotation, configuration);
    // check if any services need to be injected
    JParameter[] params = null;
    String className = null;
    String serviceName = null;
    InjectService serviceAnnotation = null;
    for (JMethod m : c.getOverridableMethods()) {
        serviceAnnotation = m.getAnnotation(InjectService.class);
        if (serviceAnnotation != null) {
            if (!m.isPublic()) {
                String err = "Only public setter method can be used to inject a service.";
                throw new Mvp4gAnnotationException(c.getQualifiedSourceName(), m.getName(), err);
            }
            params = m.getParameters();
            if (params.length != 1) {
                String err = "Only setter method with one parameter can be used to inject a service";
                throw new Mvp4gAnnotationException(c.getQualifiedSourceName(), m.getName(), err);
            }
            serviceName = serviceAnnotation.serviceName();
            if ((serviceName == null) || (serviceName.length() == 0)) {
                className = params[0].getType().getQualifiedSourceName();
                serviceName = getServiceName(configuration, className);
            }
            element.getInjectedServices().add(new InjectedElement(serviceName, m.getName()));
        }
    }
}
Also used : InjectedElement(com.mvp4g.rebind.config.element.InjectedElement) JParameter(com.google.gwt.core.ext.typeinfo.JParameter) Mvp4gAnnotationException(com.mvp4g.rebind.exception.loader.Mvp4gAnnotationException) Mvp4gWithServicesElement(com.mvp4g.rebind.config.element.Mvp4gWithServicesElement) JMethod(com.google.gwt.core.ext.typeinfo.JMethod) InjectService(com.mvp4g.client.annotation.InjectService)

Example 14 with Mvp4gAnnotationException

use of com.mvp4g.rebind.exception.loader.Mvp4gAnnotationException in project mvp4g by mvp4g.

the class EventsAnnotationsLoader method loadEvents.

/**
 * Load events defined by this class
 *
 * @param c             annoted class
 * @param annotation    annotation of the class
 * @param configuration configuration containing loaded elements of the application
 * @throws Mvp4gAnnotationException if events are properly described
 */
private void loadEvents(JClassType c, Events annotation, Mvp4gConfiguration configuration) throws Mvp4gAnnotationException {
    Event event = null;
    EventElement element = null;
    Set<EventElement> events = configuration.getEvents();
    JParameter[] params = null;
    JClassType eventBusWithLookupType = configuration.getOracle().findType(EventBusWithLookup.class.getCanonicalName());
    JClassType eventBusType = configuration.getOracle().findType(EventBus.class.getCanonicalName());
    JClassType enclosingType = null;
    String historyName;
    Class<?> broadcast;
    for (JMethod method : c.getOverridableMethods()) {
        event = method.getAnnotation(Event.class);
        if (event == null) {
            enclosingType = method.getEnclosingType();
            if (!(eventBusType.equals(enclosingType) || (eventBusWithLookupType.equals(enclosingType)))) {
                String err = Event.class.getSimpleName() + " annotation missing.";
                throw new Mvp4gAnnotationException(c.getQualifiedSourceName(), method.getName(), err);
            }
            // in this case, it's a method by Mvp4g EventBus interface, no need to create an event
            continue;
        }
        params = method.getParameters();
        int nbParams = params.length;
        String[] paramClasses;
        if (nbParams > 0) {
            paramClasses = new String[nbParams];
            for (int i = 0; i < nbParams; i++) {
                paramClasses[i] = params[i].getType().getQualifiedSourceName();
            }
        } else {
            paramClasses = null;
        }
        historyName = event.name();
        element = new EventElement();
        element.setType(method.getName());
        element.setHandlers(buildPresentersAndEventHandlers(c, method, event.handlers(), event.handlerNames(), configuration));
        element.setBinds(buildPresentersAndEventHandlers(c, method, event.bind(), event.bindNames(), configuration));
        element.setCalledMethod(event.calledMethod());
        element.setForwardToModules(buildChildModules(c, method, event, configuration));
        element.setForwardToParent(Boolean.toString(event.forwardToParent()));
        element.setActivate(buildPresentersAndEventHandlers(c, method, event.activate(), event.activateNames(), configuration));
        element.setDeactivate(buildPresentersAndEventHandlers(c, method, event.deactivate(), event.deactivateNames(), configuration));
        element.setGenerate(buildPresentersAndEventHandlers(c, method, event.generate(), event.generateNames(), configuration));
        element.setNavigationEvent(Boolean.toString(event.navigationEvent()));
        element.setWithTokenGeneration(Boolean.toString(method.getReturnType().getQualifiedSourceName().equals(String.class.getName())));
        element.setPassive(Boolean.toString(event.passive()));
        broadcast = event.broadcastTo();
        if (!Event.NoBroadcast.class.equals(broadcast)) {
            element.setBroadcastTo(broadcast.getCanonicalName());
        }
        if (paramClasses != null) {
            element.setEventObjectClass(paramClasses);
        }
        if (!Event.DEFAULT_NAME.equals(historyName)) {
            element.setName(historyName);
        }
        addElement(events, element, c, method);
        if (method.getAnnotation(Start.class) != null) {
            if (configuration.getStart().getEventType() == null) {
                configuration.getStart().setEventType(method.getName());
            } else {
                String err = "Duplicate value for Start event. It is already defined by another method.";
                throw new Mvp4gAnnotationException(c.getQualifiedSourceName(), method.getName(), err);
            }
        }
        if (method.getAnnotation(Forward.class) != null) {
            if (configuration.getStart().getForwardEventType() == null) {
                configuration.getStart().setForwardEventType(method.getName());
            } else {
                String err = "Duplicate value for Forward event. It is already defined by another method.";
                throw new Mvp4gAnnotationException(c.getQualifiedSourceName(), method.getName(), err);
            }
        }
        loadHistoryEvent(c, method, configuration);
        loadHistory(c, method, event, element, configuration);
        loadEventToLoadChildModuleView(c, method, configuration);
        loadChildConfig(c, method, configuration);
    }
}
Also used : JClassType(com.google.gwt.core.ext.typeinfo.JClassType) JParameter(com.google.gwt.core.ext.typeinfo.JParameter) Mvp4gAnnotationException(com.mvp4g.rebind.exception.loader.Mvp4gAnnotationException) JMethod(com.google.gwt.core.ext.typeinfo.JMethod)

Example 15 with Mvp4gAnnotationException

use of com.mvp4g.rebind.exception.loader.Mvp4gAnnotationException in project mvp4g by mvp4g.

the class EventsAnnotationsLoader method loadStartView.

/**
 * Load information about the start view
 *
 * @param c             annoted class
 * @param annotation    Events annotation of the class
 * @param configuration configuration containing loaded elements of the application
 * @throws Mvp4gAnnotationException if no view with the given class and name exist
 */
private void loadStartView(JClassType c, Events annotation, Mvp4gConfiguration configuration) throws Mvp4gAnnotationException {
    Set<PresenterElement> presenters = configuration.getPresenters();
    String presenterName = annotation.startPresenterName();
    Class<?> presenterClass = annotation.startPresenter();
    boolean hasStartPresenter = !NoStartPresenter.class.equals(presenterClass);
    if (hasStartPresenter) {
        if ((presenterName != null) && (presenterName.length() > 0)) {
            boolean found = false;
            for (PresenterElement presenter : presenters) {
                if (presenterName.equals(presenter.getName())) {
                    TypeOracle oracle = configuration.getOracle();
                    JClassType presenterType = oracle.findType(presenter.getClassName());
                    JClassType startType = oracle.findType(presenterClass.getCanonicalName());
                    if (!presenterType.isAssignableTo(startType)) {
                        String err = "There is no instance with name " + presenterName + " that extends " + presenterType;
                        throw new Mvp4gAnnotationException(c.getQualifiedSourceName(), null, err);
                    }
                    found = true;
                    break;
                }
            }
            if (!found) {
                String err = "There is no presenter named " + presenterName;
                throw new Mvp4gAnnotationException(c.getQualifiedSourceName(), null, err);
            }
        } else {
            presenterName = getElementName(presenters, presenterClass.getCanonicalName());
            if (presenterName == null) {
                String err = "There is no instance of " + presenterClass.getCanonicalName() + ". Have you forgotten to annotate it with @Presenter or @EventHander?";
                throw new Mvp4gAnnotationException(c.getQualifiedSourceName(), null, err);
            }
        }
    }
    StartElement element = new StartElement();
    if (hasStartPresenter) {
        element.setPresenter(presenterName);
    }
    element.setHistory(Boolean.toString(annotation.historyOnStart()));
    configuration.setStart(element);
}
Also used : JClassType(com.google.gwt.core.ext.typeinfo.JClassType) TypeOracle(com.google.gwt.core.ext.typeinfo.TypeOracle) Mvp4gAnnotationException(com.mvp4g.rebind.exception.loader.Mvp4gAnnotationException) NoStartPresenter(com.mvp4g.client.presenter.NoStartPresenter)

Aggregations

Mvp4gAnnotationException (com.mvp4g.rebind.exception.loader.Mvp4gAnnotationException)29 JClassType (com.google.gwt.core.ext.typeinfo.JClassType)26 Test (org.junit.Test)25 Events (com.mvp4g.rebind.test_tools.annotation.Events)17 PresenterWithName (com.mvp4g.rebind.test_tools.annotation.presenters.PresenterWithName)12 ArrayList (java.util.ArrayList)5 JMethod (com.google.gwt.core.ext.typeinfo.JMethod)2 JParameter (com.google.gwt.core.ext.typeinfo.JParameter)2 SimplePresenter01 (com.mvp4g.rebind.test_tools.annotation.presenters.SimplePresenter01)2 TypeOracle (com.google.gwt.core.ext.typeinfo.TypeOracle)1 InjectService (com.mvp4g.client.annotation.InjectService)1 ChildModules (com.mvp4g.client.annotation.module.ChildModules)1 NoStartPresenter (com.mvp4g.client.presenter.NoStartPresenter)1 InjectedElement (com.mvp4g.rebind.config.element.InjectedElement)1 Mvp4gWithServicesElement (com.mvp4g.rebind.config.element.Mvp4gWithServicesElement)1 Modules (com.mvp4g.rebind.test_tools.Modules)1 EventHandlers (com.mvp4g.rebind.test_tools.annotation.EventHandlers)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1