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 b5c7ba3 commit dbac07f
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 26 deletions.
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 @@ -35,6 +35,7 @@
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 {
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> getAllTokens() {
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 @@ -16,6 +16,9 @@
about language structures.
&lt;/p&gt;</description>
<properties>
<property default-value="" name="tokens" type="allTokenTypes">
<description>All checkstyle token types.</description>
</property>
<property default-value=""
name="limitedTokens"
type="java.lang.String[]"
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 isSpecialAllTokensType = propertyType.contains("all tokens");
final boolean isPropertyTokenType = propertyType.contains("subset of tokens")
|| propertyType.contains("subset of javadoc tokens");
|| propertyType.contains("subset of javadoc tokens")
|| isSpecialAllTokensType;
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,30 @@ 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 (propertyName.endsWith("token") || propertyName.endsWith("tokens")
&& !isSpecialAllTokensType) {
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
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 dbac07f

Please sign in to comment.