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

The following examples show how to use com.puppycrawl.tools.checkstyle.api.Configuration. 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: ClassFileSetCheck.java    From contribution with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Instantiates, configures and registers a Check that is specified
 * in the provided configuration.
 * @see com.puppycrawl.tools.checkstyle.api.AutomaticBean
 */
public void setupChild(Configuration aChildConf)
    throws CheckstyleException
{
    // TODO: improve the error handing
    final String name = aChildConf.getName();
    final Object module = mModuleFactory.createModule(name);
    if (!(module instanceof AbstractCheckVisitor)) {
        throw new CheckstyleException(
            "ClassFileSet is not allowed as a parent of " + name);
    }
    final AbstractCheckVisitor c = (AbstractCheckVisitor) module;
    c.contextualize(mChildContext);
    c.configure(aChildConf);
    c.init();

    registerCheck(c);
}
 
Example #2
Source File: ClassFileSetCheck.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Instantiates, configures and registers a Check that is specified
 * in the provided configuration.
 * @see com.puppycrawl.tools.checkstyle.api.AutomaticBean
 */
public void setupChild(Configuration aChildConf)
    throws CheckstyleException
{
    // TODO: improve the error handing
    final String name = aChildConf.getName();
    final Object module = mModuleFactory.createModule(name);
    if (!(module instanceof AbstractCheckVisitor)) {
        throw new CheckstyleException(
            "ClassFileSet is not allowed as a parent of " + name);
    }
    final AbstractCheckVisitor c = (AbstractCheckVisitor) module;
    c.contextualize(mChildContext);
    c.configure(aChildConf);
    c.init();

    registerCheck(c);
}
 
Example #3
Source File: CheckstyleConfigurationTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void getTargetXmlReport() {
    final org.sonar.api.config.Configuration settings =
            new ConfigurationBridge(new MapSettings());
    final CheckstyleConfiguration configuration = new CheckstyleConfiguration(settings,
            null, null, fileSystem);
    assertThat(configuration.getTargetXmlReport()).isNull();

    final MapSettings mapSettings = new MapSettings();
    mapSettings.setProperty(CheckstyleConfiguration.PROPERTY_GENERATE_XML, "true");
    final org.sonar.api.config.Configuration settings2 = new ConfigurationBridge(mapSettings);
    final CheckstyleConfiguration configuration2 = new CheckstyleConfiguration(settings2,
            null, null, fileSystem);
    assertThat(configuration2.getTargetXmlReport()).isEqualTo(
            new File(fileSystem.workDir(), "checkstyle-result.xml"));
}
 
Example #4
Source File: CheckstyleTransformer.java    From eclipse-cs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Creates a new instance of class CheckstyleTransformer.
 *
 * @param project
 *          the project the transformer is operating on
 * @param ruleList
 *          A list of checkstyle-rules.
 * @throws CheckstylePluginException
 *           if an unexpected internal exception occurred
 */
public CheckstyleTransformer(IProject project, final List<Configuration> ruleList)
        throws CheckstylePluginException {
  mProject = project;
  mRules = ruleList;

  final List<String> classnames = new ArrayList<>();
  final Iterator<Configuration> it = mRules.iterator();

  while (it.hasNext()) {
    Configuration item = it.next();
    classnames.add("net.sf.eclipsecs.core.transformer.ctransformerclasses." + item.getName()
            + "Transformer");
  }

  loadTransformationClasses(classnames);
}
 
Example #5
Source File: CheckstyleConfiguration.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Configuration getCheckstyleConfiguration() throws CheckstyleException {
    final File xmlConfig = getXmlDefinitionFile();

    LOG.info("Checkstyle configuration: {}", xmlConfig.getAbsolutePath());
    final Configuration configuration = toCheckstyleConfiguration(xmlConfig);
    defineCharset(configuration);
    return configuration;
}
 
