com.puppycrawl.tools.checkstyle.api.AbstractCheck Java Examples

The following examples show how to use com.puppycrawl.tools.checkstyle.api.AbstractCheck. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CSUtility.java    From kieker with Apache License 2.0 6 votes vote down vote up
/**
 * This method checks whether the given component has a javadoc comment with a valid since tag.
 *
 * @param check
 *            The current check.
 * @param ast
 *            The component to check for the tag.
 *
 * @return true if and only if there is a since tag in the javadoc comment of the given component.
 */
public static boolean sinceTagAvailable(final AbstractCheck check, final DetailAST ast) {
	// Get the corresponding javadoc block
	final FileContents contents = check.getFileContents();
	final TextBlock cmt = contents.getJavadocBefore(ast.getFirstChild().getLineNo());

	// Make sure that there is a comment block available
	if (cmt != null) {
		// Now extract the tags
		final JavadocTags tags = JavadocUtils.getJavadocTags(cmt, JavadocTagType.ALL);

		// Run through the tags and find the potential since tag
		for (final JavadocTag tag : tags.getValidTags()) {
			if (SINCE_TAG_NAME.equals(tag.getTagName())) {
				return true;
			}
		}
	}

	return false;
}
 
Example #2
Source File: FilteredModuleFactory.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
@Override
public Object createModule(String name) throws CheckstyleException {
	Object module = this.moduleFactory.createModule(name);
	if (module instanceof AbstractCheck) {
		module = filter((AbstractCheck) module);
	}
	return module;
}
 
Example #3
Source File: CheckFilter.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
private ModuleFactory createModuleFactory() {
	try {
		ClassLoader classLoader = AbstractCheck.class.getClassLoader();
		Set<String> packageNames = PackageNamesLoader.getPackageNames(classLoader);
		return new PackageObjectFactory(packageNames, classLoader);
	}
	catch (CheckstyleException ex) {
		throw new IllegalStateException(ex);
	}
}
 
Example #4
Source File: FilteredModuleFactory.java    From spring-javaformat with Apache License 2.0 4 votes vote down vote up
private Object filter(AbstractCheck check) {
	if (this.excludes != null && this.excludes.contains(check.getClass().getName())) {
		return new FilteredCheck(check);
	}
	return check;
}
 
Example #5
Source File: FilteredModuleFactory.java    From spring-javaformat with Apache License 2.0 4 votes vote down vote up
FilteredCheck(AbstractCheck check) {
	this.check = check;
}
 
Example #6
Source File: ChecksTest.java    From eclipse-cs with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static void validateEclipseCsMetaXmlFileRuleTokens(String packge, Class<?> module,
        String moduleName, String propertyName, Node propertyNode) throws Exception {
  final AbstractCheck check = (AbstractCheck) module.newInstance();
  final String defaultText = CheckUtil.getTokenText(check.getDefaultTokens(),
          check.getRequiredTokens());
  final String acceptableText = CheckUtil.getTokenText(check.getAcceptableTokens(),
          check.getRequiredTokens());

  final Node defaultValueNode = propertyNode.getAttributes().getNamedItem("default-value");

  if (defaultText == null) {
    assertNull(defaultValueNode, packge + " checkstyle-metadata.xml should not have a default value for "
            + moduleName + ", " + propertyName);
  } else {
    assertNotNull(defaultValueNode, packge + " checkstyle-metadata.xml requires a default value for "
            + moduleName + ", " + propertyName);

    assertEquals(defaultText, defaultValueNode.getTextContent(), packge + " checkstyle-metadata.xml requires a valid default value for "
            + moduleName + ", " + propertyName);
  }

  final Node enumerationChild = propertyNode.getFirstChild().getNextSibling().getNextSibling()
          .getNextSibling();

  if (acceptableText == null) {
    assertNull(enumerationChild, 
            packge + " checkstyle-metadata.xml should not have an enumeration child for "
                    + moduleName + ", " + propertyName);
  } else {
    assertNotNull(enumerationChild, packge + " checkstyle-metadata.xml requires an enumeration child for "
            + moduleName + ", " + propertyName);
    assertEquals("enumeration", enumerationChild.getNodeName(), 
            packge + " checkstyle-metadata.xml should have a enumeration for the " + " child of "
                    + moduleName + ", " + propertyName);

    if ("TokenTypes".equals(acceptableText)) {
      // TODO
    } else {
      final Set<String> options = new HashSet<>();
      Collections.addAll(options, acceptableText.split(","));

      for (Node child : XmlUtil.getChildrenElements(enumerationChild)) {
        switch (child.getNodeName()) {
          case "property-value-option":
            final String value = child.getAttributes().getNamedItem("value").getTextContent();

            assertTrue(options.remove(value), 
                    packge + " checkstyle-metadata.xml has an unknown acceptable token for "
                            + moduleName + ", " + propertyName + ": " + value);
            break;
          default:
            fail(packge + " checkstyle-metadata.xml unknown node for " + moduleName + ", "
                    + propertyName + ": " + child.getNodeName());
            break;
        }
      }

      for (String option : options) {
        fail(packge + " checkstyle-metadata.xml missing acceptable token for " + moduleName
                + ", " + propertyName + ": " + option);
      }
    }
  }
}
 
