| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.build.code; |
| 7 |
|
| 8 |
import java.io.IOException; |
| 9 |
import java.nio.file.DirectoryStream; |
| 10 |
import java.nio.file.Files; |
| 11 |
import java.nio.file.Path; |
| 12 |
import java.nio.file.Paths; |
| 13 |
import java.util.List; |
| 14 |
|
| 15 |
/** |
| 16 |
* This tool checks that for each .java file there is a package.html file, |
| 17 |
* that for each .java file there is at least one (class level) javadoc comment, |
| 18 |
* and that lines with comments are not too long. |
| 19 |
*/ |
| 20 |
public class CheckJavadoc { |
| 21 |
|
| 22 |
private static final int MAX_COMMENT_LINE_SIZE = 100; |
| 23 |
private static final int MAX_SOURCE_LINE_SIZE = 120; |
| 24 |
private int errorCount; |
| 25 |
|
| 26 |
/** |
| 27 |
* This method is called when executing this application from the command |
| 28 |
* line. |
| 29 |
* |
| 30 |
* @param args the command line parameters |
| 31 |
*/ |
| 32 |
public static void main(String... args) throws Exception { |
| 33 |
new CheckJavadoc().run(); |
| 34 |
} |
| 35 |
|
| 36 |
private void run() throws Exception { |
| 37 |
check(Paths.get("src")); |
| 38 |
if (errorCount > 0) { |
| 39 |
throw new Exception(errorCount + " errors found"); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
private int check(Path file) throws Exception { |
| 44 |
String name = file.getFileName().toString(); |
| 45 |
if (Files.isDirectory(file)) { |
| 46 |
boolean foundPackageHtml = false, foundJava = false; |
| 47 |
try (DirectoryStream<Path> stream = Files.newDirectoryStream(file)) { |
| 48 |
for (Path f : stream) { |
| 49 |
int type = check(f); |
| 50 |
if (type == 1) { |
| 51 |
foundJava = true; |
| 52 |
} else if (type == 2) { |
| 53 |
foundPackageHtml = true; |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
if (foundJava && !foundPackageHtml) { |
| 58 |
System.out.println("No package.html file, but a Java file found at: " + file.toAbsolutePath()); |
| 59 |
errorCount++; |
| 60 |
} |
| 61 |
} else { |
| 62 |
if (name.endsWith(".java")) { |
| 63 |
checkJavadoc(file); |
| 64 |
return 1; |
| 65 |
} else if (name.equals("package.html")) { |
| 66 |
return 2; |
| 67 |
} |
| 68 |
} |
| 69 |
return 0; |
| 70 |
} |
| 71 |
|
| 72 |
private void checkJavadoc(Path file) throws IOException { |
| 73 |
List<String> lines = Files.readAllLines(file); |
| 74 |
boolean inComment = false, hasJavadoc = false; |
| 75 |
for (int lineNumber = 0, size = lines.size(); lineNumber < size;) { |
| 76 |
String rawLine = lines.get(lineNumber++); |
| 77 |
String line = rawLine.trim(); |
| 78 |
if (line.startsWith("/*")) { |
| 79 |
if (!hasJavadoc && line.startsWith("/**")) { |
| 80 |
hasJavadoc = true; |
| 81 |
} |
| 82 |
inComment = true; |
| 83 |
} |
| 84 |
int rawLength = rawLine.length(); |
| 85 |
if (inComment) { |
| 86 |
int i = line.indexOf("*/", 2); |
| 87 |
if (i >= 0) { |
| 88 |
inComment = false; |
| 89 |
} |
| 90 |
if (i == rawLength - 2 && rawLength > MAX_COMMENT_LINE_SIZE |
| 91 |
&& !line.trim().startsWith("* http://") |
| 92 |
&& !line.trim().startsWith("* https://")) { |
| 93 |
System.out.println("Long line: " + file.toAbsolutePath() |
| 94 |
+ " (" + file.getFileName() + ":" + lineNumber + ")"); |
| 95 |
errorCount++; |
| 96 |
} |
| 97 |
} |
| 98 |
if (!inComment && line.startsWith("//")) { |
| 99 |
if (rawLength > MAX_COMMENT_LINE_SIZE |
| 100 |
&& !line.trim().startsWith("// http://") |
| 101 |
&& !line.trim().startsWith("// https://")) { |
| 102 |
System.out.println("Long line: " + file.toAbsolutePath() |
| 103 |
+ " (" + file.getFileName() + ":" + lineNumber + ")"); |
| 104 |
errorCount++; |
| 105 |
} |
| 106 |
} else if (!inComment && rawLength > MAX_SOURCE_LINE_SIZE) { |
| 107 |
System.out.println("Long line: " + file.toAbsolutePath() |
| 108 |
+ " (" + file.getFileName() + ":" + lineNumber + ")"); |
| 109 |
errorCount++; |
| 110 |
} |
| 111 |
} |
| 112 |
if (!hasJavadoc) { |
| 113 |
System.out.println("No Javadoc comment: " + file.toAbsolutePath()); |
| 114 |
errorCount++; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
} |