Example #6
Source File: CheckStyleExecutor.java    From repositoryminer with Apache License 2.0 5 votes vote down vote up
public Map<String, List<StyleProblem>> execute() throws CheckstyleException {
	Properties properties;
	if (propertiesFile == null) {
		properties = System.getProperties();
	} else {
		properties = loadProperties(new File(propertiesFile));
	}

	if (configFile == null) {
		configFile = "/google_checks.xml";
	}
	
	// create configurations
	Configuration config = ConfigurationLoader.loadConfiguration(configFile, new PropertiesExpander(properties));

	// create our custom audit listener
	RepositoryMinerAudit listener = new RepositoryMinerAudit();
	listener.setRepositoryPathLength(repository.length());

	ClassLoader moduleClassLoader = Checker.class.getClassLoader();
	RootModule rootModule = getRootModule(config.getName(), moduleClassLoader);

	rootModule.setModuleClassLoader(moduleClassLoader);
	rootModule.configure(config);
	rootModule.addListener(listener);

	// executes checkstyle
	rootModule.process((List<File>) FileUtils.listFiles(new File(repository), EXTENSION_FILE_FILTER, true));
	rootModule.destroy();

	return listener.getFileErrors();
}
 
Example #7
Source File: CheckstyleSourceCodeFileAnalyzerPlugin.java    From coderadar with MIT License 5 votes vote down vote up
private void init(Configuration configuration) {
  checker = new Checker();
  try {
    auditListener = new CoderadarAuditListener();
    final ClassLoader moduleClassLoader = Checker.class.getClassLoader();
    checker.setModuleClassLoader(moduleClassLoader);
    checker.configure(configuration);
    checker.addListener(auditListener);
  } catch (CheckstyleException e) {
    throw new AnalyzerConfigurationException(e);
  }
}
 
Example #8
Source File: BcelCheckTestCase.java    From contribution with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected DefaultConfiguration createCheckerConfig(Configuration aConfig)
{
    final DefaultConfiguration dc =
        new DefaultConfiguration("configuration");
    final DefaultConfiguration twConf = createCheckConfig(ClassFileSetCheck.class);
    dc.addChild(twConf);
    twConf.addChild(aConfig);
    return dc;
}
 
Example #9
Source File: BcelCheckTestCase.java    From contribution with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected Checker createChecker(Configuration aCheckConfig)
    throws Exception
{
    final DefaultConfiguration dc = createCheckerConfig(aCheckConfig);
    final Checker c = new Checker();
    // make sure the tests always run with english error messages
    // so the tests don't fail in supported locales like german
    final Locale locale = Locale.ENGLISH;
    c.setLocaleCountry(locale.getCountry());
    c.setLocaleLanguage(locale.getLanguage());
    c.configure(dc);
    c.addListener(new BriefLogger(mStream));
    return c;
}
 
Example #10
Source File: BcelCheckTestCase.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected DefaultConfiguration createCheckerConfig(Configuration aConfig)
{
    final DefaultConfiguration dc =
        new DefaultConfiguration("configuration");
    final DefaultConfiguration twConf = createCheckConfig(ClassFileSetCheck.class);
    dc.addChild(twConf);
    twConf.addChild(aConfig);
    return dc;
}
 
Example #11
Source File: BcelCheckTestCase.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected Checker createChecker(Configuration aCheckConfig)
    throws Exception
{
    final DefaultConfiguration dc = createCheckerConfig(aCheckConfig);
    final Checker c = new Checker();
    // make sure the tests always run with english error messages
    // so the tests don't fail in supported locales like german
    final Locale locale = Locale.ENGLISH;
    c.setLocaleCountry(locale.getCountry());
    c.setLocaleLanguage(locale.getLanguage());
    c.configure(dc);
    c.addListener(new BriefLogger(mStream));
    return c;
}
 
