From 9d73f81e9cae810c0ae9cf8dfdd270e5a37006b0 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Tue, 8 Nov 2022 14:23:22 +0100 Subject: [PATCH] Avoid String allocations with Assert.notNull() --- .../cache/jcache/interceptor/AbstractCacheOperationTests.java | 4 ++-- .../interceptor/AnnotationCacheOperationSourceTests.java | 4 ++-- .../springframework/core/test/tools/DynamicClassLoader.java | 2 +- .../test/context/aot/TestContextAotGenerator.java | 2 +- .../context/cache/DefaultCacheAwareContextLoaderDelegate.java | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/AbstractCacheOperationTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/AbstractCacheOperationTests.java index c7a0cfcd477..18ca02df38f 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/AbstractCacheOperationTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/AbstractCacheOperationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 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. @@ -62,7 +62,7 @@ public abstract class AbstractCacheOperationTests> Class targetType, String methodName, Class... parameterTypes) { Method method = ReflectionUtils.findMethod(targetType, methodName, parameterTypes); - Assert.notNull(method, "requested method '" + methodName + "'does not exist"); + Assert.notNull(method, () -> "requested method '" + methodName + "'does not exist"); A cacheAnnotation = method.getAnnotation(annotationType); return new DefaultCacheMethodDetails<>(method, cacheAnnotation, getCacheName(cacheAnnotation)); } diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/AnnotationCacheOperationSourceTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/AnnotationCacheOperationSourceTests.java index bcc9276243b..693db9b13c7 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/AnnotationCacheOperationSourceTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/AnnotationCacheOperationSourceTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 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. @@ -202,7 +202,7 @@ public class AnnotationCacheOperationSourceTests extends AbstractJCacheTests { private JCacheOperation getCacheOperation(Class targetType, String methodName, Class... parameterTypes) { Method method = ReflectionUtils.findMethod(targetType, methodName, parameterTypes); - Assert.notNull(method, "requested method '" + methodName + "'does not exist"); + Assert.notNull(method, () -> "requested method '" + methodName + "'does not exist"); return source.getCacheOperation(method, targetType); } diff --git a/spring-core-test/src/main/java/org/springframework/core/test/tools/DynamicClassLoader.java b/spring-core-test/src/main/java/org/springframework/core/test/tools/DynamicClassLoader.java index e5ff9a9411e..6412cd0b060 100644 --- a/spring-core-test/src/main/java/org/springframework/core/test/tools/DynamicClassLoader.java +++ b/spring-core-test/src/main/java/org/springframework/core/test/tools/DynamicClassLoader.java @@ -155,7 +155,7 @@ public class DynamicClassLoader extends ClassLoader { private static Method lookupMethod(Class target, String name, Class... parameterTypes) { Method method = ReflectionUtils.findMethod(target, name, parameterTypes); - Assert.notNull(method, "Expected method '" + name + "' on '" + target.getName()); + Assert.notNull(method, () -> "Could not find method '%s' on '%s'".formatted(name, target.getName())); return method; } diff --git a/spring-test/src/main/java/org/springframework/test/context/aot/TestContextAotGenerator.java b/spring-test/src/main/java/org/springframework/test/context/aot/TestContextAotGenerator.java index fac0cfc6df2..a6522477be0 100644 --- a/spring-test/src/main/java/org/springframework/test/context/aot/TestContextAotGenerator.java +++ b/spring-test/src/main/java/org/springframework/test/context/aot/TestContextAotGenerator.java @@ -205,7 +205,7 @@ public class TestContextAotGenerator { Class testClass = mergedConfig.getTestClass(); ContextLoader contextLoader = mergedConfig.getContextLoader(); - Assert.notNull(contextLoader, """ + Assert.notNull(contextLoader, () -> """ Cannot load an ApplicationContext with a NULL 'contextLoader'. \ Consider annotating test class [%s] with @ContextConfiguration or \ @ContextHierarchy.""".formatted(testClass.getName())); diff --git a/spring-test/src/main/java/org/springframework/test/context/cache/DefaultCacheAwareContextLoaderDelegate.java b/spring-test/src/main/java/org/springframework/test/context/cache/DefaultCacheAwareContextLoaderDelegate.java index c41719720f9..608b78b30f6 100644 --- a/spring-test/src/main/java/org/springframework/test/context/cache/DefaultCacheAwareContextLoaderDelegate.java +++ b/spring-test/src/main/java/org/springframework/test/context/cache/DefaultCacheAwareContextLoaderDelegate.java @@ -184,7 +184,7 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext } else { String[] locations = mergedContextConfiguration.getLocations(); - Assert.notNull(locations, """ + Assert.notNull(locations, () -> """ Cannot load an ApplicationContext with a NULL 'locations' array. \ Consider annotating test class [%s] with @ContextConfiguration or \ @ContextHierarchy.""".formatted(mergedContextConfiguration.getTestClass().getName())); @@ -224,7 +224,7 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext private ContextLoader getContextLoader(MergedContextConfiguration mergedConfig) { ContextLoader contextLoader = mergedConfig.getContextLoader(); - Assert.notNull(contextLoader, """ + Assert.notNull(contextLoader, () -> """ Cannot load an ApplicationContext with a NULL 'contextLoader'. \ Consider annotating test class [%s] with @ContextConfiguration or \ @ContextHierarchy.""".formatted(mergedConfig.getTestClass().getName()));