-
Notifications
You must be signed in to change notification settings - Fork 145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue #598: Forbid field access check implemented #813
Open
yaziza
wants to merge
1
commit into
sevntu-checkstyle:master
Choose a base branch
from
yaziza:598-forbid-field-access
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
198 changes: 198 additions & 0 deletions
198
...ecks/src/main/java/com/github/sevntu/checkstyle/checks/coding/ForbidFieldAccessCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2020 the original author or authors. | ||
// | ||
// This library is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public | ||
// License as published by the Free Software Foundation; either | ||
// version 2.1 of the License, or (at your option) any later version. | ||
// | ||
// This library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public | ||
// License along with this library; if not, write to the Free Software | ||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
package com.github.sevntu.checkstyle.checks.coding; | ||
|
||
import com.github.sevntu.checkstyle.SevntuUtil; | ||
import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | ||
import com.puppycrawl.tools.checkstyle.api.DetailAST; | ||
import com.puppycrawl.tools.checkstyle.api.FullIdent; | ||
import com.puppycrawl.tools.checkstyle.api.TokenTypes; | ||
|
||
/** | ||
* <p> | ||
* Checks that certain fields are not used. This can be used to enforce that fields like e.g. | ||
* {@link java.util.Locale#ROOT}. | ||
* </p> | ||
* | ||
* <p> | ||
* Parameters are: | ||
* </p> | ||
* | ||
* <ul> | ||
* <li><b>packageName</b> - The field package name to be forbidden.</li> | ||
* <li><b>className</b> - The field class name to be forbidden.</li> | ||
* <li><b>fieldName</b> - The field name to be forbidden.</li> | ||
* </ul> | ||
* | ||
* <p> | ||
* Together, these three parameters forms the field fully qualified name. | ||
* </p> | ||
* | ||
* <p> | ||
* Default parameters are: | ||
* </p> | ||
* | ||
* <ul> | ||
* <li><b>packageName</b>java.util</li> | ||
* <li><b>className</b>Locale</li> | ||
* <li><b>fieldName</b>ROOT</li> | ||
* </ul> | ||
* | ||
* <p> | ||
* which forbids the usage of {@link java.util.Locale#ROOT}. | ||
* </p> | ||
* | ||
* @author <a href="mailto:yasser.aziza@gmail.com">Yasser Aziza</a> | ||
* @since 1.38.0 | ||
*/ | ||
public class ForbidFieldAccessCheck extends AbstractCheck { | ||
|
||
/** | ||
* Warning message key. | ||
*/ | ||
public static final String MSG_KEY = "forbid.field.access"; | ||
|
||
/** | ||
* '.' character used as separator between FQN elements. | ||
*/ | ||
private static final char DOT = '.'; | ||
|
||
/** | ||
* Package name. | ||
*/ | ||
private String packageName = "java.util"; | ||
|
||
/** | ||
* Class name. | ||
*/ | ||
private String className = "Locale"; | ||
|
||
/** | ||
* Field name. | ||
*/ | ||
private String fieldName = "ROOT"; | ||
|
||
/** | ||
* Whether the field class was imported. | ||
*/ | ||
private boolean wasPackageImported; | ||
|
||
/** | ||
* Sets the package name, in which the field is declared. | ||
* @param packageName the field name | ||
*/ | ||
public void setPackageName(String packageName) { | ||
this.packageName = packageName; | ||
} | ||
|
||
/** | ||
* Sets the class name, which declares the field. | ||
* @param className the class name | ||
*/ | ||
public void setClassName(String className) { | ||
this.className = className; | ||
} | ||
|
||
/** | ||
* Sets the Field name, which should be forbidden to use. | ||
* @param fieldName the field name | ||
*/ | ||
public void setFieldName(String fieldName) { | ||
this.fieldName = fieldName; | ||
} | ||
|
||
/** | ||
* Gets the field fully qualified name. | ||
* @return {@link String} containing the field FQN | ||
*/ | ||
private String getFieldFullyQualifiedName() { | ||
return packageName + DOT + className + DOT + fieldName; | ||
} | ||
|
||
@Override | ||
public int[] getDefaultTokens() { | ||
return new int[] { | ||
TokenTypes.STATIC_IMPORT, | ||
TokenTypes.IMPORT, | ||
TokenTypes.IDENT, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to view PACKAGE_DEF as the class being looked for can be in the same package. |
||
}; | ||
} | ||
|
||
@Override | ||
public int[] getAcceptableTokens() { | ||
return getDefaultTokens(); | ||
} | ||
|
||
@Override | ||
public int[] getRequiredTokens() { | ||
return getDefaultTokens(); | ||
} | ||
|
||
@Override | ||
public void visitToken(DetailAST ast) { | ||
if (isStaticImported(ast)) { | ||
log(ast, MSG_KEY, getFieldFullyQualifiedName()); | ||
} | ||
else if (isPackageImported(ast)) { | ||
// Mark forbidden field package as imported | ||
wasPackageImported = true; | ||
} | ||
else if (wasPackageImported && TokenTypes.IDENT == ast.getType() && isSameType(ast)) { | ||
log(ast.getPreviousSibling(), MSG_KEY, getFieldFullyQualifiedName()); | ||
} | ||
} | ||
|
||
/** | ||
* Checks whether the field is static imported. | ||
* | ||
* @param ast the {@link TokenTypes#STATIC_IMPORT} node to be checked | ||
* @return {@code true} if the field was static imported, {@code false} otherwise | ||
*/ | ||
private boolean isStaticImported(DetailAST ast) { | ||
return TokenTypes.STATIC_IMPORT == ast.getType() | ||
&& getFieldFullyQualifiedName().equals(SevntuUtil.getText(ast)); | ||
} | ||
|
||
/** | ||
* Checks whether the field package is imported. | ||
* | ||
* @param ast the {@link TokenTypes#IMPORT} node to be checked | ||
* @return {@code true} if the field was imported, {@code false} otherwise | ||
*/ | ||
private boolean isPackageImported(DetailAST ast) { | ||
final String importName = packageName + DOT + className; | ||
|
||
return TokenTypes.IMPORT == ast.getType() | ||
&& importName.equals(FullIdent.createFullIdentBelow(ast).getText()); | ||
} | ||
|
||
/** | ||
* Checks if the given {@link TokenTypes#IDENT} node has the same type as the forbidden field. | ||
* | ||
* @param ast the {@link TokenTypes#IDENT} node to be checked | ||
* @return {@code true} if the field has the same FQN, {@code false} otherwise | ||
*/ | ||
private boolean isSameType(DetailAST ast) { | ||
return fieldName.equals(ast.getText()) | ||
&& ast.getPreviousSibling() != null | ||
&& className.equals(ast.getPreviousSibling().getText()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You will need a beginTree method to reset this variable on each new file.