/** * Returns the Javadoc comment before the specified line. * A return value of {@code null} means there is no such comment. * @param lineNoBefore the line number to check before * @return the Javadoc comment, or {@code null} if none **/ public TextBlock getJavadocBefore(int lineNoBefore) { // Lines start at 1 to the callers perspective, so need to take off 2 int lineNo = lineNoBefore - 2; // skip blank lines while (lineNo > 0 && (lineIsBlank(lineNo) || lineIsComment(lineNo))) { lineNo--; } return javadocComments.get(lineNo); }
/** * Checks, whether there are empty lines within the specified line range. Line numbering is * started from 1 for parameter values * @param startLine number of the first line in the range * @param endLine number of the second line in the range * @return {@code true} if found any blank line within the range, {@code false} * otherwise */ private boolean hasEmptyLine(int startLine, int endLine) { // Initial value is false - blank line not found boolean result = false; final FileContents fileContents = getFileContents(); for (int line = startLine; line <= endLine; line++) { // Check, if the line is blank. Lines are numbered from 0, so subtract 1 if (fileContents.lineIsBlank(line - 1)) { result = true; break; } } return result; }
/** * Get list of empty lines. * @param ast the ast to check. * @return list of line numbers for empty lines. */ private List<Integer> getEmptyLines(DetailAST ast) { final DetailAST lastToken = ast.getLastChild().getLastChild(); int lastTokenLineNo = 0; if (lastToken != null) { // -1 as count starts from 0 // -2 as last token line cannot be empty, because it is a RCURLY lastTokenLineNo = lastToken.getLineNo() - 2; } final List<Integer> emptyLines = new ArrayList<>(); final FileContents fileContents = getFileContents(); for (int lineNo = ast.getLineNo(); lineNo <= lastTokenLineNo; lineNo++) { if (fileContents.lineIsBlank(lineNo)) { emptyLines.add(lineNo); } } return emptyLines; }
/** * Returns length of code only without comments and blank lines. * @param openingBrace block opening brace * @param closingBrace block closing brace * @return number of lines with code for current block */ private int getLengthOfBlock(DetailAST openingBrace, DetailAST closingBrace) { int length = closingBrace.getLineNo() - openingBrace.getLineNo() + 1; if (!countEmpty) { final FileContents contents = getFileContents(); final int lastLine = closingBrace.getLineNo(); // lastLine - 1 is actual last line index. Last line is line with curly brace, // which is always not empty. So, we make it lastLine - 2 to cover last line that // actually may be empty. for (int i = openingBrace.getLineNo() - 1; i <= lastLine - 2; i++) { if (contents.lineIsBlank(i) || contents.lineIsComment(i)) { length--; } } } return length; }
/** * Returns the Javadoc comment before the specified line. * A return value of {@code null} means there is no such comment. * @param lineNoBefore the line number to check before * @return the Javadoc comment, or {@code null} if none **/ public TextBlock getJavadocBefore(int lineNoBefore) { // Lines start at 1 to the callers perspective, so need to take off 2 int lineNo = lineNoBefore - 2; // skip blank lines while (lineNo > 0 && (lineIsBlank(lineNo) || lineIsComment(lineNo))) { lineNo--; } return javadocComments.get(lineNo); }
/** * Checks, whether there are empty lines within the specified line range. Line numbering is * started from 1 for parameter values * @param startLine number of the first line in the range * @param endLine number of the second line in the range * @return {@code true} if found any blank line within the range, {@code false} * otherwise */ private boolean hasEmptyLine(int startLine, int endLine) { // Initial value is false - blank line not found boolean result = false; final FileContents fileContents = getFileContents(); for (int line = startLine; line <= endLine; line++) { // Check, if the line is blank. Lines are numbered from 0, so subtract 1 if (fileContents.lineIsBlank(line - 1)) { result = true; break; } } return result; }
/** * Get list of empty lines. * @param ast the ast to check. * @return list of line numbers for empty lines. */ private List<Integer> getEmptyLines(DetailAST ast) { final DetailAST lastToken = ast.getLastChild().getLastChild(); int lastTokenLineNo = 0; if (lastToken != null) { // -1 as count starts from 0 // -2 as last token line cannot be empty, because it is a RCURLY lastTokenLineNo = lastToken.getLineNo() - 2; } final List<Integer> emptyLines = new ArrayList<>(); final FileContents fileContents = getFileContents(); for (int lineNo = ast.getLineNo(); lineNo <= lastTokenLineNo; lineNo++) { if (fileContents.lineIsBlank(lineNo)) { emptyLines.add(lineNo); } } return emptyLines; }
/** * Determines the number semicolons in a method excluding those in * comments. * @param method Method to count * @return The number of semicolons in the method as an int */ private int countSemiColons(final DetailAST method) { final DetailAST openingbrace = method.findFirstToken(TokenTypes.SLIST); int count = 0; if (openingbrace != null) { final DetailAST closingbrace = openingbrace.findFirstToken(TokenTypes.RCURLY); final int lastline = closingbrace.getLineNo(); final int firstline = openingbrace.getLineNo(); final FileContents contents = this.getFileContents(); for (int line = firstline - 1; line < lastline; line += 1) { if (!contents.lineIsBlank(line) && !contents.lineIsComment(line) && contents.getLine(line).contains(";")) { count += 1; } } } return count; } }
/** * Determines the number semicolons in a method excluding those in * comments. * @param method Method to count * @return The number of semicolons in the method as an int */ private int countSemiColons(final DetailAST method) { final DetailAST openingbrace = method.findFirstToken(TokenTypes.SLIST); int count = 0; if (openingbrace != null) { final DetailAST closingbrace = openingbrace.findFirstToken(TokenTypes.RCURLY); final int lastline = closingbrace.getLineNo(); final int firstline = openingbrace.getLineNo(); final FileContents contents = this.getFileContents(); for (int line = firstline - 1; line < lastline; line += 1) { if (!contents.lineIsBlank(line) && !contents.lineIsComment(line) && contents.getLine(line).contains(";")) { count += 1; } } } return count; } }
/** * Returns length of code only without comments and blank lines. * @param openingBrace block opening brace * @param closingBrace block closing brace * @return number of lines with code for current block */ private int getLengthOfBlock(DetailAST openingBrace, DetailAST closingBrace) { int length = closingBrace.getLineNo() - openingBrace.getLineNo() + 1; if (!countEmpty) { final FileContents contents = getFileContents(); final int lastLine = closingBrace.getLineNo(); // lastLine - 1 is actual last line index. Last line is line with curly brace, // which is always not empty. So, we make it lastLine - 2 to cover last line that // actually may be empty. for (int i = openingBrace.getLineNo() - 1; i <= lastLine - 2; i++) { if (contents.lineIsBlank(i) || contents.lineIsComment(i)) { length--; } } } return length; }