Use pattern matching for instanceof where appropriate
See gh-31475
This commit is contained in:
committed by
Andy Wilkinson
parent
a7b98e7312
commit
5db04da275
@@ -212,11 +212,11 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
|
||||
AnnotationMirror annotation = this.metadataEnv.getConfigurationPropertiesAnnotation(element);
|
||||
if (annotation != null) {
|
||||
String prefix = getPrefix(annotation);
|
||||
if (element instanceof TypeElement) {
|
||||
processAnnotatedTypeElement(prefix, (TypeElement) element, new Stack<>());
|
||||
if (element instanceof TypeElement typeElement) {
|
||||
processAnnotatedTypeElement(prefix, typeElement, new Stack<>());
|
||||
}
|
||||
else if (element instanceof ExecutableElement) {
|
||||
processExecutableElement(prefix, (ExecutableElement) element, new Stack<>());
|
||||
else if (element instanceof ExecutableElement executableElement) {
|
||||
processExecutableElement(prefix, executableElement, new Stack<>());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -235,7 +235,7 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
|
||||
if ((!element.getModifiers().contains(Modifier.PRIVATE))
|
||||
&& (TypeKind.VOID != element.getReturnType().getKind())) {
|
||||
Element returns = this.processingEnv.getTypeUtils().asElement(element.getReturnType());
|
||||
if (returns instanceof TypeElement) {
|
||||
if (returns instanceof TypeElement typeElement) {
|
||||
ItemMetadata group = ItemMetadata.newGroup(prefix,
|
||||
this.metadataEnv.getTypeUtils().getQualifiedName(returns),
|
||||
this.metadataEnv.getTypeUtils().getQualifiedName(element.getEnclosingElement()),
|
||||
@@ -246,7 +246,7 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
|
||||
}
|
||||
else {
|
||||
this.metadataCollector.add(group);
|
||||
processTypeElement(prefix, (TypeElement) returns, element, seen);
|
||||
processTypeElement(prefix, typeElement, element, seen);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -273,8 +273,8 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
|
||||
try {
|
||||
String annotationName = this.metadataEnv.getTypeUtils().getQualifiedName(annotations.get(0));
|
||||
AnnotationMirror annotation = this.metadataEnv.getAnnotation(element, annotationName);
|
||||
if (element instanceof TypeElement) {
|
||||
processEndpoint(annotation, (TypeElement) element);
|
||||
if (element instanceof TypeElement typeElement) {
|
||||
processEndpoint(annotation, typeElement);
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
* Copyright 2012-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -284,8 +284,7 @@ class TypeUtils {
|
||||
public String visitTypeVariable(TypeVariable t, TypeDescriptor descriptor) {
|
||||
TypeMirror typeMirror = descriptor.resolveGeneric(t);
|
||||
if (typeMirror != null) {
|
||||
if (typeMirror instanceof TypeVariable) {
|
||||
TypeVariable typeVariable = (TypeVariable) typeMirror;
|
||||
if (typeMirror instanceof TypeVariable typeVariable) {
|
||||
// Still unresolved, let's use the upper bound, checking first if
|
||||
// a cycle may exist
|
||||
if (!hasCycle(typeVariable)) {
|
||||
@@ -302,9 +301,8 @@ class TypeUtils {
|
||||
|
||||
private boolean hasCycle(TypeVariable variable) {
|
||||
TypeMirror upperBound = variable.getUpperBound();
|
||||
if (upperBound instanceof DeclaredType) {
|
||||
return ((DeclaredType) upperBound).getTypeArguments().stream()
|
||||
.anyMatch((candidate) -> candidate.equals(variable));
|
||||
if (upperBound instanceof DeclaredType declaredType) {
|
||||
return declaredType.getTypeArguments().stream().anyMatch((candidate) -> candidate.equals(variable));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -333,18 +331,17 @@ class TypeUtils {
|
||||
return getQualifiedName(enclosingElement) + "$"
|
||||
+ ((DeclaredType) element.asType()).asElement().getSimpleName();
|
||||
}
|
||||
if (element instanceof TypeElement) {
|
||||
return ((TypeElement) element).getQualifiedName().toString();
|
||||
if (element instanceof TypeElement typeElement) {
|
||||
return typeElement.getQualifiedName().toString();
|
||||
}
|
||||
throw new IllegalStateException("Could not extract qualified name from " + element);
|
||||
}
|
||||
|
||||
private TypeElement getEnclosingTypeElement(TypeMirror type) {
|
||||
if (type instanceof DeclaredType) {
|
||||
DeclaredType declaredType = (DeclaredType) type;
|
||||
if (type instanceof DeclaredType declaredType) {
|
||||
Element enclosingElement = declaredType.asElement().getEnclosingElement();
|
||||
if (enclosingElement instanceof TypeElement) {
|
||||
return (TypeElement) enclosingElement;
|
||||
if (enclosingElement instanceof TypeElement typeElement) {
|
||||
return typeElement;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
@@ -373,8 +370,7 @@ class TypeUtils {
|
||||
}
|
||||
|
||||
private void registerIfNecessary(TypeMirror variable, TypeMirror resolution) {
|
||||
if (variable instanceof TypeVariable) {
|
||||
TypeVariable typeVariable = (TypeVariable) variable;
|
||||
if (variable instanceof TypeVariable typeVariable) {
|
||||
if (this.generics.keySet().stream()
|
||||
.noneMatch((candidate) -> getParameterName(candidate).equals(getParameterName(typeVariable)))) {
|
||||
this.generics.put(typeVariable, resolution);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -52,11 +52,11 @@ public class JsonMarshaller {
|
||||
outputStream.write(object.toString(2).getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
if (ex instanceof IOException) {
|
||||
throw (IOException) ex;
|
||||
if (ex instanceof IOException ioException) {
|
||||
throw ioException;
|
||||
}
|
||||
if (ex instanceof RuntimeException) {
|
||||
throw (RuntimeException) ex;
|
||||
if (ex instanceof RuntimeException runtimeException) {
|
||||
throw runtimeException;
|
||||
}
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
@@ -150,8 +150,7 @@ public class JsonMarshaller {
|
||||
}
|
||||
|
||||
private Object readItemValue(Object value) throws Exception {
|
||||
if (value instanceof JSONArray) {
|
||||
JSONArray array = (JSONArray) value;
|
||||
if (value instanceof JSONArray array) {
|
||||
Object[] content = new Object[array.length()];
|
||||
for (int i = 0; i < array.length(); i++) {
|
||||
content[i] = array.get(i);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
* Copyright 2012-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -126,9 +126,9 @@ public abstract class AbstractFieldValuesProcessorTests {
|
||||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||
for (TypeElement annotation : annotations) {
|
||||
for (Element element : roundEnv.getElementsAnnotatedWith(annotation)) {
|
||||
if (element instanceof TypeElement) {
|
||||
if (element instanceof TypeElement typeElement) {
|
||||
try {
|
||||
this.values.putAll(this.processor.getFieldValues((TypeElement) element));
|
||||
this.values.putAll(this.processor.getFieldValues(typeElement));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
|
||||
Reference in New Issue
Block a user