com.puppycrawl.tools.checkstyle.PackageNamesLoader Java Examples

The following examples show how to use com.puppycrawl.tools.checkstyle.PackageNamesLoader. 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: MetadataFactory.java    From eclipse-cs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Helper method to get all potential metadata files using the checkstyle_packages.xml as base
 * where to look. It is not guaranteed that the files returned acutally exist.
 *
 * @return the collection of potential metadata files.
 * @throws CheckstylePluginException
 *           an unexpected exception ocurred
 */
private static Collection<String> getAllPotentialMetadataFiles(ClassLoader classLoader)
        throws CheckstylePluginException {

  Collection<String> potentialMetadataFiles = new ArrayList<>();

  Set<String> packages = null;
  try {
    packages = PackageNamesLoader.getPackageNames(classLoader);
  } catch (CheckstyleException e) {
    CheckstylePluginException.rethrow(e);
  }

  for (String packageName : packages) {
    String metaFileLocation = packageName.replace('.', '/');
    if (!metaFileLocation.endsWith("/")) {
      metaFileLocation = metaFileLocation + "/";
    }
    metaFileLocation = metaFileLocation + METADATA_FILENAME;
    potentialMetadataFiles.add(metaFileLocation);
  }

  return potentialMetadataFiles;
}
 
Example #2
Source File: CheckstyleExecutor.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Execute Checkstyle and return the generated XML report.
 * @noinspection TooBroadScope
 */
public void execute(SensorContext context) {
    if (Objects.nonNull(listener)) {
        listener.setContext(context);
    }

    final Locale initialLocale = Locale.getDefault();
    Locale.setDefault(Locale.ENGLISH);
    final ClassLoader initialClassLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(PackageNamesLoader.class.getClassLoader());
    try {
        executeWithClassLoader();
    }
    finally {
        Thread.currentThread().setContextClassLoader(initialClassLoader);
        Locale.setDefault(initialLocale);
    }
}
 
Example #3
Source File: CheckFilter.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
private ModuleFactory createModuleFactory() {
	try {
		ClassLoader classLoader = AbstractCheck.class.getClassLoader();
		Set<String> packageNames = PackageNamesLoader.getPackageNames(classLoader);
		return new PackageObjectFactory(packageNames, classLoader);
	}
	catch (CheckstyleException ex) {
		throw new IllegalStateException(ex);
	}
}
 
Example #4
Source File: CheckUtil.java    From eclipse-cs with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static Set<Class<?>> getCheckstyleModules() throws Exception {
  final ClassLoader loader = Thread.currentThread().getContextClassLoader();
  return ModuleReflectionUtil.getCheckstyleModules(PackageNamesLoader.getPackageNames(loader),
          loader);
}