22
33import java .io .IOException ;
44import java .io .PrintWriter ;
5+ import java .util .ArrayList ;
56import java .util .HashMap ;
67import java .util .HashSet ;
78import java .util .List ;
@@ -108,10 +109,19 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
108109 *
109110 * <p>In this case, {@code A} <emph>depends on</emph> {@code B}, so we need to ensure that {@code
110111 * B} is processed first (a super class is generated for it first).
112+ *
113+ * @return List of classes ordered by their references. The list has the same
114+ * size as the input set.
111115 */
112116 private List <TypeElement > orderByReferences (Set <TypeElement > classesToProcess ) {
113- var classesToProcessNames =
117+ var classesToProcessSimpleNames =
114118 classesToProcess .stream ().map (type -> type .getSimpleName ().toString ()).toList ();
119+ var classesToProcessMap = classesToProcess
120+ .stream ()
121+ .collect (Collectors .toMap (
122+ tp -> tp .getQualifiedName ().toString (),
123+ tp -> tp
124+ ));
115125 var dependencies = new HashMap <String , Set <String >>();
116126 for (var clazz : classesToProcess ) {
117127 var annotatedCtors =
@@ -141,7 +151,7 @@ private List<TypeElement> orderByReferences(Set<TypeElement> classesToProcess) {
141151 })
142152 .toList ();
143153 for (var childTypeName : childTypeNames ) {
144- if (classesToProcessNames .contains (childTypeName )) {
154+ if (classesToProcessSimpleNames .contains (childTypeName )) {
145155 var clazzName = clazz .getSimpleName ().toString ();
146156 var deps = dependencies .computeIfAbsent (clazzName , k -> new HashSet <>());
147157 deps .add (childTypeName );
@@ -160,7 +170,7 @@ private List<TypeElement> orderByReferences(Set<TypeElement> classesToProcess) {
160170 }
161171 var sortedDeps = DependencySorter .topologicalSort (dependencies );
162172 // Map class names to their TypeElements
163- return sortedDeps .stream ()
173+ var sortedDepTypes = sortedDeps .stream ()
164174 .map (
165175 depName -> {
166176 var depClazz =
@@ -170,7 +180,29 @@ private List<TypeElement> orderByReferences(Set<TypeElement> classesToProcess) {
170180 .orElseThrow (() -> new IRProcessingException ("Class not found: " + depName , null ));
171181 return depClazz ;
172182 })
173- .toList ();
183+ .collect (Collectors .toCollection (ArrayList ::new ));
184+
185+ // Append the rest of the classesToProcess to the sortedDepTypes
186+ for (var entry : classesToProcessMap .entrySet ()) {
187+ var fqn = entry .getKey ();
188+ var tp = entry .getValue ();
189+ // Poor man's solution. Let's hope the number of classes to process is small.
190+ var isInSortedDeps = sortedDepTypes
191+ .stream ()
192+ .anyMatch (depTp -> depTp .getQualifiedName ().toString ().equals (fqn ));
193+ if (!isInSortedDeps ) {
194+ sortedDepTypes .add (tp );
195+ }
196+ }
197+ if (sortedDepTypes .size () != classesToProcess .size ()) {
198+ throw new IRProcessingException (
199+ "orderByReferences failure: " +
200+ "sortedDepTypes: " + sortedDepTypes +
201+ ", classesToProcess: " + classesToProcessSimpleNames ,
202+ null
203+ );
204+ }
205+ return sortedDepTypes ;
174206 }
175207
176208 /**
0 commit comments