/** * Creates a new instance of the root module that will control and run * Checkstyle. * @param name The name of the module. This will either be a short name that * will have to be found or the complete package name. * @param moduleClassLoader Class loader used to load the root module. * @return The new instance of the root module. * @throws CheckstyleException if no module can be instantiated from name */ private static RootModule getRootModule(String name, ClassLoader moduleClassLoader) throws CheckstyleException { final ModuleFactory factory = new PackageObjectFactory( Checker.class.getPackage().getName(), moduleClassLoader); return (RootModule) factory.createModule(name); }
@Override public void addError(AuditEvent event) { final String xpathQuery = XpathFileGeneratorAstFilter.findCorrespondingXpathQuery(event); if (xpathQuery != null) { printXmlHeader(); final File file = new File(event.getFileName()); writer.println("<suppress-xpath"); writer.print(" files=\""); writer.print(file.getName()); writer.println(QUOTE_CHAR); if (event.getModuleId() == null) { final String checkName = PackageObjectFactory.getShortFromFullModuleNames(event.getSourceName()); writer.print(" checks=\""); writer.print(checkName); } else { writer.print(" id=\""); writer.print(event.getModuleId()); } writer.println(QUOTE_CHAR); writer.print(" query=\""); writer.print(xpathQuery); writer.println("\"/>"); } }
instance = createFromStandardCheckSet(name); generateThirdPartyNameToFullModuleName(moduleClassLoader); instance = createObjectFromMap(name, thirdPartyNameToFullModuleNames); instance = createObject(name); instance = createModuleByTryInEachPackage(name); if (!name.contains(PACKAGE_SEPARATOR)) { final String nameCheck = name + CHECK_SUFFIX; attemptedNames = joinPackageNamesWithClassName(name, packages) + STRING_SEPARATOR + nameCheck + STRING_SEPARATOR + joinPackageNamesWithClassName(nameCheck, packages);
/** * Fill short-to-full module names map. */ private static void fillShortToFullModuleNamesMap() { fillChecksFromAnnotationPackage(); fillChecksFromBlocksPackage(); fillChecksFromCodingPackage(); fillChecksFromDesignPackage(); fillChecksFromHeaderPackage(); fillChecksFromImportsPackage(); fillChecksFromIndentationPackage(); fillChecksFromJavadocPackage(); fillChecksFromMetricsPackage(); fillChecksFromModifierPackage(); fillChecksFromNamingPackage(); fillChecksFromRegexpPackage(); fillChecksFromSizesPackage(); fillChecksFromWhitespacePackage(); fillModulesFromChecksPackage(); fillModulesFromFilefiltersPackage(); fillModulesFromFiltersPackage(); fillModulesFromCheckstylePackage(); }
@Test public void testCreateStandardCheckModuleObjectFromMap() throws Exception { final String moduleName = "TypeName"; final String packageName = BASE_PACKAGE + ".packageobjectfactory.bar"; final String fullName = BASE_PACKAGE + PACKAGE_SEPARATOR + "checks" + PACKAGE_SEPARATOR + "naming" + PACKAGE_SEPARATOR + moduleName + CHECK_SUFFIX; final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); final PackageObjectFactory objectFactory = new PackageObjectFactory(packageName, classLoader); final Object instance = objectFactory.createModule(moduleName); assertEquals("Invalid canonical name", fullName, instance.getClass().getCanonicalName()); }
@Override public Object createModule(String name) throws CheckstyleException { Object adapter = auditAdapter; if (!name.equals(DebugAuditAdapter.class.getName())) { adapter = super.createModule(name); } return adapter; } };
/** * Create object from one of Checkstyle module names. * @param name name of module. * @return instance of module. * @throws CheckstyleException if the class fails to instantiate or there are ambiguous classes. */ private Object createFromStandardCheckSet(String name) throws CheckstyleException { final String fullModuleName = NAME_TO_FULL_MODULE_NAME.get(name); Object instance = null; if (fullModuleName == null) { final String fullCheckModuleName = NAME_TO_FULL_MODULE_NAME.get(name + CHECK_SUFFIX); if (fullCheckModuleName != null) { instance = createObject(fullCheckModuleName); } } else { instance = createObject(fullModuleName); } return instance; }
/** * Create object with the help of the supplied map. * @param name name of module. * @param map the supplied map. * @return instance of module if it is found in modules map and no ambiguous classes exist. * @throws CheckstyleException if the class fails to instantiate or there are ambiguous classes. */ private Object createObjectFromMap(String name, Map<String, Set<String>> map) throws CheckstyleException { final Set<String> fullModuleNames = map.get(name); Object instance = null; if (fullModuleNames == null) { final Set<String> fullCheckModuleNames = map.get(name + CHECK_SUFFIX); if (fullCheckModuleNames != null) { instance = createObjectFromFullModuleNames(name, fullCheckModuleNames); } } else { instance = createObjectFromFullModuleNames(name, fullModuleNames); } return instance; }
/** * Fill short-to-full module names map. */ private static void fillShortToFullModuleNamesMap() { fillChecksFromAnnotationPackage(); fillChecksFromBlocksPackage(); fillChecksFromCodingPackage(); fillChecksFromDesignPackage(); fillChecksFromHeaderPackage(); fillChecksFromImportsPackage(); fillChecksFromIndentationPackage(); fillChecksFromJavadocPackage(); fillChecksFromMetricsPackage(); fillChecksFromModifierPackage(); fillChecksFromNamingPackage(); fillChecksFromRegexpPackage(); fillChecksFromSizesPackage(); fillChecksFromWhitespacePackage(); fillModulesFromChecksPackage(); fillModulesFromFilefiltersPackage(); fillModulesFromFiltersPackage(); fillModulesFromCheckstylePackage(); }
@Test public void testCreateStandardModuleObjectFromMap() throws Exception { final String moduleName = "TreeWalker"; final String packageName = BASE_PACKAGE + ".packageobjectfactory.bar"; final String fullName = BASE_PACKAGE + PACKAGE_SEPARATOR + moduleName; final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); final PackageObjectFactory objectFactory = new PackageObjectFactory(packageName, classLoader); final Object instance = objectFactory.createModule(moduleName); assertEquals("Invalid canonical name", fullName, instance.getClass().getCanonicalName()); }
@Override public Object createModule(String name) throws CheckstyleException { Object filter = fileFilter; if (!name.equals(TestBeforeExecutionFileFilter.class.getName())) { filter = super.createModule(name); } return filter; } };
/** * Searching to class with given name (or name concatenated with "Check") in existing * packages. Returns instance if class found or, otherwise, null. * @param name the name of a class. * @return the {@code Object} created by loader. * @throws CheckstyleException if an error occurs. */ private Object createModuleByTryInEachPackage(String name) throws CheckstyleException { final List<String> possibleNames = packages.stream() .map(packageName -> packageName + PACKAGE_SEPARATOR + name) .flatMap(className -> Stream.of(className, className + CHECK_SUFFIX)) .collect(Collectors.toList()); Object instance = null; for (String possibleName : possibleNames) { instance = createObject(possibleName); if (instance != null) { break; } } return instance; }
/** * Create object with the help of the supplied map. * @param name name of module. * @param map the supplied map. * @return instance of module if it is found in modules map and no ambiguous classes exist. * @throws CheckstyleException if the class fails to instantiate or there are ambiguous classes. */ private Object createObjectFromMap(String name, Map<String, Set<String>> map) throws CheckstyleException { final Set<String> fullModuleNames = map.get(name); Object instance = null; if (fullModuleNames == null) { final Set<String> fullCheckModuleNames = map.get(name + CHECK_SUFFIX); if (fullCheckModuleNames != null) { instance = createObjectFromFullModuleNames(name, fullCheckModuleNames); } } else { instance = createObjectFromFullModuleNames(name, fullModuleNames); } return instance; }
/** * Returns the default PackageObjectFactory with the default package names. * @return the default PackageObjectFactory. */ public static PackageObjectFactory getPackageObjectFactory() throws CheckstyleException { final ClassLoader cl = TestUtil.class.getClassLoader(); final Set<String> packageNames = PackageNamesLoader.getPackageNames(cl); return new PackageObjectFactory(packageNames, cl); }
@Test public void testCreateCheckWithPartialPackageNameByBruteForce() throws Exception { final String checkName = "checks.annotation.AnnotationLocation"; final PackageObjectFactory packageObjectFactory = new PackageObjectFactory( new HashSet<>(Collections.singletonList(BASE_PACKAGE)), Thread.currentThread().getContextClassLoader(), TRY_IN_ALL_REGISTERED_PACKAGES); final AnnotationLocationCheck check = (AnnotationLocationCheck) packageObjectFactory .createModule(checkName); assertNotNull("Check should not be null when creating module from name", check); }
instance = createFromStandardCheckSet(name); generateThirdPartyNameToFullModuleName(moduleClassLoader); instance = createObjectFromMap(name, thirdPartyNameToFullModuleNames); instance = createObject(name); instance = createModuleByTryInEachPackage(name); if (!name.contains(PACKAGE_SEPARATOR)) { final String nameCheck = name + CHECK_SUFFIX; attemptedNames = joinPackageNamesWithClassName(name, packages) + STRING_SEPARATOR + nameCheck + STRING_SEPARATOR + joinPackageNamesWithClassName(nameCheck, packages);
@Test public void testGetShortFromFullModuleNamesThirdParty() { final String fullName = "java.util.stream.Collectors"; assertEquals("Invalid simple check name", fullName, PackageObjectFactory.getShortFromFullModuleNames(fullName)); }
@Override public Object createModule(String name) throws CheckstyleException { Object check = fileSet; if (!name.equals(DummyFileSet.class.getName())) { check = super.createModule(name); } return check; } };
/** * Create Object from optional full module names. * In most case, there should be only one element in {@code fullModuleName}, otherwise * an exception would be thrown. * @param name name of module * @param fullModuleNames the supplied full module names set * @return instance of module if there is only one element in {@code fullModuleName} * @throws CheckstyleException if the class fails to instantiate or there are more than one * element in {@code fullModuleName} */ private Object createObjectFromFullModuleNames(String name, Set<String> fullModuleNames) throws CheckstyleException { final Object returnValue; if (fullModuleNames.size() == 1) { returnValue = createObject(fullModuleNames.iterator().next()); } else { final String optionalNames = fullModuleNames.stream() .sorted() .collect(Collectors.joining(STRING_SEPARATOR)); final LocalizedMessage exceptionMessage = new LocalizedMessage(1, Definitions.CHECKSTYLE_BUNDLE, AMBIGUOUS_MODULE_NAME_EXCEPTION_MESSAGE, new String[] {name, optionalNames}, null, getClass(), null); throw new CheckstyleException(exceptionMessage.getMessage()); } return returnValue; }
@Test public void testCreateCheckByBruteForce() throws Exception { final String checkName = "AnnotationLocation"; final Method createModuleByBruteForce = PackageObjectFactory.class.getDeclaredMethod( "createModuleByTryInEachPackage", String.class); final PackageObjectFactory packageObjectFactory = new PackageObjectFactory( new HashSet<>(Arrays.asList(BASE_PACKAGE, BASE_PACKAGE + ".checks.annotation")), Thread.currentThread().getContextClassLoader(), TRY_IN_ALL_REGISTERED_PACKAGES); createModuleByBruteForce.setAccessible(true); final AnnotationLocationCheck check = (AnnotationLocationCheck) createModuleByBruteForce .invoke(packageObjectFactory, checkName); assertNotNull("Check should not be null when creating module from name", check); }