From 31c3b8a291ee79b1f6735955d5a457d41d976ac5 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 3 May 2019 00:10:56 +0200 Subject: [PATCH] Avoid unguarded String concatenation for debug/trace logging Closes gh-22874 --- .../AbstractTypeHierarchyTraversingFilter.java | 14 +++++++++----- .../CustomSQLExceptionTranslatorRegistry.java | 18 ++++++++++-------- .../DefaultJmsActivationSpecFactory.java | 18 +++++++++++++----- .../AbstractWebArgumentResolverAdapter.java | 6 ++++-- .../resource/CachingResourceTransformer.java | 6 ++++-- .../servlet/i18n/LocaleChangeInterceptor.java | 6 ++++-- .../resource/CachingResourceTransformer.java | 6 ++---- 7 files changed, 46 insertions(+), 28 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/type/filter/AbstractTypeHierarchyTraversingFilter.java b/spring-core/src/main/java/org/springframework/core/type/filter/AbstractTypeHierarchyTraversingFilter.java index 4d93f8005b3..cfa9ba2b939 100644 --- a/spring-core/src/main/java/org/springframework/core/type/filter/AbstractTypeHierarchyTraversingFilter.java +++ b/spring-core/src/main/java/org/springframework/core/type/filter/AbstractTypeHierarchyTraversingFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -85,8 +85,10 @@ public abstract class AbstractTypeHierarchyTraversingFilter implements TypeFilte } } catch (IOException ex) { - logger.debug("Could not read super class [" + metadata.getSuperClassName() + - "] of type-filtered class [" + metadata.getClassName() + "]"); + if (logger.isDebugEnabled()) { + logger.debug("Could not read super class [" + metadata.getSuperClassName() + + "] of type-filtered class [" + metadata.getClassName() + "]"); + } } } } @@ -109,8 +111,10 @@ public abstract class AbstractTypeHierarchyTraversingFilter implements TypeFilte } } catch (IOException ex) { - logger.debug("Could not read interface [" + ifc + "] for type-filtered class [" + - metadata.getClassName() + "]"); + if (logger.isDebugEnabled()) { + logger.debug("Could not read interface [" + ifc + "] for type-filtered class [" + + metadata.getClassName() + "]"); + } } } } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/support/CustomSQLExceptionTranslatorRegistry.java b/spring-jdbc/src/main/java/org/springframework/jdbc/support/CustomSQLExceptionTranslatorRegistry.java index c5f471bb223..2908e65bac3 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/support/CustomSQLExceptionTranslatorRegistry.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/support/CustomSQLExceptionTranslatorRegistry.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -74,13 +74,15 @@ public final class CustomSQLExceptionTranslatorRegistry { */ public void registerTranslator(String dbName, SQLExceptionTranslator translator) { SQLExceptionTranslator replaced = this.translatorMap.put(dbName, translator); - if (replaced != null) { - logger.debug("Replacing custom translator [" + replaced + "] for database '" + dbName + - "' with [" + translator + "]"); - } - else { - logger.debug("Adding custom translator of type [" + translator.getClass().getName() + - "] for database '" + dbName + "'"); + if (logger.isDebugEnabled()) { + if (replaced != null) { + logger.debug("Replacing custom translator [" + replaced + "] for database '" + dbName + + "' with [" + translator + "]"); + } + else { + logger.debug("Adding custom translator of type [" + translator.getClass().getName() + + "] for database '" + dbName + "'"); + } } } diff --git a/spring-jms/src/main/java/org/springframework/jms/listener/endpoint/DefaultJmsActivationSpecFactory.java b/spring-jms/src/main/java/org/springframework/jms/listener/endpoint/DefaultJmsActivationSpecFactory.java index 21aeca6dbde..1702070063b 100644 --- a/spring-jms/src/main/java/org/springframework/jms/listener/endpoint/DefaultJmsActivationSpecFactory.java +++ b/spring-jms/src/main/java/org/springframework/jms/listener/endpoint/DefaultJmsActivationSpecFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -85,7 +85,9 @@ public class DefaultJmsActivationSpecFactory extends StandardJmsActivationSpecFa return adapter.getClass().getClassLoader().loadClass(specClassName); } catch (ClassNotFoundException ex) { - logger.debug("No default ActivationSpec class found: " + specClassName); + if (logger.isDebugEnabled()) { + logger.debug("No default ActivationSpec class found: " + specClassName); + } } } @@ -98,7 +100,9 @@ public class DefaultJmsActivationSpecFactory extends StandardJmsActivationSpecFa return adapter.getClass().getClassLoader().loadClass(specClassName); } catch (ClassNotFoundException ex) { - logger.debug("No default ActivationSpecImpl class found: " + specClassName); + if (logger.isDebugEnabled()) { + logger.debug("No default ActivationSpecImpl class found: " + specClassName); + } } } @@ -109,7 +113,9 @@ public class DefaultJmsActivationSpecFactory extends StandardJmsActivationSpecFa return adapter.getClass().getClassLoader().loadClass(specClassName); } catch (ClassNotFoundException ex) { - logger.debug("No default ActivationSpecImpl class found in provider package: " + specClassName); + if (logger.isDebugEnabled()) { + logger.debug("No default ActivationSpecImpl class found in provider package: " + specClassName); + } } // ActivationSpecImpl class in "inbound" subpackage (WebSphere MQ 6.0.2.1) @@ -118,7 +124,9 @@ public class DefaultJmsActivationSpecFactory extends StandardJmsActivationSpecFa return adapter.getClass().getClassLoader().loadClass(specClassName); } catch (ClassNotFoundException ex) { - logger.debug("No default ActivationSpecImpl class found in inbound subpackage: " + specClassName); + if (logger.isDebugEnabled()) { + logger.debug("No default ActivationSpecImpl class found in inbound subpackage: " + specClassName); + } } throw new IllegalStateException("No ActivationSpec class defined - " + diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/AbstractWebArgumentResolverAdapter.java b/spring-web/src/main/java/org/springframework/web/method/annotation/AbstractWebArgumentResolverAdapter.java index c57e66df6aa..0d0272a373e 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/AbstractWebArgumentResolverAdapter.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/AbstractWebArgumentResolverAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -81,7 +81,9 @@ public abstract class AbstractWebArgumentResolverAdapter implements HandlerMetho } catch (Exception ex) { // ignore (see class-level doc) - logger.debug("Error in checking support for parameter [" + parameter + "], message: " + ex.getMessage()); + if (logger.isDebugEnabled()) { + logger.debug("Error in checking support for parameter [" + parameter + "]: " + ex.getMessage()); + } return false; } } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/CachingResourceTransformer.java b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/CachingResourceTransformer.java index 456d0ef0591..29492a0e23c 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/CachingResourceTransformer.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/CachingResourceTransformer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 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. @@ -69,7 +69,9 @@ public class CachingResourceTransformer implements ResourceTransformer { Resource cachedResource = this.cache.get(resource, Resource.class); if (cachedResource != null) { - logger.trace(exchange.getLogPrefix() + "Resource resolved from cache"); + if (logger.isTraceEnabled()) { + logger.trace(exchange.getLogPrefix() + "Resource resolved from cache"); + } return Mono.just(cachedResource); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/i18n/LocaleChangeInterceptor.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/i18n/LocaleChangeInterceptor.java index 9b0c15bb8e7..1fb9ef29ed5 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/i18n/LocaleChangeInterceptor.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/i18n/LocaleChangeInterceptor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -154,7 +154,9 @@ public class LocaleChangeInterceptor extends HandlerInterceptorAdapter { } catch (IllegalArgumentException ex) { if (isIgnoreInvalidLocale()) { - logger.debug("Ignoring invalid locale value [" + newLocale + "]: " + ex.getMessage()); + if (logger.isDebugEnabled()) { + logger.debug("Ignoring invalid locale value [" + newLocale + "]: " + ex.getMessage()); + } } else { throw ex; diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/CachingResourceTransformer.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/CachingResourceTransformer.java index d2431f6de0b..25c0aee859c 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/CachingResourceTransformer.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/CachingResourceTransformer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 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. @@ -71,9 +71,7 @@ public class CachingResourceTransformer implements ResourceTransformer { Resource transformed = this.cache.get(resource, Resource.class); if (transformed != null) { - if (logger.isTraceEnabled()) { - logger.trace("Resource resolved from cache"); - } + logger.trace("Resource resolved from cache"); return transformed; }