Repository: avaje/avaje-inject
Pull request #714 (2024-10-30 04:32:43)

Change MetaData to be Comparable and sort to deterministic order


Patch:
diff --git a/blackbox-aspect/pom.xml b/blackbox-aspect/pom.xml
index 113f2ca6..07aa434a 100644
--- a/blackbox-aspect/pom.xml
+++ b/blackbox-aspect/pom.xml
@@ -5,7 +5,7 @@
   <parent>
     <artifactId>avaje-inject-parent</artifactId>
     <groupId>io.avaje</groupId>
-    <version>10.5-RC2</version>
+    <version>10.5-RC3</version>
   </parent>
   <modelVersion>4.0.0</modelVersion>
 
diff --git a/blackbox-other/pom.xml b/blackbox-other/pom.xml
index 451e147c..83982371 100644
--- a/blackbox-other/pom.xml
+++ b/blackbox-other/pom.xml
@@ -6,7 +6,7 @@
   <parent>
     <artifactId>avaje-inject-parent</artifactId>
     <groupId>io.avaje</groupId>
-    <version>10.5-RC2</version>
+    <version>10.5-RC3</version>
   </parent>
 
   <artifactId>blackbox-other</artifactId>
diff --git a/inject-aop/pom.xml b/inject-aop/pom.xml
index e96cfc0d..3d03e31b 100644
--- a/inject-aop/pom.xml
+++ b/inject-aop/pom.xml
@@ -5,7 +5,7 @@
   <parent>
     <groupId>io.avaje</groupId>
     <artifactId>avaje-inject-parent</artifactId>
-    <version>10.5-RC2</version>
+    <version>10.5-RC3</version>
   </parent>
   <artifactId>avaje-inject-aop</artifactId>
   <dependencies>
diff --git a/inject-events/pom.xml b/inject-events/pom.xml
index a69af056..b41dabe5 100644
--- a/inject-events/pom.xml
+++ b/inject-events/pom.xml
@@ -3,7 +3,7 @@
   <parent>
     <groupId>io.avaje</groupId>
     <artifactId>avaje-inject-parent</artifactId>
-    <version>10.5-RC2</version>
+    <version>10.5-RC3</version>
   </parent>
   <artifactId>avaje-inject-events</artifactId>
   <name>avaje inject events</name>
diff --git a/inject-generator/pom.xml b/inject-generator/pom.xml
index 7ac3765c..24451303 100644
--- a/inject-generator/pom.xml
+++ b/inject-generator/pom.xml
@@ -6,7 +6,7 @@
   <parent>
     <groupId>io.avaje</groupId>
     <artifactId>avaje-inject-parent</artifactId>
-    <version>10.5-RC2</version>
+    <version>10.5-RC3</version>
   </parent>
 
   <artifactId>avaje-inject-generator</artifactId>
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 0b03d1e4..153b278f 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
@@ -8,10 +8,14 @@ import java.util.stream.Collectors;
 /**
  * Holds the data as per <code>@DependencyMeta</code>
  */
-final class MetaData {
+final class MetaData implements Comparable<MetaData> {
 
-  private static final String INDENT = "      ";
+  private static final Comparator<MetaData> COMPARATOR =
+    Comparator.comparing(MetaData::type)
+      .thenComparing(MetaData::name, Comparator.nullsFirst(Comparator.naturalOrder()))
+      .thenComparing(MetaData::compareProvides);
 
+  private static final String INDENT = "      ";
   private static final String NEWLINE = "\n";
 
   private final String type;
@@ -314,4 +318,13 @@ final class MetaData {
       }
     }
   }
+
+  private String compareProvides() {
+    return provides.toString();
+  }
+
+  @Override
+  public int compareTo(MetaData meta) {
+    return COMPARATOR.compare(this, meta);
+  }
 }
diff --git a/inject-generator/src/main/java/io/avaje/inject/generator/ScopeInfo.java b/inject-generator/src/main/java/io/avaje/inject/generator/ScopeInfo.java
index b1a71add..054206bd 100644
--- a/inject-generator/src/main/java/io/avaje/inject/generator/ScopeInfo.java
+++ b/inject-generator/src/main/java/io/avaje/inject/generator/ScopeInfo.java
@@ -3,14 +3,8 @@ package io.avaje.inject.generator;
 import static io.avaje.inject.generator.ProcessingContext.*;
 import static io.avaje.inject.generator.APContext.*;
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.LinkedHashMap;
-import java.util.LinkedHashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
+import java.util.stream.Collectors;
 
 import javax.annotation.processing.FilerException;
 import javax.lang.model.element.Element;
