Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add something like Jakarta @Observes #491

Merged
merged 23 commits into from
Apr 17, 2024
Merged

Conversation

SentryMan
Copy link
Collaborator

@SentryMan SentryMan commented Jan 27, 2024

  • Adds the Observer, Event<T>, and ObserverManager classes for registering observer methods and firing events.
  • Adds new @Observes annotation and generates code to register in DI classes
  • Adds an @EventType marker annotation that will auto-generate Event<T> implementations
  • like Jakarta EE, supports generic events, wildcards, and injecting beans when an event is fired.

Given the following two classes:

@EventType
class CustomEvent {
}

@Singleton
class TestObserver {
  void observe(@Observes @Named("qualified")  CustomEvent e) {}
}

Will generate these two classes:

@Component
@Generated("avaje-inject-generator")
public class CustomEventPublisher extends Event<CustomEvent> {
//for generic types and arrays the user will have to manually create this publisher though
  public CustomEventPublisher (ObserverManager manager) {
    super(manager.observers(CustomEvent.class));
  }
}

@Generated("io.avaje.inject.generator")
public final class TestObserver$DI  {

  /**
   * Create and register TestObserver.
   */
  public static void build(Builder builder) {
    if (builder.isAddBeanFor(TestObserver.class)) {
      var bean = new TestObserver();
      builder.register(bean);
      Consumer<CustomEvent> observe = bean::observe;
      builder.<CustomEvent>registerObserver(false, CustomEvent.class, observe, "qualified");
    }
  }
}

resolves #489

@SentryMan SentryMan requested a review from rbygrave January 27, 2024 05:00
@SentryMan SentryMan added the enhancement New feature or request label Jan 27, 2024
@SentryMan SentryMan self-assigned this Jan 27, 2024
@rbygrave rbygrave added this to the 10.0 milestone Apr 17, 2024
@rbygrave rbygrave merged commit b8749b0 into avaje:master Apr 17, 2024
4 checks passed
@SentryMan SentryMan deleted the observes branch April 17, 2024 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add something like @Observes from CDI
2 participants