Skip to content

Commit

Permalink
Fix compiler warnings in :server - part 4 (elastic#76302)
Browse files Browse the repository at this point in the history
Closes elastic#40366.

Fix the last remaining javac issues when linting is enforced in `server/`.
  • Loading branch information
pugnascotia committed Aug 11, 2021
1 parent 0178de9 commit 921aaf7
Show file tree
Hide file tree
Showing 80 changed files with 292 additions and 215 deletions.
8 changes: 2 additions & 6 deletions server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,6 @@ dependencies {
}
}

tasks.withType(JavaCompile).configureEach {
options.compilerArgs << "-Xlint:-cast,-rawtypes,-unchecked"
}

// Until this project is always being formatted with spotless, we need to
// guard against `spotless()` not existing.
try {
Expand Down Expand Up @@ -383,7 +379,7 @@ tasks.named('splitPackagesAudit').configure {
'org.elasticsearch.cli.LoggingAwareMultiCommand',

// these should be temporary, query needs package private access to TermScorer though
'org.apache.lucene.search.XCombinedFieldQuery',
'org.apache.lucene.search.XMultiNormsLeafSimScorer'
'org.apache.lucene.search.XCombinedFieldQuery',
'org.apache.lucene.search.XMultiNormsLeafSimScorer'

}
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ public void testDelayedMappingPropagationOnPrimary() throws Exception {
throw new AssertionError(e);
}
assertNotNull(properties);
@SuppressWarnings("unchecked")
Object fieldMapping = ((Map<String, Object>) properties).get("field");
assertNotNull(fieldMapping);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,15 @@ public void testGlobalTemplatesDoNotApply() {
assertThat(mappingsResponse.mappings().size(), is(1));
MappingMetadata mappingMetadata = mappingsResponse.mappings().get("a_hidden_index").get("_doc");
assertNotNull(mappingMetadata);
@SuppressWarnings("unchecked")
Map<String, Object> propertiesMap = (Map<String, Object>) mappingMetadata.getSourceAsMap().get("properties");
assertNotNull(propertiesMap);
assertThat(propertiesMap.size(), is(2));
@SuppressWarnings("unchecked")
Map<String, Object> barMap = (Map<String, Object>) propertiesMap.get("bar");
assertNotNull(barMap);
assertThat(barMap.get("type"), is("text"));
@SuppressWarnings("unchecked")
Map<String, Object> bazMap = (Map<String, Object>) propertiesMap.get("baz");
assertNotNull(bazMap);
assertThat(bazMap.get("type"), is("text"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ private static void assertMappingsHaveField(GetMappingsResponse mappings, String
MappingMetadata typeMappings = indexMappings.get(type);
assertNotNull(typeMappings);
Map<String, Object> typeMappingsMap = typeMappings.getSourceAsMap();
@SuppressWarnings("unchecked")
Map<String, Object> properties = (Map<String, Object>) typeMappingsMap.get("properties");
assertTrue("Could not find [" + field + "] in " + typeMappingsMap.toString(), properties.containsKey(field));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,8 @@ public void testResolvePath() throws Exception {
nodeNameToNodeId.put(cursor.value.getName(), cursor.key);
}

final GroupShardsIterator shardIterators = state.getRoutingTable().activePrimaryShardsGrouped(new String[]{indexName}, false);
final GroupShardsIterator<ShardIterator> shardIterators = state.getRoutingTable()
.activePrimaryShardsGrouped(new String[] { indexName }, false);
final List<ShardIterator> iterators = iterableAsArrayList(shardIterators);
final ShardRouting shardRouting = iterators.iterator().next().nextOrNull();
assertThat(shardRouting, notNullValue());
Expand Down Expand Up @@ -576,7 +577,8 @@ public void testResolvePath() throws Exception {

private Path getPathToShardData(String indexName, String dirSuffix) {
ClusterState state = client().admin().cluster().prepareState().get().getState();
GroupShardsIterator shardIterators = state.getRoutingTable().activePrimaryShardsGrouped(new String[]{indexName}, false);
GroupShardsIterator<ShardIterator> shardIterators = state.getRoutingTable()
.activePrimaryShardsGrouped(new String[] { indexName }, false);
List<ShardIterator> iterators = iterableAsArrayList(shardIterators);
ShardIterator shardIterator = RandomPicks.randomFrom(random(), iterators);
ShardRouting shardRouting = shardIterator.nextOrNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ public void testReplicaCorruption() throws Exception {

private int numShards(String... index) {
ClusterState state = client().admin().cluster().prepareState().get().getState();
GroupShardsIterator shardIterators = state.getRoutingTable().activePrimaryShardsGrouped(index, false);
GroupShardsIterator<?> shardIterators = state.getRoutingTable().activePrimaryShardsGrouped(index, false);
return shardIterators.size();
}

Expand Down Expand Up @@ -630,7 +630,7 @@ private ShardRouting corruptRandomPrimaryFile() throws IOException {
private ShardRouting corruptRandomPrimaryFile(final boolean includePerCommitFiles) throws IOException {
ClusterState state = client().admin().cluster().prepareState().get().getState();
Index test = state.metadata().index("test").getIndex();
GroupShardsIterator shardIterators = state.getRoutingTable().activePrimaryShardsGrouped(new String[]{"test"}, false);
GroupShardsIterator<ShardIterator> shardIterators = state.getRoutingTable().activePrimaryShardsGrouped(new String[]{"test"}, false);
List<ShardIterator> iterators = iterableAsArrayList(shardIterators);
ShardIterator shardIterator = RandomPicks.randomFrom(random(), iterators);
ShardRouting shardRouting = shardIterator.nextOrNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private Set<String> nodeIdsWithIndex(String... indices) {

protected int numAssignedShards(String... indices) {
ClusterState state = client().admin().cluster().prepareState().execute().actionGet().getState();
GroupShardsIterator allAssignedShardsGrouped = state.routingTable().allAssignedShardsGrouped(indices, true);
GroupShardsIterator<?> allAssignedShardsGrouped = state.routingTable().allAssignedShardsGrouped(indices, true);
return allAssignedShardsGrouped.size();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ private void assertMappingOnMaster(final String index, final String type, final
assertTrue(mappingSource.containsKey("properties"));

for (String fieldName : fieldNames) {
@SuppressWarnings("unchecked")
Map<String, Object> mappingProperties = (Map<String, Object>) mappingSource.get("properties");
if (fieldName.indexOf('.') != -1) {
fieldName = fieldName.replace(".", ".properties.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ public void testResolveSearchRouting() {
"test3", newSet("0", "1", "2", "tw ", " ltw ", " lw"))));
}

@SafeVarargs
@SuppressWarnings("varargs")
private <T> Set<T> newSet(T... elements) {
return newHashSet(elements);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public void testOpenContextsAfterRejections() throws Exception {
refresh();

int numSearches = 10;
@SuppressWarnings({"rawtypes", "unchecked"})
Future<SearchResponse>[] responses = new Future[numSearches];
SearchType searchType = randomFrom(SearchType.DEFAULT, SearchType.QUERY_THEN_FETCH, SearchType.DFS_QUERY_THEN_FETCH);
logger.info("search type is {}", searchType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ private void assertMetadata(Map<String, Object> returnedMetadata) {
Object nestedObject = returnedMetadata.get("complex");
assertNotNull(nestedObject);

@SuppressWarnings("unchecked")
Map<String, Object> nestedMap = (Map<String, Object>)nestedObject;
assertEquals("value", nestedMap.get("nested"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
aggScript(vars, state -> {
// Lazily populate state.list for tests without an init script
if (state.containsKey("list") == false) {
state.put("list", new ArrayList());
state.put("list", new ArrayList<>());
}

((List) state.get("list")).add(1);
((List<Object>) state.get("list")).add(1);
}));

scripts.put("state[param1] = param2", vars ->
Expand All @@ -100,7 +100,7 @@ protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
((Map<String, Object>) vars.get("vars")).put("multiplier", 3);

Map<String, Object> state = (Map<String, Object>) vars.get("state");
state.put("list", new ArrayList());
state.put("list", new ArrayList<>());

return state;
});
Expand All @@ -109,10 +109,10 @@ protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
aggScript(vars, state -> {
// Lazily populate state.list for tests without an init script
if (state.containsKey("list") == false) {
state.put("list", new ArrayList());
state.put("list", new ArrayList<>());
}

((List) state.get("list")).add(XContentMapValues.extractValue("vars.multiplier", vars));
((List<Object>) state.get("list")).add(XContentMapValues.extractValue("vars.multiplier", vars));
}));

// Equivalent to:
Expand All @@ -128,12 +128,12 @@ protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
// return newaggregation"
//
scripts.put("sum state values as a new aggregation", vars -> {
List newAggregation = new ArrayList();
List<Integer> newAggregation = new ArrayList<>();
Map<String, Object> state = (Map<String, Object>) vars.get("state");
List<?> list = (List<?>) state.get("list");

if (list != null) {
Integer sum = 0;
int sum = 0;
for (Object s : list) {
sum += ((Number) s).intValue();
}
Expand All @@ -142,13 +142,9 @@ protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
return newAggregation;
});

scripts.put("no-op aggregation", vars -> {
return (Map<String, Object>) vars.get("state");
});
scripts.put("no-op aggregation", vars -> vars.get("state"));

scripts.put("no-op list aggregation", vars -> {
return (List<List<?>>) vars.get("states");
});
scripts.put("no-op list aggregation", vars -> vars.get("states"));

// Equivalent to:
//
Expand All @@ -165,8 +161,8 @@ protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
// return newaggregation"
//
scripts.put("sum all states (lists) values as a new aggregation", vars -> {
List newAggregation = new ArrayList();
Integer sum = 0;
List<Integer> newAggregation = new ArrayList<>();
int sum = 0;

List<List<?>> states = (List<List<?>>) vars.get("states");
for (List<?> list : states) {
Expand All @@ -181,8 +177,8 @@ protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
});

scripts.put("sum all states' state.list values as a new aggregation", vars -> {
List newAggregation = new ArrayList();
Integer sum = 0;
List<Integer> newAggregation = new ArrayList<>();
int sum = 0;

List<Map<String, Object>> states = (List<Map<String, Object>>) vars.get("states");

Expand Down Expand Up @@ -218,8 +214,8 @@ protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
//
scripts.put("multiplied sum all states (lists) values as a new aggregation", vars -> {
Integer multiplier = (Integer) vars.get("multiplier");
List newAggregation = new ArrayList();
Integer sum = 0;
List<Integer> newAggregation = new ArrayList<>();
int sum = 0;

List<List<?>> states = (List<List<?>>) vars.get("states");
for (List<?> list : states) {
Expand Down Expand Up @@ -253,8 +249,8 @@ protected Map<String, Function<Map<String, Object>, Object>> nonDeterministicPlu
return scripts;
}

@SuppressWarnings("unchecked")
static Map<String, Object> aggScript(Map<String, Object> vars, Consumer<Map<String, Object>> fn) {
@SuppressWarnings("unchecked")
Map<String, Object> aggState = (Map<String, Object>) vars.get("state");
fn.accept(aggState);
return aggState;
Expand Down Expand Up @@ -377,7 +373,7 @@ public void testMap() {
if (map.size() == 1) {
assertThat(map.get("count"), notNullValue());
assertThat(map.get("count"), instanceOf(Number.class));
assertThat(map.get("count"), equalTo((Number) 1));
assertThat(map.get("count"), equalTo(1));
numShardsRun++;
}
}
Expand Down Expand Up @@ -431,7 +427,7 @@ public void testMapWithParams() {
String stringValue = (String) entry.getKey();
assertThat(stringValue, equalTo("12"));
Number numberValue = (Number) entry.getValue();
assertThat(numberValue, equalTo((Number) 1));
assertThat(numberValue, equalTo(1));
numShardsRun++;
}
}
Expand Down Expand Up @@ -476,6 +472,7 @@ public void testInitMutatesParams() {
for (Object object : aggregationList) {
assertThat(object, notNullValue());
assertThat(object, instanceOf(HashMap.class));
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) object;
assertThat(map, hasKey("list"));
assertThat(map.get("list"), instanceOf(List.class));
Expand All @@ -484,7 +481,7 @@ public void testInitMutatesParams() {
assertThat(o, notNullValue());
assertThat(o, instanceOf(Number.class));
Number numberValue = (Number) o;
assertThat(numberValue, equalTo((Number) 3));
assertThat(numberValue, equalTo(3));
totalCount += numberValue.longValue();
}
}
Expand Down Expand Up @@ -698,8 +695,8 @@ public void testInitMapCombineReduceGetProperty() throws Exception {
assertThat(object, instanceOf(Number.class));
assertThat(((Number) object).longValue(), equalTo(numDocs * 3));
assertThat(((InternalAggregation)global).getProperty("scripted"), sameInstance(scriptedMetricAggregation));
assertThat((List) ((InternalAggregation)global).getProperty("scripted.value"), sameInstance((List) aggregationList));
assertThat((List) ((InternalAggregation)scriptedMetricAggregation).getProperty("value"), sameInstance((List) aggregationList));
assertThat((List) ((InternalAggregation)global).getProperty("scripted.value"), sameInstance(aggregationList));
assertThat((List) ((InternalAggregation)scriptedMetricAggregation).getProperty("value"), sameInstance(aggregationList));
}

public void testMapCombineReduceWithParams() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public String toString(){
}
}

private void assertValidIterators(Iterator expectedBucketIter, Iterator expectedCountsIter, Iterator expectedValuesIter) {
private void assertValidIterators(Iterator<?> expectedBucketIter, Iterator<?> expectedCountsIter, Iterator<?> expectedValuesIter) {
if (expectedBucketIter.hasNext() == false) {
fail("`expectedBucketIter` iterator ended before `actual` iterator, size mismatch");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,27 +85,28 @@ protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();

scripts.put("doc['num1'].value", vars -> {
Map<?, ?> doc = (Map) vars.get("doc");
Map<?, ?> doc = (Map<?, ?>) vars.get("doc");
ScriptDocValues.Doubles num1 = (ScriptDocValues.Doubles) doc.get("num1");
return num1.getValue();
});

scripts.put("doc['num1'].value * factor", vars -> {
Map<?, ?> doc = (Map) vars.get("doc");
Map<?, ?> doc = (Map<?, ?>) vars.get("doc");
ScriptDocValues.Doubles num1 = (ScriptDocValues.Doubles) doc.get("num1");
@SuppressWarnings("unchecked")
Map<String, Object> params = (Map<String, Object>) vars.get("params");
Double factor = (Double) params.get("factor");
return num1.getValue() * factor;
});

scripts.put("doc['date'].date.millis", vars -> {
Map<?, ?> doc = (Map) vars.get("doc");
Map<?, ?> doc = (Map<?, ?>) vars.get("doc");
ScriptDocValues.Dates dates = (ScriptDocValues.Dates) doc.get("date");
return dates.getValue().toInstant().toEpochMilli();
});

scripts.put("doc['date'].date.nanos", vars -> {
Map<?, ?> doc = (Map) vars.get("doc");
Map<?, ?> doc = (Map<?, ?>) vars.get("doc");
ScriptDocValues.Dates dates = (ScriptDocValues.Dates) doc.get("date");
return DateUtils.toLong(dates.getValue().toInstant());
});
Expand Down Expand Up @@ -135,19 +136,19 @@ protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
}

static Object fieldsScript(Map<String, Object> vars, String fieldName) {
Map<?, ?> fields = (Map) vars.get("_fields");
Map<?, ?> fields = (Map<?, ?>) vars.get("_fields");
FieldLookup fieldLookup = (FieldLookup) fields.get(fieldName);
return fieldLookup.getValue();
}

@SuppressWarnings("unchecked")
static Object sourceScript(Map<String, Object> vars, String path) {
Map<String, Object> source = (Map) vars.get("_source");
@SuppressWarnings("unchecked")
Map<String, Object> source = (Map<String, Object>) vars.get("_source");
return XContentMapValues.extractValue(path, source);
}

static Object docScript(Map<String, Object> vars, String fieldName) {
Map<?, ?> doc = (Map) vars.get("doc");
Map<?, ?> doc = (Map<?, ?>) vars.get("doc");
ScriptDocValues<?> values = (ScriptDocValues<?>) doc.get(fieldName);
return values;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void testExists() throws Exception {
Map<String, Object> barObject = new HashMap<>();
barObject.put("foo", "bar");
barObject.put("bar", singletonMap("bar", "foo"));
@SuppressWarnings("unchecked")
@SuppressWarnings({"rawtypes", "unchecked"})
final Map<String, Object>[] sources = new Map[] {
// simple property
singletonMap("foo", "bar"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private TestContext create(SearchType... searchTypes) throws Exception {
}
refresh();

final SortBuilder sort;
final SortBuilder<?> sort;
if (randomBoolean()) {
if (randomBoolean()) {
sort = SortBuilders.fieldSort("field1").missing(1);
Expand Down Expand Up @@ -170,10 +170,10 @@ class TestContext {

final int numDocs;
final int scrollRequestSize;
final SortBuilder sort;
final SortBuilder<?> sort;
final SearchType searchType;

TestContext(int numDocs, int scrollRequestSize, SortBuilder sort, SearchType searchType) {
TestContext(int numDocs, int scrollRequestSize, SortBuilder<?> sort, SearchType searchType) {
this.numDocs = numDocs;
this.scrollRequestSize = scrollRequestSize;
this.sort = sort;
Expand Down
Loading

0 comments on commit 921aaf7

Please sign in to comment.