Example #7
Source File: ChecksTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static void validateSonarRuleProperties(Class<?> module, Set<Node> parameters) {
    final Object instance;

    try {
        instance = module.getConstructor().newInstance();
    }
    catch (ReflectiveOperationException ex) {
        throw new IllegalStateException(ex);
    }

    final String moduleName = module.getName();
    final Set<String> properties = getFinalProperties(module);

    for (Node parameter : parameters) {
        final NamedNodeMap attributes = parameter.getAttributes();
        final Node paramKeyNode = attributes.getNamedItem("key");

        Assert.assertNotNull(moduleName
                + " requires a key for unknown parameter in sonar rules"
                + " (" + RULES_PATH + ")", paramKeyNode);

        final String paramKey = paramKeyNode.getTextContent();

        Assert.assertFalse(moduleName
                + " requires a valid key for unknown parameter in sonar rules"
                        + " (" + RULES_PATH + ")",
                paramKey.isEmpty());

        Assert.assertTrue(moduleName + " has an unknown parameter in sonar rules"
                + " (" + RULES_PATH + ")" + ": " + paramKey, properties.remove(paramKey));

        final Node typeNode = parameter.getAttributes().getNamedItem("type");

        Assert.assertNotNull(moduleName + " has no parameter type in sonar rules"
                        + " (" + RULES_PATH + ")" + ": " + paramKey,
                typeNode);

        if ("tokens".equals(paramKey) || "javadocTokens".equals(paramKey)) {
            String expectedTokenType;

            if ("tokens".equals(paramKey)) {
                expectedTokenType = "s[" + CheckUtil.getTokenText(
                        ((AbstractCheck) instance).getAcceptableTokens(),
                        ((AbstractCheck) instance).getRequiredTokens()) + "]";
            }
            else {
                expectedTokenType = "s[" + CheckUtil.getJavadocTokenText(
                        ((AbstractJavadocCheck) instance).getAcceptableJavadocTokens(),
                        ((AbstractJavadocCheck) instance).getRequiredJavadocTokens()) + "]";
            }

            // Type can't be too long as it is stored in a database field with a max limit
            // sonar adds its own text to the type affecting the limit of data we can store
            // see https://github.com/checkstyle/sonar-checkstyle/issues/75 and
            // https://github.com/checkstyle/sonar-checkstyle/pull/77#issuecomment-281247278
            if (expectedTokenType.length() + 44 > 512) {
                expectedTokenType = "STRING";
            }

            final String type = typeNode.getTextContent();
            Assert.assertEquals(moduleName + " has the parameter '" + paramKey
                    + "' in sonar rules with the incorrect type", expectedTokenType, type);

            final Set<Node> values = XmlUtil.getChildrenElements(parameter);

            Assert.assertEquals(moduleName + " has the parameter '" + paramKey
                    + "' in sonar rules with no defaultValue", 1, values.size());

            final String expectedDefaultTokens;

            if ("tokens".equals(paramKey)) {
                expectedDefaultTokens = CheckUtil.getTokenText(
                        ((AbstractCheck) instance).getDefaultTokens(),
                        ((AbstractCheck) instance).getRequiredTokens());
            }
            else {
                expectedDefaultTokens = CheckUtil.getJavadocTokenText(
                        ((AbstractJavadocCheck) instance).getDefaultJavadocTokens(),
                        ((AbstractJavadocCheck) instance).getRequiredJavadocTokens());
            }

            Assert.assertEquals(moduleName + " has the parameter '" + paramKey
                    + "' in sonar rules with the incorrect defaultValue", expectedDefaultTokens,
                    values.iterator().next().getTextContent());
        }
    }

    for (String property : properties) {
        Assert.fail(moduleName + " parameter not found in sonar rules"
                + " (" + RULES_PATH + ")" + ": " + property);
    }
}
 
Example #8
Source File: CheckUtil.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Checks whether a class may be considered as the checkstyle check.
 * Checkstyle's checks are classes which implement 'AbstractCheck' interface.
 * @param loadedClass class to check.
 * @return true if a class may be considered as the checkstyle check.
 */
public static boolean isCheckstyleCheck(Class<?> loadedClass) {
    return AbstractCheck.class.isAssignableFrom(loadedClass);
}