Skip to content

Commit

Permalink
Issue #11507: remove usages of getFileContents.inPackageInfo method, …
Browse files Browse the repository at this point in the history
…add getFilePath method to api
  • Loading branch information
strkkk authored and rnveach committed Apr 12, 2022
1 parent fcba302 commit f6a15cf
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 43 deletions.
5 changes: 0 additions & 5 deletions config/suppressions.xml
Expand Up @@ -101,26 +101,21 @@
files="[\\/]src[\\/]xdocs[\\/]beginning_development.xml"/>

<!-- these are legacy use cases until #11166 -->
<suppress id="noUsageOfGetFileContentsMethod" files="PackageAnnotationCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="FallThroughCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="PackageDeclarationCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="ImportControlCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="UnusedImportsCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="JavadocMethodCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="JavadocStyleCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="JavadocTypeCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="JavadocVariableCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="MissingJavadocMethodCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="MissingJavadocPackageCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="MissingJavadocTypeCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="WriteTagCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="RegexpCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="RegexpSinglelineJavaCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="MethodLengthCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="EmptyLineSeparatorCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="AvoidEscapedUnicodeCharactersCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="OuterTypeFilenameCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="JavadocMetadataScraper.java"/>

<!-- until https://github.com/checkstyle/checkstyle/issues/5234 -->
<suppress id="MatchXPathBranchContains" files="[\\/]DetailAstImplTest.java"/>
Expand Down
2 changes: 2 additions & 0 deletions pom.xml
Expand Up @@ -3376,6 +3376,8 @@
<param>com.puppycrawl.tools.checkstyle.api.*</param>
<!-- 1% mutation in FullIdent -->
<param>com.puppycrawl.tools.checkstyle.checks.coding.UnusedLocalVariableCheckTest</param>
<!-- 1% mutation in CheckUtil -->
<param>com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheckTest</param>
</targetTests>
<excludedTestClasses>
<param>*.Input*</param>
Expand Down
Expand Up @@ -298,6 +298,15 @@ public final String getLine(int index) {
return context.get().fileContents.getLine(index);
}

/**
* Returns full path to the file.
*
* @return full path to file.
*/
public final String getFilePath() {
return context.get().fileContents.getFileName();
}

