Search in sources :

Example 1 with JParameter

use of com.google.gwt.core.ext.typeinfo.JParameter 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 2 with JParameter

use of com.google.gwt.core.ext.typeinfo.JParameter 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)

Aggregations

JMethod (com.google.gwt.core.ext.typeinfo.JMethod)2 JParameter (com.google.gwt.core.ext.typeinfo.JParameter)2 Mvp4gAnnotationException (com.mvp4g.rebind.exception.loader.Mvp4gAnnotationException)2 JClassType (com.google.gwt.core.ext.typeinfo.JClassType)1 InjectService (com.mvp4g.client.annotation.InjectService)1 InjectedElement (com.mvp4g.rebind.config.element.InjectedElement)1 Mvp4gWithServicesElement (com.mvp4g.rebind.config.element.Mvp4gWithServicesElement)1