Repository: avaje/avaje-inject (java)

Issue:
Using dashes in @Named results in "not a statement" compiler error
Example:
```java
@Factory
public class UserMaker {
    @Bean @Named("User-Id")
    String userId() {
        return "bla bla";
    }
}
```

```java
@DependencyMeta(type="java.lang.String", name="user-id", method="com...UserMaker$DI.build_userId", provides={"java.io.Serializable","java.lang.Comparable<java.lang.String>","java.lang.CharSequence","java.lang.constant.Constable","java.lang.constant.ConstantDesc"}, dependsOn={"com...UserMaker"}, autoProvides="java.io.Serializable")
  private void build_lang_String_user-id() {
    UserMaker$DI.build_userId(builder);
  }
```

Should avaje maybe normalize invalid characters into underscores?

Regards

Pull request description:
Normalize Invalid @Named characters
fixes #400 

Patch:
diff --git a/inject-generator/src/main/java/io/avaje/inject/generator/MetaData.java b/inject-generator/src/main/java/io/avaje/inject/generator/MetaData.java
index 7237046d..54274fbe 100644
--- a/inject-generator/src/main/java/io/avaje/inject/generator/MetaData.java
+++ b/inject-generator/src/main/java/io/avaje/inject/generator/MetaData.java
@@ -37,7 +37,7 @@ final class MetaData {
 
   private boolean generateProxy;
   private boolean usesExternalDependency;
-  private Set<String> externalDependencies = new HashSet<>();
+  private final Set<String> externalDependencies = new HashSet<>();
   private boolean importedComponent;
 
   MetaData(DependencyMetaPrism meta) {
@@ -83,7 +83,7 @@ final class MetaData {
     } else {
       final String trimType = Util.trimMethod(type);
       if (name != null) {
-        return trimType + "_" + name;
+        return trimType + "_" + name.replaceAll("[^a-zA-Z0-9_$]+", "_");
       } else {
         return trimType;
       }
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 4d6d1f28..1edcb12b 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
@@ -137,11 +137,18 @@ final class Util {
       var result = "";
       var foundClass = false;
       for (final String part : fullType.split("\\.")) {
-        if (foundClass || Character.isUpperCase(part.charAt(0))) {
+        char firstChar = part.charAt(0);
+        if (foundClass
+            || Character.isUpperCase(firstChar)
+            || (!Character.isAlphabetic(firstChar) && Character.isJavaIdentifierStart(firstChar))) {
           foundClass = true;
           result += (result.isEmpty() ? "" : ".") + part;
         }
       }
+      // when in doubt, do the basic thing
+      if (result.isBlank()) {
+        return fullType.substring(p + 1);
+      }
       return result;
     }
   }
@@ -229,9 +236,7 @@ final class Util {
     return currentTop;
   }
 
-  /**
-   * Return the name via <code>@Named</code> or a Qualifier annotation.
-   */
+  /** Return the name via <code>@Named</code> or a Qualifier annotation. */
   public static String getNamed(Element p) {
     final NamedPrism named = NamedPrism.getInstanceOn(p);
     if (named != null) {


Test patch:
diff --git a/inject-generator/src/test/java/io/avaje/inject/generator/models/valid/UserMaker.java b/inject-generator/src/test/java/io/avaje/inject/generator/models/valid/UserMaker.java
new file mode 100644
index 00000000..48d9715f
--- /dev/null
+++ b/inject-generator/src/test/java/io/avaje/inject/generator/models/valid/UserMaker.java
@@ -0,0 +1,14 @@
+package io.avaje.inject.generator.models.valid;
+
+import io.avaje.inject.Bean;
+import io.avaje.inject.Factory;
+import jakarta.inject.Named;
+
+@Factory
+public class UserMaker {
+  @Bean
+  @Named("User-  ;():	' []{}/*&^%$#@<>?|+-= Id")
+  String userId() {
+    return "bla bla";
+  }
+}
diff --git a/inject-generator/src/test/java/io/avaje/inject/generator/models/valid/_Wiring.java b/inject-generator/src/test/java/io/avaje/inject/generator/models/valid/_Wiring.java
new file mode 100644
index 00000000..b7c8bea0
--- /dev/null
+++ b/inject-generator/src/test/java/io/avaje/inject/generator/models/valid/_Wiring.java
@@ -0,0 +1,7 @@
+package io.avaje.inject.generator.models.valid;
+
+import io.avaje.inject.Factory;
+
+@Factory
+class _Wiring {
+}
\ No newline at end of file