Search in sources :

Example 1 with PackageObjectFactory

use of com.puppycrawl.tools.checkstyle.PackageObjectFactory in project eclipse-cs by checkstyle.

the class CheckerFactory method createCheckerInternal.

/**
 * Creates a new checker and configures it with the given configuration file.
 *
 * @param input
 *          the input source for the configuration file
 * @param configFileUri
 *          the URI of the configuration file, or <code>null</code> if it could not be determined
 * @param propResolver
 *          a property resolver null
 * @param project
 *          the project
 * @return the newly created Checker
 * @throws CheckstyleException
 *           an exception during the creation of the checker occured
 */
private static Checker createCheckerInternal(InputSource input, PropertyResolver propResolver, IProject project) throws CheckstyleException, CheckstylePluginException {
    // load configuration
    final Configuration configuration = ConfigurationLoader.loadConfiguration(input, propResolver, IgnoredModulesOptions.OMIT);
    ClassLoader moduleClassLoader = CheckstylePlugin.getDefault().getAddonExtensionClassLoader();
    Set<String> packageNames = PackageNamesLoader.getPackageNames(moduleClassLoader);
    // create and configure checker
    Checker checker = new Checker();
    checker.setModuleFactory(new PackageObjectFactory(packageNames, moduleClassLoader, ModuleLoadOption.TRY_IN_ALL_REGISTERED_PACKAGES));
    try {
        checker.setCharset(project.getDefaultCharset());
    } catch (UnsupportedEncodingException e) {
        CheckstylePluginException.rethrow(e);
    } catch (CoreException e) {
        CheckstylePluginException.rethrow(e);
    }
    // set the eclipse platform locale
    Locale platformLocale = CheckstylePlugin.getPlatformLocale();
    checker.setLocaleLanguage(platformLocale.getLanguage());
    checker.setLocaleCountry(platformLocale.getCountry());
    checker.setClassLoader(sSharedClassLoader);
    checker.configure(configuration);
    // reset the basedir if it is set so it won't get into the plugins way
    // of determining workspace resources from checkstyle reported file
    // names, see
    // https://sourceforge.net/tracker/?func=detail&aid=2880044&group_id=80344&atid=559497
    checker.setBasedir(null);
    return checker;
}
Also used : Locale(java.util.Locale) Checker(com.puppycrawl.tools.checkstyle.Checker) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) ICheckConfiguration(net.sf.eclipsecs.core.config.ICheckConfiguration) CoreException(org.eclipse.core.runtime.CoreException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PackageObjectFactory(com.puppycrawl.tools.checkstyle.PackageObjectFactory)

Example 2 with PackageObjectFactory

use of com.puppycrawl.tools.checkstyle.PackageObjectFactory in project checkstyle by checkstyle.

the class CheckstyleAntTask method createRootModule.

/**
 * Creates new instance of the root module.
 *
 * @return new instance of the root module
 * @throws BuildException if the root module could not be created.
 */
private RootModule createRootModule() {
    final RootModule rootModule;
    try {
        final Properties props = createOverridingProperties();
        final ThreadModeSettings threadModeSettings = ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE;
        final ConfigurationLoader.IgnoredModulesOptions ignoredModulesOptions;
        if (executeIgnoredModules) {
            ignoredModulesOptions = ConfigurationLoader.IgnoredModulesOptions.EXECUTE;
        } else {
            ignoredModulesOptions = ConfigurationLoader.IgnoredModulesOptions.OMIT;
        }
        final Configuration configuration = ConfigurationLoader.loadConfiguration(config, new PropertiesExpander(props), ignoredModulesOptions, threadModeSettings);
        final ClassLoader moduleClassLoader = Checker.class.getClassLoader();
        final ModuleFactory factory = new PackageObjectFactory(Checker.class.getPackage().getName() + ".", moduleClassLoader);
        rootModule = (RootModule) factory.createModule(configuration.getName());
        rootModule.setModuleClassLoader(moduleClassLoader);
        rootModule.configure(configuration);
    } catch (final CheckstyleException ex) {
        throw new BuildException(String.format(Locale.ROOT, "Unable to create Root Module: " + "config {%s}, classpath {%s}.", config, classpath), ex);
    }
    return rootModule;
}
Also used : Checker(com.puppycrawl.tools.checkstyle.Checker) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) PropertiesExpander(com.puppycrawl.tools.checkstyle.PropertiesExpander) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Properties(java.util.Properties) ModuleFactory(com.puppycrawl.tools.checkstyle.ModuleFactory) ThreadModeSettings(com.puppycrawl.tools.checkstyle.ThreadModeSettings) RootModule(com.puppycrawl.tools.checkstyle.api.RootModule) BuildException(org.apache.tools.ant.BuildException) PackageObjectFactory(com.puppycrawl.tools.checkstyle.PackageObjectFactory) ConfigurationLoader(com.puppycrawl.tools.checkstyle.ConfigurationLoader)

Aggregations

Checker (com.puppycrawl.tools.checkstyle.Checker)2 PackageObjectFactory (com.puppycrawl.tools.checkstyle.PackageObjectFactory)2 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)2 ConfigurationLoader (com.puppycrawl.tools.checkstyle.ConfigurationLoader)1 ModuleFactory (com.puppycrawl.tools.checkstyle.ModuleFactory)1 PropertiesExpander (com.puppycrawl.tools.checkstyle.PropertiesExpander)1 ThreadModeSettings (com.puppycrawl.tools.checkstyle.ThreadModeSettings)1 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)1 RootModule (com.puppycrawl.tools.checkstyle.api.RootModule)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Locale (java.util.Locale)1 Properties (java.util.Properties)1 ICheckConfiguration (net.sf.eclipsecs.core.config.ICheckConfiguration)1 BuildException (org.apache.tools.ant.BuildException)1 CoreException (org.eclipse.core.runtime.CoreException)1