Repository: avaje/avaje-inject
Pull request #558 (2024-05-02 11:57:14)

Fix escape quotes on Event with qualifiers with string members (otherwise compiler error)
Quotes need to be escaped 


Patch:
diff --git a/inject-generator/src/main/java/io/avaje/inject/generator/TypeReader.java b/inject-generator/src/main/java/io/avaje/inject/generator/TypeReader.java
index 17bbc3a8..de8d2018 100644
--- a/inject-generator/src/main/java/io/avaje/inject/generator/TypeReader.java
+++ b/inject-generator/src/main/java/io/avaje/inject/generator/TypeReader.java
@@ -110,7 +110,7 @@ final class TypeReader {
   String name() {
     NamedPrism named = NamedPrism.getInstanceOn(beanType);
     if (named != null) {
-      return named.value();
+      return named.value().replace("\"", "\\\"");
     }
     if (annotationReader.hasQualifierName()) {
       return annotationReader.qualifierName();
diff --git a/inject-generator/src/main/java/io/avaje/inject/generator/Util.java b/inject-generator/src/main/java/io/avaje/inject/generator/Util.java
index b3127b30..1ab8810d 100644
--- a/inject-generator/src/main/java/io/avaje/inject/generator/Util.java
+++ b/inject-generator/src/main/java/io/avaje/inject/generator/Util.java
@@ -284,7 +284,7 @@ final class Util {
   static String getNamed(Element p) {
     final NamedPrism named = NamedPrism.getInstanceOn(p);
     if (named != null) {
-      return named.value();
+      return named.value().replace("\"", "\\\"");
     }
     for (final AnnotationMirror annotationMirror : p.getAnnotationMirrors()) {
       final DeclaredType annotationType = annotationMirror.getAnnotationType();


Test patch:
diff --git a/inject-generator/src/test/java/io/avaje/inject/generator/models/valid/observes/EventSender.java b/inject-generator/src/test/java/io/avaje/inject/generator/models/valid/observes/EventSender.java
index d22c8299..a3fcd743 100644
--- a/inject-generator/src/test/java/io/avaje/inject/generator/models/valid/observes/EventSender.java
+++ b/inject-generator/src/test/java/io/avaje/inject/generator/models/valid/observes/EventSender.java
@@ -4,6 +4,8 @@ import java.util.List;
 
 import io.avaje.inject.event.Event;
 import io.avaje.inject.spi.GenericType;
+import io.avaje.inject.generator.models.valid.qualifier.TempQualifier;
+import io.avaje.inject.generator.models.valid.qualifier.TempQualifier.Scale;
 import jakarta.inject.Inject;
 import jakarta.inject.Named;
 import jakarta.inject.Singleton;
@@ -17,7 +19,7 @@ public class EventSender {
   public EventSender(
       @Named("same") Event<CustomEvent> event,
       @Named("same") Event<CustomEvent> dup,
-      @Named("different") Event<CustomEvent> celsius) {
+      @TempQualifier(value = Scale.CELSIUS, someOtherString = "celsi") Event<CustomEvent> celsius) {
     this.event = event;
   }
 
diff --git a/inject-test/src/test/java/org/example/observes/EventSender2.java b/inject-test/src/test/java/org/example/observes/EventSender2.java
new file mode 100644
index 00000000..64bc8243
--- /dev/null
+++ b/inject-test/src/test/java/org/example/observes/EventSender2.java
@@ -0,0 +1,13 @@
+package org.example.observes;
+
+import io.avaje.inject.Component;
+import io.avaje.inject.event.Event;
+import jakarta.inject.Inject;
+
+@Component
+public class EventSender2 {
+
+  @Inject
+  @StrQualifier(value = "foo")
+  Event<CustomEvent> event;
+}
diff --git a/inject-test/src/test/java/org/example/observes/MyObserver2.java b/inject-test/src/test/java/org/example/observes/MyObserver2.java
new file mode 100644
index 00000000..67bf5376
--- /dev/null
+++ b/inject-test/src/test/java/org/example/observes/MyObserver2.java
@@ -0,0 +1,16 @@
+package org.example.observes;
+
+import io.avaje.inject.event.Observes;
+import jakarta.inject.Singleton;
+
+@Singleton
+public class MyObserver2 {
+
+  boolean invoked = false;
+  CustomEvent event;
+
+  void observe(@Observes @StrQualifier("foo") CustomEvent e) {
+    invoked = true;
+    event = e;
+  }
+}
diff --git a/inject-test/src/test/java/org/example/observes/MyStrQualifiedObserver.java b/inject-test/src/test/java/org/example/observes/MyStrQualifiedObserver.java
new file mode 100644
index 00000000..25cda79f
--- /dev/null
+++ b/inject-test/src/test/java/org/example/observes/MyStrQualifiedObserver.java
@@ -0,0 +1,21 @@
+package org.example.observes;
+
+import io.avaje.inject.event.Observes;
+import jakarta.inject.Singleton;
+
+@Singleton
+public class MyStrQualifiedObserver {
+
+  boolean invoked = false;
+  CustomEvent event;
+
+  void observe(@Observes @StrQualifier("foo") CustomEvent e) {
+    invoked = true;
+    event = e;
+  }
+
+  void clear() {
+    invoked = false;
+    event = null;
+  }
+}
diff --git a/inject-test/src/test/java/org/example/observes/StrQualifier.java b/inject-test/src/test/java/org/example/observes/StrQualifier.java
new file mode 100644
index 00000000..29a9035d
--- /dev/null
+++ b/inject-test/src/test/java/org/example/observes/StrQualifier.java
@@ -0,0 +1,13 @@
+package org.example.observes;
+
+import jakarta.inject.Qualifier;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+@Qualifier
+@Retention(RetentionPolicy.RUNTIME)
+public @interface StrQualifier {
+
+  String value();
+}
diff --git a/inject-test/src/test/java/org/example/observes/TestEventMessaging.java b/inject-test/src/test/java/org/example/observes/TestEventMessaging.java
index a6617d1a..cc6167ed 100644
--- a/inject-test/src/test/java/org/example/observes/TestEventMessaging.java
+++ b/inject-test/src/test/java/org/example/observes/TestEventMessaging.java
@@ -2,6 +2,7 @@ package org.example.observes;
 
 import static org.assertj.core.api.Assertions.assertThat;
 
+import jakarta.inject.Named;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
@@ -15,7 +16,11 @@ class TestEventMessaging {
   @Inject MyObserver observer;
   @Inject MyQualifiedObserver qualifiedObserver;
   @Inject MyObserverInjected observerInjected;
+  @Inject MyStrQualifiedObserver strQualifiedObserver;
   @Inject Event<CustomEvent> event;
+  @Inject EventSender2 sender2;
+  // @Inject @StrQualifier(value = "foo") Event<CustomEvent> strEvent; Bug: This is not supported yet.
+  @Inject @Named("StrQualifier(value=\"foo\")") Event<CustomEvent> strEvent;
 
   @BeforeEach
   void before() {
@@ -53,4 +58,19 @@ class TestEventMessaging {
     assertThat(observerInjected.invoked).isFalse();
     assertThat(observerInjected.event).isNull();
   }
+
+  @Test
+  void testStringQualifiedEvent() {
+    var message = new CustomEvent("hi!");
+
+    sender2.event.fire(message);
+    assertThat(strQualifiedObserver.invoked).isTrue();
+    assertThat(strQualifiedObserver.event).isSameAs(message);
+
+    strQualifiedObserver.clear();
+    strEvent.fire(message);
+
+    assertThat(strQualifiedObserver.invoked).isTrue();
+    assertThat(strQualifiedObserver.event).isSameAs(message);
+  }
 }