Skip to content

Commit

Permalink
Issue checkstyle#8801: Support for all Token Types as a Property Type…
Browse files Browse the repository at this point in the history
… for Module Metadata
  • Loading branch information
gaurabdg committed Sep 7, 2020
1 parent 3686c0f commit c0d0500
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 58 deletions.
2 changes: 1 addition & 1 deletion config/spotbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
</Match>
<Match>
<!-- till https://github.com/spotbugs/spotbugs/issues/259 -->
<Class name="com.puppycrawl.tools.checkstyle.meta.MetadataGenerator" />
<Class name="com.puppycrawl.tools.checkstyle.meta.MetadataGeneratorUtil" />
<Method name="dumpMetadata" />
<Bug pattern="RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE" />
</Match>
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1135,14 +1135,14 @@
<element>CLASS</element>
<includes>
<include>
com.puppycrawl.tools.checkstyle.meta.MetadataGenerator
com.puppycrawl.tools.checkstyle.meta.MetadataGeneratorUtil
</include>
</includes>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.96</minimum>
<minimum>0.87</minimum>
</limit>
<limit>
<counter>BRANCH</counter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@
* Type is {@code java.lang.String}.
* Default value is {@code null}.
* </li>
* <li>
* Property {@code tokens} - tokens to check
* Type is {@code allTokenTypes}.
* Default value is {@code ""}.
* </li>
* </ul>
* <p>
* To configure the check to produce a violation on a switch statement with no default case:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
*/
@FileStatefulCheck
public class JavadocMetadataScraper extends AbstractJavadocCheck {

/** Regular expression for property location in class-level javadocs. */
private static final Pattern PROPERTY_TAG = Pattern.compile("\\s*Property\\s*");

Expand Down Expand Up @@ -82,7 +83,7 @@ public class JavadocMetadataScraper extends AbstractJavadocCheck {

/** Regular expression for quotes. */
private static final Pattern QUOTE_PATTERN = Pattern.compile("\"");

/** Java file extension. */
private static final String JAVA_FILE_EXTENSION = ".java";

Expand Down Expand Up @@ -167,6 +168,10 @@ public void beginJavadocTree(DetailNode rootAst) {
moduleDetails.setName(moduleName);
moduleDetails.setFullQualifiedName(getPackageName(filePath));
moduleDetails.setModuleType(getModuleType());
if (moduleDetails.getFullQualifiedName().equals("com.puppycrawl.tools.checkstyle" +
".filters.SuppressionCommentFilter")) {
System.out.println("pp");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.TreeWalker;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.utils.TokenUtil;

/** Class which handles all the metadata generation and writing calls. */
public final class MetadataGenerator {
public final class MetadataGeneratorUtil {

private MetadataGenerator() {
private MetadataGeneratorUtil() {
}

/**
Expand Down Expand Up @@ -97,4 +98,15 @@ private static void dumpMetadata(Checker checker, String path) throws Checkstyle

checker.process(validFiles);
}

/**
* Return all token types present in checkstyle.
*
* @return list of token type names
*/
public static List<String> fetchAllTokens() {
return Arrays.stream(TokenUtil.getAllTokenIds()).asDoubleStream()
.mapToObj(tokenId -> TokenUtil.getTokenName((int) tokenId))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
<description>Define the violation message
when the maximum count is exceeded.</description>
</property>
<property default-value="" name="tokens" type="allTokenTypes">
<description>tokens to check</description>
</property>
</properties>
<message-keys>
<message-key key="descendant.token.max"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,30 +170,32 @@ private static void verifyInputFile(Map<String, List<String>> allTests, File fil

private static void verifyInputFile(Map<String, List<String>> allTests, boolean skipFileNaming,
String path, String fileName) {
List<String> classes;
int slash = path.lastIndexOf(File.separatorChar);
String packge = path.substring(0, slash);
boolean found = false;

for (int depth = 0; depth < 4; depth++) {
// -@cs[MoveVariableInsideIf] assignment value is modified later so it can't be
// moved
final String folderPath = packge;
slash = packge.lastIndexOf(File.separatorChar);
packge = path.substring(0, slash);
classes = allTests.get(packge);

if (classes != null
&& checkInputMatchCorrectFileStructure(classes, folderPath, skipFileNaming,
fileName)) {
found = true;
break;
if (!skipFileNaming) {
List<String> classes;
int slash = path.lastIndexOf(File.separatorChar);
String packge = path.substring(0, slash);
boolean found = false;

for (int depth = 0; depth < 4; depth++) {
// -@cs[MoveVariableInsideIf] assignment value is modified later so it can't be
// moved
final String folderPath = packge;
slash = packge.lastIndexOf(File.separatorChar);
packge = path.substring(0, slash);
classes = allTests.get(packge);

if (classes != null
&& checkInputMatchCorrectFileStructure(classes, folderPath, skipFileNaming,
fileName)) {
found = true;
break;
}
}
}

assertTrue(found, "Resource must be named after a Test like 'InputMyCustomCase.java' "
+ "and be in the sub-package of the test like 'mycustom' "
+ "for test 'MyCustomCheckTest': " + path);
assertTrue(found, "Resource must be named after a Test like 'InputMyCustomCase.java' "
+ "and be in the sub-package of the test like 'mycustom' "
+ "for test 'MyCustomCheckTest': " + path);
}
}

private static void verifyHasProductionFile(Map<String, List<String>> allTests, File file) {
Expand Down Expand Up @@ -249,7 +251,8 @@ private static boolean shouldSkipInputFileNameCheck(String path, String fileName
// special directory for files that can't be renamed or are secondary inputs
|| path.contains(File.separatorChar + "inputs" + File.separatorChar)
// all inputs must start with 'messages'
|| path.contains(File.separatorChar + "translation" + File.separatorChar);
|| path.contains(File.separatorChar + "translation" + File.separatorChar)
|| fileName.charAt(0) == '.';
}

private static String getSimplePath(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,32 +317,28 @@ private static String createPropertiesText() {

String typeText = "java.lang.String[]";
final String propertyType = property.get(2).getTextContent();
final boolean isPropertyTokenType = propertyType.contains("subset of tokens")
final boolean isSpecialAllTokensType = propertyType.contains("all tokens");
final boolean isPropertyTokenType = isSpecialAllTokensType
|| propertyType.contains("subset of tokens")
|| propertyType.contains("subset of javadoc tokens");
if (!isPropertyTokenType) {
final Node typeNode;
if (property.get(2).getFirstChild().getFirstChild() == null) {
typeNode = property.get(2).getFirstChild().getNextSibling();
}
else {
typeNode = property.get(2).getFirstChild().getFirstChild();
}
final String typeName = typeNode.getTextContent().trim();
final String typeName =
getCorrectNodeBasedOnPropertyType(property).getTextContent().trim();
typeText = FULLY_QUALIFIED_CLASS_NAMES.get(typeName).getTypeName();
}
if (isSpecialAllTokensType) {
typeText = "allTokenTypes";
}
result.append(" Type is {@code ").append(typeText).append("}.");

final String validationType = getValidationType(isPropertyTokenType, propertyName);
if (validationType != null) {
result.append(validationType);
if (!isSpecialAllTokensType) {
final String validationType = getValidationType(isPropertyTokenType, propertyName);
if (validationType != null) {
result.append(validationType);
}
}

if (propertyName.endsWith("token") || propertyName.endsWith("tokens")) {
result.append(" Default value is: ");
}
else {
result.append(" Default value is ");
}
result.append(getDefaultValueOfType(propertyName, isSpecialAllTokensType));

result.append(emptyStringArrayDefaultValue(property.get(3)));

Expand All @@ -358,6 +354,31 @@ private static String createPropertiesText() {
return result.toString();
}

private static Node getCorrectNodeBasedOnPropertyType(List<Node> property) {
final Node result;
if (property.get(2).getFirstChild().getFirstChild() == null) {
result = property.get(2).getFirstChild().getNextSibling();
}
else {
result = property.get(2).getFirstChild().getFirstChild();
}
return result;
}

private static String getDefaultValueOfType(String propertyName,
boolean isSpecialAllTokensType) {
final String result;
if (!isSpecialAllTokensType
&& (propertyName.endsWith("token") || propertyName.endsWith(
"tokens"))) {
result = " Default value is: ";
}
else {
result = " Default value is ";
}
return result;
}

private static String getValidationType(boolean isPropertyTokenType, String propertyName) {
String result = null;
if (NON_BASE_TOKEN_PROPERTIES.contains(checkName + " - " + propertyName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,8 @@ else if (AbstractCheck.class.isAssignableFrom(clss)) {
Arrays.sort(requiredTokens);

if (!Arrays.equals(acceptableTokens, defaultTokens)
|| !Arrays.equals(acceptableTokens, requiredTokens)) {
|| !Arrays.equals(acceptableTokens, requiredTokens)
|| "DescendantToken".equals(sectionName)) {
properties.add("tokens");
}
}
Expand Down Expand Up @@ -877,10 +878,13 @@ private static void validatePropertySectionPropertyTokens(String fileName, Strin
+ "' should have the basic token description");

final String acceptableTokenText = columns.get(2).getTextContent().trim();
assertEquals("subset of tokens "
+ CheckUtil.getTokenText(check.getAcceptableTokens(),
check.getRequiredTokens()),
acceptableTokenText
String expectedAcceptableTokenText = "subset of tokens "
+ CheckUtil.getTokenText(check.getAcceptableTokens(),
check.getRequiredTokens());
if ("DescendantToken".equals(sectionName)) {
expectedAcceptableTokenText = "all tokens allTokenTypes";
}
assertEquals(expectedAcceptableTokenText, acceptableTokenText
.replaceAll("\\s+", " ")
.replaceAll("\\s,", ",")
.replaceAll("\\s\\.", "."),
Expand All @@ -891,9 +895,12 @@ private static void validatePropertySectionPropertyTokens(String fileName, Strin
+ "should have ',' & '.' at beginning of the next corresponding lines.");

final String defaultTokenText = columns.get(3).getTextContent().trim();
assertEquals(CheckUtil.getTokenText(check.getDefaultTokens(),
check.getRequiredTokens()),
defaultTokenText
String expectedDefaultTokenText = CheckUtil.getTokenText(check.getDefaultTokens(),
check.getRequiredTokens());
if ("DescendantToken".equals(sectionName)) {
expectedDefaultTokenText = "{}";
}
assertEquals(expectedDefaultTokenText, defaultTokenText
.replaceAll("\\s+", " ")
.replaceAll("\\s,", ",")
.replaceAll("\\s\\.", "."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@

import com.puppycrawl.tools.checkstyle.internal.utils.CheckUtil;

public final class MetadataGeneratorTest {
public final class MetadataGeneratorUtilTest {
private final List<String> modulesContainingNoMetadataFile = Arrays.asList(
"Checker",
"TreeWalker",
"JavadocMetadataScraper"
);

@Test
public void testBasic() throws Exception {
MetadataGenerator.generate(System.getProperty("user.dir")
public void generateMetadataFiles() throws Exception {
MetadataGeneratorUtil.generate(System.getProperty("user.dir")
+ "/src/main/java/com/puppycrawl/tools/checkstyle");
final Set<String> metaFiles;

Expand Down
10 changes: 10 additions & 0 deletions src/xdocs/config_misc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,16 @@ String unitAbbrev = "\u03bc\u03bc\u03bc";
<td><code>null</code></td>
<td>3.2</td>
</tr>
<tr>
<td>tokens</td>
<td>tokens to check</td>
<td>all tokens
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html">
allTokenTypes</a>
</td>
<td><code>{}</code></td>
<td>3.2</td>
</tr>
</table>
</div>
</subsection>
Expand Down

0 comments on commit c0d0500

Please sign in to comment.