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

The following examples show how to use com.puppycrawl.tools.checkstyle.api.AutomaticBean. 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: Main.java    From diff-check with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Create {@link AutomaticBean.OutputStreamOptions} for the given location.
 * @param outputLocation output location
 * @return output stream options
 */
private static AutomaticBean.OutputStreamOptions getOutputStreamOptions(String outputLocation) {
    final AutomaticBean.OutputStreamOptions result;
    if (outputLocation == null) {
        result = AutomaticBean.OutputStreamOptions.NONE;
    }
    else {
        result = AutomaticBean.OutputStreamOptions.CLOSE;
    }
    return result;
}
 
Example #2
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 #3
Source File: CheckstyleExecutor.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void executeWithClassLoader() {
    final Checker checker = new Checker();
    OutputStream xmlOutput = null;
    try {
        checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
        checker.addListener(listener);

        final File xmlReport = configuration.getTargetXmlReport();
        if (xmlReport != null) {
            LOG.info("Checkstyle output report: {}", xmlReport.getAbsolutePath());
            xmlOutput = FileUtils.openOutputStream(xmlReport);
            checker.addListener(
                    new XMLLogger(xmlOutput, AutomaticBean.OutputStreamOptions.CLOSE));
        }

        checker.setCharset(configuration.getCharset().name());
        checker.configure(configuration.getCheckstyleConfiguration());
        checker.process(configuration
                .getSourceFiles()
                .stream()
                .map(InputFile::file)
                .collect(Collectors.toList()));
    }
    catch (Exception ex) {
        throw new IllegalStateException("Can not execute Checkstyle", ex);
    }
    finally {
        checker.destroy();
        if (Objects.nonNull(xmlOutput)) {
            close(xmlOutput);
        }
    }
}
 
Example #4
Source File: Main.java    From diff-check with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Executes required Checkstyle actions based on passed parameters.
 * @param cliOptions
 *        pojo object that contains all options
 * @return number of violations of ERROR level
 * @throws IOException
 *         when output file could not be found
 * @throws CheckstyleException
 *         when properties file could not be loaded
 */
private static int runCheckstyle(CliOptions cliOptions)
        throws CheckstyleException, IOException {
    // setup the properties
    final Properties props;

    if (cliOptions.propertiesLocation == null) {
        props = System.getProperties();
    }
    else {
        props = loadProperties(new File(cliOptions.propertiesLocation));
    }

    // create a configuration
    final ThreadModeSettings multiThreadModeSettings =
            new ThreadModeSettings(
                    cliOptions.checkerThreadsNumber, cliOptions.treeWalkerThreadsNumber);

    final ConfigurationLoader.IgnoredModulesOptions ignoredModulesOptions;
    if (cliOptions.executeIgnoredModules) {
        ignoredModulesOptions = ConfigurationLoader.IgnoredModulesOptions.EXECUTE;
    }
    else {
        ignoredModulesOptions = ConfigurationLoader.IgnoredModulesOptions.OMIT;
    }

    final Configuration config = ConfigurationLoader.loadConfiguration(
            cliOptions.configLocation, new PropertiesExpander(props),
            ignoredModulesOptions, multiThreadModeSettings);

    // create RootModule object and run it
    final int errorCounter;
    final ClassLoader moduleClassLoader = Checker.class.getClassLoader();
    final RootModule rootModule = getRootModule(config.getName(), moduleClassLoader);

    try {
        final AuditListener listener;
        if (cliOptions.generateXpathSuppressionsFile) {
            // create filter to print generated xpath suppressions file
            final Configuration treeWalkerConfig = getTreeWalkerConfig(config);
            if (treeWalkerConfig != null) {
                final DefaultConfiguration moduleConfig =
                        new DefaultConfiguration(
                                XpathFileGeneratorAstFilter.class.getName());
                moduleConfig.addAttribute(OPTION_TAB_WIDTH_NAME,
                        Integer.toString(cliOptions.tabWidth));
                ((DefaultConfiguration) treeWalkerConfig).addChild(moduleConfig);
            }

            listener = new XpathFileGeneratorAuditListener(System.out,
                    AutomaticBean.OutputStreamOptions.NONE);
        }
        else {
            listener = createListener(cliOptions.format,
                    cliOptions.outputLocation);
        }

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

        if (CollectionUtils.isNotEmpty(DIFF_ENTRY_LIST) && rootModule instanceof Checker) {
            Checker checker = (Checker) rootModule;
            checker.addFilter(new DiffLineFilter(DIFF_ENTRY_LIST));
        }

        // run RootModule
        errorCounter = rootModule.process(cliOptions.files);
    }
    finally {
        rootModule.destroy();
    }

    return errorCounter;
}
 
Example #5
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 #6
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 extends 'AutomaticBean', is non-abstract, and doesn't start with the
 * word 'Input' (are not input files for UTs).
 * @param loadedClass class to check.
 * @param className class name to check.
 * @return true if a class may be considered a valid production class.
 */
public static boolean isValidCheckstyleClass(Class<?> loadedClass, String className) {
    return AutomaticBean.class.isAssignableFrom(loadedClass)
            && !Modifier.isAbstract(loadedClass.getModifiers())
            && !className.contains("Input");
}