Example #12
Source File: CheckstyleConfigurationTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void getCheckstyleConfiguration() throws Exception {
    fileSystem.setEncoding(StandardCharsets.UTF_8);
    final MapSettings mapSettings = new MapSettings(new PropertyDefinitions(
            new CheckstylePlugin().getExtensions()));
    mapSettings.setProperty(CheckstyleConstants.CHECKER_FILTERS_KEY,
            CheckstyleConstants.CHECKER_FILTERS_DEFAULT_VALUE);
    final org.sonar.api.config.Configuration settings = new ConfigurationBridge(mapSettings);

    final RulesProfile profile = RulesProfile.create("sonar way", "java");

    final Rule rule = Rule.create("checkstyle", "CheckStyleRule1", "checkstyle rule one");
    rule.setConfigKey("checkstyle/rule1");
    profile.activateRule(rule, null);

    final CheckstyleConfiguration configuration = new CheckstyleConfiguration(settings,
            new CheckstyleProfileExporter(settings),
            new DefaultActiveRules(Collections.emptyList()), fileSystem);
    final Configuration checkstyleConfiguration = configuration.getCheckstyleConfiguration();
    assertThat(checkstyleConfiguration).isNotNull();
    assertThat(checkstyleConfiguration.getAttribute("charset")).isEqualTo("UTF-8");
    final File xmlFile = new File("checkstyle.xml");
    assertThat(xmlFile.exists()).isTrue();

    FileUtils.forceDelete(xmlFile);
}
 
Example #13
Source File: CheckstyleConfiguration.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
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);
    }
}
 
Example #14
Source File: CheckstyleConfiguration.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
public CheckstyleConfiguration(
        org.sonar.api.config.Configuration conf,
        CheckstyleProfileExporter confExporter,
        ActiveRules activeRules,
        FileSystem fileSystem) {
    this.conf = conf;
    this.confExporter = confExporter;
    this.activeRules = activeRules;
    this.fileSystem = fileSystem;
}
 
Example #15
Source File: CTransformationClass.java    From eclipse-cs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Method for setting the field attributes.
 * 
 * @param rule
 *          The checkstyle-rule associated to this class.
 */
protected final void setRule(final Configuration rule) {
  final String[] attrs = rule.getAttributeNames();
  for (final String att : attrs) {
    try {
      mAttributes.put(att, rule.getAttribute(att));
    } catch (CheckstyleException e) {
      // shouldn't happen since we only use existing attribute names
      throw new RuntimeException(e);
    }
  }
}
 
Example #16
Source File: SpringConfigurationLoader.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
private FileSetCheck load(Configuration configuration) {
	Object module = createModule(configuration);
	if (!(module instanceof FileSetCheck)) {
		throw new IllegalStateException(configuration.getName() + " is not allowed");
	}
	return (FileSetCheck) module;
}
 
Example #17
Source File: Main.java    From diff-check with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 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;
}
 
Example #18
Source File: NoHttpCheckITest.java    From nohttp with Apache License 2.0 5 votes vote down vote up
@Test
public void processHasExpectedResults() throws Exception {
	Configuration configuration = loadConfiguration();
	RootModule rootModule = createRootModule(configuration);
	try {
		processAndCheckResults(rootModule);
	}
	finally {
		rootModule.destroy();
	}
}
 
Example #19
Source File: NoHttpCheckITest.java    From nohttp with Apache License 2.0 5 votes vote down vote up
private Configuration loadConfiguration() throws Exception {
	Properties properties = this.parameter.getProperties();
	try (InputStream inputStream = this.parameter.getConfig()) {
		Configuration configuration = ConfigurationLoader.loadConfiguration(
				new InputSource(inputStream),
				new PropertiesExpander(properties),
				ConfigurationLoader.IgnoredModulesOptions.EXECUTE,
				ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE);
		return configuration;
	}
}
 
Example #20
Source File: NoHttpCheckITest.java    From nohttp with Apache License 2.0 5 votes vote down vote up
private RootModule createRootModule(Configuration configuration)
		throws CheckstyleException {
	ModuleFactory factory = new PackageObjectFactory(
			Checker.class.getPackage().getName(), getClass().getClassLoader());
	RootModule rootModule = (RootModule) factory
			.createModule(configuration.getName());
	rootModule.setModuleClassLoader(getClass().getClassLoader());
	rootModule.configure(configuration);
	return rootModule;
}
 
