@@ -186,6 +186,30 @@ private static void generate(File outputDir, String packageName, File opDefs) {
186186 generate (outputDir , packageName , defs );
187187 }
188188
189+ private static void writeToFile (TypeSpec spec , File outputDir , String packageName ) {
190+ JavaFile file =
191+ JavaFile .builder (packageName , spec ).indent (" " ).skipJavaLangImports (true ).build ();
192+
193+ File outputFile =
194+ new File (outputDir , packageName .replace ('.' , '/' ) + '/' + spec .name + ".java" );
195+ outputFile .getParentFile ().mkdirs ();
196+ try {
197+ StringBuilder builder = new StringBuilder ();
198+ builder .append (LICENSE + '\n' );
199+ builder .append ("// This class has been generated, DO NOT EDIT!\n \n " );
200+ file .writeTo (builder );
201+
202+ Files .write (
203+ outputFile .toPath (),
204+ builder .toString ().getBytes (StandardCharsets .UTF_8 ),
205+ StandardOpenOption .WRITE ,
206+ StandardOpenOption .CREATE ,
207+ StandardOpenOption .TRUNCATE_EXISTING );
208+ } catch (IOException ioException ) {
209+ throw new IllegalStateException ("Failed to write file " + outputFile , ioException );
210+ }
211+ }
212+
189213 /** Generate all the ops that pass {@link ClassGenerator#canGenerateOp(OpDef, ApiDef)}. */
190214 private static void generate (File outputDir , String basePackage , Map <OpDef , ApiDef > ops ) {
191215 List <FullOpDef > fullOps =
@@ -219,34 +243,19 @@ private static void generate(File outputDir, String basePackage, Map<OpDef, ApiD
219243 }))
220244 .collect (Collectors .toList ());
221245
246+ List <StatefulPair > statefulPairs = StatefulPair .extractStatefulPairs (fullOps );
247+
222248 fullOps .forEach (
223249 (def ) -> {
224250 TypeSpec spec = def .buildOpClass ();
225251
226- JavaFile file =
227- JavaFile .builder (def .packageName , spec )
228- .indent (" " )
229- .skipJavaLangImports (true )
230- .build ();
231-
232- File outputFile =
233- new File (outputDir , def .packageName .replace ('.' , '/' ) + '/' + spec .name + ".java" );
234- outputFile .getParentFile ().mkdirs ();
235- try {
236- StringBuilder builder = new StringBuilder ();
237- builder .append (LICENSE + '\n' );
238- builder .append ("// This class has been generated, DO NOT EDIT!\n \n " );
239- file .writeTo (builder );
240-
241- Files .write (
242- outputFile .toPath (),
243- builder .toString ().getBytes (StandardCharsets .UTF_8 ),
244- StandardOpenOption .WRITE ,
245- StandardOpenOption .CREATE ,
246- StandardOpenOption .TRUNCATE_EXISTING );
247- } catch (IOException ioException ) {
248- throw new IllegalStateException ("Failed to write file " + outputFile , ioException );
249- }
252+ writeToFile (spec , outputDir , def .packageName );
253+ });
254+
255+ statefulPairs .forEach (
256+ (pair ) -> {
257+ pair .buildOpClasses ()
258+ .forEach ((spec ) -> writeToFile (spec , outputDir , pair .getPackageName ()));
250259 });
251260 }
252261}
0 commit comments