Use new Java features (switch expressions, text blocks, new JDK methods)

Closes gh-29747
This commit is contained in:
Krzysztof Krason
2022-12-28 08:59:08 +01:00
committed by Sam Brannen
parent 48abd493fe
commit afb8a0d1b1
53 changed files with 498 additions and 552 deletions

View File

@@ -587,12 +587,11 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo
*/
protected ObjectName registerBeanNameOrInstance(Object mapValue, String beanKey) throws MBeanExportException {
try {
if (mapValue instanceof String) {
if (mapValue instanceof String beanName) {
// Bean name pointing to a potentially lazy-init bean in the factory.
if (this.beanFactory == null) {
throw new MBeanExportException("Cannot resolve bean names if not running in a BeanFactory");
}
String beanName = (String) mapValue;
if (isBeanDefinitionLazyInit(this.beanFactory, beanName)) {
ObjectName objectName = registerLazyInit(beanName, beanKey);
replaceNotificationListenerBeanNameKeysIfNecessary(beanName, objectName);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-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.
@@ -90,8 +90,7 @@ public abstract class BshScriptUtils {
throws EvalError {
Object result = evaluateBshScript(scriptSource, scriptInterfaces, classLoader);
if (result instanceof Class) {
Class<?> clazz = (Class<?>) result;
if (result instanceof Class<?> clazz) {
try {
return ReflectionUtils.accessibleConstructor(clazz).newInstance();
}

View File

@@ -160,8 +160,7 @@ public class StandardScriptFactory implements ScriptFactory, BeanClassLoaderAwar
}
}
if (script instanceof Class) {
Class<?> scriptClass = (Class<?>) script;
if (script instanceof Class<?> scriptClass) {
try {
return ReflectionUtils.accessibleConstructor(scriptClass).newInstance();
}

View File

@@ -191,8 +191,7 @@ public class ResourceBundleThemeSource implements HierarchicalThemeSource, BeanC
* @param theme the Theme to (re-)initialize
*/
protected void initParent(Theme theme) {
if (theme.getMessageSource() instanceof HierarchicalMessageSource) {
HierarchicalMessageSource messageSource = (HierarchicalMessageSource) theme.getMessageSource();
if (theme.getMessageSource() instanceof HierarchicalMessageSource messageSource) {
if (getParentThemeSource() != null && messageSource.getParentMessageSource() == null) {
Theme parentTheme = getParentThemeSource().getTheme(theme.getName());
if (parentTheme != null) {

View File

@@ -852,8 +852,7 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
if (pv.getValue() instanceof String) {
empty = !StringUtils.hasText((String) pv.getValue());
}
else if (pv.getValue() instanceof String[]) {
String[] values = (String[]) pv.getValue();
else if (pv.getValue() instanceof String[] values) {
empty = (values.length == 0 || !StringUtils.hasText(values[0]));
}
}

View File

@@ -49,33 +49,35 @@ class ScriptFactoryPostProcessorTests {
private static final String PROCESSOR_BEAN_NAME = "processor";
private static final String CHANGED_SCRIPT = "package org.springframework.scripting.groovy\n" +
"import org.springframework.scripting.Messenger\n" +
"class GroovyMessenger implements Messenger {\n" +
" private String message = \"Bingo\"\n" +
" public String getMessage() {\n" +
// quote the returned message (this is the change)...
" return \"'\" + this.message + \"'\"\n" +
" }\n" +
" public void setMessage(String message) {\n" +
" this.message = message\n" +
" }\n" +
"}";
// quote the returned message (this is the change)...
private static final String CHANGED_SCRIPT = """
package org.springframework.scripting.groovy
import org.springframework.scripting.Messenger
class GroovyMessenger implements Messenger {
private String message = "Bingo"
public String getMessage() {
return "'" + this.message + "'"
}
public void setMessage(String message) {
this.message = message
}
}""";
private static final String EXPECTED_CHANGED_MESSAGE_TEXT = "'" + MESSAGE_TEXT + "'";
private static final int DEFAULT_SECONDS_TO_PAUSE = 1;
private static final String DELEGATING_SCRIPT = "inline:package org.springframework.scripting;\n" +
"class DelegatingMessenger implements Messenger {\n" +
" private Messenger wrappedMessenger;\n" +
" public String getMessage() {\n" +
" return this.wrappedMessenger.getMessage()\n" +
" }\n" +
" public void setMessenger(Messenger wrappedMessenger) {\n" +
" this.wrappedMessenger = wrappedMessenger\n" +
" }\n" +
"}";
private static final String DELEGATING_SCRIPT = """
inline:package org.springframework.scripting;
class DelegatingMessenger implements Messenger {
private Messenger wrappedMessenger;
public String getMessage() {
return this.wrappedMessenger.getMessage()
}
public void setMessenger(Messenger wrappedMessenger) {
this.wrappedMessenger = wrappedMessenger
}
}""";
@Test
@@ -245,16 +247,17 @@ class ScriptFactoryPostProcessorTests {
private static BeanDefinition createScriptedGroovyBean() {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(GroovyScriptFactory.class);
builder.addConstructorArgValue("inline:package org.springframework.scripting;\n" +
"class GroovyMessenger implements Messenger {\n" +
" private String message = \"Bingo\"\n" +
" public String getMessage() {\n" +
" return this.message\n" +
" }\n" +
" public void setMessage(String message) {\n" +
" this.message = message\n" +
" }\n" +
"}");
builder.addConstructorArgValue("""
inline:package org.springframework.scripting;
class GroovyMessenger implements Messenger {
private String message = "Bingo"
public String getMessage() {
return this.message
}
public void setMessage(String message) {
this.message = message
}
}""");
builder.addPropertyValue("message", MESSAGE_TEXT);
return builder.getBeanDefinition();
}