From 6e38830cbe925f141596d086d769462692620460 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Mon, 7 Oct 2024 19:06:41 +0200 Subject: [PATCH] clean up lots of warnings in ConfigurationHelper deprecate some unused methods add a useful method --- .../util/config/ConfigurationHelper.java | 194 ++++++++++-------- 1 file changed, 104 insertions(+), 90 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/config/ConfigurationHelper.java b/hibernate-core/src/main/java/org/hibernate/internal/util/config/ConfigurationHelper.java index 6c81301b9d..956d5eda5c 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/util/config/ConfigurationHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/util/config/ConfigurationHelper.java @@ -12,6 +12,7 @@ import java.util.Properties; import java.util.StringTokenizer; import java.util.function.Supplier; +import org.checkerframework.checker.nullness.qual.NonNull; import org.hibernate.Incubating; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.cfg.AvailableSettings; @@ -50,12 +51,24 @@ public final class ConfigurationHelper { * * @return The value, or null if not found */ - public static String getString(String name, Map values) { - Object value = values.get( name ); - if ( value == null ) { - return null; - } - return value.toString(); + public static String getString(String name, Map values) { + final Object value = values.get( name ); + return value == null ? null : value.toString(); + } + + /** + * Get the config value as a {@link String} + * + * @param preferred The preferred config setting name. + * @param fallback The fallback config setting name, when the preferred + * configuration is not set. + * @param values The map of config values + * + * @return The value, or null if not found + */ + public static String getString(String preferred, String fallback, Map values) { + final String preferredValue = getString( preferred, values ); + return preferredValue == null ? getString( fallback, values ) : preferredValue; } /** @@ -67,7 +80,7 @@ public final class ConfigurationHelper { * * @return The value. */ - public static String getString(String name, Map values, String defaultValue) { + public static String getString(String name, Map values, String defaultValue) { return getString( name, values, () -> defaultValue ); } @@ -81,11 +94,7 @@ public final class ConfigurationHelper { */ public static String getString(String name, Map values, Supplier defaultValueSupplier) { final Object value = values.get( name ); - if ( value != null ) { - return value.toString(); - } - - return defaultValueSupplier.get(); + return value != null ? value.toString() : defaultValueSupplier.get(); } /** @@ -96,7 +105,7 @@ public final class ConfigurationHelper { * * @return The value. */ - public static boolean getBoolean(String name, Map values) { + public static boolean getBoolean(String name, Map values) { return getBoolean( name, values, false ); } @@ -109,33 +118,32 @@ public final class ConfigurationHelper { * * @return The value. */ - public static boolean getBoolean(String name, Map values, boolean defaultValue) { + public static boolean getBoolean(String name, Map values, boolean defaultValue) { final Object raw = values.get( name ); - final Boolean value = toBoolean( raw, defaultValue ); if ( value == null ) { throw new ConfigurationException( "Could not determine how to handle configuration raw [name=" + name + ", value=" + raw + "] as boolean" ); } - - return value; + else { + return value; + } } public static Boolean toBoolean(Object value, boolean defaultValue) { if ( value == null ) { return defaultValue; } - - if (value instanceof Boolean) { - return (Boolean) value; + else if (value instanceof Boolean bool) { + return bool; } - - if (value instanceof String) { - return Boolean.parseBoolean( (String) value ); + else if (value instanceof String string) { + return Boolean.parseBoolean(string); + } + else { + return null; } - - return null; } /** @@ -146,20 +154,22 @@ public final class ConfigurationHelper { * * @return The value. */ - public static Boolean getBooleanWrapper(String name, Map values, Boolean defaultValue) { - Object value = values.get( name ); + public static Boolean getBooleanWrapper(String name, Map values, Boolean defaultValue) { + final Object value = values.get( name ); if ( value == null ) { return defaultValue; } - if (value instanceof Boolean) { - return (Boolean) value; + else if (value instanceof Boolean bool) { + return bool; } - if (value instanceof String) { - return Boolean.valueOf( (String) value ); + else if (value instanceof String string) { + return Boolean.valueOf(string); + } + else { + throw new ConfigurationException( + "Could not determine how to handle configuration value [name=" + name + ", value=" + value + "] as boolean" + ); } - throw new ConfigurationException( - "Could not determine how to handle configuration value [name=" + name + ", value=" + value + "] as boolean" - ); } /** @@ -171,16 +181,16 @@ public final class ConfigurationHelper { * * @return The value. */ - public static int getInt(String name, Map values, int defaultValue) { - Object value = values.get( name ); + public static int getInt(String name, Map values, int defaultValue) { + final Object value = values.get( name ); if ( value == null ) { return defaultValue; } - if (value instanceof Integer) { - return (Integer) value; + if (value instanceof Integer integer) { + return integer; } - if (value instanceof String) { - return Integer.parseInt( (String) value ); + if (value instanceof String string) { + return Integer.parseInt(string); } throw new ConfigurationException( "Could not determine how to handle configuration value [name=" + name + @@ -196,21 +206,18 @@ public final class ConfigurationHelper { * * @return The value, or null if not found */ - public static Integer getInteger(String name, Map values) { - Object value = values.get( name ); + public static Integer getInteger(String name, Map values) { + final Object value = values.get( name ); if ( value == null ) { return null; } - if (value instanceof Integer) { - return (Integer) value; + else if (value instanceof Integer integer) { + return integer; } - if (value instanceof String) { + else if (value instanceof String string) { //empty values are ignored - final String trimmed = value.toString().trim(); - if ( trimmed.isEmpty() ) { - return null; - } - return Integer.valueOf( trimmed ); + final String trimmed = string.trim(); + return trimmed.isEmpty() ? null : Integer.valueOf( trimmed ); } throw new ConfigurationException( "Could not determine how to handle configuration value [name=" + name + @@ -218,21 +225,23 @@ public final class ConfigurationHelper { ); } - public static long getLong(String name, Map values, int defaultValue) { - Object value = values.get( name ); + public static long getLong(String name, Map values, int defaultValue) { + final Object value = values.get( name ); if ( value == null ) { return defaultValue; } - if (value instanceof Long) { - return (Long) value; + else if (value instanceof Long number) { + return number; } - if (value instanceof String) { - return Long.parseLong( (String) value ); + else if (value instanceof String string) { + return Long.parseLong(string); + } + else { + throw new ConfigurationException( + "Could not determine how to handle configuration value [name=" + name + + ", value=" + value + "(" + value.getClass().getName() + ")] as long" + ); } - throw new ConfigurationException( - "Could not determine how to handle configuration value [name=" + name + - ", value=" + value + "(" + value.getClass().getName() + ")] as long" - ); } /** @@ -241,26 +250,25 @@ public final class ConfigurationHelper { * @param configurationValues The config values to clone * * @return The clone + * + * @deprecated No longer used */ - @SuppressWarnings("unchecked") + @SuppressWarnings("rawtypes") + @Deprecated(since = "7", forRemoval = true) public static Map clone(Map configurationValues) { if ( configurationValues == null ) { return null; } - // If a Properties object, leverage its clone() impl - if (configurationValues instanceof Properties) { - return (Properties) ( (Properties) configurationValues ).clone(); + else if ( configurationValues instanceof Properties properties ) { + // If a Properties object, leverage its clone() impl + return (Properties) properties.clone(); } - // Otherwise make a manual copy - HashMap clone = new HashMap(); - for ( Map.Entry entry : configurationValues.entrySet() ) { - clone.put( entry.getKey(), entry.getValue() ); + else { + // Otherwise make a manual copy + return new HashMap<>( configurationValues ); } - return clone; } - - /** * replace a property by a starred version * @@ -270,17 +278,13 @@ public final class ConfigurationHelper { * @return cloned and masked properties */ public static Properties maskOut(Properties props, String key) { - Properties clone = ( Properties ) props.clone(); + final Properties clone = (Properties) props.clone(); if ( clone.get( key ) != null ) { clone.setProperty( key, "****" ); } return clone; } - - - - /** * Extract a property value by name from the given properties object. *

@@ -310,7 +314,7 @@ public final class ConfigurationHelper { * @param properties The properties object * @return The property value; may be null. */ - public static String extractPropertyValue(String propertyName, Map properties) { + public static String extractPropertyValue(String propertyName, Map properties) { String value = (String) properties.get( propertyName ); if ( value == null ) { return null; @@ -324,7 +328,7 @@ public final class ConfigurationHelper { public static String extractValue( String name, - Map values, + Map values, Supplier fallbackValueFactory) { final String value = extractPropertyValue( name, values ); if ( value != null ) { @@ -346,9 +350,13 @@ public final class ConfigurationHelper { * @param delim The string defining tokens used as both entry and key/value delimiters. * @param properties The properties object * @return The resulting map; never null, though perhaps empty. + * + * @deprecated No longer used */ + @SuppressWarnings("rawtypes") + @Deprecated(since = "7", forRemoval = true) public static Map toMap(String propertyName, String delim, Properties properties) { - Map map = new HashMap(); + Map map = new HashMap<>(); String value = extractPropertyValue( propertyName, properties ); if ( value != null ) { StringTokenizer tokens = new StringTokenizer( value, delim ); @@ -371,9 +379,13 @@ public final class ConfigurationHelper { * @param delim The string defining tokens used as both entry and key/value delimiters. * @param properties The properties object * @return The resulting map; never null, though perhaps empty. + * + * @deprecated No longer used */ - public static Map toMap(String propertyName, String delim, Map properties) { - Map map = new HashMap(); + @SuppressWarnings("rawtypes") + @Deprecated(since = "7", forRemoval = true) + public static Map toMap(String propertyName, String delim, Map properties) { + Map map = new HashMap<>(); String value = extractPropertyValue( propertyName, properties ); if ( value != null ) { StringTokenizer tokens = new StringTokenizer( value, delim ); @@ -423,10 +435,10 @@ public final class ConfigurationHelper { * * @param configurationValues The configuration map. */ - public static void resolvePlaceHolders(Map configurationValues) { - Iterator itr = configurationValues.entrySet().iterator(); + public static void resolvePlaceHolders(Map configurationValues) { + final Iterator> itr = configurationValues.entrySet().iterator(); while ( itr.hasNext() ) { - final Map.Entry entry = ( Map.Entry ) itr.next(); + final Map.Entry entry = itr.next(); final Object value = entry.getValue(); if (value instanceof String) { final String resolved = resolvePlaceHolder( ( String ) value ); @@ -449,7 +461,7 @@ public final class ConfigurationHelper { * @return The (possibly) interpolated property value. */ public static String resolvePlaceHolder(String property) { - if ( property.indexOf( PLACEHOLDER_START ) < 0 ) { + if ( !property.contains( PLACEHOLDER_START ) ) { return property; } StringBuilder buff = new StringBuilder(); @@ -469,7 +481,7 @@ public final class ConfigurationHelper { throw new IllegalArgumentException( "unmatched placeholder start [" + property + "]" ); } } - String systemProperty = extractFromSystem( systemPropertyName ); + final String systemProperty = extractFromSystem( systemPropertyName ); buff.append( systemProperty == null ? "" : systemProperty ); pos = x + 1; // make sure spinning forward did not put us past the end of the buffer... @@ -480,8 +492,8 @@ public final class ConfigurationHelper { } buff.append( chars[pos] ); } - String rtn = buff.toString(); - return rtn.isEmpty() ? null : rtn; + final String result = buff.toString(); + return result.isEmpty() ? null : result; } private static String extractFromSystem(String systemPropertyName) { @@ -591,6 +603,7 @@ public final class ConfigurationHelper { } } + @Deprecated(since = "7", forRemoval = true) public static void setIfNotNull(Object value, String settingName, Map configuration) { if ( value != null ) { configuration.put( settingName, value ); @@ -602,9 +615,10 @@ public final class ConfigurationHelper { public static final TypeCodeConverter INSTANCE = new TypeCodeConverter(); @Override + @NonNull public Integer convert(Object value) { - if ( value instanceof Number ) { - return ( (Number) value ).intValue(); + if ( value instanceof Number number ) { + return number.intValue(); } final String string = value.toString().toUpperCase( Locale.ROOT );