Skip to content

Commit 931f050

Browse files
committed
Unify and clean-up code of JavadocBasher
1 parent 0518519 commit 931f050

1 file changed

Lines changed: 71 additions & 101 deletions

File tree

  • bundles/org.eclipse.swt.tools/JavadocBasher/org/eclipse/swt/tools/internal

bundles/org.eclipse.swt.tools/JavadocBasher/org/eclipse/swt/tools/internal/JavadocBasher.java

Lines changed: 71 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.nio.file.*;
55
import java.util.*;
66
import java.util.Map.*;
7+
import java.util.stream.*;
78

89
import org.eclipse.jdt.core.dom.*;
910
import org.eclipse.jface.text.*;
@@ -61,25 +62,12 @@
6162
*/
6263
public class JavadocBasher {
6364
static final boolean fVerbose = false; // set to true for verbose output
64-
List<String> fBashed;
65-
List<String> fUnchanged;
66-
List<String> fSkipped;
6765

68-
public JavadocBasher() {
69-
fBashed = new ArrayList<>();
70-
fUnchanged = new ArrayList<>();
71-
fSkipped = new ArrayList<>();
72-
}
73-
74-
public static class Edit {
75-
int start, length;
76-
String text;
66+
final List<File> fBashed = new ArrayList<>();
67+
final List<File> fUnchanged = new ArrayList<>();
68+
final List<String> fSkipped = new ArrayList<>();
7769

78-
public Edit(int start, int length, String text) {
79-
this.start = start;
80-
this.length = length;
81-
this.text = text;
82-
}
70+
record Edit(int start, int length, String text) {
8371
}
8472

8573
public static void main(String[] args) {
@@ -124,14 +112,13 @@ public static void main(String[] args) {
124112
JavadocBasher basher = new JavadocBasher();
125113
System.out.println("\n==== Start Bashing " + targetSubdir);
126114
basher.bashJavaSourceTree(source, target, out);
127-
List<String> bashedList = basher.getBashed();
128-
basher.status("Bashed", bashedList, targetSubdir);
129-
if (!bashedList.isEmpty()) {
130-
totalBashed += bashedList.size();
131-
if (fVerbose)
132-
basher.status("Didn't change", basher.getUnchanged(),
133-
targetSubdir);
134-
basher.status("Skipped", basher.getSkipped(), targetSubdir);
115+
basher.status("Bashed", basher.fBashed, targetSubdir);
116+
if (!basher.fBashed.isEmpty()) {
117+
totalBashed += basher.fBashed.size();
118+
if (fVerbose) {
119+
basher.status("Didn't change", basher.fUnchanged, targetSubdir);
120+
}
121+
basher.status("Skipped", basher.fSkipped, targetSubdir);
135122
}
136123
System.out.println("==== Done Bashing " + targetSubdir);
137124
}
@@ -140,14 +127,15 @@ public static void main(String[] args) {
140127
+ " files in total) - Be sure to Refresh (F5) project(s) ====");
141128
}
142129

143-
void status(String label, List<String> list, String targetSubdir) {
130+
void status(String label, List<?> list, String targetSubdir) {
144131
int count = list.size();
145132
System.out.println(label + " " + count
146133
+ ((count == 1) ? " file" : " files") + " in " + targetSubdir
147134
+ ((count > 0) ? ":" : "."));
148135
if (count > 0) {
149-
for(String s : list)
136+
for (Object s : list) {
150137
System.out.println(label + ": " + s);
138+
}
151139
System.out.println();
152140
}
153141
}
@@ -220,7 +208,6 @@ void bashJavaSourceTree(File sourceDir, File targetDir, File outDir) {
220208
}
221209
}
222210

223-
224211
void bashFile(final File source, final File target, File out) {
225212
char[] contents = readFile(source);
226213
if (contents == null) return;
@@ -236,63 +223,50 @@ void bashFile(final File source, final File target, File out) {
236223
parser.setSource(contents);
237224
ASTNode targetUnit = parser.createAST(null);
238225

239-
final HashMap<String, String> comments = new HashMap<>();
226+
final Map<String, String> comments = new HashMap<>();
240227
sourceUnit.accept(new ASTVisitor() {
241228
String prefix = "";
242229
@Override
243230
public boolean visit(Block node) {
244231
return false;
245232
}
233+
246234
@Override
247235
public boolean visit(VariableDeclarationFragment node) {
248236
FieldDeclaration field = (FieldDeclaration)node.getParent();
249-
int mods = field.getModifiers();
250-
if (Modifier.isPublic(mods) || Modifier.isProtected(mods)) {
237+
if (isExternallyVisible(field)) {
251238
Javadoc javadoc = field.getJavadoc();
252239
if (field.fragments().size() > 1 && javadoc != null) {
253240
System.err.println("Field declaration with multiple variables is not supported. -> " + source + " " + node.getName().getFullyQualifiedName());
254241
}
255-
try {
256-
String key = prefix + "." + node.getName().getFullyQualifiedName();
257-
comments.put(key, javadoc != null ? sourceDocument.get(javadoc.getStartPosition(), getJavadocLength(sourceDocument, javadoc)) : "");
258-
} catch (BadLocationException e) {}
242+
addComment(sourceDocument, comments, prefix, node.getName(), javadoc, "");
259243
return true;
260244
}
261245
return false;
262246
}
247+
263248
@Override
264249
public boolean visit(MethodDeclaration node) {
265-
int mods = node.getModifiers();
266-
if (Modifier.isPublic(mods) || Modifier.isProtected(mods)) {
250+
if (isExternallyVisible(node)) {
267251
Javadoc javadoc = node.getJavadoc();
268-
try {
269-
String key = prefix + "." + node.getName().getFullyQualifiedName();
270-
for (Iterator<SingleVariableDeclaration> iterator = node.parameters().iterator(); iterator.hasNext();) {
271-
SingleVariableDeclaration param = iterator.next();
272-
key += param.getType().toString();
273-
}
274-
comments.put(key, javadoc != null ? sourceDocument.get(javadoc.getStartPosition(), getJavadocLength(sourceDocument, javadoc)) : "");
275-
} catch (BadLocationException e) {}
252+
addComment(sourceDocument, comments, prefix, node.getName(), javadoc, parameterSuffix(node));
276253
return true;
277254
}
278255
return false;
279256
}
257+
280258
@Override
281259
public boolean visit(TypeDeclaration node) {
282-
int mods = node.getModifiers();
283-
if (Modifier.isPublic(mods) || Modifier.isProtected(mods)) {
260+
if (isExternallyVisible(node)) {
284261
Javadoc javadoc = node.getJavadoc();
285-
try {
286-
String key = prefix + "." + node.getName().getFullyQualifiedName();
287-
comments.put(key, javadoc != null ? sourceDocument.get(javadoc.getStartPosition(), getJavadocLength(sourceDocument, javadoc)) : "");
288-
} catch (BadLocationException e) {}
262+
addComment(sourceDocument, comments, prefix, node.getName(), javadoc, "");
289263
prefix = node.getName().getFullyQualifiedName();
290264
return true;
291265
}
292266
return false;
293267
}
294-
});
295268

269+
});
296270

297271
final List<Edit> edits = new ArrayList<>();
298272
targetUnit.accept(new ASTVisitor() {
@@ -304,73 +278,53 @@ public boolean visit(Block node) {
304278
@Override
305279
public boolean visit(VariableDeclarationFragment node) {
306280
FieldDeclaration field = (FieldDeclaration)node.getParent();
307-
int mods = field.getModifiers();
308-
if (Modifier.isPublic(mods) || Modifier.isProtected(mods)) {
281+
if (isExternallyVisible(field)) {
309282
Javadoc javadoc = field.getJavadoc();
310283
if (field.fragments().size() > 1 && javadoc != null) {
311284
System.err.println("Field declaration with multiple variables is not supported. -> " + target + " " + node.getName().getFullyQualifiedName());
312285
}
313286
String key = prefix + "." + node.getName().getFullyQualifiedName();
314-
String newComment = comments.get(key);
287+
String newComment = comments.remove(key);
315288
if (newComment != null) {
316-
comments.remove(key);
317-
if (javadoc != null) {
318-
edits.add(new Edit(javadoc.getStartPosition(), getJavadocLength(targetDocument, javadoc), newComment));
319-
} else {
320-
edits.add(new Edit(field.getStartPosition(), 0, newComment));
321-
}
289+
edits.add(createJavaDocEdit(field, javadoc, newComment, targetDocument));
322290
}
323291
return true;
324292
}
325293
return false;
326294
}
295+
327296
@Override
328297
public boolean visit(MethodDeclaration node) {
329-
int mods = node.getModifiers();
330-
if (Modifier.isPublic(mods) || Modifier.isProtected(mods)) {
298+
if (isExternallyVisible(node)) {
331299
Javadoc javadoc = node.getJavadoc();
332-
String key = prefix + "." + node.getName().getFullyQualifiedName();
333-
for (Iterator<SingleVariableDeclaration> iterator = node.parameters().iterator(); iterator.hasNext();) {
334-
SingleVariableDeclaration param = iterator.next();
335-
key += param.getType().toString();
336-
}
337-
String newComment = comments.get(key);
300+
String key = prefix + "." + node.getName().getFullyQualifiedName() + parameterSuffix(node);
301+
String newComment = comments.remove(key);
338302
if (newComment != null) {
339-
comments.remove(key);
340-
if (javadoc != null) {
341-
edits.add(new Edit(javadoc.getStartPosition(), getJavadocLength(targetDocument, javadoc), newComment));
342-
} else {
343-
edits.add(new Edit(node.getStartPosition(), 0, newComment));
344-
}
303+
edits.add(createJavaDocEdit(node, javadoc, newComment, targetDocument));
345304
}
346305
return true;
347306
}
348307
return false;
349308
}
309+
350310
@Override
351311
public boolean visit(TypeDeclaration node) {
352-
int mods = node.getModifiers();
353-
if (Modifier.isPublic(mods) || Modifier.isProtected(mods)) {
312+
if (isExternallyVisible(node)) {
354313
Javadoc javadoc = node.getJavadoc();
355314
String key = prefix + "." + node.getName().getFullyQualifiedName();
356-
String newComment = comments.get(key);
315+
String newComment = comments.remove(key);
357316
if (newComment != null) {
358-
comments.remove(key);
359-
if (javadoc != null) {
360-
edits.add(new Edit(javadoc.getStartPosition(), getJavadocLength(targetDocument, javadoc), newComment));
361-
} else {
362-
edits.add(new Edit(node.getStartPosition(), 0, newComment));
363-
}
317+
edits.add(createJavaDocEdit(node, javadoc, newComment, targetDocument));
364318
}
365319
prefix = node.getName().getFullyQualifiedName();
366320
return true;
367321
}
368322
return false;
369323
}
324+
370325
});
371326

372-
for (int i = edits.size() - 1; i >=0 ; i--) {
373-
Edit edit = edits.get(i);
327+
for (Edit edit : edits.reversed()) {
374328
try {
375329
targetDocument.replace(edit.start, edit.length, edit.text);
376330
} catch (BadLocationException e) {
@@ -452,19 +406,47 @@ public boolean visit(TypeDeclaration node) {
452406
if (!targetContents.equals(newContents)) {
453407
if (makeDirectory(out.getParentFile())) {
454408
writeFile(newContents, out);
455-
fBashed.add(target.toString());
409+
fBashed.add(target);
456410
} else {
457411
System.out.println("*** Could not create " + out.getParent());
458412
}
459413
} else {
460-
fUnchanged.add(target.toString());
414+
fUnchanged.add(target);
415+
}
416+
}
417+
418+
static boolean isExternallyVisible(BodyDeclaration node) {
419+
int mods = node.getModifiers();
420+
return Modifier.isPublic(mods) || Modifier.isProtected(mods);
421+
}
422+
423+
void addComment(Document sourceDocument, Map<String, String> comments, String prefix, SimpleName name,
424+
Javadoc javadoc, String suffix) {
425+
try {
426+
String key = prefix + "." + name.getFullyQualifiedName() + suffix;
427+
String doc = javadoc != null
428+
? sourceDocument.get(javadoc.getStartPosition(), getJavadocLength(sourceDocument, javadoc))
429+
: "";
430+
comments.put(key, doc);
431+
} catch (BadLocationException e) {
461432
}
462433
}
463434

435+
static String parameterSuffix(MethodDeclaration node) {
436+
List<SingleVariableDeclaration> parameters = node.parameters();
437+
return parameters.stream().map(p -> p.getType().toString()).collect(Collectors.joining());
438+
}
439+
440+
Edit createJavaDocEdit(ASTNode node, Javadoc javadoc, String newComment, Document targetDocument) {
441+
int startPosition = javadoc != null ? javadoc.getStartPosition() : node.getStartPosition();
442+
int length = javadoc != null ? getJavadocLength(targetDocument, javadoc) : 0;
443+
return new Edit(startPosition, length, newComment);
444+
}
445+
464446
int getJavadocLength(Document sourceDocument, Javadoc javadoc) {
465447
return skipWhitespace(sourceDocument, javadoc.getStartPosition() + javadoc.getLength()) - javadoc.getStartPosition();
466448
}
467-
449+
468450
int skipWhitespace(Document doc, int offset) {
469451
try {
470452
while (Character.isWhitespace(doc.getChar(offset))){
@@ -480,16 +462,4 @@ boolean makeDirectory(File directory) {
480462
return true;
481463
return directory.mkdirs();
482464
}
483-
484-
List<String> getBashed() {
485-
return fBashed;
486-
}
487-
488-
List<String> getUnchanged() {
489-
return fUnchanged;
490-
}
491-
492-
List<String> getSkipped() {
493-
return fSkipped;
494-
}
495465
}

0 commit comments

Comments
 (0)