/** * Returns {@code TreeWalker} module configuration. * @param config The configuration object. * @return The {@code TreeWalker} module configuration. */ private static Configuration getTreeWalkerConfig(Configuration config) { Configuration result = null; final Configuration[] children = config.getChildren(); for (Configuration child : children) { if ("TreeWalker".equals(child.getName())) { result = child; break; } } return result; }
/** * Implements the Configurable interface using bean introspection. * * <p>Subclasses are allowed to add behaviour. After the bean * based setup has completed first the method * {@link #finishLocalSetup finishLocalSetup} * is called to allow completion of the bean's local setup, * after that the method {@link #setupChild setupChild} * is called for each {@link Configuration#getChildren child Configuration} * of {@code configuration}. * * @see Configurable */ @Override public final void configure(Configuration config) throws CheckstyleException { configuration = config; final String[] attributes = config.getAttributeNames(); for (final String key : attributes) { final String value = config.getAttribute(key); tryCopyProperty(config.getName(), key, value, true); } finishLocalSetup(); final Configuration[] childConfigs = config.getChildren(); for (final Configuration childConfig : childConfigs) { setupChild(childConfig); } }
@Test public void testCustomMessages() throws Exception { final Properties props = new Properties(); props.setProperty("checkstyle.basedir", "basedir"); final DefaultConfiguration config = (DefaultConfiguration) loadConfiguration( "InputConfigurationLoaderCustomMessages.xml", props); final Configuration[] children = config.getChildren(); final Configuration[] grandchildren = children[0].getChildren(); final String expectedKey = "name.invalidPattern"; assertTrue("Messages should contain key: " + expectedKey, grandchildren[0].getMessages() .containsKey(expectedKey)); }
if (containsAttribute(recentModule, SEVERITY)) { try { final String severity = recentModule.getAttribute(SEVERITY); level = SeverityLevel.getInstance(severity); + recentModule.getName(), ex);
/** * Called by configure() for every child of this component's Configuration. * <p> * The default implementation throws {@link CheckstyleException} if * {@code childConf} is {@code null} because it doesn't support children. It * must be overridden to validate and support children that are wanted. * </p> * * @param childConf a child of this component's Configuration * @throws CheckstyleException if there is a configuration error. * @see Configuration#getChildren */ protected void setupChild(Configuration childConf) throws CheckstyleException { if (childConf != null) { throw new CheckstyleException(childConf.getName() + " is not allowed as a child in " + configuration.getName() + ". Please review 'Parent Module' section " + "for this Check in web documentation if Check is standard."); } }
@Test public void testConfigWithIgnoreUsingInputSource() throws Exception { final DefaultConfiguration config = (DefaultConfiguration) ConfigurationLoader.loadConfiguration(new InputSource( new File(getPath("InputConfigurationLoaderModuleIgnoreSeverity.xml")) .toURI().toString()), new PropertiesExpander(new Properties()), true); final Configuration[] children = config.getChildren(); assertEquals("Invalid children count", 0, children[0].getChildren().length); }
String ruleName = checkerConfig.getName(); List<String> attribnames = new ArrayList<>( Arrays.asList( checkerConfig.getAttributeNames() ) ); attribnames.remove( "severity" ); // special value (deserves unique column) if ( !attribnames.isEmpty() )
@Override public void configure(Configuration configuration) throws CheckstyleException { config = configuration; property = configuration.getAttribute("property"); }
/** * Util method to recheck attribute in module. * @param module module to check * @param attributeName name of attribute in module to find * @return true if attribute is present in module */ private boolean containsAttribute(Configuration module, String attributeName) { final String[] names = module.getAttributeNames(); final Optional<String> result = Arrays.stream(names) .filter(name -> name.equals(attributeName)).findFirst(); return result.isPresent(); }
/** * Returns an unmodifiable map instance containing the custom messages * for this configuration. * @return unmodifiable map containing custom messages */ protected Map<String, String> getCustomMessages() { return getConfiguration().getMessages(); }
/** * Set the header file to check against. * @param uri the uri of the header to load. * @throws CheckstyleException if fileName is empty. */ public void setHeaderFile(URI uri) throws CheckstyleException { if (uri == null) { throw new CheckstyleException( "property 'headerFile' is missing or invalid in module " + getConfiguration().getName()); } headerFile = uri; }
@Test public void testLoadConfigurationDeprecated() throws Exception { final DefaultConfiguration config = (DefaultConfiguration) ConfigurationLoader.loadConfiguration( Files.newInputStream(Paths.get( getPath("InputConfigurationLoaderModuleIgnoreSeverity.xml"))), new PropertiesExpander(new Properties()), true); final Configuration[] children = config.getChildren(); assertEquals("Invalid children count", 0, children[0].getChildren().length); }
final String checkName = checkConfig.getName(); final Object instance; configTokens.addAll(Arrays.asList(checkConfig.getAttribute("tokens").trim() .split(",\\s*")));
private void verifyWarns(Configuration config, String filePath, String... expected) throws Exception { final int tabWidth = Integer.parseInt(config.getAttribute("tabWidth")); final IndentComment[] linesWithWarn = getLinesWithWarnAndCheckComments(filePath, tabWidth); verify(config, filePath, expected, linesWithWarn); assertEquals("Expected warning count in UT does not match warn" + " comment count in input file", linesWithWarn.length, expected.length); }
/** * Util method to recheck attribute in module. * @param module module to check * @param attributeName name of attribute in module to find * @return true if attribute is present in module */ private boolean containsAttribute(Configuration module, String attributeName) { final String[] names = module.getAttributeNames(); final Optional<String> result = Arrays.stream(names) .filter(name -> name.equals(attributeName)).findFirst(); return result.isPresent(); }
/** * Returns an unmodifiable map instance containing the custom messages * for this configuration. * @return unmodifiable map containing custom messages */ protected Map<String, String> getCustomMessages() { return getConfiguration().getMessages(); }
public static Set<Configuration> getModules(Configuration config) { final Set<Configuration> result = new HashSet<>(); for (Configuration child : config.getChildren()) { if ("TreeWalker".equals(child.getName())) { result.addAll(getModules(child)); } else { result.add(child); } } return result; }
Checker.class.getPackage().getName() + ".", moduleClassLoader); rootModule = (RootModule) factory.createModule(configuration.getName()); rootModule.setModuleClassLoader(moduleClassLoader);
/** * Implements the Configurable interface using bean introspection. * * <p>Subclasses are allowed to add behaviour. After the bean * based setup has completed first the method * {@link #finishLocalSetup finishLocalSetup} * is called to allow completion of the bean's local setup, * after that the method {@link #setupChild setupChild} * is called for each {@link Configuration#getChildren child Configuration} * of {@code configuration}. * * @see Configurable */ @Override public final void configure(Configuration config) throws CheckstyleException { configuration = config; final String[] attributes = config.getAttributeNames(); for (final String key : attributes) { final String value = config.getAttribute(key); tryCopyProperty(config.getName(), key, value, true); } finishLocalSetup(); final Configuration[] childConfigs = config.getChildren(); for (final Configuration childConfig : childConfigs) { setupChild(childConfig); } }
@Test public void testConfigWithIgnore() throws Exception { final DefaultConfiguration config = (DefaultConfiguration) ConfigurationLoader.loadConfiguration( getPath("InputConfigurationLoaderModuleIgnoreSeverity.xml"), new PropertiesExpander(new Properties()), true); final Configuration[] children = config.getChildren(); assertEquals("Invalid children count", 0, children[0].getChildren().length); }