Skip to content

Commit 16b5061

Browse files
committed
#162 - Module classes could expose the list of beans prior to building.
1 parent 7fb2fe5 commit 16b5061

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

inject-generator/src/main/java/io/avaje/inject/generator/SimpleModuleWriter.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package io.avaje.inject.generator;
22

3+
import javax.lang.model.element.Modifier;
4+
import javax.lang.model.element.TypeElement;
35
import javax.tools.FileObject;
46
import java.io.IOException;
57
import java.io.Writer;
8+
import java.util.LinkedHashSet;
69
import java.util.Map;
710
import java.util.Set;
811
import java.util.TreeSet;
@@ -16,7 +19,7 @@ class SimpleModuleWriter {
1619
"/**\n" +
1720
" * Generated source - avaje inject module for %s.\n" +
1821
" * \n" +
19-
" * With JPMS Java module system this generated class should be explicitly\n" +
22+
" * With Java module system this generated class should be explicitly\n" +
2023
" * registered in module-info via a <code>provides</code> clause like:\n" +
2124
" * \n" +
2225
" * <pre>{@code\n" +
@@ -38,9 +41,16 @@ class SimpleModuleWriter {
3841
" * Creates all the beans in order based on constructor dependencies.\n" +
3942
" * The beans are registered into the builder along with callbacks for\n" +
4043
" * field injection, method injection and lifecycle support.\n" +
41-
" * <p>\n" +
4244
" */";
4345

46+
private static final String CLASSES_COMMENT =
47+
" /**\n" +
48+
" * Return public classes of the beans that would be registered by this module.\n" +
49+
" * <p>\n" +
50+
" * This method allows code to use reflection to inspect the modules classes \n" +
51+
" * before the module is wired. This method is not required for DI wiring.\n" +
52+
" */";
53+
4454
private final ProcessingContext context;
4555
private final String modulePackage;
4656
private final String shortName;
@@ -63,6 +73,7 @@ void write(ScopeInfo.Type scopeType) throws IOException {
6373
writer = new Append(createFileWriter());
6474
writePackage();
6575
writeStartClass();
76+
writeClassesMethod();
6677
writeBuildMethod();
6778
writeBuildMethods();
6879
writeEndClass();
@@ -86,6 +97,36 @@ private void writeServicesFile(ScopeInfo.Type scopeType) {
8697
}
8798
}
8899

100+
private void writeClassesMethod() {
101+
Set<String> allClasses = distinctPublicClasses();
102+
writer.append(CLASSES_COMMENT).eol();
103+
writer.append(" public static Class<?>[] classes() {").eol();
104+
writer.append(" return new Class<?>[]{").eol();
105+
for (String rawType : allClasses) {
106+
writer.append(" %s.class,", rawType).eol();
107+
}
108+
writer.append(" };").eol();
109+
writer.append(" }").eol().eol();
110+
}
111+
112+
/**
113+
* Return the distinct set of public classes that are dependency types.
114+
*/
115+
private Set<String> distinctPublicClasses() {
116+
Set<String> publicClasses = new LinkedHashSet<>();
117+
for (MetaData metaData : ordering.ordered()) {
118+
String rawType = metaData.getType();
119+
if (!"void".equals(rawType)) {
120+
String type = GenericType.parse(rawType).topType();
121+
TypeElement element = context.element(type);
122+
if (element != null && element.getModifiers().contains(Modifier.PUBLIC)) {
123+
publicClasses.add(type);
124+
}
125+
}
126+
}
127+
return publicClasses;
128+
}
129+
89130
private void writeBuildMethod() {
90131
writer.append(CODE_COMMENT_CREATE_CONTEXT).eol();
91132
writer.append(" @Override").eol();

inject-test/src/test/java/org/example/custom3/ParentScopeTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import org.example.custom2.*;
55
import org.junit.jupiter.api.Test;
66

7+
import java.util.Arrays;
8+
import java.util.HashSet;
79
import java.util.List;
10+
import java.util.Set;
811

912
import static org.assertj.core.api.Assertions.assertThat;
1013

@@ -44,4 +47,20 @@ void parentScope() {
4447
assertThat(blue).isNotNull();
4548
assertThat(blue.getDependency()).isSameAs(ocsThree);
4649
}
50+
51+
@Test
52+
void module_classes() {
53+
Class<?>[] classes = MyThreeModule.classes();
54+
Set<Class<?>> asSet = new HashSet<>(Arrays.asList(classes));
55+
56+
assertThat(asSet).contains(
57+
org.example.custom3.TcsFactory.class,
58+
org.example.custom3.TcsCart.class,
59+
org.example.custom3.TcsGreen.class,
60+
org.example.custom3.TcsBlue.class,
61+
org.example.custom3.TcsBart.class,
62+
org.example.custom3.TcsArt.class,
63+
org.example.custom3.TcsRed.class,
64+
org.example.custom3.TcsA.class);
65+
}
4766
}

0 commit comments

Comments
 (0)