1
1
package org .eclipse .swt .tools .internal ;
2
2
3
3
import java .io .*;
4
+ import java .nio .file .*;
4
5
import java .util .*;
5
6
import java .util .Map .*;
6
7
@@ -125,7 +126,7 @@ public static void main(String[] args) {
125
126
basher .bashJavaSourceTree (source , target , out );
126
127
List <String > bashedList = basher .getBashed ();
127
128
basher .status ("Bashed" , bashedList , targetSubdir );
128
- if (bashedList .size () > 0 ) {
129
+ if (! bashedList .isEmpty () ) {
129
130
totalBashed += bashedList .size ();
130
131
if (fVerbose )
131
132
basher .status ("Didn't change" , basher .getUnchanged (),
@@ -152,26 +153,17 @@ void status(String label, List<String> list, String targetSubdir) {
152
153
}
153
154
154
155
char [] readFile (File file ) {
155
- try (Reader in = new FileReader (file )) {
156
- CharArrayWriter storage = new CharArrayWriter ();
157
- char [] chars = new char [8192 ];
158
- int read = in .read (chars );
159
- while (read > 0 ) {
160
- storage .write (chars , 0 , read );
161
- storage .flush ();
162
- read = in .read (chars );
163
- }
164
- return storage .toCharArray ();
156
+ try {
157
+ return Files .readString (file .toPath ()).toCharArray ();
165
158
} catch (IOException ioe ) {
166
159
System .out .println ("*** Could not read " + file );
167
160
}
168
161
return null ;
169
162
}
170
163
171
- void writeFile (char [] contents , File file ) {
172
- try (Writer out = new FileWriter (file )) {
173
- out .write (contents );
174
- out .flush ();
164
+ void writeFile (String contents , File file ) {
165
+ try {
166
+ Files .writeString (file .toPath (), contents );
175
167
} catch (IOException ioe ) {
176
168
System .out .println ("*** Could not write to " + file );
177
169
if (fVerbose ) {
@@ -196,11 +188,8 @@ void bashJavaSourceTree(File sourceDir, File targetDir, File outDir) {
196
188
197
189
String [] list = sourceDir .list ();
198
190
if (list != null ) {
199
- int count = list .length ;
200
- for (int i = 0 ; i < count ; i ++) {
201
- String filename = list [i ];
202
- if (filename .equals ("CVS" ) || filename .equals ("internal" )
203
- || filename .equals ("library" ))
191
+ for (String filename : list ) {
192
+ if (filename .equals ("internal" ) || filename .equals ("library" ))
204
193
continue ;
205
194
File source = new File (sourceDir , filename );
206
195
File target = new File (targetDir , filename );
@@ -238,14 +227,14 @@ void bashFile(final File source, final File target, File out) {
238
227
ASTParser parser = ASTParser .newParser (AST .getJLSLatest ());
239
228
final Document sourceDocument = new Document (new String (contents ));
240
229
parser .setSource (contents );
241
- CompilationUnit sourceUnit = ( CompilationUnit ) parser .createAST (null );
230
+ ASTNode sourceUnit = parser .createAST (null );
242
231
243
232
contents = readFile (target );
244
233
if (contents == null ) return ;
245
234
String targetContents = new String (contents );
246
235
final Document targetDocument = new Document (targetContents );
247
236
parser .setSource (contents );
248
- CompilationUnit targetUnit = ( CompilationUnit ) parser .createAST (null );
237
+ ASTNode targetUnit = parser .createAST (null );
249
238
250
239
final HashMap <String , String > comments = new HashMap <>();
251
240
sourceUnit .accept (new ASTVisitor () {
@@ -395,7 +384,7 @@ public boolean visit(TypeDeclaration node) {
395
384
* c) names that are in the filter list are never API,
396
385
* or they are old API that is defined in the super on some platforms
397
386
*/
398
- if (comments .size () > 0 ) {
387
+ if (! comments .isEmpty () ) {
399
388
String [] filter = new String [] {
400
389
"Color.win32_newDeviceint" ,
401
390
"Cursor.win32_newDeviceint" ,
@@ -447,7 +436,7 @@ public boolean visit(TypeDeclaration node) {
447
436
};
448
437
for (Entry <String , String > entry : comments .entrySet ()) {
449
438
String name = entry .getKey ();
450
- if (entry .getValue ().length () > 0 ){
439
+ if (! entry .getValue ().isEmpty () ){
451
440
int i = 0 ;
452
441
for (i = 0 ; i < filter .length ; i ++) {
453
442
if (name .equals (filter [i ])) break ;
@@ -462,7 +451,7 @@ public boolean visit(TypeDeclaration node) {
462
451
String newContents = targetDocument .get ();
463
452
if (!targetContents .equals (newContents )) {
464
453
if (makeDirectory (out .getParentFile ())) {
465
- writeFile (newContents . toCharArray () , out );
454
+ writeFile (newContents , out );
466
455
fBashed .add (target .toString ());
467
456
} else {
468
457
System .out .println ("*** Could not create " + out .getParent ());
0 commit comments