Skip to content

Commit

Permalink
Fix shadowed vars pt3 (#80744)
Browse files Browse the repository at this point in the history
Part of #19752. Fix more instances where local variable names were shadowing field names.
  • Loading branch information
pugnascotia authored Nov 16, 2021
1 parent d4d4211 commit 1d3b096
Show file tree
Hide file tree
Showing 29 changed files with 149 additions and 141 deletions.
1 change: 1 addition & 0 deletions build-tools-internal/src/main/resources/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
<property name="ignoreSetter" value="true" />
<property name="setterCanReturnItsClass" value="true"/>
<property name="ignoreFormat" value="^(threadPool)$"/>
<property name="ignoreAbstractMethods" value="true"/>
<message key="hidden.field" value="''{0}'' hides a field." />
</module>
-->
Expand Down
64 changes: 32 additions & 32 deletions libs/nio/src/test/java/org/elasticsearch/nio/EventHandlerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,18 @@ public void testRegisterCallsContext() throws IOException {
}

public void testActiveNonServerAddsOP_CONNECTAndOP_READInterest() throws IOException {
SocketChannelContext context = mock(SocketChannelContext.class);
when(context.getSelectionKey()).thenReturn(new TestSelectionKey(0));
handler.handleActive(context);
assertEquals(SelectionKey.OP_READ | SelectionKey.OP_CONNECT, context.getSelectionKey().interestOps());
SocketChannelContext mockContext = mock(SocketChannelContext.class);
when(mockContext.getSelectionKey()).thenReturn(new TestSelectionKey(0));
handler.handleActive(mockContext);
assertEquals(SelectionKey.OP_READ | SelectionKey.OP_CONNECT, mockContext.getSelectionKey().interestOps());
}

public void testHandleServerActiveSetsOP_ACCEPTInterest() throws IOException {
ServerChannelContext serverContext = mock(ServerChannelContext.class);
when(serverContext.getSelectionKey()).thenReturn(new TestSelectionKey(0));
handler.handleActive(serverContext);
ServerChannelContext mockServerContext = mock(ServerChannelContext.class);
when(mockServerContext.getSelectionKey()).thenReturn(new TestSelectionKey(0));
handler.handleActive(mockServerContext);

assertEquals(SelectionKey.OP_ACCEPT, serverContext.getSelectionKey().interestOps());
assertEquals(SelectionKey.OP_ACCEPT, mockServerContext.getSelectionKey().interestOps());
}

public void testHandleAcceptAccept() throws IOException {
Expand Down Expand Up @@ -146,9 +146,9 @@ public void testConnectExceptionCallsExceptionHandler() throws IOException {
}

public void testHandleReadDelegatesToContext() throws IOException {
SocketChannelContext context = mock(SocketChannelContext.class);
handler.handleRead(context);
verify(context).read();
SocketChannelContext mockContext = mock(SocketChannelContext.class);
handler.handleRead(mockContext);
verify(mockContext).read();
}

public void testReadExceptionCallsExceptionHandler() {
Expand All @@ -165,53 +165,53 @@ public void testWriteExceptionCallsExceptionHandler() {

public void testPostHandlingCallWillCloseTheChannelIfReady() throws IOException {
NioSocketChannel channel = mock(NioSocketChannel.class);
SocketChannelContext context = mock(SocketChannelContext.class);
SocketChannelContext mockContext = mock(SocketChannelContext.class);

when(channel.getContext()).thenReturn(context);
when(context.selectorShouldClose()).thenReturn(true);
handler.postHandling(context);
when(channel.getContext()).thenReturn(mockContext);
when(mockContext.selectorShouldClose()).thenReturn(true);
handler.postHandling(mockContext);

verify(context).closeFromSelector();
verify(mockContext).closeFromSelector();
}

public void testPostHandlingCallWillNotCloseTheChannelIfNotReady() throws IOException {
SocketChannelContext context = mock(SocketChannelContext.class);
when(context.getSelectionKey()).thenReturn(new TestSelectionKey(SelectionKey.OP_READ | SelectionKey.OP_WRITE));
when(context.selectorShouldClose()).thenReturn(false);
SocketChannelContext mockContext = mock(SocketChannelContext.class);
when(mockContext.getSelectionKey()).thenReturn(new TestSelectionKey(SelectionKey.OP_READ | SelectionKey.OP_WRITE));
when(mockContext.selectorShouldClose()).thenReturn(false);

NioSocketChannel channel = mock(NioSocketChannel.class);
when(channel.getContext()).thenReturn(context);
when(channel.getContext()).thenReturn(mockContext);

handler.postHandling(context);
handler.postHandling(mockContext);

verify(context, times(0)).closeFromSelector();
verify(mockContext, times(0)).closeFromSelector();
}

public void testPostHandlingWillAddWriteIfNecessary() throws IOException {
TestSelectionKey selectionKey = new TestSelectionKey(SelectionKey.OP_READ);
SocketChannelContext context = mock(SocketChannelContext.class);
when(context.getSelectionKey()).thenReturn(selectionKey);
when(context.readyForFlush()).thenReturn(true);
SocketChannelContext mockContext = mock(SocketChannelContext.class);
when(mockContext.getSelectionKey()).thenReturn(selectionKey);
when(mockContext.readyForFlush()).thenReturn(true);

NioSocketChannel channel = mock(NioSocketChannel.class);
when(channel.getContext()).thenReturn(context);
when(channel.getContext()).thenReturn(mockContext);

assertEquals(SelectionKey.OP_READ, selectionKey.interestOps());
handler.postHandling(context);
handler.postHandling(mockContext);
assertEquals(SelectionKey.OP_READ | SelectionKey.OP_WRITE, selectionKey.interestOps());
}

public void testPostHandlingWillRemoveWriteIfNecessary() throws IOException {
TestSelectionKey key = new TestSelectionKey(SelectionKey.OP_READ | SelectionKey.OP_WRITE);
SocketChannelContext context = mock(SocketChannelContext.class);
when(context.getSelectionKey()).thenReturn(key);
when(context.readyForFlush()).thenReturn(false);
SocketChannelContext mockContext = mock(SocketChannelContext.class);
when(mockContext.getSelectionKey()).thenReturn(key);
when(mockContext.readyForFlush()).thenReturn(false);

NioSocketChannel channel = mock(NioSocketChannel.class);
when(channel.getContext()).thenReturn(context);
when(channel.getContext()).thenReturn(mockContext);

assertEquals(SelectionKey.OP_READ | SelectionKey.OP_WRITE, key.interestOps());
handler.postHandling(context);
handler.postHandling(mockContext);
assertEquals(SelectionKey.OP_READ, key.interestOps());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ public void setUp() throws Exception {

@SuppressWarnings({ "unchecked", "rawtypes" })
public void testQueueChannelForClosed() throws IOException {
NioChannel channel = mock(NioChannel.class);
NioChannel mockChannel = mock(NioChannel.class);
ChannelContext context = mock(ChannelContext.class);
when(channel.getContext()).thenReturn(context);
when(mockChannel.getContext()).thenReturn(context);
when(context.getSelector()).thenReturn(selector);

selector.queueChannelClose(channel);
selector.queueChannelClose(mockChannel);

selector.singleLoop();

Expand All @@ -100,12 +100,12 @@ public void testQueueChannelForClosed() throws IOException {
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testCloseException() throws IOException, InterruptedException {
IOException ioException = new IOException();
NioChannel channel = mock(NioChannel.class);
NioChannel mockChannel = mock(NioChannel.class);
ChannelContext context = mock(ChannelContext.class);
when(channel.getContext()).thenReturn(context);
when(mockChannel.getContext()).thenReturn(context);
when(context.getSelector()).thenReturn(selector);

executeOnNewThread(() -> selector.queueChannelClose(channel));
executeOnNewThread(() -> selector.queueChannelClose(mockChannel));

doThrow(ioException).when(eventHandler).handleClose(context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ public void testRegisterInitiatesConnect() throws IOException {
isAccepted
);
InboundChannelBuffer buffer = InboundChannelBuffer.allocatingInstance();
TestSocketChannelContext context = new TestSocketChannelContext(channel, selector, exceptionHandler, handler, buffer, config);
context.register();
TestSocketChannelContext testContext = new TestSocketChannelContext(channel, selector, exceptionHandler, handler, buffer, config);
testContext.register();
if (isAccepted) {
verify(rawChannel, times(0)).connect(any(InetSocketAddress.class));
} else {
Expand Down Expand Up @@ -205,11 +205,11 @@ public void testConnectCanSetSocketOptions() throws IOException {
false
);
InboundChannelBuffer buffer = InboundChannelBuffer.allocatingInstance();
TestSocketChannelContext context = new TestSocketChannelContext(channel, selector, exceptionHandler, handler, buffer, config);
TestSocketChannelContext testContext = new TestSocketChannelContext(channel, selector, exceptionHandler, handler, buffer, config);
doThrow(new SocketException()).doNothing().when(rawSocket).setReuseAddress(tcpReuseAddress);
context.register();
testContext.register();
when(rawChannel.finishConnect()).thenReturn(true);
context.connect();
testContext.connect();

verify(rawSocket, times(2)).setReuseAddress(tcpReuseAddress);
verify(rawSocket).setKeepAlive(tcpKeepAlive);
Expand Down Expand Up @@ -339,15 +339,15 @@ public void testCloseClosesWriteProducer() throws IOException {
when(channel.getRawChannel()).thenReturn(realChannel);
when(channel.isOpen()).thenReturn(true);
InboundChannelBuffer buffer = InboundChannelBuffer.allocatingInstance();
BytesChannelContext context = new BytesChannelContext(
BytesChannelContext bytesChannelContext = new BytesChannelContext(
channel,
selector,
mock(Config.Socket.class),
exceptionHandler,
handler,
buffer
);
context.closeFromSelector();
bytesChannelContext.closeFromSelector();
verify(handler).close();
}
}
Expand All @@ -360,8 +360,8 @@ public void testCloseClosesChannelBuffer() throws IOException {
IntFunction<Page> pageAllocator = (n) -> new Page(ByteBuffer.allocate(n), closer);
InboundChannelBuffer buffer = new InboundChannelBuffer(pageAllocator);
buffer.ensureCapacity(1);
TestSocketChannelContext context = new TestSocketChannelContext(channel, selector, exceptionHandler, handler, buffer);
context.closeFromSelector();
TestSocketChannelContext testContext = new TestSocketChannelContext(channel, selector, exceptionHandler, handler, buffer);
testContext.closeFromSelector();
verify(closer).close();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

public class TestSelectionKey extends AbstractSelectionKey {

private int ops = 0;
private int interestOps = 0;
private int readyOps;

public TestSelectionKey(int ops) {
this.ops = ops;
this.interestOps = ops;
}

@Override
Expand All @@ -34,12 +34,12 @@ public Selector selector() {

@Override
public int interestOps() {
return ops;
return interestOps;
}

@Override
public SelectionKey interestOps(int ops) {
this.ops = ops;
this.interestOps = ops;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ public RestApiVersion restApiVersion() {
/**
* Replace the configured filtering.
*/
public XContentParserConfiguration withFiltering(Set<String> includes, Set<String> excludes) {
public XContentParserConfiguration withFiltering(Set<String> includeStrings, Set<String> excludeStrings) {
return new XContentParserConfiguration(
registry,
deprecationHandler,
restApiVersion,
FilterPath.compile(includes),
FilterPath.compile(excludes)
FilterPath.compile(includeStrings),
FilterPath.compile(excludeStrings)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
private static ConstructingObjectParser<HasCtorArguments, Void> buildParser(boolean animalRequired, boolean vegetableRequired) {
ConstructingObjectParser<HasCtorArguments, Void> parser = new ConstructingObjectParser<>(
"has_required_arguments",
a -> new HasCtorArguments((String) a[0], (Integer) a[1])
args -> new HasCtorArguments((String) args[0], (Integer) args[1])
);
parser.declareString(animalRequired ? constructorArg() : optionalConstructorArg(), new ParseField("animal"));
parser.declareInt(vegetableRequired ? constructorArg() : optionalConstructorArg(), new ParseField("vegetable"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public void setName(String name) {
this.name = name;
}

public void setURI(URI uri) {
public void setUri(URI uri) {
this.uri = uri;
}
}
Expand All @@ -180,9 +180,9 @@ class CustomParseContext {
this.parser = parser;
}

public URI parseURI(XContentParser parser) {
public URI parseURI(XContentParser xContentParser) {
try {
return this.parser.parseURI(parser);
return this.parser.parseURI(xContentParser);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand All @@ -194,7 +194,7 @@ public URI parseURI(XContentParser parser) {
);
ObjectParser<Foo, CustomParseContext> objectParser = new ObjectParser<>("foo");
objectParser.declareString(Foo::setName, new ParseField("name"));
objectParser.declareObjectOrDefault(Foo::setURI, (p, s) -> s.parseURI(p), () -> null, new ParseField("url"));
objectParser.declareObjectOrDefault(Foo::setUri, (p, s) -> s.parseURI(p), () -> null, new ParseField("url"));
Foo s = objectParser.parse(parser, new Foo(), new CustomParseContext(new ClassicParser()));
assertEquals(s.uri.getHost(), "foobar");
assertEquals(s.uri.getPort(), 80);
Expand Down Expand Up @@ -705,8 +705,8 @@ public void setInts(List<Integer> ints) {
this.ints = ints;
}

public void setArray(List<Object> testArray) {
this.testArray = testArray;
public void setArray(List<Object> array) {
this.testArray = array;
}
}
ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("foo");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ protected final void doWriteTo(StreamOutput out) throws IOException {
* Sets the field to use for this aggregation.
*/
@SuppressWarnings("unchecked")
public AB fields(List<String> fields) {
if (fields == null) {
public AB fields(List<String> fieldsArg) {
if (fieldsArg == null) {
throw new IllegalArgumentException("[field] must not be null: [" + name + "]");
}
this.fields = fields;
this.fields = fieldsArg;
return (AB) this;
}

Expand All @@ -149,11 +149,11 @@ public List<String> fields() {
* Sets the format to use for the output of the aggregation.
*/
@SuppressWarnings("unchecked")
public AB format(String format) {
if (format == null) {
public AB format(String formatArg) {
if (formatArg == null) {
throw new IllegalArgumentException("[format] must not be null: [" + name + "]");
}
this.format = format;
this.format = formatArg;
return (AB) this;
}

Expand All @@ -169,11 +169,11 @@ public String format() {
* document
*/
@SuppressWarnings("unchecked")
public AB missingMap(Map<String, Object> missingMap) {
if (missingMap == null) {
public AB missingMap(Map<String, Object> missingMapArg) {
if (missingMapArg == null) {
throw new IllegalArgumentException("[missing] must not be null: [" + name + "]");
}
this.missingMap = missingMap;
this.missingMap = missingMapArg;
return (AB) this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ public InternalAggregation reduce(List<InternalAggregation> aggregations, Reduce
}

if (reduceContext.isFinalReduce()) {
MatrixStatsResults results = new MatrixStatsResults(runningStats);
return new InternalMatrixStats(name, results.getDocCount(), runningStats, results, getMetadata());
MatrixStatsResults matrixStatsResults = new MatrixStatsResults(runningStats);
return new InternalMatrixStats(name, matrixStatsResults.getDocCount(), runningStats, matrixStatsResults, getMetadata());
}
return new InternalMatrixStats(name, runningStats.docCount, runningStats, null, getMetadata());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ protected InternalMatrixStats mutateInstance(InternalMatrixStats instance) {
name += randomAlphaOfLength(5);
break;
case 1:
String[] fields = Arrays.copyOf(this.fields, this.fields.length + 1);
fields[fields.length - 1] = "field_" + (fields.length - 1);
double[] values = new double[fields.length];
for (int i = 0; i < fields.length; i++) {
String[] fieldsCopy = Arrays.copyOf(this.fields, this.fields.length + 1);
fieldsCopy[fieldsCopy.length - 1] = "field_" + (fieldsCopy.length - 1);
double[] values = new double[fieldsCopy.length];
for (int i = 0; i < fieldsCopy.length; i++) {
values[i] = randomDouble() * 200;
}
runningStats = new RunningStats();
runningStats.add(fields, values);
runningStats.add(fieldsCopy, values);
break;
case 2:
if (matrixStatsResults == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public final class CJKBigramFilterFactory extends AbstractTokenFilterFactory {
private final int flags;
private final boolean outputUnigrams;

@SuppressWarnings("HiddenField")
CJKBigramFilterFactory(IndexSettings indexSettings, Environment environment, String name, Settings settings) {
super(indexSettings, name, settings);
outputUnigrams = settings.getAsBoolean("output_unigrams", false);
Expand Down
Loading

0 comments on commit 1d3b096

Please sign in to comment.