6363public class JavadocBasher {
6464 static final boolean fVerbose = false ; // set to true for verbose output
6565
66- final List <File > fBashed = new ArrayList <>();
67- final List <File > fUnchanged = new ArrayList <>();
66+ final List <Path > fBashed = new ArrayList <>();
67+ final List <Path > fUnchanged = new ArrayList <>();
6868 final List <String > fSkipped = new ArrayList <>();
6969
7070 record Edit (int start , int length , String text ) {
7171 }
7272
73- public static void main (String [] args ) {
74- String workspaceDir = ".." ; // use forward slashes, no final slash
75- String outputDir = ".." ; // can point to another directory for debugging
73+ public static void main (String [] args ) throws IOException {
74+ Path swtProject = Path .of (".." , "org.eclipse.swt" ).toRealPath ();
7675 String [] folders = new String [] { // commented folders do not need to be
7776 // bashed
7877 "Eclipse SWT" , "Eclipse SWT Accessibility" ,
@@ -99,16 +98,14 @@ public static void main(String[] args) {
9998 };
10099
101100 System .out .println ("==== Start Bashing ====" );
101+ System .out .println (" in " + swtProject );
102102 int totalBashed = 0 ;
103103 for (String dir : targetSubdirs ) {
104104 for (String folder : folders ) {
105- String targetSubdir = folder + "/" + dir ;
106- File source = new File (workspaceDir + "/org.eclipse.swt/"
107- + folder + "/" + sourceSubdir );
108- File target = new File (workspaceDir + "/org.eclipse.swt/"
109- + targetSubdir );
110- File out = new File (outputDir + "/org.eclipse.swt/"
111- + targetSubdir );
105+ Path targetSubdir = Path .of (folder , dir );
106+ Path source = swtProject .resolve (Path .of (folder , sourceSubdir ));
107+ Path target = swtProject .resolve (targetSubdir );
108+ Path out = swtProject .resolve (targetSubdir );
112109 JavadocBasher basher = new JavadocBasher ();
113110 System .out .println ("\n ==== Start Bashing " + targetSubdir );
114111 basher .bashJavaSourceTree (source , target , out );
@@ -127,7 +124,7 @@ public static void main(String[] args) {
127124 + " files in total) - Be sure to Refresh (F5) project(s) ====" );
128125 }
129126
130- void status (String label , List <?> list , String targetSubdir ) {
127+ void status (String label , List <?> list , Path targetSubdir ) {
131128 int count = list .size ();
132129 System .out .println (label + " " + count
133130 + ((count == 1 ) ? " file" : " files" ) + " in " + targetSubdir
@@ -140,87 +137,58 @@ void status(String label, List<?> list, String targetSubdir) {
140137 }
141138 }
142139
143- char [] readFile (File file ) {
144- try {
145- return Files .readString (file .toPath ()).toCharArray ();
146- } catch (IOException ioe ) {
147- System .out .println ("*** Could not read " + file );
148- }
149- return null ;
150- }
151-
152- void writeFile (String contents , File file ) {
153- try {
154- Files .writeString (file .toPath (), contents );
155- } catch (IOException ioe ) {
156- System .out .println ("*** Could not write to " + file );
157- if (fVerbose ) {
158- System .out .println ("<dump filename=\" " + file + "\" >" );
159- System .out .println (contents );
160- System .out .println ("</dump>" );
161- }
162- }
163- }
164-
165- void bashJavaSourceTree (File sourceDir , File targetDir , File outDir ) {
140+ void bashJavaSourceTree (Path sourceDir , Path targetDir , Path outDir ) throws IOException {
166141 if (fVerbose )
167142 System .out .println ("Reading source javadoc from " + sourceDir );
168- if (!sourceDir .exists ()) {
143+ if (!Files .exists (sourceDir )) {
169144 System .out .println ("Source: " + sourceDir + " was missing" );
170145 return ;
171146 }
172- if (!targetDir .exists ()) {
147+ if (!Files .exists (targetDir )) {
173148 System .out .println ("Target: " + targetDir + " was missing" );
174149 return ;
175150 }
176-
177- String [] list = sourceDir .list ();
178- if (list != null ) {
179- for (String filename : list ) {
151+ try (var list = Files .list (sourceDir )) {
152+ for (Path source : list .toList ()) {
153+ String filename = source .getFileName ().toString ();
180154 if (filename .equals ("internal" ) || filename .equals ("library" ))
181155 continue ;
182- File source = new File (sourceDir , filename );
183- File target = new File (targetDir , filename );
184- File out = new File (outDir , filename );
185- if (source .exists () && target .exists ()) {
186- if (source .isDirectory ()) {
187- if (target .isDirectory ()) {
156+ Path target = targetDir .resolve (filename );
157+ Path out = outDir .resolve (filename );
158+ if (!Files .exists (source )) {
159+ throw new IllegalStateException ("Source not exists: " + source );
160+ }
161+ if (Files .exists (target )) {
162+ if (Files .isDirectory (source )) {
163+ if (Files .isDirectory (target )) {
188164 bashJavaSourceTree (source , target , out );
189165 } else {
190- System .out .println ("*** " + target
191- + " should have been a directory." );
166+ throw new IllegalStateException ("Target should have been a directory: " + target );
192167 }
193168 } else {
194- if (filename .toLowerCase (). endsWith (".java" )) {
169+ if (filename .endsWith (".java" )) {
195170 bashFile (source , target , out );
196171 } else {
197- fSkipped . add ( source + " (not a java file)" );
172+ throw new IllegalStateException ( "Not a java file: " + source );
198173 }
199174 }
200175 } else {
201- if (source .exists ()) {
202- fSkipped .add (target + " (does not exist)" );
203- } else {
204- fSkipped .add (source + " (does not exist)" );
205- }
176+ fSkipped .add (target + " (does not exist)" );
206177 }
207178 }
208179 }
209180 }
210181
211- void bashFile (final File source , final File target , File out ) {
212- char [] contents = readFile (source );
213- if (contents == null ) return ;
182+ void bashFile (Path source , Path target , Path out ) throws IOException {
183+ String contents = Files .readString (source );
214184 ASTParser parser = ASTParser .newParser (AST .getJLSLatest ());
215- final Document sourceDocument = new Document (new String ( contents ) );
216- parser .setSource (contents );
185+ final Document sourceDocument = new Document (contents );
186+ parser .setSource (contents . toCharArray () );
217187 ASTNode sourceUnit = parser .createAST (null );
218188
219- contents = readFile (target );
220- if (contents == null ) return ;
221- String targetContents = new String (contents );
189+ String targetContents = Files .readString (target );
222190 final Document targetDocument = new Document (targetContents );
223- parser .setSource (contents );
191+ parser .setSource (targetContents . toCharArray () );
224192 ASTNode targetUnit = parser .createAST (null );
225193
226194 final Map <String , String > comments = new HashMap <>();
@@ -404,12 +372,9 @@ public boolean visit(TypeDeclaration node) {
404372
405373 String newContents = targetDocument .get ();
406374 if (!targetContents .equals (newContents )) {
407- if (makeDirectory (out .getParentFile ())) {
408- writeFile (newContents , out );
409- fBashed .add (target );
410- } else {
411- System .out .println ("*** Could not create " + out .getParent ());
412- }
375+ Files .createDirectories (out .getParent ());
376+ Files .writeString (out , newContents );
377+ fBashed .add (target );
413378 } else {
414379 fUnchanged .add (target );
415380 }
@@ -456,10 +421,4 @@ int skipWhitespace(Document doc, int offset) {
456421 }
457422 return offset ;
458423 }
459-
460- boolean makeDirectory (File directory ) {
461- if (directory .exists ())
462- return true ;
463- return directory .mkdirs ();
464- }
465424}
0 commit comments