diff options
author | Andrei Selkin <andreyselkin@gmail.com> | 2016-09-22 01:35:17 +0300 |
---|---|---|
committer | Roman Ivanov <romani@users.noreply.github.com> | 2016-09-21 15:35:17 -0700 |
commit | ee66d82723cb6f2f4c74f6d91dd9c4f58beb5775 (patch) | |
tree | 5042eaa84c62fa7ab02a5b39965c5597d2762e8e | |
parent | fbf3f95c2273be23b966cba6e6ef89390a6fb020 (diff) | |
download | checkstyle-ee66d82723cb6f2f4c74f6d91dd9c4f58beb5775.tar.gz |
Issue #3433: Cut down on Checkstyle's dependencies on Guava (part 3) (#3458)
11 files changed, 80 insertions, 45 deletions
diff --git a/config/checkstyle_sevntu_checks.xml b/config/checkstyle_sevntu_checks.xml index ebcd9211f..8aca176eb 100644 --- a/config/checkstyle_sevntu_checks.xml +++ b/config/checkstyle_sevntu_checks.xml @@ -44,7 +44,8 @@ com.google.common.base.Preconditions,com.google.common.base.Predicate, com.google.common.io.CharSource,com.google.common.annotations.Beta, com.google.common.collect.Queues,com.google.common.collect.Sets, - com.google.common.collect.Lists"/> + com.google.common.collect.Lists,com.google.common.annotations.GwtCompatible, + com.google.common.io.Files,com.google.common.collect.TreeTraverser"/> </module> <module name="ForbidCCommentsInMethods"/> <module name="FinalizeImplementationCheck"/> @@ -111,7 +112,9 @@ com\.google\.common\.base\.Equivalence|com\.google\.common\.base\.Preconditions| com\.google\.common\.base\.Optional|com\.google\.common\.io\.CharSource| com\.google\.common\.primitives.*|com\.google\.common\.collect\.Sets| - com\.google\.common\.collect\.Queues|com\.google\.common\.collect\.Lists"/> + com\.google\.common\.collect\.Queues|com\.google\.common\.collect\.Lists| + com\.google\.common\.annotations\.GwtCompatible|com\.google\.common\.io\.Files| + com\.google\.common\.collect\.TreeTraverser"/> <property name="forbiddenImportsExcludesRegexp" value=""/> </module> <module name="ForbidCertainImports"> diff --git a/config/import-control.xml b/config/import-control.xml index 92d1aaf7a..a07897eaf 100644 --- a/config/import-control.xml +++ b/config/import-control.xml @@ -70,7 +70,6 @@ <subpackage name="checks"> <allow pkg="com.puppycrawl.tools.checkstyle.checks"/> <allow class="com.puppycrawl.tools.checkstyle.Definitions"/> - <allow class="com.google.common.io.Files" local-only="true"/> <allow class="com.google.common.io.Closeables" local-only="true"/> <allow class="com.google.common.collect.HashMultiset" local-only="true"/> <allow class="com.google.common.collect.HashMultimap" local-only="true"/> @@ -99,24 +98,16 @@ <allow pkg="com.puppycrawl.tools.checkstyle.grammars.javadoc"/> <allow pkg="java.lang.reflect"/> <allow class="com.google.common.base.CharMatcher" local-only="true"/> - <allow class="com.google.common.annotations.GwtCompatible" local-only="true"/> - <allow class="com.google.common.annotations.GwtIncompatible" local-only="true"/> <allow class="com.google.common.collect.ImmutableList" local-only="true"/> <allow class="com.google.common.collect.ImmutableMap" local-only="true"/> <allow class="com.google.common.collect.ImmutableSortedSet" local-only="true"/> <allow class="com.google.common.collect.Multiset" local-only="true"/> </subpackage> - <subpackage name="whitespace"> - <allow class="com.google.common.annotations.GwtCompatible" local-only="true"/> - </subpackage> <subpackage name="design"> <allow class="com.google.common.annotations.VisibleForTesting" local-only="true"/> <allow class="com.google.common.collect.ImmutableList" local-only="true"/> <allow class="com.google.common.collect.ImmutableMap" local-only="true"/> </subpackage> - <subpackage name="regexp"> - <allow class="com.google.common.io.Files" local-only="true"/> - </subpackage> <subpackage name="imports"> <allow class="com.google.common.collect.HashMultimap" local-only="true"/> <allow class="com.google.common.collect.ImmutableSet" local-only="true"/> @@ -164,8 +155,6 @@ <subpackage name="internal"> <allow class="com.google.common.reflect.ClassPath" local-only="true"/> - <allow class="com.google.common.io.Files" local-only="true"/> <allow class="com.google.common.collect.FluentIterable" local-only="true"/> - <allow class="com.google.common.collect.TreeTraverser"/> </subpackage> </import-control> diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java index ab9a73a4c..5c41f0921 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java @@ -44,7 +44,6 @@ import org.apache.commons.logging.LogFactory; import com.google.common.collect.HashMultimap; import com.google.common.collect.SetMultimap; import com.google.common.io.Closeables; -import com.google.common.io.Files; import com.puppycrawl.tools.checkstyle.Definitions; import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck; import com.puppycrawl.tools.checkstyle.api.LocalizedMessage; @@ -351,7 +350,7 @@ public class TranslationCheck extends AbstractFileSetCheck { final String baseName = extractBaseName(fileName); final Matcher baseNameMatcher = baseNameRegexp.matcher(baseName); if (baseNameMatcher.matches()) { - final String extension = Files.getFileExtension(fileName); + final String extension = CommonUtils.getFileExtension(fileName); final String path = getPath(currentFile.getAbsolutePath()); final ResourceBundle newBundle = new ResourceBundle(baseName, path, extension); final Optional<ResourceBundle> bundle = findBundle(resourceBundles, newBundle); diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpOnFilenameCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpOnFilenameCheck.java index b98b6b9e0..2d090fcda 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpOnFilenameCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpOnFilenameCheck.java @@ -24,7 +24,6 @@ import java.io.IOException; import java.util.List; import java.util.regex.Pattern; -import com.google.common.io.Files; import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; import com.puppycrawl.tools.checkstyle.utils.CommonUtils; @@ -272,7 +271,7 @@ public class RegexpOnFilenameCheck extends AbstractFileSetCheck { String fileName = file.getName(); if (ignoreFileNameExtensions) { - fileName = Files.getNameWithoutExtension(fileName); + fileName = CommonUtils.getFileNameWithoutExtension(fileName); } return fileName; diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/utils/CommonUtils.java b/src/main/java/com/puppycrawl/tools/checkstyle/utils/CommonUtils.java index 3bc4b830d..2b00c1451 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/utils/CommonUtils.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/utils/CommonUtils.java @@ -425,4 +425,46 @@ public final class CommonUtils { public static boolean isBlank(String str) { return str == null || CharMatcher.WHITESPACE.matchesAllOf(str); } + + /** + * Returns file name without extension. + * We do not use the method from Guava library to reduce Checkstyle's dependencies + * on external libraries. + * @param fullFilename file name with extension. + * @return file name without extension. + */ + public static String getFileNameWithoutExtension(String fullFilename) { + final String fileName = new File(fullFilename).getName(); + final int dotIndex = fileName.lastIndexOf('.'); + final String fileNameWithoutExtension; + if (dotIndex == -1) { + fileNameWithoutExtension = fileName; + } + else { + fileNameWithoutExtension = fileName.substring(0, dotIndex); + } + return fileNameWithoutExtension; + } + + /** + * Returns file extension for the given file name + * or empty string if file does not have an extension. + * We do not use the method from Guava library to reduce Checkstyle's dependencies + * on external libraries. + * @param fileNameWithExtension file name with extension. + * @return file extension for the given file name + * or empty string if file does not have an extension. + */ + public static String getFileExtension(String fileNameWithExtension) { + final String fileName = Paths.get(fileNameWithExtension).toString(); + final int dotIndex = fileName.lastIndexOf('.'); + final String extension; + if (dotIndex == -1) { + extension = ""; + } + else { + extension = fileName.substring(dotIndex + 1); + } + return extension; + } } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocMethodCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocMethodCheckTest.java index 4bf0e89c6..6a20e3b40 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocMethodCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocMethodCheckTest.java @@ -91,7 +91,7 @@ public class JavadocMethodCheckTest extends BaseCheckTestSupport { config.addAttribute("allowedAnnotations", "MyAnnotation, Override"); config.addAttribute("minLineCount", "2"); final String[] expected = { - "46:1: " + getCheckMessage(MSG_JAVADOC_MISSING), + "44:1: " + getCheckMessage(MSG_JAVADOC_MISSING), }; verify(config, getPath("InputExtendAnnotation.java"), expected); } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpMultilineCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpMultilineCheckTest.java index 71804ceb8..ac5345e00 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpMultilineCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpMultilineCheckTest.java @@ -27,13 +27,13 @@ import static com.puppycrawl.tools.checkstyle.checks.regexp.MultilineDetector.MS import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import com.google.common.io.Files; import com.puppycrawl.tools.checkstyle.BaseFileSetCheckTestSupport; import com.puppycrawl.tools.checkstyle.DefaultConfiguration; import com.puppycrawl.tools.checkstyle.utils.CommonUtils; @@ -121,7 +121,8 @@ public class RegexpMultilineCheckTest extends BaseFileSetCheckTestSupport { }; final File file = temporaryFolder.newFile(); - Files.write("first line \r\n second line \n\r third line", file, StandardCharsets.UTF_8); + Files.write(file.toPath(), + "first line \r\n second line \n\r third line".getBytes(StandardCharsets.UTF_8)); verify(checkConfig, file.getPath(), expected); } @@ -160,7 +161,7 @@ public class RegexpMultilineCheckTest extends BaseFileSetCheckTestSupport { }; final File file = temporaryFolder.newFile(); - Files.write(makeLargeXyString(), file, StandardCharsets.UTF_8); + Files.write(file.toPath(), makeLargeXyString().toString().getBytes(StandardCharsets.UTF_8)); verify(checkConfig, file.getPath(), expected); } @@ -175,7 +176,7 @@ public class RegexpMultilineCheckTest extends BaseFileSetCheckTestSupport { }; final File file = temporaryFolder.newFile(); - Files.write("", file, StandardCharsets.UTF_8); + Files.write(file.toPath(), "".getBytes(StandardCharsets.UTF_8)); verify(checkConfig, file.getPath(), expected); } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XDocsPagesTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XDocsPagesTest.java index 1a4951d13..2af998d68 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XDocsPagesTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XDocsPagesTest.java @@ -26,7 +26,9 @@ import java.io.File; import java.io.IOException; import java.io.StringReader; import java.lang.reflect.Field; +import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; @@ -49,7 +51,6 @@ import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import com.google.common.collect.ImmutableSet; -import com.google.common.io.Files; import com.puppycrawl.tools.checkstyle.Checker; import com.puppycrawl.tools.checkstyle.ConfigurationLoader; import com.puppycrawl.tools.checkstyle.ModuleFactory; @@ -60,9 +61,8 @@ import com.puppycrawl.tools.checkstyle.api.CheckstyleException; import com.puppycrawl.tools.checkstyle.api.Configuration; public class XDocsPagesTest { - private static final File JAVA_SOURCES_DIRECTORY = new File("src/main/java"); - private static final String AVAILABLE_CHECKS_PATH = "src/xdocs/checks.xml"; - private static final File AVAILABLE_CHECKS_FILE = new File(AVAILABLE_CHECKS_PATH); + private static final Path JAVA_SOURCES_DIRECTORY = Paths.get("src/main/java"); + private static final Path AVAILABLE_CHECKS_PATH = Paths.get("src/xdocs/checks.xml"); private static final String CHECK_FILE_NAME = ".+Check.java$"; private static final String CHECK_SUFFIX = "Check.java"; private static final String LINK_TEMPLATE = @@ -125,18 +125,18 @@ public class XDocsPagesTest { @Test public void testAllChecksPresentOnAvailableChecksPage() throws IOException { - final String availableChecks = Files.toString(AVAILABLE_CHECKS_FILE, UTF_8); - for (File file : Files.fileTreeTraverser().preOrderTraversal(JAVA_SOURCES_DIRECTORY)) { - final String fileName = file.getName(); + final String availableChecks = new String(Files.readAllBytes(AVAILABLE_CHECKS_PATH), UTF_8); + Files.walk(JAVA_SOURCES_DIRECTORY).forEach(filePath -> { + final String fileName = filePath.getFileName().toString(); if (fileName.matches(CHECK_FILE_NAME) && !CHECKS_ON_PAGE_IGNORE_LIST.contains(fileName)) { final String checkName = fileName.replace(CHECK_SUFFIX, ""); if (!isPresent(availableChecks, checkName)) { Assert.fail(checkName + " is not correctly listed on Available Checks page" - + " - add it to " + AVAILABLE_CHECKS_PATH); + + " - add it to " + AVAILABLE_CHECKS_PATH); } } - } + }); } private static boolean isPresent(String availableChecks, String checkName) { @@ -147,9 +147,8 @@ public class XDocsPagesTest { @Test public void testAllXmlExamples() throws Exception { for (Path path : XDocUtil.getXdocsFilePaths()) { - final File file = path.toFile(); - final String input = Files.toString(file, UTF_8); - final String fileName = file.getName(); + final String input = new String(Files.readAllBytes(path), UTF_8); + final String fileName = path.getFileName().toString(); final Document document = XmlUtil.getRawXml(fileName, input, input); final NodeList sources = document.getElementsByTagName("source"); @@ -254,14 +253,13 @@ public class XDocsPagesTest { final ModuleFactory moduleFactory = TestUtils.getPackageObjectFactory(); for (Path path : XDocUtil.getXdocsConfigFilePaths(XDocUtil.getXdocsFilePaths())) { - final File file = path.toFile(); - final String fileName = file.getName(); + final String fileName = path.getFileName().toString(); if ("config_reporting.xml".equals(fileName)) { continue; } - final String input = Files.toString(file, UTF_8); + final String input = new String(Files.readAllBytes(path), UTF_8); final Document document = XmlUtil.getRawXml(fileName, input, input); final NodeList sources = document.getElementsByTagName("section"); String lastSectioName = null; @@ -767,9 +765,8 @@ public class XDocsPagesTest { @Test public void testAllStyleRules() throws Exception { for (Path path : XDocUtil.getXdocsStyleFilePaths(XDocUtil.getXdocsFilePaths())) { - final File file = path.toFile(); - final String fileName = file.getName(); - final String input = Files.toString(file, UTF_8); + final String fileName = path.getFileName().toString(); + final String input = new String(Files.readAllBytes(path), UTF_8); final Document document = XmlUtil.getRawXml(fileName, input, input); final NodeList sources = document.getElementsByTagName("tr"); String lastRuleName = null; diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/utils/CommonUtilsTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/utils/CommonUtilsTest.java index 8f442a14c..82d7a99da 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/utils/CommonUtilsTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/utils/CommonUtilsTest.java @@ -204,6 +204,13 @@ public class CommonUtilsTest { } @Test + public void testGetFileExtensionForFileNameWithoutExtension() { + final String fileNameWithoutExtension = "file"; + final String extension = CommonUtils.getFileExtension(fileNameWithoutExtension); + assertEquals("", extension); + } + + @Test @PrepareForTest({ CommonUtils.class, CommonUtilsTest.class }) @SuppressWarnings("unchecked") public void testLoadSuppressionsUriSyntaxException() throws Exception { diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/InputExtendAnnotation.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/InputExtendAnnotation.java index 09792fb81..09e710041 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/InputExtendAnnotation.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/InputExtendAnnotation.java @@ -16,8 +16,6 @@ package com.puppycrawl.tools.checkstyle.checks.javadoc; -import com.google.common.annotations.GwtCompatible; -import com.google.common.annotations.GwtIncompatible; import com.google.common.collect.Multiset; import com.google.common.collect.Multiset.Entry; @@ -36,7 +34,7 @@ import java.util.List; * * @author Chris Povirk */ -@GwtCompatible(emulated = true) +@SuppressWarnings(value = "unchecked") public abstract class InputExtendAnnotation<E> { diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/InputEmptyTypesAndCycles.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/InputEmptyTypesAndCycles.java index e1794a342..0309ecabe 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/InputEmptyTypesAndCycles.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/InputEmptyTypesAndCycles.java @@ -12,7 +12,7 @@ import java.lang.annotation.Target; import java.util.function.Function; import java.util.function.Supplier; -import com.google.common.annotations.GwtCompatible; + class myFoo { @@ -56,7 +56,7 @@ class WithEmptyAnonymous @Target( ElementType.ANNOTATION_TYPE) @Documented -@GwtCompatible +@Deprecated @interface Beta {} @interface MapFeature { @interface Require { |