com.google.web.bindery.event.shared.binder.GenericEvent Java Examples

The following examples show how to use com.google.web.bindery.event.shared.binder.GenericEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EventBinderGenerator.java    From gwteventbinder with Apache License 2.0 6 votes vote down vote up
@Override
public String generate(TreeLogger logger, GeneratorContext context,
    String typeName) throws UnableToCompleteException {
  try {
    JClassType eventBinderType = context.getTypeOracle().getType(typeName);
    JClassType targetType = getTargetType(eventBinderType, context.getTypeOracle());
    SourceWriter writer = createSourceWriter(logger, context, eventBinderType, targetType);
    if (writer != null) { // Otherwise the class was already created
      new EventBinderWriter(
          logger,
          context.getTypeOracle().getType(GenericEvent.class.getCanonicalName()))
              .writeDoBindEventHandlers(targetType, writer, context.getTypeOracle());
      writer.commit(logger);
    }
    return getFullyQualifiedGeneratedClassName(eventBinderType);
  } catch (NotFoundException e) {
    logger.log(Type.ERROR, "Error generating " + typeName, e);
    throw new UnableToCompleteException();
  }
}
 
Example #2
Source File: EventBinderGenerator.java    From gwteventbinder with Apache License 2.0 6 votes vote down vote up
private SourceWriter createSourceWriter(
    TreeLogger logger,
    GeneratorContext context,
    JClassType eventBinderType,
    JClassType targetType) {
  String simpleName = getSimpleGeneratedClassName(eventBinderType);
  String packageName = eventBinderType.getPackage().getName();
  ClassSourceFileComposerFactory composer =
      new ClassSourceFileComposerFactory(packageName, simpleName);

  composer.setSuperclass(AbstractEventBinder.class.getCanonicalName()
      + "<" + targetType.getQualifiedSourceName() + ">");
  composer.addImplementedInterface(eventBinderType.getName());

  composer.addImport(EventBinder.class.getCanonicalName());
  composer.addImport(EventBus.class.getCanonicalName());
  composer.addImport(GenericEvent.class.getCanonicalName());
  composer.addImport(GenericEventHandler.class.getCanonicalName());
  composer.addImport(HandlerRegistration.class.getCanonicalName());
  composer.addImport(LinkedList.class.getCanonicalName());
  composer.addImport(List.class.getCanonicalName());

  PrintWriter printWriter = context.tryCreate(logger, packageName, simpleName);
  return (printWriter != null) ? composer.createSourceWriter(context, printWriter) : null;
}
 
Example #3
Source File: EventBinderWriterTest.java    From gwteventbinder with Apache License 2.0 6 votes vote down vote up
private TypeOracle createTypeOracle() {
  TypeOracle typeOracle = mock(TypeOracle.class);
  when(typeOracle.findType(anyString())).then(new Answer<JClassType>() {
    @Override
    public JClassType answer(InvocationOnMock invocationOnMock) throws Throwable {
      String parameter = (String) invocationOnMock.getArguments()[0];
      Class<? extends GenericEvent> klass;
      try {
        klass = (Class<? extends GenericEvent>) Class.forName(parameter);
      } catch (ClassNotFoundException ex) {
        char[] klassName = parameter.toCharArray();
        klassName[parameter.lastIndexOf('.')] = '$';
        klass = (Class<? extends GenericEvent>) Class.forName(String.valueOf(klassName));
      }
      return getEventType(klass);
    }
  });
  return typeOracle;
}
 
Example #4
Source File: AbstractEventBinder.java    From gwteventbinder with Apache License 2.0 5 votes vote down vote up
/**
 * Registers the given handler for the given event class on the given event bus. Factored out
 * into a method here instead of generated directly in order to simplify the generated code and
 * save a little space.
 */
protected final <U extends GenericEvent> void bind(
    EventBus eventBus,
    List<HandlerRegistration> registrations,
    Class<U> type,
    GenericEventHandler handler) {
  registrations.add(eventBus.addHandler(GenericEventType.getTypeOf(type), handler));
}
 
