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
This commit is contained in:
Dave Borowitz 2018-12-21 11:27:39 -08:00
parent 4e22200be9
commit 737b035223
21 changed files with 27 additions and 35 deletions

View File

@ -725,7 +725,7 @@ public abstract class AbstractDaemonTest {
}
private static final List<Character> 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);

View File

@ -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 {

View File

@ -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...]"

View File

@ -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<String, String[]> parameterMap = (Map<String, String[]>) request.getParameterMap();
Map<String, String[]> parameterMap = request.getParameterMap();
ImmutableListMultimap.Builder<String, String> b = ImmutableListMultimap.builder();
parameterMap.forEach(b::putAll);
return b.build();

View File

@ -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);

View File

@ -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<String, URL> jars, String prefix, List<URL> extapi) {

View File

@ -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();
}
}

View File

@ -120,7 +120,7 @@ abstract class BucketedCallback<V> implements BucketedMetric {
@Override
public Map<Object, Metric> getCells() {
return Maps.transformValues(cells, in -> (Metric) in);
return Maps.transformValues(cells, in -> in);
}
final class ValueGauge implements Gauge<V> {

View File

@ -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();

View File

@ -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);
}

View File

@ -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");
}

View File

@ -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));

View File

@ -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() {

View File

@ -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) {

View File

@ -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);
}

View File

@ -407,7 +407,7 @@ public class WorkQueue {
new Supplier<Long>() {
@Override
public Long get() {
return (long) getTaskCount();
return getTaskCount();
}
});
metrics.newCallbackMetric(
@ -419,7 +419,7 @@ public class WorkQueue {
new Supplier<Long>() {
@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();
}
}
}

View File

@ -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",

View File

@ -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"));

View File

@ -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) {

View File

@ -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<Edit> edits = (List<Edit>) entry.getEdits();
List<Edit> edits = entry.getEdits();
if (edits.isEmpty()) {
continue;
}

View File

@ -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);