Fix 'Potential heap pollution via vargs parameter' warnings

Most existing sites are safe.  The implementating method only scans
the array and does not retain it beyond the method invocation.  In the
questionable cases use ImmutableList instead of a varargs array.

Change-Id: Ic2d0c1c1801a68ec58b642552e97e2d193b40a98
This commit is contained in:
Shawn Pearce 2013-11-28 22:49:31 -08:00
parent 42e2944d3a
commit d170274b24
9 changed files with 30 additions and 31 deletions

View File

@ -30,7 +30,7 @@ import java.util.Map;
/** Secondary index schemas for changes. */
public class ChangeSchemas {
@SuppressWarnings({"unchecked", "deprecation"})
@SuppressWarnings("deprecation")
static final Schema<ChangeData> V1 = release(
ChangeField.LEGACY_ID,
ChangeField.ID,
@ -50,7 +50,7 @@ public class ChangeSchemas {
ChangeField.COMMIT_MESSAGE,
ChangeField.COMMENT);
@SuppressWarnings({"unchecked", "deprecation"})
@SuppressWarnings("deprecation")
static final Schema<ChangeData> V2 = release(
ChangeField.LEGACY_ID,
ChangeField.ID,
@ -72,7 +72,6 @@ public class ChangeSchemas {
ChangeField.CHANGE,
ChangeField.APPROVAL);
@SuppressWarnings("unchecked")
static final Schema<ChangeData> V3 = release(
ChangeField.LEGACY_ID,
ChangeField.ID,
@ -97,7 +96,6 @@ public class ChangeSchemas {
// For upgrade to Lucene 4.4.0 index format only.
static final Schema<ChangeData> V4 = release(V3.getFields().values());
@SuppressWarnings("unchecked")
static final Schema<ChangeData> V5 = release(
ChangeField.LEGACY_ID,
ChangeField.ID,
@ -124,10 +122,12 @@ public class ChangeSchemas {
return new Schema<ChangeData>(true, fields);
}
@SafeVarargs
private static Schema<ChangeData> release(FieldDef<ChangeData, ?>... fields) {
return release(Arrays.asList(fields));
}
@SafeVarargs
@SuppressWarnings("unused")
private static Schema<ChangeData> developer(FieldDef<ChangeData, ?>... fields) {
return new Schema<ChangeData>(false, Arrays.asList(fields));

View File

@ -27,6 +27,7 @@ public class AndPredicate<T> extends Predicate<T> {
private final List<Predicate<T>> children;
private final int cost;
@SafeVarargs
protected AndPredicate(final Predicate<T>... that) {
this(Arrays.asList(that));
}

View File

@ -27,6 +27,7 @@ public class OrPredicate<T> extends Predicate<T> {
private final List<Predicate<T>> children;
private final int cost;
@SafeVarargs
protected OrPredicate(final Predicate<T>... that) {
this(Arrays.asList(that));
}

View File

@ -50,6 +50,7 @@ public abstract class Predicate<T> {
}
/** Combine the passed predicates into a single AND node. */
@SafeVarargs
public static <T> Predicate<T> and(final Predicate<T>... that) {
if (that.length == 1) {
return that[0];
@ -67,6 +68,7 @@ public abstract class Predicate<T> {
}
/** Combine the passed predicates into a single OR node. */
@SafeVarargs
public static <T> Predicate<T> or(final Predicate<T>... that) {
if (that.length == 1) {
return that[0];

View File

@ -97,7 +97,7 @@ public abstract class QueryRewriter<T> {
}
/** Combine the passed predicates into a single AND node. */
public Predicate<T> and(Predicate<T>... that) {
public Predicate<T> and(@SuppressWarnings("unchecked") Predicate<T>... that) {
return and(Arrays.asList(that));
}
@ -107,7 +107,7 @@ public abstract class QueryRewriter<T> {
}
/** Combine the passed predicates into a single OR node. */
public Predicate<T> or(Predicate<T>... that) {
public Predicate<T> or(@SuppressWarnings("unchecked") Predicate<T>... that) {
return or(Arrays.asList(that));
}

View File

@ -14,7 +14,8 @@
package com.google.gerrit.server.query;
import java.util.Arrays;
import com.google.common.collect.ImmutableList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
@ -24,10 +25,10 @@ public abstract class RewritePredicate<T> extends Predicate<T> {
private String name = getClass().getSimpleName();
private List<Predicate<T>> children = Collections.emptyList();
protected void init(String name, Predicate<T>... args) {
protected void init(String name, @SuppressWarnings("unchecked") Predicate<T>... args) {
this.init = true;
this.name = name;
this.children = Arrays.asList(args);
this.children = ImmutableList.copyOf(args);
}
@Override

View File

@ -14,6 +14,7 @@
package com.google.gerrit.server.query;
import static com.google.common.collect.ImmutableList.of;
import static com.google.gerrit.server.query.Predicate.and;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@ -23,7 +24,6 @@ import static org.junit.Assert.assertTrue;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@ -70,23 +70,23 @@ public class AndPredicateTest {
n.getChildren().clear();
} catch (RuntimeException e) {
}
assertChildren("clear", n, list(a, b));
assertChildren("clear", n, of(a, b));
try {
n.getChildren().remove(0);
} catch (RuntimeException e) {
}
assertChildren("remove(0)", n, list(a, b));
assertChildren("remove(0)", n, of(a, b));
try {
n.getChildren().iterator().remove();
} catch (RuntimeException e) {
}
assertChildren("remove(0)", n, list(a, b));
assertChildren("remove(0)", n, of(a, b));
}
private static void assertChildren(String o, Predicate<String> p,
final List<Predicate<String>> l) {
List<? extends Predicate<String>> l) {
assertEquals(o + " did not affect child", l, p.getChildren());
}
@ -134,8 +134,8 @@ public class AndPredicateTest {
final TestPredicate a = f("author", "alice");
final TestPredicate b = f("author", "bob");
final TestPredicate c = f("author", "charlie");
final List<Predicate<String>> s2 = list(a, b);
final List<Predicate<String>> s3 = list(a, b, c);
final List<TestPredicate> s2 = of(a, b);
final List<TestPredicate> s3 = of(a, b, c);
final Predicate<String> n2 = and(a, b);
assertNotSame(n2, n2.copy(s2));
@ -148,8 +148,4 @@ public class AndPredicateTest {
assertEquals("Need at least two predicates", e.getMessage());
}
}
private static List<Predicate<String>> list(final Predicate<String>... predicates) {
return Arrays.asList(predicates);
}
}

View File

@ -14,6 +14,7 @@
package com.google.gerrit.server.query;
import static com.google.common.collect.ImmutableList.of;
import static com.google.gerrit.server.query.Predicate.or;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@ -23,7 +24,6 @@ import static org.junit.Assert.assertTrue;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@ -70,23 +70,23 @@ public class OrPredicateTest {
n.getChildren().clear();
} catch (RuntimeException e) {
}
assertChildren("clear", n, list(a, b));
assertChildren("clear", n, of(a, b));
try {
n.getChildren().remove(0);
} catch (RuntimeException e) {
}
assertChildren("remove(0)", n, list(a, b));
assertChildren("remove(0)", n, of(a, b));
try {
n.getChildren().iterator().remove();
} catch (RuntimeException e) {
}
assertChildren("remove(0)", n, list(a, b));
assertChildren("remove(0)", n, of(a, b));
}
private static void assertChildren(String o, Predicate<String> p,
final List<Predicate<String>> l) {
List<? extends Predicate<String>> l) {
assertEquals(o + " did not affect child", l, p.getChildren());
}
@ -134,8 +134,8 @@ public class OrPredicateTest {
final TestPredicate a = f("author", "alice");
final TestPredicate b = f("author", "bob");
final TestPredicate c = f("author", "charlie");
final List<Predicate<String>> s2 = list(a, b);
final List<Predicate<String>> s3 = list(a, b, c);
final List<TestPredicate> s2 = of(a, b);
final List<TestPredicate> s3 = of(a, b, c);
final Predicate<String> n2 = or(a, b);
assertNotSame(n2, n2.copy(s2));
@ -148,8 +148,4 @@ public class OrPredicateTest {
assertEquals("Need at least two predicates", e.getMessage());
}
}
private static <T> List<Predicate<T>> list(final Predicate<T>... predicates) {
return Arrays.asList(predicates);
}
}

View File

@ -406,6 +406,7 @@ public class SshDaemon extends SshServer implements SshInfo, LifecycleListener {
new HMACSHA196.Factory()));
}
@SafeVarargs
private static <T> List<NamedFactory<T>> filter(final Config cfg,
final String key, final NamedFactory<T>... avail) {
final ArrayList<NamedFactory<T>> def = new ArrayList<NamedFactory<T>>();
@ -462,6 +463,7 @@ public class SshDaemon extends SshServer implements SshInfo, LifecycleListener {
return def;
}
@SafeVarargs
private static <T> NamedFactory<T> find(final String name,
final NamedFactory<T>... avail) {
for (final NamedFactory<T> n : avail) {