Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@
/**
* The base class for checks.
*
* <p>
* @see <a href="{@docRoot}/../writingchecks.html" target="_top">Writing
* your own checks</a>
* </p>
*
* @noinspection NoopMethodInAbstractClass
* @noinspectionreason NoopMethodInAbstractClass - we allow each check to
* define these methods, as needed. They should be overridden only
Expand Down Expand Up @@ -120,14 +123,14 @@ public final Set<String> getTokenNames() {
* @return the sorted set of {@link Violation}.
*/
public SortedSet<Violation> getViolations() {
return new TreeSet<>(context.get().violations);
return new TreeSet<>(getContext().violations);
}

/**
* Clears the sorted set of {@link Violation} of the check.
*/
public final void clearViolations() {
context.get().violations.clear();
getContext().violations.clear();
}

/**
Expand Down Expand Up @@ -175,7 +178,7 @@ public void visitToken(DetailAST ast) {
}

/**
* Called after all the child nodes have been process.
* Called after all the child nodes have been processed.
*
* @param ast the token leaving
*/
Expand All @@ -189,7 +192,7 @@ public void leaveToken(DetailAST ast) {
* @param contents the manager
*/
public final void setFileContents(FileContents contents) {
context.get().fileContents = contents;
getContext().fileContents = contents;
}

/**
Expand All @@ -204,7 +207,7 @@ public final void setFileContents(FileContents contents) {
*/
@Deprecated(since = "9.3")
public final FileContents getFileContents() {
return context.get().fileContents;
return getContext().fileContents;
}

/**
Expand All @@ -220,14 +223,18 @@ protected final int getTabWidth() {
* Set the tab width to report audit events with.
*
* @param tabWidth an {@code int} value
* @throws IllegalArgumentException if tabWidth is negative
*/
public final void setTabWidth(int tabWidth) {
if (tabWidth < 0) {
throw new IllegalArgumentException("tabWidth must not be negative: " + tabWidth);
}
this.tabWidth = tabWidth;
}

@Override
public final void log(int line, String key, Object... args) {
context.get().violations.add(
getContext().violations.add(
new Violation(
line,
getMessageBundle(),
Expand All @@ -244,7 +251,7 @@ public final void log(int lineNo, int colNo, String key,
Object... args) {
final int col = 1 + CommonUtil.lengthExpandedTabs(
getLines()[lineNo - 1], colNo, tabWidth);
context.get().violations.add(
getContext().violations.add(
new Violation(
lineNo,
col,
Expand Down Expand Up @@ -274,7 +281,7 @@ public final void log(DetailAST ast, String key, Object... args) {

final int col = 1 + CommonUtil.lengthExpandedTabs(
getLines()[ast.getLineNo() - 1], ast.getColumnNo(), tabWidth);
context.get().violations.add(
getContext().violations.add(
new Violation(
ast.getLineNo(),
col,
Expand All @@ -292,20 +299,30 @@ public final void log(DetailAST ast, String key, Object... args) {
/**
* Returns the lines associated with the tree.
*
* @return the file contents
* @return the file contents lines; never null (empty array if not set)
*/
public final String[] getLines() {
return context.get().fileContents.getLines();
final FileContents fc = getContext().fileContents;
if (fc == null) {
return new String[0];
}
final String[] lines = fc.getLines();
return lines == null ? new String[0] : lines;
}

/**
* Returns the line associated with the tree.
*
* @param index index of the line
* @return the line from the file contents
* @return the line from the file contents, or empty string if not available
*/
public final String getLine(int index) {
return context.get().fileContents.getLine(index);
final FileContents fc = getContext().fileContents;
if (fc == null) {
return "";
}
final String line = fc.getLine(index);
return line == null ? "" : line;
}

/**
Expand All @@ -314,17 +331,33 @@ public final String getLine(int index) {
* @return full path to file.
*/
public final String getFilePath() {
return context.get().fileContents.getFileName();
final FileContents fc = getContext().fileContents;
return fc == null ? "" : fc.getFileName();
}

/**
* Returns code point representation of file text from given line number.
*
* @param index index of the line
* @return the array of Unicode code points
* @return the array of Unicode code points (empty array if line not available)
*/
public final int[] getLineCodePoints(int index) {
return getLine(index).codePoints().toArray();
final String line = getLine(index);
if (line == null || line.isEmpty()) {
return new int[0];
}
return line.codePoints().toArray();
}

/**
* Protected accessor for the ThreadLocal FileContext.
* Using this method centralizes ThreadLocal access and makes future
* thread-safety audits or changes easier.
*
* @return the current thread's FileContext
*/
protected final FileContext getContext() {
return context.get();
}

/**
Expand Down
34 changes: 25 additions & 9 deletions src/main/java/com/puppycrawl/tools/checkstyle/api/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@
package com.puppycrawl.tools.checkstyle.api;

import java.util.Arrays;
import java.util.Objects;

/**
* Representation of the comment block.
*
* <p>This class stores the text and positional metadata of a comment block
* found in source code. It implements {@link TextBlock} to provide information
* about where the comment starts and ends, both in terms of lines and columns.
*/
public class Comment implements TextBlock {

Expand All @@ -43,15 +47,21 @@ public class Comment implements TextBlock {
private final int endColNo;

/**
* Creates new instance.
* Creates new instance of {@code Comment}.
*
* @param text the lines that make up the comment.
* @param firstCol number of the first column of the comment.
* @param lastLine number of the last line of the comment.
* @param lastCol number of the last column of the comment.
* @param text the lines that make up the comment (must not be null)
* @param firstCol number of the first column of the comment
* @param lastLine number of the last line of the comment
* @param lastCol number of the last column of the comment
* @throws IllegalArgumentException if text is null or empty
*/
public Comment(final String[] text, final int firstCol,
final int lastLine, final int lastCol) {
final int lastLine, final int lastCol) {
// ✅ Enhancement 1: Basic validation
if (text == null || text.length == 0) {
throw new IllegalArgumentException("Comment text must not be null or empty");
}

this.text = text.clone();
startLineNo = lastLine - text.length + 1;
endLineNo = lastLine;
Expand Down Expand Up @@ -84,11 +94,18 @@ public int getEndColNo() {
return endColNo;
}

/**
* Checks whether this comment block intersects with a given range of lines and columns.
*
* @param startLine start line of the range
* @param startCol start column of the range
* @param endLine end line of the range
* @param endCol end column of the range
* @return true if the ranges overlap; false otherwise
*/
@Override
public boolean intersects(int startLine, int startCol,
int endLine, int endCol) {
// compute a single number for start and end
// to simplify conditional logic
final long multiplier = Integer.MAX_VALUE;
final long thisStart = startLineNo * multiplier + startColNo;
final long thisEnd = endLineNo * multiplier + endColNo;
Expand All @@ -106,5 +123,4 @@ public String toString() {
+ ", startColNo=" + startColNo
+ ", endColNo=" + endColNo + ']';
}

}
Loading