Path: blob/jdk8u272-b10-aarch32-20201026/jdk/make/non-build-utils/src/build/tools/commentchecker/CommentChecker.java
48795 views
/*1* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package build.tools.commentchecker;2627import java.io.*;28import java.util.StringTokenizer;2930/**31* CommentChecker is a utility which verifies that there aren't32* "/*" or "/**" tokens inside any comment blocks in one or more33* Java source files. Although it is legal to have beginning34* comment delimiters inside of a comment block (JLS 3.7), there35* have been errors where a dropped end-comment delimiter in a36* method'd doc-comment effectively "erased" that method. We're37* therefore restricting beginning comment delimiters inside of38* JDK source (at least the Swing team is for their portion).39*40* To scan a few files, run CommentChecker as follows:41*42* java CommentChecker file1.java file2.java ...43*44* There are too many Java files in the JDK base for most shells45* to support listing in a single command, so CommentChecker also46* supports cpio and tar-style filename passing, where "-"47* indicates that the list of files is read from stdin:48*49* find . -name SCCS -prune -o -name '*.java' -print | \50* java CommentChecker -51*52* @author Thomas Ball53*/54public class CommentChecker {5556static int errors = 0;5758// Turn on this flag and recompile to dump this tool's state changes.59final static boolean verbose = false;6061static void check(String fileName) {62BufferedReader in = null;63boolean inComment = false;64boolean inLineComment = false;65boolean inQuote = false;66boolean inEscape = false;67int lastChar = -1;68int lineNumber = 1;6970try {71in = new BufferedReader(new FileReader(fileName));72while (true) {73int ch = in.read();74if (ch == -1) {75if (inQuote || inComment) {76error(fileName + ": premature EOF.");77}78return;79}8081if (verbose) {82System.out.print((char)ch);83}8485switch (ch) {86case '\n':87if (inQuote && !inComment) {88error(fileName + ":" + lineNumber +89" dangling quote.");90inQuote = false;91}92if (inLineComment) {93inLineComment = false;94if (verbose) {95System.out.println("\ninLineComment=false");96}97}98lineNumber++;99break;100101case '\"':102if (!inComment && !inLineComment && !inEscape &&103!(!inQuote && lastChar == '\'')) {104inQuote = !inQuote;105if (verbose) {106System.out.println("\ninQuote=" + inQuote);107}108}109break;110111case '/':112if (!inQuote && lastChar == '*') {113inComment = false;114if (verbose) {115System.out.println("\ninComment=false");116}117}118if (!inQuote && lastChar == '/') {119inLineComment = true;120if (verbose) {121System.out.println("\ninLineComment=true");122}123}124break;125126case '*':127if (!inQuote && lastChar == '/') {128if (inComment) {129error(fileName + ":" + lineNumber +130" nested comment.");131}132inComment = true;133if (verbose) {134System.out.println("\ninComment=true");135}136}137break;138}139140lastChar = ch;141142// Watch for escaped characters, such as '\"'.143if (ch == '\\' && !inEscape) {144inEscape = true;145if (verbose) {146System.out.println("\ninEscape set");147}148} else {149inEscape = false;150}151}152} catch (FileNotFoundException fnfe) {153error(fileName + " not found.");154} catch (IOException ioe) {155error(fileName + ": " + ioe);156} finally {157if (in != null) {158try {159in.close();160} catch (IOException e) {161error(fileName + ": " + e);162}163}164}165}166167static void error(String description) {168System.err.println(description);169errors++;170}171172static void exit() {173if (errors != 1) {174System.out.println("There were " + errors + " errors.");175} else {176System.out.println("There was 1 error.");177}178System.exit(errors);179}180181public static void main(String[] args) {182if (args.length == 0) {183System.err.println("usage: java CommentChecker [-] file.java ...");184System.exit(1);185}186187if (args.length == 1 && args[0].equals("-")) {188/* read filenames in one per line from stdin, ala cpio.189* This is good for checking the whole JDK in one pass:190*191* cpio . -name SCCS -prune -o -name '*.java' -print | \192* java CommentChecker -193*/194try {195BufferedReader br =196new BufferedReader(new InputStreamReader(System.in));197while (true) {198String fileName = br.readLine();199if (fileName == null) {200break;201}202check(fileName);203}204br.close();205} catch (Exception e) {206error("error reading System.in: " + e);207}208} else {209for (int i = 0; i < args.length; i++) {210check(args[i]);211}212}213214exit();215}216}217218219