/**
* Returns code point representation of file text from given line number.
*
Expand Down
Expand Up @@ -28,6 +28,7 @@
import java.util.regex.Pattern;

import com.puppycrawl.tools.checkstyle.grammar.CommentListener;
import com.puppycrawl.tools.checkstyle.utils.CheckUtil;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;

/**
Expand Down Expand Up @@ -328,9 +329,11 @@ public Map<Integer, List<TextBlock>> getBlockComments() {
* Checks if the current file is a package-info.java file.
*
* @return true if the package file.
* @deprecated use {@link CheckUtil#isPackageInfo(String)} for the same functionality,
* or use {@link AbstractCheck#getFilePath()} to process your own standards.
*/
@Deprecated(since = "10.2")
public boolean inPackageInfo() {
return getFileName().endsWith("package-info.java");
}

}
Expand Up @@ -130,7 +130,7 @@ public int[] getRequiredTokens() {

@Override
public void beginTree(DetailAST rootAST) {
fileName = getFileName();
fileName = getSourceFileName();
seenFirstToken = false;
hasPublic = false;
wrongType = null;
Expand Down Expand Up @@ -167,10 +167,8 @@ public void finishTree(DetailAST rootAST) {
*
* @return source file name.
*/
// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
@SuppressWarnings("deprecation")
private String getFileName() {
String name = getFileContents().getFileName();
private String getSourceFileName() {
String name = getFilePath();
name = name.substring(name.lastIndexOf(File.separatorChar) + 1);
return FILE_EXTENSION_PATTERN.matcher(name).replaceAll("");
}
Expand Down
Expand Up @@ -24,6 +24,7 @@
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil;
import com.puppycrawl.tools.checkstyle.utils.CheckUtil;

/**
* <p>
Expand Down Expand Up @@ -102,16 +103,12 @@ public int[] getAcceptableTokens() {
return getRequiredTokens();
}

// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
@SuppressWarnings("deprecation")
@Override
public void visitToken(final DetailAST ast) {
final boolean containsAnnotation =
AnnotationUtil.containsAnnotation(ast);
final boolean inPackageInfo =
getFileContents().inPackageInfo();

if (containsAnnotation && !inPackageInfo) {
if (containsAnnotation && !CheckUtil.isPackageInfo(getFilePath())) {
log(ast, MSG_KEY);
}
}
Expand Down
Expand Up @@ -179,10 +179,8 @@ public void visitToken(DetailAST ast) {
*
* @return Directory name.
*/
// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
@SuppressWarnings("deprecation")
private String getDirectoryName() {
final String fileName = getFileContents().getFileName();
final String fileName = getFilePath();
final int lastSeparatorPos = fileName.lastIndexOf(File.separatorChar);
return fileName.substring(0, lastSeparatorPos);
}
Expand Down
Expand Up @@ -521,7 +521,7 @@ public int[] getRequiredTokens() {
@Override
public void beginTree(DetailAST rootAST) {
currentImportControl = null;
processCurrentFile = path.matcher(getFileContents().getFileName()).find();
processCurrentFile = path.matcher(getFilePath()).find();
fileName = getFileContents().getText().getFile().getName();

final int period = fileName.lastIndexOf('.');
Expand Down
Expand Up @@ -416,13 +416,11 @@ public void visitToken(DetailAST ast) {
* @param ast a given node.
* @return whether we should check a given node.
*/
// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
@SuppressWarnings("deprecation")
private boolean shouldCheck(final DetailAST ast) {
boolean check = false;

if (ast.getType() == TokenTypes.PACKAGE_DEF) {
check = getFileContents().inPackageInfo();
check = CheckUtil.isPackageInfo(getFilePath());
}
else if (!ScopeUtil.isInCodeBlock(ast)) {
final Scope customScope = ScopeUtil.getScope(ast);
Expand All @@ -447,15 +445,13 @@ else if (!ScopeUtil.isInCodeBlock(ast)) {
* @see #checkFirstSentenceEnding(DetailAST, TextBlock)
* @see #checkHtmlTags(DetailAST, TextBlock)
*/
// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
@SuppressWarnings("deprecation")
private void checkComment(final DetailAST ast, final TextBlock comment) {
if (comment == null) {
// checking for missing docs in JavadocStyleCheck is not consistent
// with the rest of CheckStyle... Even though, I didn't think it
// made sense to make another check just to ensure that the
// package-info.java file actually contains package Javadocs.
if (getFileContents().inPackageInfo()) {
if (CheckUtil.isPackageInfo(getFilePath())) {
log(ast, MSG_JAVADOC_MISSING);
}
}
Expand Down
Expand Up @@ -24,8 +24,8 @@
import com.puppycrawl.tools.checkstyle.StatelessCheck;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FileContents;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.CheckUtil;
import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;

/**
Expand Down Expand Up @@ -99,12 +99,9 @@ public boolean isCommentNodesRequired() {
return true;
}

// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
@SuppressWarnings("deprecation")
@Override
public void visitToken(DetailAST ast) {
final FileContents contents = getFileContents();
if (contents.inPackageInfo() && !hasJavadoc(ast)) {
if (CheckUtil.isPackageInfo(getFilePath()) && !hasJavadoc(ast)) {
log(ast, MSG_PKG_JAVADOC_MISSING);
}
}
Expand Down
Expand Up @@ -29,6 +29,7 @@
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FileContents;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.CheckUtil;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
Expand Down Expand Up @@ -641,11 +642,9 @@ private boolean hasMultipleLinesBefore(DetailAST ast) {
* @param ast token
* @param nextToken next token
*/
// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
@SuppressWarnings("deprecation")
private void processPackage(DetailAST ast, DetailAST nextToken) {
if (ast.getLineNo() > 1 && !hasEmptyLineBefore(ast)) {
if (getFileContents().getFileName().endsWith("package-info.java")) {
if (CheckUtil.isPackageInfo(getFilePath())) {
if (!ast.getFirstChild().hasChildren() && !isPrecededByJavadoc(ast)) {
log(ast, MSG_SHOULD_BE_SEPARATED, ast.getText());
}
Expand Down
Expand Up @@ -48,8 +48,6 @@
/**
* Class for scraping module metadata from the corresponding class' class-level javadoc.
*/
// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
@SuppressWarnings("deprecation")
@FileStatefulCheck
public class JavadocMetadataScraper extends AbstractJavadocCheck {

Expand Down Expand Up @@ -188,8 +186,6 @@ public int[] getRequiredJavadocTokens() {
return getAcceptableJavadocTokens();
}

// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
@SuppressWarnings("deprecation")
@Override
public void beginJavadocTree(DetailNode rootAst) {
if (isTopLevelClassJavadoc()) {
Expand All @@ -200,15 +196,14 @@ public void beginJavadocTree(DetailNode rootAst) {
exampleSectionStartIdx = -1;
parentSectionStartIdx = -1;

final String filePath = getFileContents().getFileName();
String moduleName = getModuleSimpleName();
final String checkModuleExtension = "Check";
if (moduleName.endsWith(checkModuleExtension)) {
moduleName = moduleName
.substring(0, moduleName.length() - checkModuleExtension.length());
}
moduleDetails.setName(moduleName);
moduleDetails.setFullQualifiedName(getPackageName(filePath));
moduleDetails.setFullQualifiedName(getPackageName(getFilePath()));
moduleDetails.setModuleType(getModuleType());
}
}
Expand Down Expand Up @@ -596,10 +591,8 @@ else if (simpleModuleName.endsWith("Filter")) {
*
* @return simple module name
*/
// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
@SuppressWarnings("deprecation")
private String getModuleSimpleName() {
final String fullFileName = getFileContents().getFileName();
final String fullFileName = getFilePath();
final String[] pathTokens = FILE_SEPARATOR_PATTERN.split(fullFileName);
final String fileName = pathTokens[pathTokens.length - 1];
return fileName.substring(0, fileName.length() - JAVA_FILE_EXTENSION.length());
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/puppycrawl/tools/checkstyle/utils/CheckUtil.java
Expand Up @@ -680,4 +680,14 @@ public static String getShortNameOfAnonInnerClass(DetailAST literalNewAst) {
final DetailAST firstChild = parentAst.getFirstChild();
return extractQualifiedName(firstChild);
}

/**
* Checks if the given file path is a package-info.java file.
*
* @param filePath path to the file.
* @return true if the package file.
*/
public static boolean isPackageInfo(String filePath) {
return filePath.endsWith("package-info.java");
}
}

0 comments on commit f6a15cf

Please sign in to comment.