diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/MethodReference.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/MethodReference.java index 0e7af3e02eb..3b60cff3ae3 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/MethodReference.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/MethodReference.java @@ -314,20 +314,22 @@ public class MethodReference extends SpelNodeImpl { boolean isStaticMethod = Modifier.isStatic(method.getModifiers()); String descriptor = cf.lastDescriptor(); - Label skipIfNull = null; if (descriptor == null && !isStaticMethod) { // Nothing on the stack but something is needed cf.loadTarget(mv); } - if ((descriptor != null || !isStaticMethod) && this.nullSafe) { - mv.visitInsn(DUP); + + Label skipIfNull = null; + if (this.nullSafe && (descriptor != null || !isStaticMethod)) { skipIfNull = new Label(); Label continueLabel = new Label(); + mv.visitInsn(DUP); mv.visitJumpInsn(IFNONNULL, continueLabel); CodeFlow.insertCheckCast(mv, this.exitTypeDescriptor); mv.visitJumpInsn(GOTO, skipIfNull); mv.visitLabel(continueLabel); } + if (descriptor != null && isStaticMethod) { // Something on the stack when nothing is needed mv.visitInsn(POP); diff --git a/spring-expression/src/test/java/org/springframework/expression/spel/SpelCompilationCoverageTests.java b/spring-expression/src/test/java/org/springframework/expression/spel/SpelCompilationCoverageTests.java index ed575bfec9f..d20cf8097ac 100644 --- a/spring-expression/src/test/java/org/springframework/expression/spel/SpelCompilationCoverageTests.java +++ b/spring-expression/src/test/java/org/springframework/expression/spel/SpelCompilationCoverageTests.java @@ -6429,10 +6429,11 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests { // at the end of createExpressionClass(SpelNodeImpl) in order to review generated // byte code for debugging purposes. // - // private static void saveGeneratedClassFile(String stringAST, String className, byte[] data) { - // Path path = Path.of("build", StringUtils.replace(className, "/", ".") + ".class"); - // System.out.println("Writing compiled SpEL expression [%s] to [%s]".formatted(stringAST, path.toAbsolutePath())); + // private static void saveGeneratedClassFile(String stringAST, String className, byte[] data) { // try { + // Path path = Path.of("build", StringUtils.replace(className, "/", ".") + ".class"); + // Files.deleteIfExists(path); + // System.out.println("Writing compiled SpEL expression [%s] to [%s]".formatted(stringAST, path.toAbsolutePath())); // Files.copy(new ByteArrayInputStream(data), path); // } // catch (IOException ex) {