Example #5
Source File: GenericEventType.java    From gwteventbinder with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new EventType for the given event class. Repeated invocations of
 * this method for the same type will return the same object. This method is
 * called by generated {@link EventBinder}s and shouldn't normally have to be
 * called directly by users.
 */
public static <T extends GenericEvent> GenericEventType getTypeOf(Class<T> clazz) {
  
  GenericEventType eventType = TYPE_MAP.get(clazz);
  
  if (eventType == null) {
    eventType = new GenericEventType();
    TYPE_MAP.put(clazz, eventType);
  }
  
  return eventType;
}
 
Example #6
Source File: EventBinderWriterTest.java    From gwteventbinder with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
  eventTypes = new HashMap<Class<? extends GenericEvent>, JClassType>();

  typeOracle = createTypeOracle();
  genericEventType = getEventType(GenericEvent.class);
  writer = new EventBinderWriter(logger, genericEventType);
  output = new StringSourceWriter();
}
 
Example #7
Source File: EventBinderWriterTest.java    From gwteventbinder with Apache License 2.0 5 votes vote down vote up
private JClassType getEventType(Class<? extends GenericEvent> event) {
  if (eventTypes.containsKey(event)) {
    return eventTypes.get(event);
  }
  JClassType type = mock(JClassType.class);
  eventTypes.put(event, type);

  when(type.isClassOrInterface()).thenReturn(type);
  when(type.isAssignableTo(getEventType(GenericEvent.class))).thenReturn(true);
  when(type.getOracle()).thenReturn(typeOracle);
  when(type.getQualifiedSourceName()).thenReturn(className(event));
  return type;
}
 
Example #8
Source File: EventBinderWriter.java    From gwteventbinder with Apache License 2.0 4 votes vote down vote up
private void writeHandlerForBindMethod(EventHandler annotation, SourceWriter writer,
    JMethod method, TypeOracle typeOracle) throws UnableToCompleteException {
  JClassType eventParameter = null;
  if (method.getParameterTypes().length == 1) {
    eventParameter = method.getParameterTypes()[0].isClassOrInterface();
  }
  if (annotation.handles().length == 0 && !isAConcreteGenericEvent(eventParameter)) {
    logger.log(Type.ERROR, "Method " + method.getName()
        + " annotated with @EventHandler without event classes must have exactly "
        + "one argument of a concrete type assignable to GenericEvent");
    throw new UnableToCompleteException();
  }

  List<String> eventTypes = new ArrayList<String>();
  if (annotation.handles().length != 0) {
    for (Class<? extends GenericEvent> event : annotation.handles()) {
      String eventTypeName = event.getCanonicalName();
      JClassType eventClassType = typeOracle.findType(eventTypeName);
      if (eventClassType == null) {
        logger.log(Type.ERROR, "Can't resolve " + eventTypeName);
        throw new UnableToCompleteException();
      }
      if (eventParameter != null && !eventClassType.isAssignableTo(eventParameter)) {
        logger.log(Type.ERROR, "Event " + eventTypeName + " isn't assignable to "
            + eventParameter.getName() + " in method: " + method.getName());
        throw new UnableToCompleteException();
      }
      eventTypes.add(eventClassType.getQualifiedSourceName());
    }
  } else {
    eventTypes.add(eventParameter.getQualifiedSourceName());
  }

  for (String eventType : eventTypes) {
    writer.println("bind(eventBus, registrations, %s.class, new GenericEventHandler() {",
        eventType);
    if (eventParameter != null) {
      writer.indentln("public void handleEvent(GenericEvent event) { target.%s((%s) event); }",
          method.getName(), eventType);
    } else {
      writer.indentln("public void handleEvent(GenericEvent event) { target.%s(); }", 
          method.getName());
    }
    writer.println("});");
  }
}
 
Example #9
Source File: EventBinderWriterTest.java    From gwteventbinder with Apache License 2.0 4 votes vote down vote up
private String className(Class<? extends GenericEvent> event) {
  return event.getName().replace('$', '.');
}
 
Example #10
Source File: GenericEventHandler.java    From gwteventbinder with Apache License 2.0 votes vote down vote up
void handleEvent(GenericEvent event);