11package io .avaje .inject .generator ;
22
3+ import javax .lang .model .element .Modifier ;
4+ import javax .lang .model .element .TypeElement ;
35import javax .tools .FileObject ;
46import java .io .IOException ;
57import java .io .Writer ;
8+ import java .util .LinkedHashSet ;
69import java .util .Map ;
710import java .util .Set ;
811import 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 ();
0 commit comments