@@ -48,7 +42,7 @@ final class ScopeInfo {
   /**
    * Map to merge the existing meta data with partially compiled code. Keyed by type and qualifier/name.
    */
-  private final Map<String, MetaData> metaData = new LinkedHashMap<>();
+  private final Map<String, MetaData> metaData = new HashMap<>();
   private final Map<String, String> constructorDependencies = new LinkedHashMap<>();
   private final List<BeanReader> beanReaders = new ArrayList<>();
   private final Set<String> readBeans = new HashSet<>();
@@ -228,7 +222,7 @@ final class ScopeInfo {
     if (moduleWritten) {
       return;
     }
-    final Collection<MetaData> meta = metaData.values();
+    final Collection<MetaData> meta = metaData.values().stream().sorted().collect(Collectors.toList());
     if (emptyModule) {
       // typically nothing in the default scope, only custom scopes
       if (meta.size() > 0) {
diff --git a/inject-maven-plugin/pom.xml b/inject-maven-plugin/pom.xml
index 3ec01e0e..790e95c4 100644
--- a/inject-maven-plugin/pom.xml
+++ b/inject-maven-plugin/pom.xml
@@ -4,7 +4,7 @@
   <parent>
     <artifactId>avaje-inject-parent</artifactId>
     <groupId>io.avaje</groupId>
-    <version>10.5-RC2</version>
+    <version>10.5-RC3</version>
   </parent>
 
   <artifactId>avaje-inject-maven-plugin</artifactId>
diff --git a/inject/pom.xml b/inject/pom.xml
index 4d9df8e6..0f85c2f7 100644
--- a/inject/pom.xml
+++ b/inject/pom.xml
@@ -4,7 +4,7 @@
   <parent>
     <groupId>io.avaje</groupId>
     <artifactId>avaje-inject-parent</artifactId>
-    <version>10.5-RC2</version>
+    <version>10.5-RC3</version>
   </parent>
 
   <artifactId>avaje-inject</artifactId>
diff --git a/pom.xml b/pom.xml
index 5c0ccd33..9a868738 100644
--- a/pom.xml
+++ b/pom.xml
@@ -3,7 +3,7 @@
   <modelVersion>4.0.0</modelVersion>
   <groupId>io.avaje</groupId>
   <artifactId>avaje-inject-parent</artifactId>
-  <version>10.5-RC2</version>
+  <version>10.5-RC3</version>
   <packaging>pom</packaging>
   <name>avaje inject parent</name>
   <description>parent pom for avaje inject library</description>


Test patch:
diff --git a/blackbox-test-inject/pom.xml b/blackbox-test-inject/pom.xml
index a4ff784f..56380d28 100644
--- a/blackbox-test-inject/pom.xml
+++ b/blackbox-test-inject/pom.xml
@@ -5,7 +5,7 @@
   <parent>
     <artifactId>avaje-inject-parent</artifactId>
     <groupId>io.avaje</groupId>
-    <version>10.5-RC2</version>
+    <version>10.5-RC3</version>
   </parent>
   <modelVersion>4.0.0</modelVersion>
 
diff --git a/inject-generator/src/test/java/io/avaje/inject/generator/MetaDataCompareToTest.java b/inject-generator/src/test/java/io/avaje/inject/generator/MetaDataCompareToTest.java
new file mode 100644
index 00000000..286fa6bd
--- /dev/null
+++ b/inject-generator/src/test/java/io/avaje/inject/generator/MetaDataCompareToTest.java
@@ -0,0 +1,53 @@
+package io.avaje.inject.generator;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.*;
+
+class MetaDataCompareToTest {
+
+  MetaData foo = new MetaData("foo.Foo", null);
+  MetaData fooEq = new MetaData("foo.Foo", null);
+  MetaData fooA = new MetaData("foo.Foo", "a");
+  MetaData fooAEq = new MetaData("foo.Foo", "a");
+  MetaData bar = new MetaData("b.Bar", null);
+  MetaData fooProv = new MetaData("foo.Foo", null);
+  MetaData fooProv2 = new MetaData("foo.Foo", null);
+  MetaData fooProv3 = new MetaData("foo.Foo", null);
+
+  MetaDataCompareToTest() {
+    fooProv.setProvides(List.of("bar.Bar"));
+    fooProv2.setProvides(List.of("bar.Bar"));
+    fooProv3.setProvides(List.of("bar.Bar", "b.B2"));
+  }
+
+  @Test
+  void compareTo_equals() {
+    assertThat(foo.compareTo(fooEq)).describedAs("type only").isEqualTo(0);
+    assertThat(fooA.compareTo(fooAEq)).describedAs("type and name").isEqualTo(0);
+    assertThat(fooProv.compareTo(fooProv2)).describedAs("type and name and provides").isEqualTo(0);
+  }
+
+  @Test
+  void compareTo_differentType() {
+    assertEquals(4, foo.compareTo(bar));
+    assertEquals(-4, bar.compareTo(foo));
+  }
+
+  @Test
+  void compareTo_differentName() {
+    assertEquals(-1, foo.compareTo(fooA));
+    assertEquals(1, fooA.compareTo(foo));
+  }
+
+  @Test
+  void compareTo_differentProvides() {
+    assertEquals(5, fooProv.compareTo(foo));
+    assertEquals(-5, foo.compareTo(fooProv));
+    assertEquals(49, fooProv.compareTo(fooProv3));
+    assertEquals(-49, fooProv3.compareTo(fooProv));
+  }
+}
diff --git a/inject-test/pom.xml b/inject-test/pom.xml
index 87990e8b..99520a73 100644
--- a/inject-test/pom.xml
+++ b/inject-test/pom.xml
@@ -4,7 +4,7 @@
   <parent>
     <groupId>io.avaje</groupId>
     <artifactId>avaje-inject-parent</artifactId>
-    <version>10.5-RC2</version>
+    <version>10.5-RC3</version>
   </parent>
 
   <artifactId>avaje-inject-test</artifactId>