Example #21
Source File: SpringConfigurationLoader.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
private Configuration loadConfiguration(InputStream inputStream, PropertyResolver propertyResolver) {
	try {
		InputSource inputSource = new InputSource(inputStream);
		return ConfigurationLoader.loadConfiguration(inputSource, propertyResolver, IgnoredModulesOptions.EXECUTE);
	}
	catch (CheckstyleException ex) {
		throw new IllegalStateException(ex);
	}
}
 
Example #22
Source File: TransformCheckstyleRulesJob.java    From eclipse-cs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static void recurseConfiguration(Configuration module, List<Configuration> flatModules) {

    flatModules.add(module);

    Configuration[] childs = module.getChildren();
    if (childs != null && childs.length > 0) {

      for (Configuration child : childs) {
        recurseConfiguration(child, flatModules);
      }
    }
  }
 
Example #23
Source File: SpringConfigurationLoader.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
private Object createModule(Configuration configuration) {
	String name = configuration.getName();
	try {
		Object module = this.moduleFactory.createModule(name);
		if (module instanceof AutomaticBean) {
			initialize(configuration, (AutomaticBean) module);
		}
		return module;
	}
	catch (CheckstyleException ex) {
		throw new IllegalStateException("cannot initialize module " + name + " - " + ex.getMessage(), ex);
	}
}
 
Example #24
Source File: SpringChecksTests.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
@Test
public void processHasExpectedResults() throws Exception {
	Locale previousLocale = Locale.getDefault();
	Locale.setDefault(Locale.ENGLISH);
	Configuration configuration = loadConfiguration();
	RootModule rootModule = createRootModule(configuration);
	try {
		processAndCheckResults(rootModule);
	}
	finally {
		rootModule.destroy();
		Locale.setDefault(previousLocale);
	}
}
 
Example #25
Source File: SpringChecksTests.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
private Configuration loadConfiguration() throws Exception {
	try (InputStream inputStream = new FileInputStream(this.parameter.getConfigFile())) {
		Configuration configuration = ConfigurationLoader.loadConfiguration(new InputSource(inputStream),
				new PropertiesExpander(new Properties()), IgnoredModulesOptions.EXECUTE,
				ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE);
		return configuration;
	}
}
 
Example #26
Source File: SpringChecksTests.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
private RootModule createRootModule(Configuration configuration) throws CheckstyleException {
	ModuleFactory factory = new PackageObjectFactory(Checker.class.getPackage().getName(),
			getClass().getClassLoader());
	RootModule rootModule = (RootModule) factory.createModule(configuration.getName());
	rootModule.setModuleClassLoader(getClass().getClassLoader());
	rootModule.configure(configuration);
	return rootModule;
}
 
Example #27
Source File: SpringConfigurationLoader.java    From spring-javaformat with Apache License 2.0 4 votes vote down vote up
private void initialize(Configuration configuration, AutomaticBean bean) throws CheckstyleException {
	bean.contextualize(this.context);
	bean.configure(configuration);
}
 
Example #28
Source File: SpringChecks.java    From spring-javaformat with Apache License 2.0 4 votes vote down vote up
@Override
public void setupChild(Configuration configuration) throws CheckstyleException {
	throw new CheckstyleException("SpringChecks is not allowed as a parent of " + configuration.getName());
}
 
Example #29
Source File: CheckstyleConfiguration.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void defineCharset(Configuration configuration) {
    defineModuleCharset(configuration);
    for (Configuration module : configuration.getChildren()) {
        defineModuleCharset(module);
    }
}
 
Example #30
Source File: BcelCheckTestCase.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected void verify(Configuration aConfig, String aFileName, String[] aExpected)
        throws Exception
{
    verify(createChecker(aConfig), aFileName, aFileName, aExpected);
}