306 defaultAction(e, false); 307 308 if (!e.isUnnamed()) { 309 if (e.isOpen()) { 310 writer.print("open "); 311 } 312 writer.println("module " + e.getQualifiedName() + " {"); 313 indentation++; 314 for (ModuleElement.Directive directive : e.getDirectives()) { 315 printDirective(directive); 316 } 317 indentation--; 318 writer.println("}"); 319 } else 320 writer.println("// Unnamed module"); // Should we do more here? 321 return this; 322 } 323 324 private void printDirective(ModuleElement.Directive directive) { 325 indent(); 326 switch (directive.getKind()) { 327 case EXPORTS: // "exports package-name [to module-name-list]" 328 { 329 ExportsDirective exportsDirective = (ExportsDirective) directive; 330 writer.print("exports "); 331 writer.print(exportsDirective.getPackage().getQualifiedName()); 332 printModuleList(exportsDirective.getTargetModules()); 333 } 334 break; 335 336 case OPENS: // opens package-name [to module-name-list] 337 { 338 OpensDirective opensDirective = (OpensDirective) directive; 339 writer.print("opens "); 340 writer.print(opensDirective.getPackage().getQualifiedName()); 341 printModuleList(opensDirective.getTargetModules()); 342 } 343 break; 344 345 case PROVIDES: // provides service-name with implementation-name 346 { 347 ProvidesDirective providesDirective = (ProvidesDirective) directive; 348 writer.print("provides "); 349 writer.print(providesDirective.getService().getQualifiedName()); 350 writer.print(" with "); 351 printNameableList(providesDirective.getImplementations()); 352 } 353 break; 354 355 case REQUIRES: // requires (static|transitive)* module-name 356 { 357 RequiresDirective requiresDirective = (RequiresDirective) directive; 358 writer.print("requires "); 359 if (requiresDirective.isStatic()) 360 writer.print("static "); 361 if (requiresDirective.isTransitive()) 362 writer.print("transitive "); 363 writer.print(requiresDirective.getDependency().getQualifiedName()); 364 } 365 break; 366 367 case USES: // uses service-name 368 { 369 UsesDirective usesDirective = (UsesDirective) directive; 370 writer.print("uses "); 371 writer.print(usesDirective.getService().getQualifiedName()); 372 } 373 break; 374 375 default: 376 throw new UnsupportedOperationException("unknown directive " + directive); 377 } 378 writer.println(";"); 379 } 380 381 private void printModuleList(List<? extends ModuleElement> modules) { 382 if (modules != null) { 383 writer.print(" to "); 384 printNameableList(modules); 385 } 386 } 387 388 private void printNameableList(List<? extends QualifiedNameable> nameables) { 389 writer.print(nameables.stream(). 390 map(QualifiedNameable::getQualifiedName). 391 collect(Collectors.joining(", "))); 392 } 393 394 public void flush() { 395 writer.flush(); 396 } 397 398 private void printDocComment(Element e) { 399 String docComment = elementUtils.getDocComment(e); 400 401 if (docComment != null) { 402 // Break comment into lines 403 java.util.StringTokenizer st = new StringTokenizer(docComment, 404 "\n\r"); 405 indent(); 406 writer.println("/**"); 407 408 while(st.hasMoreTokens()) { 409 indent(); 410 writer.print(" *"); 411 writer.println(st.nextToken()); | 306 defaultAction(e, false); 307 308 if (!e.isUnnamed()) { 309 if (e.isOpen()) { 310 writer.print("open "); 311 } 312 writer.println("module " + e.getQualifiedName() + " {"); 313 indentation++; 314 for (ModuleElement.Directive directive : e.getDirectives()) { 315 printDirective(directive); 316 } 317 indentation--; 318 writer.println("}"); 319 } else 320 writer.println("// Unnamed module"); // Should we do more here? 321 return this; 322 } 323 324 private void printDirective(ModuleElement.Directive directive) { 325 indent(); 326 (new PrintDirective(writer)).visit(directive); 327 writer.println(";"); 328 } 329 330 private static class PrintDirective implements ModuleElement.DirectiveVisitor<Void, Void> { 331 private final PrintWriter writer; 332 333 PrintDirective(PrintWriter writer) { 334 this.writer = writer; 335 } 336 337 @Override 338 public Void visitExports(ExportsDirective d, Void p) { 339 // "exports package-name [to module-name-list]" 340 writer.print("exports "); 341 writer.print(d.getPackage().getQualifiedName()); 342 printModuleList(d.getTargetModules()); 343 return null; 344 } 345 346 @Override 347 public Void visitOpens(OpensDirective d, Void p) { 348 // opens package-name [to module-name-list] 349 writer.print("opens "); 350 writer.print(d.getPackage().getQualifiedName()); 351 printModuleList(d.getTargetModules()); 352 return null; 353 } 354 355 @Override 356 public Void visitProvides(ProvidesDirective d, Void p) { 357 // provides service-name with implementation-name 358 writer.print("provides "); 359 writer.print(d.getService().getQualifiedName()); 360 writer.print(" with "); 361 printNameableList(d.getImplementations()); 362 return null; 363 } 364 365 @Override 366 public Void visitRequires(RequiresDirective d, Void p) { 367 // requires (static|transitive)* module-name 368 writer.print("requires "); 369 if (d.isStatic()) 370 writer.print("static "); 371 if (d.isTransitive()) 372 writer.print("transitive "); 373 writer.print(d.getDependency().getQualifiedName()); 374 return null; 375 } 376 377 @Override 378 public Void visitUses(UsesDirective d, Void p) { 379 // uses service-name 380 writer.print("uses "); 381 writer.print(d.getService().getQualifiedName()); 382 return null; 383 } 384 385 private void printModuleList(List<? extends ModuleElement> modules) { 386 if (modules != null) { 387 writer.print(" to "); 388 printNameableList(modules); 389 } 390 } 391 392 private void printNameableList(List<? extends QualifiedNameable> nameables) { 393 writer.print(nameables.stream(). 394 map(QualifiedNameable::getQualifiedName). 395 collect(Collectors.joining(", "))); 396 } 397 } 398 399 public void flush() { 400 writer.flush(); 401 } 402 403 private void printDocComment(Element e) { 404 String docComment = elementUtils.getDocComment(e); 405 406 if (docComment != null) { 407 // Break comment into lines 408 java.util.StringTokenizer st = new StringTokenizer(docComment, 409 "\n\r"); 410 indent(); 411 writer.println("/**"); 412 413 while(st.hasMoreTokens()) { 414 indent(); 415 writer.print(" *"); 416 writer.println(st.nextToken()); |