Skip to content

Commit

Permalink
Switch to Java 17 instanceof pattern matching for folders test and x-…
Browse files Browse the repository at this point in the history
…pack/qa (#82683)
  • Loading branch information
astefan committed Jan 17, 2022
1 parent d332729 commit c018c2c
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,14 @@ private <T> void assertInstanceBindingWithAnnotation(
) {
List<Element> elements = Elements.getElements(module);
for (Element element : elements) {
if (element instanceof InstanceBinding) {
InstanceBinding<?> binding = (InstanceBinding<?>) element;
if (element instanceof InstanceBinding<?> binding) {
if (to.equals(binding.getKey().getTypeLiteral().getType())) {
if (annotation == null || annotation.equals(binding.getKey().getAnnotationType())) {
assertTrue(tester.test(to.cast(binding.getInstance())));
return;
}
}
} else if (element instanceof ProviderInstanceBinding) {
ProviderInstanceBinding<?> binding = (ProviderInstanceBinding<?>) element;
} else if (element instanceof ProviderInstanceBinding<?> binding) {
if (to.equals(binding.getKey().getTypeLiteral().getType())) {
assertTrue(tester.test(to.cast(binding.getProviderInstance().get())));
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1082,8 +1082,7 @@ public static void assertOpsOnReplica(
) throws IOException {
final Engine.Operation lastOp = ops.get(ops.size() - 1);
final String lastFieldValue;
if (lastOp instanceof Engine.Index) {
Engine.Index index = (Engine.Index) lastOp;
if (lastOp instanceof Engine.Index index) {
lastFieldValue = index.docs().get(0).get("value");
} else {
// delete
Expand Down Expand Up @@ -1582,11 +1581,9 @@ private static LazySoftDeletesDirectoryReaderWrapper.LazyBits lazyBits(LeafReade
return ((LazySoftDeletesDirectoryReaderWrapper.LazySoftDeletesFilterLeafReader) reader).getLiveDocs();
} else if (reader instanceof LazySoftDeletesDirectoryReaderWrapper.LazySoftDeletesFilterCodecReader) {
return ((LazySoftDeletesDirectoryReaderWrapper.LazySoftDeletesFilterCodecReader) reader).getLiveDocs();
} else if (reader instanceof FilterLeafReader) {
final FilterLeafReader fReader = (FilterLeafReader) reader;
} else if (reader instanceof final FilterLeafReader fReader) {
return lazyBits(FilterLeafReader.unwrap(fReader));
} else if (reader instanceof FilterCodecReader) {
final FilterCodecReader fReader = (FilterCodecReader) reader;
} else if (reader instanceof final FilterCodecReader fReader) {
return lazyBits(FilterCodecReader.unwrap(fReader));
} else if (reader instanceof SegmentReader) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ && deepEquals(docA.getSourceAndMetadata(), docB.getSourceAndMetadata(), false))
}

private static boolean deepEquals(Object a, Object b, boolean isIngestMeta) {
if (a instanceof Map) {
Map<?, ?> mapA = (Map<?, ?>) a;
if (a instanceof Map<?, ?> mapA) {
if (b instanceof Map == false) {
return false;
}
Expand All @@ -44,8 +43,7 @@ private static boolean deepEquals(Object a, Object b, boolean isIngestMeta) {
}
}
return true;
} else if (a instanceof List) {
List<?> listA = (List<?>) a;
} else if (a instanceof List<?> listA) {
if (b instanceof List == false) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -801,13 +801,11 @@ protected <T extends AggregationBuilder, V extends InternalAggregation> void ver

Set<String> valueNames = new HashSet<>();

if (agg instanceof NumericMetricsAggregation.MultiValue) {
NumericMetricsAggregation.MultiValue multiValueAgg = (NumericMetricsAggregation.MultiValue) agg;
if (agg instanceof NumericMetricsAggregation.MultiValue multiValueAgg) {
for (String name : multiValueAgg.valueNames()) {
valueNames.add(name);
}
} else if (agg instanceof MultiValueAggregation) {
MultiValueAggregation multiValueAgg = (MultiValueAggregation) agg;
} else if (agg instanceof MultiValueAggregation multiValueAgg) {
for (String name : multiValueAgg.valueNames()) {
valueNames.add(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,7 @@ private void assertLuceneQuery(QB queryBuilder, Query query, SearchExecutionCont
if (query != null) {
if (queryBuilder.boost() != AbstractQueryBuilder.DEFAULT_BOOST) {
assertThat(query, either(instanceOf(BoostQuery.class)).or(instanceOf(MatchNoDocsQuery.class)));
if (query instanceof BoostQuery) {
BoostQuery boostQuery = (BoostQuery) query;
if (query instanceof BoostQuery boostQuery) {
if (boostQuery.getQuery() instanceof MatchNoDocsQuery == false) {
assertThat(boostQuery.getBoost(), equalTo(queryBuilder.boost()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,12 @@ private void assertSchemaStrictness(Collection<JsonValidator> validatorSet, Stri
boolean subSchemaFound = false;

for (JsonValidator validator : validatorSet) {
if (validator instanceof PropertiesValidator) {
if (validator instanceof PropertiesValidator propertiesValidator) {
subSchemaFound = true;
PropertiesValidator propertiesValidator = (PropertiesValidator) validator;
for (Entry<String, JsonSchema> subSchema : propertiesValidator.getSchemas().entrySet()) {
assertSchemaStrictness(subSchema.getValue().getValidators().values(), propertiesValidator.getSchemaPath());
}
} else if (validator instanceof ItemsValidator) {
ItemsValidator itemValidator = (ItemsValidator) validator;
} else if (validator instanceof ItemsValidator itemValidator) {
if (itemValidator.getSchema() != null) {
assertSchemaStrictness(itemValidator.getSchema().getValidators().values(), itemValidator.getSchemaPath());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1080,8 +1080,7 @@ private synchronized void reset(boolean wipeData) throws IOException {
// clear all rules for mock transport services
for (NodeAndClient nodeAndClient : nodes.values()) {
TransportService transportService = nodeAndClient.node.injector().getInstance(TransportService.class);
if (transportService instanceof MockTransportService) {
final MockTransportService mockTransportService = (MockTransportService) transportService;
if (transportService instanceof final MockTransportService mockTransportService) {
mockTransportService.clearAllRules();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ public void compare(String field, boolean hadKey, @Nullable Object actual, Objec
return;
}
if (Objects.equals(expected, actual)) {
if (expected instanceof String) {
String expectedString = (String) expected;
if (expected instanceof String expectedString) {
if (expectedString.length() > 50) {
expectedString = expectedString.substring(0, 50) + "...";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ public Map<String, Object> replaceStashedValues(Map<String, Object> map) throws
}

private Object unstashObject(List<Object> path, Object obj) throws IOException {
if (obj instanceof List) {
List<?> list = (List<?>) obj;
if (obj instanceof List<?> list) {
List<Object> result = new ArrayList<>();
int index = 0;
for (Object o : list) {
Expand All @@ -137,8 +136,7 @@ private Object unstashObject(List<Object> path, Object obj) throws IOException {
}
return result;
}
if (obj instanceof Map) {
Map<?, ?> map = (Map<?, ?>) obj;
if (obj instanceof Map<?, ?> map) {
Map<String, Object> result = new HashMap<>();
for (Map.Entry<?, ?> entry : map.entrySet()) {
String key = (String) entry.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public final double getError() {
@Override
protected void doAssert(Object actualValue, Object expectedValue) {
logger.trace("assert that [{}] is close to [{}] with error [{}] (field [{}])", actualValue, expectedValue, error, getField());
if (actualValue instanceof Number) {
assertThat(((Number) actualValue).doubleValue(), closeTo((Double) expectedValue, error));
if (actualValue instanceof Number actualValueNumber) {
assertThat(actualValueNumber.doubleValue(), closeTo((Double) expectedValue, error));
} else {
throw new AssertionError("excpected a value close to " + expectedValue + " but got " + actualValue + ", which is not a number");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ public void findBadLoggerUsages(MethodNode methodNode) {
int lineNumber = -1;
for (int i = 0; i < insns.length; i++) {
AbstractInsnNode insn = insns[i];
if (insn instanceof LineNumberNode) {
LineNumberNode lineNumberNode = (LineNumberNode) insn;
if (insn instanceof LineNumberNode lineNumberNode) {
lineNumber = lineNumberNode.line;
}
if (insn.getOpcode() == Opcodes.INVOKEINTERFACE) {
Expand Down Expand Up @@ -506,8 +505,7 @@ private int getChainedParams(AbstractInsnNode startNode) {
AbstractInsnNode current = startNode;
while (current.getNext() != null) {
current = current.getNext();
if (current instanceof MethodInsnNode) {
MethodInsnNode method = (MethodInsnNode) current;
if (current instanceof MethodInsnNode method) {
if (method.name.equals("argAndField")) {
c++;
}
Expand Down Expand Up @@ -656,20 +654,18 @@ private static final class PlaceHolderStringInterpreter extends BasicInterpreter
public BasicValue newOperation(AbstractInsnNode insnNode) throws AnalyzerException {
if (insnNode.getOpcode() == Opcodes.LDC) {
Object constant = ((LdcInsnNode) insnNode).cst;
if (constant instanceof String) {
return new PlaceHolderStringBasicValue(calculateNumberOfPlaceHolders((String) constant));
if (constant instanceof String s) {
return new PlaceHolderStringBasicValue(calculateNumberOfPlaceHolders(s));
}
}
return super.newOperation(insnNode);
}

@Override
public BasicValue merge(BasicValue value1, BasicValue value2) {
if (value1 instanceof PlaceHolderStringBasicValue
&& value2 instanceof PlaceHolderStringBasicValue
if (value1 instanceof PlaceHolderStringBasicValue c1
&& value2 instanceof PlaceHolderStringBasicValue c2
&& value1.equals(value2) == false) {
PlaceHolderStringBasicValue c1 = (PlaceHolderStringBasicValue) value1;
PlaceHolderStringBasicValue c2 = (PlaceHolderStringBasicValue) value2;
return new PlaceHolderStringBasicValue(Math.min(c1.minValue, c2.minValue), Math.max(c1.maxValue, c2.maxValue));
}
return super.merge(value1, value2);
Expand Down Expand Up @@ -702,8 +698,8 @@ public BasicValue newOperation(AbstractInsnNode insnNode) throws AnalyzerExcepti
return new IntegerConstantBasicValue(Type.INT_TYPE, ((IntInsnNode) insnNode).operand);
case Opcodes.LDC: {
Object constant = ((LdcInsnNode) insnNode).cst;
if (constant instanceof Integer) {
return new IntegerConstantBasicValue(Type.INT_TYPE, (Integer) constant);
if (constant instanceof Integer integer) {
return new IntegerConstantBasicValue(Type.INT_TYPE, integer);
} else {
return super.newOperation(insnNode);
}
Expand All @@ -715,22 +711,17 @@ public BasicValue newOperation(AbstractInsnNode insnNode) throws AnalyzerExcepti

@Override
public BasicValue merge(BasicValue value1, BasicValue value2) {
if (value1 instanceof IntegerConstantBasicValue && value2 instanceof IntegerConstantBasicValue) {
IntegerConstantBasicValue c1 = (IntegerConstantBasicValue) value1;
IntegerConstantBasicValue c2 = (IntegerConstantBasicValue) value2;
if (value1 instanceof IntegerConstantBasicValue c1 && value2 instanceof IntegerConstantBasicValue c2) {
return new IntegerConstantBasicValue(Type.INT_TYPE, Math.min(c1.minValue, c2.minValue), Math.max(c1.maxValue, c2.maxValue));
} else if (value1 instanceof ArraySizeBasicValue && value2 instanceof ArraySizeBasicValue) {
ArraySizeBasicValue c1 = (ArraySizeBasicValue) value1;
ArraySizeBasicValue c2 = (ArraySizeBasicValue) value2;
} else if (value1 instanceof ArraySizeBasicValue c1 && value2 instanceof ArraySizeBasicValue c2) {
return new ArraySizeBasicValue(Type.INT_TYPE, Math.min(c1.minValue, c2.minValue), Math.max(c1.maxValue, c2.maxValue));
}
return super.merge(value1, value2);
}

@Override
public BasicValue unaryOperation(AbstractInsnNode insnNode, BasicValue value) throws AnalyzerException {
if (insnNode.getOpcode() == Opcodes.ANEWARRAY && value instanceof IntegerConstantBasicValue) {
IntegerConstantBasicValue constantBasicValue = (IntegerConstantBasicValue) value;
if (insnNode.getOpcode() == Opcodes.ANEWARRAY && value instanceof IntegerConstantBasicValue constantBasicValue) {
String desc = ((TypeInsnNode) insnNode).desc;
return new ArraySizeBasicValue(
Type.getType("[" + Type.getObjectType(desc)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,7 @@ static class KrbCallbackHandler implements CallbackHandler {

public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (Callback callback : callbacks) {
if (callback instanceof PasswordCallback) {
PasswordCallback pc = (PasswordCallback) callback;
if (callback instanceof PasswordCallback pc) {
if (pc.getPrompt().contains(principal)) {
pc.setPassword(password.getChars());
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ static <T> T doAsPrivilegedWrapper(final Subject subject, final PrivilegedExcept
try {
return AccessController.doPrivileged((PrivilegedExceptionAction<T>) () -> Subject.doAsPrivileged(subject, action, acc));
} catch (PrivilegedActionException pae) {
if (pae.getCause() instanceof PrivilegedActionException) {
throw (PrivilegedActionException) pae.getCause();
if (pae.getCause()instanceof PrivilegedActionException privilegedActionException) {
throw privilegedActionException;
}
throw pae;
}
Expand Down Expand Up @@ -258,8 +258,7 @@ private static class KrbCallbackHandler implements CallbackHandler {

public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (Callback callback : callbacks) {
if (callback instanceof PasswordCallback) {
PasswordCallback pc = (PasswordCallback) callback;
if (callback instanceof PasswordCallback pc) {
if (pc.getPrompt().contains(principal)) {
pc.setPassword(password.getChars());
break;
Expand Down

0 comments on commit c018c2c

Please sign in to comment.