Repository: avaje/avaje-inject
Pull request #636 (2024-07-03 15:21:03)

Update Generated Imports
Remove unused generated imports


After this we should be good for 10.x


Patch:
diff --git a/inject-generator/src/main/java/io/avaje/inject/generator/AssistBeanReader.java b/inject-generator/src/main/java/io/avaje/inject/generator/AssistBeanReader.java
index 49d2bcb6..5419e1f1 100644
--- a/inject-generator/src/main/java/io/avaje/inject/generator/AssistBeanReader.java
+++ b/inject-generator/src/main/java/io/avaje/inject/generator/AssistBeanReader.java
@@ -140,12 +140,8 @@ final class AssistBeanReader {
   }
 
   private Set<String> importTypes() {
-    importTypes.add("io.avaje.inject.AssistFactory");
     Optional.ofNullable(targetType).ifPresent(t -> importTypes.add(t.getQualifiedName().toString()));
 
-    if (Util.validImportType(type)) {
-      importTypes.add(type);
-    }
     if (qualifierName != null) {
       importTypes.add(NamedPrism.PRISM_TYPE);
     }
@@ -159,13 +155,13 @@ final class AssistBeanReader {
     return importTypes.forImport();
   }
 
-  void writeImports(Append writer) {
+  void writeImports(Append writer, String pkgName) {
     importTypes.add(Constants.COMPONENT);
     if (!injectFields().isEmpty()) {
       importTypes.add(Constants.TYPE);
     }
     for (String importType : importTypes()) {
-      if (Util.validImportType(importType)) {
+      if (Util.validImportType(importType, pkgName)) {
         writer.append("import %s;", Util.sanitizeImports(importType)).eol();
       }
     }
diff --git a/inject-generator/src/main/java/io/avaje/inject/generator/BeanReader.java b/inject-generator/src/main/java/io/avaje/inject/generator/BeanReader.java
index b51daffe..d0110d1c 100644
--- a/inject-generator/src/main/java/io/avaje/inject/generator/BeanReader.java
+++ b/inject-generator/src/main/java/io/avaje/inject/generator/BeanReader.java
@@ -346,9 +346,7 @@ final class BeanReader {
   }
 
   private Set<String> importTypes() {
-    if (Util.validImportType(type)) {
-      importTypes.add(type);
-    }
+    importTypes.add(type);
     typeReader.extraImports(importTypes);
     requestParams.addImports(importTypes);
     aspects.extraImports(importTypes);
@@ -365,7 +363,7 @@ final class BeanReader {
     if (!suppressGeneratedImport){
       importTypes.add(Constants.GENERATED);
     }
-    if (!suppressBuilderImport) {
+    if (!suppressBuilderImport && !isGenerateProxy()) {
       importTypes.add(Constants.BUILDER);
     }
     return importTypes.forImport();
@@ -384,13 +382,13 @@ final class BeanReader {
     return suppressGeneratedImport ? "@io.avaje.inject.spi.Generated" : "@Generated";
   }
 
-  void writeImports(Append writer) {
+  void writeImports(Append writer, String pkgName) {
     if (!allGenericTypes().isEmpty()) {
       importTypes.add(Constants.TYPE);
       importTypes.add(Constants.GENERICTYPE);
     }
     for (String importType : importTypes()) {
-      if (Util.validImportType(importType)) {
+      if (Util.validImportType(importType, pkgName)) {
         writer.append("import %s;", Util.sanitizeImports(importType)).eol();
       }
     }
diff --git a/inject-generator/src/main/java/io/avaje/inject/generator/EventPublisherWriter.java b/inject-generator/src/main/java/io/avaje/inject/generator/EventPublisherWriter.java
index ec896074..b073c83b 100644
--- a/inject-generator/src/main/java/io/avaje/inject/generator/EventPublisherWriter.java
+++ b/inject-generator/src/main/java/io/avaje/inject/generator/EventPublisherWriter.java
@@ -19,7 +19,7 @@ import javax.tools.JavaFileObject;
 /** Write the source code for the bean. */
 final class EventPublisherWriter {
   private static final Map<String, String> GENERATED_PUBLISHERS = new HashMap<>();
-  private final String TEMPLATE =
+  private static final String TEMPLATE =
       "package {0};\n\n"
           + "{1}"
           + "@Component\n"
@@ -131,7 +131,7 @@ final class EventPublisherWriter {
 
     StringBuilder writer = new StringBuilder();
     for (String importType : importTypes.forImport()) {
-      if (Util.validImportType(importType)) {
+      if (Util.validImportType(importType, packageName)) {
         writer.append(String.format("import %s;\n", Util.sanitizeImports(importType)));
       }
     }
diff --git a/inject-generator/src/main/java/io/avaje/inject/generator/SimpleAssistWriter.java b/inject-generator/src/main/java/io/avaje/inject/generator/SimpleAssistWriter.java
index c05f3a7c..0cc67ce2 100644
--- a/inject-generator/src/main/java/io/avaje/inject/generator/SimpleAssistWriter.java
+++ b/inject-generator/src/main/java/io/avaje/inject/generator/SimpleAssistWriter.java
@@ -72,7 +72,7 @@ final class SimpleAssistWriter {
   }
 
   private void writeImports() {
-    beanReader.writeImports(writer);
+    beanReader.writeImports(writer, packageName);
   }
 
   private void writeClassStart() {
diff --git a/inject-generator/src/main/java/io/avaje/inject/generator/SimpleBeanProxyWriter.java b/inject-generator/src/main/java/io/avaje/inject/generator/SimpleBeanProxyWriter.java
index e2413b83..d66cebf9 100644
--- a/inject-generator/src/main/java/io/avaje/inject/generator/SimpleBeanProxyWriter.java
+++ b/inject-generator/src/main/java/io/avaje/inject/generator/SimpleBeanProxyWriter.java
@@ -95,7 +95,7 @@ final class SimpleBeanProxyWriter {
     writer.append("import %s;", Constants.INVOCATION_EXCEPTION).eol();
     writer.append("import %s;", Constants.METHOD_INTERCEPTOR).eol();
     writer.append("import %s;", Constants.PROXY).eol();
-    beanReader.writeImports(writer);
+    beanReader.writeImports(writer, packageName);
   }
 
   private void writeClassEnd() {
diff --git a/inject-generator/src/main/java/io/avaje/inject/generator/SimpleBeanWriter.java b/inject-generator/src/main/java/io/avaje/inject/generator/SimpleBeanWriter.java
index 8fba70c9..5a678a8a 100644
--- a/inject-generator/src/main/java/io/avaje/inject/generator/SimpleBeanWriter.java
+++ b/inject-generator/src/main/java/io/avaje/inject/generator/SimpleBeanWriter.java
@@ -325,7 +325,7 @@ final class SimpleBeanWriter {
   }
 
   private void writeImports() {
-    beanReader.writeImports(writer);
+    beanReader.writeImports(writer, packageName);
   }
 
   private void writeClassEnd() {
diff --git a/inject-generator/src/main/java/io/avaje/inject/generator/SimpleModuleWriter.java b/inject-generator/src/main/java/io/avaje/inject/generator/SimpleModuleWriter.java
index a00c3c94..bb57c7ab 100644
--- a/inject-generator/src/main/java/io/avaje/inject/generator/SimpleModuleWriter.java
+++ b/inject-generator/src/main/java/io/avaje/inject/generator/SimpleModuleWriter.java
@@ -194,7 +194,7 @@ final class SimpleModuleWriter {
       writer.append("import %s;", type).eol();
     }
     for (String type : scopeInfo.initModuleDependencies(ordering.importTypes())) {
-      if (Util.validImportType(type)) {
+      if (Util.validImportType(type, modulePackage)) {
         writer.append("import %s;", type).eol();
       }
     }
diff --git a/inject-generator/src/main/java/io/avaje/inject/generator/SimpleOrderWriter.java b/inject-generator/src/main/java/io/avaje/inject/generator/SimpleOrderWriter.java
index 36b77205..42190ae6 100644
--- a/inject-generator/src/main/java/io/avaje/inject/generator/SimpleOrderWriter.java
+++ b/inject-generator/src/main/java/io/avaje/inject/generator/SimpleOrderWriter.java
@@ -1,14 +1,8 @@
 package io.avaje.inject.generator;
 
-import static io.avaje.inject.generator.APContext.logError;
-import static io.avaje.inject.generator.ProcessingContext.createMetaInfWriterFor;
-
 import java.io.IOException;
-import java.io.Writer;
 import java.util.Set;
 
-import javax.tools.FileObject;
-
 /** Write the source code for the factory. */
 final class SimpleOrderWriter {
 
@@ -44,7 +38,6 @@ final class SimpleOrderWriter {
     writer
       .append(
         "import static java.util.Map.entry;\n"
-          + "import static java.util.List.of;\n"
           + "\n"
           + "import java.util.List;\n"
           + "import java.util.Map;\n"
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 00a9f6a8..c66cbdc1 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
@@ -6,6 +6,7 @@ import javax.lang.model.type.DeclaredType;
 import javax.lang.model.type.TypeKind;
 import javax.lang.model.type.TypeMirror;
 import java.util.Optional;
+import java.util.function.Function;
 import java.util.regex.Pattern;
 
 final class Util {
@@ -24,8 +25,19 @@ final class Util {
     return "void".equalsIgnoreCase(type);
   }
 
-  static boolean validImportType(String type) {
-    return type.indexOf('.') > 0;
+  static boolean validImportType(String type, String packageName) {
+    return type.indexOf('.') > -1
+      && !type.startsWith("java.lang.")
+      && importDifferentPackage(type, packageName)
+      || importJavaLangSubpackage(type);
+  }
+
+  private static boolean importDifferentPackage(String type, String packageName) {
+    return type.replace(packageName + '.', "").indexOf('.') > -1;
+  }
+
+  private static boolean importJavaLangSubpackage(String type) {
+    return type.startsWith("java.lang.") && importDifferentPackage(type, "java.lang");
   }
 
   static String classOfMethod(String method) {


Test patch:
diff --git a/inject-generator/src/test/java/io/avaje/inject/generator/UtilTest.java b/inject-generator/src/test/java/io/avaje/inject/generator/UtilTest.java
index edeb06e1..61cf8683 100644
--- a/inject-generator/src/test/java/io/avaje/inject/generator/UtilTest.java
+++ b/inject-generator/src/test/java/io/avaje/inject/generator/UtilTest.java
@@ -98,15 +98,16 @@ class UtilTest {
 
   @Test
   void validImportType() {
-    assertTrue(Util.validImportType("my.Foo"));
-    assertTrue(Util.validImportType("other.pack.Foo"));
+    assertTrue(Util.validImportType("java.lang.something.Foo", "org.foo"));
+    assertTrue(Util.validImportType("org.foo.some.Bar", "org.foo"));
+    assertTrue(Util.validImportType("org.other.Bar", "org.foo"));
   }
 
   @Test
   void validImportType_not() {
-    assertFalse(Util.validImportType("void"));
-    assertFalse(Util.validImportType("Foo"));
-    assertFalse(Util.validImportType("NoPackage"));
+    assertFalse(Util.validImportType("int", "org.foo"));
+    assertFalse(Util.validImportType("java.lang.Integer", "org.foo"));
+    assertFalse(Util.validImportType("org.foo.Bar", "org.foo"));
   }
 
   @Test