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 8, 2020
1 parent 3686c0f commit 659efe2
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 35 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 anyTokenTypesSet}.
* 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 @@ -82,7 +82,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
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())
.mapToObj(TokenUtil::getTokenName)
.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="anyTokenTypesSet">
<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 @@ -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("set of any supported");
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 = "anyTokenTypesSet";
}
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 = "set of any supported tokens";
}
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>set of any supported
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html">
tokens</a>
</td>
<td><code>{}</code></td>
<td>3.2</td>
</tr>
</table>
</div>
</subsection>
Expand Down

0 comments on commit 659efe2

Please sign in to comment.