/** * 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."); } }
/** * 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; }
/** * 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; }
+ recentModule.getName(), ex);
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; }
public static Set<Configuration> getChecks(Configuration config) { final Set<Configuration> result = new HashSet<>(); for (Configuration child : config.getChildren()) { if ("TreeWalker".equals(child.getName())) { result.addAll(getModules(child)); } } return result; }
Checker.class.getPackage().getName() + ".", moduleClassLoader); rootModule = (RootModule) factory.createModule(configuration.getName()); rootModule.setModuleClassLoader(moduleClassLoader);
/** * {@inheritDoc} Creates child module. * @noinspection ChainOfInstanceofChecks */ @Override public void setupChild(Configuration childConf) throws CheckstyleException { final String name = childConf.getName(); final Object module = moduleFactory.createModule(name); if (module instanceof AutomaticBean) { final AutomaticBean bean = (AutomaticBean) module; bean.contextualize(childContext); bean.configure(childConf); } if (module instanceof AbstractCheck) { final AbstractCheck check = (AbstractCheck) module; check.init(); registerCheck(check); } else if (module instanceof TreeWalkerFilter) { final TreeWalkerFilter filter = (TreeWalkerFilter) module; filters.add(filter); } else { throw new CheckstyleException( "TreeWalker is not allowed as a parent of " + name + " Please review 'Parent Module' section for this Check in web" + " documentation if Check is standard."); } }
/** * Creates {@link Checker} instance based on the given {@link Configuration} instance. * @param moduleConfig {@link Configuration} instance. * @return {@link Checker} instance based on the given {@link Configuration} instance. * @throws Exception if an exception occurs during checker configuration. */ public final Checker createChecker(Configuration moduleConfig) throws Exception { ModuleCreationOption moduleCreationOption = ModuleCreationOption.IN_CHECKER; final String moduleName = moduleConfig.getName(); if (!ROOT_MODULE_NAME.equals(moduleName)) { try { final Class<?> moduleClass = Class.forName(moduleName); if (ModuleReflectionUtil.isCheckstyleTreeWalkerCheck(moduleClass) || ModuleReflectionUtil.isTreeWalkerFilterModule(moduleClass)) { moduleCreationOption = ModuleCreationOption.IN_TREEWALKER; } } catch (ClassNotFoundException ignore) { // ignore exception, assume it is not part of TreeWalker } } return createChecker(moduleConfig, moduleCreationOption); }
final String checkName = checkConfig.getName(); final Object instance;
/** * 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 testConstructors() throws Exception { final Properties props = new Properties(); props.setProperty("checkstyle.basedir", "basedir"); final String fName = getPath("InputConfigurationLoaderChecks.xml"); final Configuration configuration = ConfigurationLoader.loadConfiguration(fName, new PropertiesExpander(props), ConfigurationLoader.IgnoredModulesOptions.OMIT); assertEquals("Name is not expected", "Checker", configuration.getName()); final DefaultConfiguration configuration1 = (DefaultConfiguration) ConfigurationLoader.loadConfiguration( new InputSource(Files.newInputStream(Paths.get( getPath("InputConfigurationLoaderModuleIgnoreSeverity.xml")))), new PropertiesExpander(new Properties()), ConfigurationLoader.IgnoredModulesOptions.EXECUTE); final Configuration[] children = configuration1.getChildren(); assertEquals("Unexpected children size", 1, children[0].getChildren().length); }
final RootModule rootModule = getRootModule(config.getName(), moduleClassLoader);
/** * Creates {@link Checker} instance based on the given {@link Configuration} instance. * @param moduleConfig {@link Configuration} instance. * @param moduleCreationOption {@code IN_TREEWALKER} if the {@code moduleConfig} should be added * under {@link TreeWalker}. * @return {@link Checker} instance based on the given {@link Configuration} instance. * @throws Exception if an exception occurs during checker configuration. */ public final Checker createChecker(Configuration moduleConfig, ModuleCreationOption moduleCreationOption) throws Exception { final Checker checker = new Checker(); checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); if (moduleCreationOption == ModuleCreationOption.IN_TREEWALKER) { final Configuration dc = createTreeWalkerConfig(moduleConfig); checker.configure(dc); } else if (ROOT_MODULE_NAME.equals(moduleConfig.getName())) { checker.configure(moduleConfig); } else { final Configuration dc = createRootConfig(moduleConfig); checker.configure(dc); } checker.addListener(new BriefUtLogger(stream)); return checker; }
protected void setupChild(Configuration childConf) throws CheckstyleException { final String name = childConf.getName(); final Object child;
@Test public void testModuleNameInSingleThreadMode() throws Exception { TestRootModuleChecker.reset(); exit.checkAssertionAfterwards(() -> { assertEquals("Unexpected output log", "", systemOut.getLog()); assertEquals("Unexpected system error log", "", systemErr.getLog()); assertTrue("Invalid checker state", TestRootModuleChecker.isProcessed()); final DefaultConfiguration config = (DefaultConfiguration) TestRootModuleChecker.getConfig(); final ThreadModeSettings multiThreadModeSettings = config.getThreadModeSettings(); assertEquals("Invalid checker thread number", 1, multiThreadModeSettings.getCheckerThreadsNumber()); assertEquals("Invalid checker thread number", 1, multiThreadModeSettings.getTreeWalkerThreadsNumber()); final Configuration checkerConfiguration = config .getChildren()[0]; assertEquals("Invalid checker name", "Checker", checkerConfiguration.getName()); final Configuration treeWalkerConfig = checkerConfiguration.getChildren()[0]; assertEquals("Invalid checker children name", "TreeWalker", treeWalkerConfig.getName()); }); Main.main("-C", "1", "-W", "1", "-c", getPath("InputMainConfig-multi-thread-mode.xml"), getPath("InputMain.java")); }
private void defineModuleCharset(Configuration module) { if (module instanceof DefaultConfiguration && ("Checker".equals(module.getName()) || "com.puppycrawl.tools.checkstyle.Checker".equals(module.getName()))) { final Charset charset = getCharset(); final String charsetName = charset.name(); LOG.info("Checkstyle charset: {}", charsetName); ((DefaultConfiguration) module).addAttribute("charset", charsetName); } }
private void defineCharset(com.puppycrawl.tools.checkstyle.api.Configuration configuration) { com.puppycrawl.tools.checkstyle.api.Configuration[] modules = configuration.getChildren(); for (com.puppycrawl.tools.checkstyle.api.Configuration module : modules) { if (("Checker".equals(module.getName()) || "com.puppycrawl.tools.checkstyle.Checker".equals(module.getName())) && (module instanceof DefaultConfiguration)) { Charset charset = getCharset(); LOG.info("Checkstyle charset: " + charset.name()); ((DefaultConfiguration) module).addAttribute("charset", charset.name()); } } }
private void defineCharset(com.puppycrawl.tools.checkstyle.api.Configuration configuration) { com.puppycrawl.tools.checkstyle.api.Configuration[] modules = configuration.getChildren(); for (com.puppycrawl.tools.checkstyle.api.Configuration module : modules) { if (("Checker".equals(module.getName()) || "com.puppycrawl.tools.checkstyle.Checker".equals(module.getName())) && module instanceof DefaultConfiguration) { Charset charset = getCharset(); LOG.info("Checkstyle charset: " + charset.name()); ((DefaultConfiguration) module).addAttribute("charset", charset.name()); } } }