From f83f70ed3921b5ed347f063b593eae36f6d950f3 Mon Sep 17 00:00:00 2001 From: Kristina Chodorow Date: Wed, 9 Nov 2016 20:20:13 +0000 Subject: Fix NPE when maven_jar sha1 isn't set and caching is enabled -- MOS_MIGRATED_REVID=138669967 --- .../bazel/repository/cache/RepositoryCache.java | 23 +++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'src/main/java/com/google/devtools') diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/cache/RepositoryCache.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/cache/RepositoryCache.java index 595e0cb991..64cdfdf14a 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/repository/cache/RepositoryCache.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/cache/RepositoryCache.java @@ -30,32 +30,33 @@ import javax.annotation.Nullable; public class RepositoryCache { /** The types of cache keys used. */ - public static enum KeyType { + public enum KeyType { SHA1("SHA-1", "\\p{XDigit}{40}", "sha1", Hashing.sha1()), SHA256("SHA-256", "\\p{XDigit}{64}", "sha256", Hashing.sha256()); private final String stringRepr; private final String regexp; - private final String cacheDirName; + private final String hashName; + @SuppressWarnings("ImmutableEnumChecker") private final HashFunction hashFunction; - KeyType(String stringRepr, String regexp, String cacheDirName, HashFunction hashFunction) { + KeyType(String stringRepr, String regexp, String hashName, HashFunction hashFunction) { this.stringRepr = stringRepr; this.regexp = regexp; - this.cacheDirName = cacheDirName; + this.hashName = hashName; this.hashFunction = hashFunction; } - public boolean isValid(String checksum) { - return checksum.matches(regexp); + public boolean isValid(@Nullable String checksum) { + return checksum != null && checksum.matches(regexp); } - public String getDirectoryName() { - return cacheDirName; + public Path getCachePath(Path parentDirectory) { + return parentDirectory.getChild(hashName); } - public Path getCachePath(Path parentDirectory) { - return parentDirectory.getChild(cacheDirName); + public Hasher newHasher() { + return hashFunction.newHasher(); } @Override @@ -205,7 +206,7 @@ public class RepositoryCache { * @throws IOException */ public static String getChecksum(KeyType keyType, Path path) throws IOException { - Hasher hasher = keyType.hashFunction.newHasher(); + Hasher hasher = keyType.newHasher(); byte[] byteBuffer = new byte[BUFFER_SIZE]; try (InputStream stream = path.getInputStream()) { int numBytesRead = stream.read(byteBuffer); -- cgit v1.2.3