diff --git a/core/src/main/java/org/elasticsearch/common/util/BloomFilter.java b/core/src/main/java/org/elasticsearch/common/util/BloomFilter.java index 0f72d6d62b1..360bd39e800 100644 --- a/core/src/main/java/org/elasticsearch/common/util/BloomFilter.java +++ b/core/src/main/java/org/elasticsearch/common/util/BloomFilter.java @@ -18,7 +18,6 @@ */ package org.elasticsearch.common.util; -import com.google.common.primitives.Ints; import org.apache.lucene.store.DataInput; import org.apache.lucene.store.DataOutput; import org.apache.lucene.store.IndexInput; @@ -325,7 +324,7 @@ public class BloomFilter { private static int size(long bits) { long quotient = bits / 64; long remainder = bits - quotient * 64; - return Ints.checkedCast(remainder == 0 ? quotient : 1 + quotient); + return Math.toIntExact(remainder == 0 ? quotient : 1 + quotient); } // Used by serialization diff --git a/core/src/main/java/org/elasticsearch/common/util/CollectionUtils.java b/core/src/main/java/org/elasticsearch/common/util/CollectionUtils.java index ec37fb21de1..e4a074b3961 100644 --- a/core/src/main/java/org/elasticsearch/common/util/CollectionUtils.java +++ b/core/src/main/java/org/elasticsearch/common/util/CollectionUtils.java @@ -277,6 +277,11 @@ public enum CollectionUtils { }.sort(0, array.size()); } + public static int[] toArray(Collection ints) { + Objects.requireNonNull(ints); + return ints.stream().mapToInt(s -> s).toArray(); + } + private static class RotatedList extends AbstractList implements RandomAccess { private final List in; diff --git a/core/src/main/java/org/elasticsearch/common/util/MultiDataPathUpgrader.java b/core/src/main/java/org/elasticsearch/common/util/MultiDataPathUpgrader.java index 761a1ee5cd0..2cbc8cbdf99 100644 --- a/core/src/main/java/org/elasticsearch/common/util/MultiDataPathUpgrader.java +++ b/core/src/main/java/org/elasticsearch/common/util/MultiDataPathUpgrader.java @@ -18,8 +18,6 @@ */ package org.elasticsearch.common.util; -import java.nio.charset.StandardCharsets; -import com.google.common.primitives.Ints; import org.apache.lucene.index.CheckIndex; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.store.Directory; @@ -43,19 +41,10 @@ import org.elasticsearch.index.shard.ShardStateMetaData; import java.io.IOException; import java.io.PrintStream; -import java.nio.file.DirectoryStream; -import java.nio.file.FileStore; -import java.nio.file.FileVisitResult; -import java.nio.file.FileVisitor; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; +import java.nio.charset.StandardCharsets; +import java.nio.file.*; import java.nio.file.attribute.BasicFileAttributes; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; /** */ @@ -369,12 +358,11 @@ public class MultiDataPathUpgrader { try (DirectoryStream stream = Files.newDirectoryStream(indexPath)) { String currentIndex = indexPath.getFileName().toString(); for (Path shardPath : stream) { - if (Files.isDirectory(shardPath)) { - Integer shardId = Ints.tryParse(shardPath.getFileName().toString()); - if (shardId != null) { - ShardId id = new ShardId(currentIndex, shardId); - shardIds.add(id); - } + String fileName = shardPath.getFileName().toString(); + if (Files.isDirectory(shardPath) && fileName.chars().allMatch(Character::isDigit)) { + int shardId = Integer.parseInt(fileName); + ShardId id = new ShardId(currentIndex, shardId); + shardIds.add(id); } } } diff --git a/core/src/main/java/org/elasticsearch/env/NodeEnvironment.java b/core/src/main/java/org/elasticsearch/env/NodeEnvironment.java index 06562f82db5..7fe71d8535c 100644 --- a/core/src/main/java/org/elasticsearch/env/NodeEnvironment.java +++ b/core/src/main/java/org/elasticsearch/env/NodeEnvironment.java @@ -20,15 +20,9 @@ package org.elasticsearch.env; import com.google.common.collect.ImmutableSet; -import com.google.common.primitives.Ints; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.SegmentInfos; -import org.apache.lucene.store.Directory; -import org.apache.lucene.store.FSDirectory; -import org.apache.lucene.store.Lock; -import org.apache.lucene.store.LockObtainFailedException; -import org.apache.lucene.store.NativeFSLockFactory; -import org.apache.lucene.store.SimpleFSDirectory; +import org.apache.lucene.store.*; import org.apache.lucene.util.IOUtils; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.cluster.metadata.IndexMetaData; @@ -48,21 +42,8 @@ import org.elasticsearch.monitor.fs.FsProbe; import java.io.Closeable; import java.io.IOException; -import java.nio.file.AtomicMoveNotSupportedException; -import java.nio.file.DirectoryStream; -import java.nio.file.FileStore; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Set; +import java.nio.file.*; +import java.util.*; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; @@ -711,12 +692,11 @@ public class NodeEnvironment extends AbstractComponent implements Closeable { try (DirectoryStream stream = Files.newDirectoryStream(indexPath)) { String currentIndex = indexPath.getFileName().toString(); for (Path shardPath : stream) { - if (Files.isDirectory(shardPath)) { - Integer shardId = Ints.tryParse(shardPath.getFileName().toString()); - if (shardId != null) { - ShardId id = new ShardId(currentIndex, shardId); - shardIds.add(id); - } + String fileName = shardPath.getFileName().toString(); + if (Files.isDirectory(shardPath) && fileName.chars().allMatch(Character::isDigit)) { + int shardId = Integer.parseInt(fileName); + ShardId id = new ShardId(currentIndex, shardId); + shardIds.add(id); } } } diff --git a/core/src/test/java/org/elasticsearch/test/discovery/ClusterDiscoveryConfiguration.java b/core/src/test/java/org/elasticsearch/test/discovery/ClusterDiscoveryConfiguration.java index bcd9384a88b..e549c185616 100644 --- a/core/src/test/java/org/elasticsearch/test/discovery/ClusterDiscoveryConfiguration.java +++ b/core/src/test/java/org/elasticsearch/test/discovery/ClusterDiscoveryConfiguration.java @@ -19,11 +19,11 @@ package org.elasticsearch.test.discovery; import com.carrotsearch.randomizedtesting.RandomizedTest; -import com.google.common.primitives.Ints; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.SuppressForbidden; import org.elasticsearch.common.network.NetworkUtils; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.util.CollectionUtils; import org.elasticsearch.test.InternalTestCluster; import org.elasticsearch.test.NodeConfigurationSource; @@ -82,7 +82,7 @@ public class ClusterDiscoveryConfiguration extends NodeConfigurationSource { while (ordinals.size() != numOfUnicastHosts) { ordinals.add(RandomizedTest.randomInt(numOfNodes - 1)); } - unicastHostOrdinals = Ints.toArray(ordinals); + unicastHostOrdinals = CollectionUtils.toArray(ordinals); } this.unicastHostPorts = unicastHostPorts(numOfNodes); assert unicastHostOrdinals.length <= unicastHostPorts.length; diff --git a/dev-tools/src/main/resources/forbidden/all-signatures.txt b/dev-tools/src/main/resources/forbidden/all-signatures.txt index 613ed07112d..8b976b55baa 100644 --- a/dev-tools/src/main/resources/forbidden/all-signatures.txt +++ b/dev-tools/src/main/resources/forbidden/all-signatures.txt @@ -125,6 +125,7 @@ com.google.common.collect.ArrayListMultimap com.google.common.collect.HashMultimap com.google.common.collect.FluentIterable com.google.common.io.Files +com.google.common.primitives.Ints @defaultMessage Do not violate java's access system java.lang.reflect.AccessibleObject#setAccessible(boolean)