From 737b035223b324096f1d4872c223e65a421d107c Mon Sep 17 00:00:00 2001 From: Dave Borowitz Date: Fri, 21 Dec 2018 11:27:39 -0800 Subject: [PATCH] Clean up redundant code constructs Results of the following IntelliJ inspections: * Redundant 'isInstance' or 'cast' call * Redundant array creation * Redundant Collection operation * Redundant String operation * Redundant type cast The only manual change was to remove a suppression and comment about a redundant cast which doesn't appear to be an issue in the current version of JDK 8 Change-Id: I34a70d436512cc4efe9dafb0917be3dfce4ba06b --- java/com/google/gerrit/acceptance/AbstractDaemonTest.java | 2 +- java/com/google/gerrit/asciidoctor/AsciiDoctor.java | 2 +- java/com/google/gerrit/common/data/ParameterizedString.java | 2 +- java/com/google/gerrit/httpd/GitOverHttpServlet.java | 2 +- java/com/google/gerrit/httpd/restapi/RestApiServlet.java | 4 +--- java/com/google/gerrit/launcher/GerritLauncher.java | 2 +- java/com/google/gerrit/mail/MailHeaderParser.java | 2 +- .../google/gerrit/metrics/dropwizard/BucketedCallback.java | 2 +- java/com/google/gerrit/metrics/dropwizard/MetricJson.java | 2 +- .../gerrit/metrics/proc/OperatingSystemMXBeanProvider.java | 5 ++--- java/com/google/gerrit/pgm/init/InitLabels.java | 2 +- java/com/google/gerrit/reviewdb/client/PatchSet.java | 2 +- java/com/google/gerrit/reviewdb/client/RefNames.java | 2 +- java/com/google/gerrit/server/IdentifiedUser.java | 4 ++-- java/com/google/gerrit/server/git/MergeUtil.java | 5 ++--- java/com/google/gerrit/server/git/WorkQueue.java | 6 +++--- java/com/google/gerrit/server/index/change/ChangeField.java | 4 ++-- java/com/google/gerrit/server/mail/MailUtil.java | 2 +- java/com/google/gerrit/server/notedb/NoteDbUtil.java | 2 +- java/gerrit/PRED_commit_edits_2.java | 6 +----- .../com/google/gerrit/acceptance/git/GitmodulesIT.java | 2 +- 21 files changed, 27 insertions(+), 35 deletions(-) diff --git a/java/com/google/gerrit/acceptance/AbstractDaemonTest.java b/java/com/google/gerrit/acceptance/AbstractDaemonTest.java index d736446baa..d77e1a197e 100644 --- a/java/com/google/gerrit/acceptance/AbstractDaemonTest.java +++ b/java/com/google/gerrit/acceptance/AbstractDaemonTest.java @@ -725,7 +725,7 @@ public abstract class AbstractDaemonTest { } private static final List RANDOM = - Chars.asList(new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}); + Chars.asList('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'); protected PushOneCommit.Result amendChange(String changeId) throws Exception { return amendChange(changeId, "refs/for/master", admin, testRepo); diff --git a/java/com/google/gerrit/asciidoctor/AsciiDoctor.java b/java/com/google/gerrit/asciidoctor/AsciiDoctor.java index 577907092d..73f0d5d62b 100644 --- a/java/com/google/gerrit/asciidoctor/AsciiDoctor.java +++ b/java/com/google/gerrit/asciidoctor/AsciiDoctor.java @@ -127,7 +127,7 @@ public class AsciiDoctor { int equalsIndex = attribute.indexOf('='); if (equalsIndex > -1) { String name = attribute.substring(0, equalsIndex); - String value = attribute.substring(equalsIndex + 1, attribute.length()); + String value = attribute.substring(equalsIndex + 1); attributeValues.put(name, value); } else { diff --git a/java/com/google/gerrit/common/data/ParameterizedString.java b/java/com/google/gerrit/common/data/ParameterizedString.java index af62c12408..84bb535829 100644 --- a/java/com/google/gerrit/common/data/ParameterizedString.java +++ b/java/com/google/gerrit/common/data/ParameterizedString.java @@ -60,7 +60,7 @@ public class ParameterizedString { break; } - raw.append(pattern.substring(i, b)); + raw.append(pattern, i, b); ops.add(new Constant(pattern.substring(i, b))); // "${parameter[.functions...]}" -> "parameter[.functions...]" diff --git a/java/com/google/gerrit/httpd/GitOverHttpServlet.java b/java/com/google/gerrit/httpd/GitOverHttpServlet.java index 7fbb8d7393..ac5386c458 100644 --- a/java/com/google/gerrit/httpd/GitOverHttpServlet.java +++ b/java/com/google/gerrit/httpd/GitOverHttpServlet.java @@ -212,7 +212,7 @@ public class GitOverHttpServlet extends GitServlet { } // Explicit cast is required to compile under Servlet API 2.5, where the return type is raw Map. @SuppressWarnings("cast") - Map parameterMap = (Map) request.getParameterMap(); + Map parameterMap = request.getParameterMap(); ImmutableListMultimap.Builder b = ImmutableListMultimap.builder(); parameterMap.forEach(b::putAll); return b.build(); diff --git a/java/com/google/gerrit/httpd/restapi/RestApiServlet.java b/java/com/google/gerrit/httpd/restapi/RestApiServlet.java index 26fa07c654..4057d54906 100644 --- a/java/com/google/gerrit/httpd/restapi/RestApiServlet.java +++ b/java/com/google/gerrit/httpd/restapi/RestApiServlet.java @@ -1361,9 +1361,7 @@ public class RestApiServlet extends HttpServlet { // generated. TraceContext traceContext = TraceContext.newTrace( - doTrace, - traceId1, - (tagName, traceId) -> res.setHeader(X_GERRIT_TRACE, traceId.toString())); + doTrace, traceId1, (tagName, traceId) -> res.setHeader(X_GERRIT_TRACE, traceId)); // If a second trace ID was specified, add a tag for it as well. if (traceId2 != null) { traceContext.addTag(RequestId.Type.TRACE_ID, traceId2); diff --git a/java/com/google/gerrit/launcher/GerritLauncher.java b/java/com/google/gerrit/launcher/GerritLauncher.java index 868c1d5ecc..30d4e15495 100644 --- a/java/com/google/gerrit/launcher/GerritLauncher.java +++ b/java/com/google/gerrit/launcher/GerritLauncher.java @@ -324,7 +324,7 @@ public final class GerritLauncher { } String name = ze.getName(); - jars.put(name.substring(name.lastIndexOf('/'), name.length()), tmp.toURI().toURL()); + jars.put(name.substring(name.lastIndexOf('/')), tmp.toURI().toURL()); } private static void move(SortedMap jars, String prefix, List extapi) { diff --git a/java/com/google/gerrit/mail/MailHeaderParser.java b/java/com/google/gerrit/mail/MailHeaderParser.java index 8eb4d97047..a4a6a037db 100644 --- a/java/com/google/gerrit/mail/MailHeaderParser.java +++ b/java/com/google/gerrit/mail/MailHeaderParser.java @@ -103,6 +103,6 @@ public class MailHeaderParser { } private static String extractFooter(String key, String line) { - return line.substring(line.indexOf(key) + key.length(), line.length()).trim(); + return line.substring(line.indexOf(key) + key.length()).trim(); } } diff --git a/java/com/google/gerrit/metrics/dropwizard/BucketedCallback.java b/java/com/google/gerrit/metrics/dropwizard/BucketedCallback.java index ee87397792..e33c242999 100644 --- a/java/com/google/gerrit/metrics/dropwizard/BucketedCallback.java +++ b/java/com/google/gerrit/metrics/dropwizard/BucketedCallback.java @@ -120,7 +120,7 @@ abstract class BucketedCallback implements BucketedMetric { @Override public Map getCells() { - return Maps.transformValues(cells, in -> (Metric) in); + return Maps.transformValues(cells, in -> in); } final class ValueGauge implements Gauge { diff --git a/java/com/google/gerrit/metrics/dropwizard/MetricJson.java b/java/com/google/gerrit/metrics/dropwizard/MetricJson.java index 20806239e7..20f4fa33a3 100644 --- a/java/com/google/gerrit/metrics/dropwizard/MetricJson.java +++ b/java/com/google/gerrit/metrics/dropwizard/MetricJson.java @@ -137,7 +137,7 @@ class MetricJson { p99_9 = s.get999thPercentile(); min = (double) s.getMin(); - avg = (double) s.getMean(); + avg = s.getMean(); max = (double) s.getMax(); sum = s.getMean() * m.getCount(); std_dev = s.getStdDev(); diff --git a/java/com/google/gerrit/metrics/proc/OperatingSystemMXBeanProvider.java b/java/com/google/gerrit/metrics/proc/OperatingSystemMXBeanProvider.java index 10d589a84e..35c147e92f 100644 --- a/java/com/google/gerrit/metrics/proc/OperatingSystemMXBeanProvider.java +++ b/java/com/google/gerrit/metrics/proc/OperatingSystemMXBeanProvider.java @@ -51,10 +51,9 @@ class OperatingSystemMXBeanProvider { private OperatingSystemMXBeanProvider(OperatingSystemMXBean sys) throws ReflectiveOperationException { this.sys = sys; - getProcessCpuTime = sys.getClass().getMethod("getProcessCpuTime", new Class[] {}); + getProcessCpuTime = sys.getClass().getMethod("getProcessCpuTime"); getProcessCpuTime.setAccessible(true); - getOpenFileDescriptorCount = - sys.getClass().getMethod("getOpenFileDescriptorCount", new Class[] {}); + getOpenFileDescriptorCount = sys.getClass().getMethod("getOpenFileDescriptorCount"); getOpenFileDescriptorCount.setAccessible(true); } diff --git a/java/com/google/gerrit/pgm/init/InitLabels.java b/java/com/google/gerrit/pgm/init/InitLabels.java index 3d1ec7b9c2..0797cf9e0a 100644 --- a/java/com/google/gerrit/pgm/init/InitLabels.java +++ b/java/com/google/gerrit/pgm/init/InitLabels.java @@ -61,7 +61,7 @@ public class InitLabels implements InitStep { KEY_LABEL, LABEL_VERIFIED, KEY_VALUE, - Arrays.asList(new String[] {"-1 Fails", "0 No score", "+1 Verified"})); + Arrays.asList("-1 Fails", "0 No score", "+1 Verified")); cfg.setBoolean(KEY_LABEL, LABEL_VERIFIED, KEY_COPY_ALL_SCORES_IF_NO_CODE_CHANGE, true); allProjectsConfig.save("Configure 'Verified' label"); } diff --git a/java/com/google/gerrit/reviewdb/client/PatchSet.java b/java/com/google/gerrit/reviewdb/client/PatchSet.java index 68653a26e2..a2c7010002 100644 --- a/java/com/google/gerrit/reviewdb/client/PatchSet.java +++ b/java/com/google/gerrit/reviewdb/client/PatchSet.java @@ -65,7 +65,7 @@ public final class PatchSet { while (true) { int idx = joinedGroups.indexOf(',', i); if (idx < 0) { - groups.add(joinedGroups.substring(i, joinedGroups.length())); + groups.add(joinedGroups.substring(i)); break; } groups.add(joinedGroups.substring(i, idx)); diff --git a/java/com/google/gerrit/reviewdb/client/RefNames.java b/java/com/google/gerrit/reviewdb/client/RefNames.java index 5e45088cbf..9e1fcca1fe 100644 --- a/java/com/google/gerrit/reviewdb/client/RefNames.java +++ b/java/com/google/gerrit/reviewdb/client/RefNames.java @@ -432,7 +432,7 @@ public class RefNames { if (i == 0) { return null; } - return Integer.valueOf(name.substring(i, name.length())); + return Integer.valueOf(name.substring(i)); } private static StringBuilder newStringBuilder() { diff --git a/java/com/google/gerrit/server/IdentifiedUser.java b/java/com/google/gerrit/server/IdentifiedUser.java index b1761f4c03..005a2b335c 100644 --- a/java/com/google/gerrit/server/IdentifiedUser.java +++ b/java/com/google/gerrit/server/IdentifiedUser.java @@ -96,13 +96,13 @@ public class IdentifiedUser extends CurrentUser { accountCache, groupBackend, disableReverseDnsLookup, - Providers.of((SocketAddress) null), + Providers.of(null), state, null); } public IdentifiedUser create(Account.Id id) { - return create((SocketAddress) null, id); + return create(null, id); } public IdentifiedUser create(SocketAddress remotePeer, Account.Id id) { diff --git a/java/com/google/gerrit/server/git/MergeUtil.java b/java/com/google/gerrit/server/git/MergeUtil.java index cc60b1028d..ee4d71ea2d 100644 --- a/java/com/google/gerrit/server/git/MergeUtil.java +++ b/java/com/google/gerrit/server/git/MergeUtil.java @@ -83,7 +83,6 @@ import org.eclipse.jgit.errors.MissingObjectException; import org.eclipse.jgit.errors.NoMergeBaseException; import org.eclipse.jgit.errors.NoMergeBaseException.MergeBaseFailureReason; import org.eclipse.jgit.errors.RevisionSyntaxException; -import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.CommitBuilder; import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.Constants; @@ -624,7 +623,7 @@ public class MergeUtil { } try (ObjectInserter ins = new InMemoryInserter(repo)) { - return newThreeWayMerger(ins, repo.getConfig()).merge(new AnyObjectId[] {mergeTip, toMerge}); + return newThreeWayMerger(ins, repo.getConfig()).merge(mergeTip, toMerge); } catch (LargeObjectException e) { logger.atWarning().log("Cannot merge due to LargeObjectException: %s", toMerge.name()); return false; @@ -722,7 +721,7 @@ public class MergeUtil { throws IntegrationException { ThreeWayMerger m = newThreeWayMerger(inserter, repoConfig); try { - if (m.merge(new AnyObjectId[] {mergeTip, n})) { + if (m.merge(mergeTip, n)) { return writeMergeCommit( author, committer, rw, inserter, destBranch, mergeTip, m.getResultTreeId(), n); } diff --git a/java/com/google/gerrit/server/git/WorkQueue.java b/java/com/google/gerrit/server/git/WorkQueue.java index a7336f0fec..e2b769732c 100644 --- a/java/com/google/gerrit/server/git/WorkQueue.java +++ b/java/com/google/gerrit/server/git/WorkQueue.java @@ -407,7 +407,7 @@ public class WorkQueue { new Supplier() { @Override public Long get() { - return (long) getTaskCount(); + return getTaskCount(); } }); metrics.newCallbackMetric( @@ -419,7 +419,7 @@ public class WorkQueue { new Supplier() { @Override public Long get() { - return (long) getCompletedTaskCount(); + return getCompletedTaskCount(); } }); } @@ -674,7 +674,7 @@ public class WorkQueue { for (Field innerField : innerObj.getClass().getDeclaredFields()) { if (innerField.getType().isAssignableFrom(Callable.class)) { innerField.setAccessible(true); - return ((Callable) innerField.get(innerObj)).toString(); + return innerField.get(innerObj).toString(); } } } diff --git a/java/com/google/gerrit/server/index/change/ChangeField.java b/java/com/google/gerrit/server/index/change/ChangeField.java index 03da9a2d48..9e8f1111f8 100644 --- a/java/com/google/gerrit/server/index/change/ChangeField.java +++ b/java/com/google/gerrit/server/index/change/ChangeField.java @@ -297,7 +297,7 @@ public class ChangeField { continue; } - Long l = Longs.tryParse(v.substring(i2 + 1, v.length())); + Long l = Longs.tryParse(v.substring(i2 + 1)); if (l == null) { logger.atWarning().log( "Failed to parse timestamp of reviewer field from change %s: %s", changeId.get(), v); @@ -350,7 +350,7 @@ public class ChangeField { continue; } - Long l = Longs.tryParse(v.substring(i2 + 1, v.length())); + Long l = Longs.tryParse(v.substring(i2 + 1)); if (l == null) { logger.atWarning().log( "Failed to parse timestamp of reviewer by email field from change %s: %s", diff --git a/java/com/google/gerrit/server/mail/MailUtil.java b/java/com/google/gerrit/server/mail/MailUtil.java index 507b53f170..185e7f0b8b 100644 --- a/java/com/google/gerrit/server/mail/MailUtil.java +++ b/java/com/google/gerrit/server/mail/MailUtil.java @@ -124,7 +124,7 @@ public class MailUtil { return Pattern.compile(".*"); } - StringBuilder sb = new StringBuilder(""); + StringBuilder sb = new StringBuilder(); for (String domain : domains) { String quoted = "\\Q" + domain.replace("\\E", "\\E\\\\E\\Q") + "\\E|"; sb.append(quoted.replace("*", "\\E.*\\Q")); diff --git a/java/com/google/gerrit/server/notedb/NoteDbUtil.java b/java/com/google/gerrit/server/notedb/NoteDbUtil.java index 21fada8173..667ceaba4d 100644 --- a/java/com/google/gerrit/server/notedb/NoteDbUtil.java +++ b/java/com/google/gerrit/server/notedb/NoteDbUtil.java @@ -33,7 +33,7 @@ public class NoteDbUtil { String email = ident.getEmailAddress(); int at = email.indexOf('@'); if (at >= 0) { - String host = email.substring(at + 1, email.length()); + String host = email.substring(at + 1); if (host.equals(serverId)) { Integer id = Ints.tryParse(email.substring(0, at)); if (id != null) { diff --git a/java/gerrit/PRED_commit_edits_2.java b/java/gerrit/PRED_commit_edits_2.java index f46a6487f0..6ca533874d 100644 --- a/java/gerrit/PRED_commit_edits_2.java +++ b/java/gerrit/PRED_commit_edits_2.java @@ -97,11 +97,7 @@ public class PRED_commit_edits_2 extends Predicate.P2 { if (fileRegex.matcher(newName).find() || (oldName != null && fileRegex.matcher(oldName).find())) { - // This cast still seems to be needed on JDK 8 as workaround for: - // https://bugs.openjdk.java.net/browse/JDK-8039214 - @SuppressWarnings("cast") - List edits = (List) entry.getEdits(); - + List edits = entry.getEdits(); if (edits.isEmpty()) { continue; } diff --git a/javatests/com/google/gerrit/acceptance/git/GitmodulesIT.java b/javatests/com/google/gerrit/acceptance/git/GitmodulesIT.java index a13c8c812d..ac0cbd83e3 100644 --- a/javatests/com/google/gerrit/acceptance/git/GitmodulesIT.java +++ b/javatests/com/google/gerrit/acceptance/git/GitmodulesIT.java @@ -47,7 +47,7 @@ public class GitmodulesIT extends AbstractDaemonTest { .commit() .insertChangeId() .message("subject: adding new subscription") - .add(".gitmodules", config.toText().toString()) + .add(".gitmodules", config.toText()) .create(); exception.expectMessage(expectedErrorMessage);