Improve names of classes generated by the SpEL compiler

Prior to this commit, the SpEL compiler generated classes in a package
named "spel" with names following the pattern "Ex#", where # was an
index starting with 2.

This resulted in class names such as:

- spel.Ex2
- spel.Ex3

This commit improves the names of classes created by the SpEL compiler
by generating classes in a package named
"org.springframework.expression.spel.generated" with names following
the pattern "CompiledExpression#####", where ##### is a 0-padded
counter starting with 00001.

This results in class names such as:

- org.springframework.expression.spel.generated.CompiledExpression00001
- org.springframework.expression.spel.generated.CompiledExpression00002

This commit also moves the saveGeneratedClassFile() method from
SpelCompilationCoverageTests to SpelCompiler and enhances it to:

- Save classes in a "build/generated-classes" directory.
- Convert package names to directories.
- Create missing parent directories.
- Use logging instead of System.out.println().

Running a test with saveGeneratedClassFile() enabled now logs something
similar to the following.

DEBUG o.s.e.s.s.SpelCompiler - Saving compiled SpEL expression [(#root.empty ? 0 : #root.size)] to [/Users/<username>/spring-framework/spring-expression/build/generated-classes/org/springframework/expression/spel/generated/CompiledExpression00001.class]

Closes gh-32497
This commit is contained in:
Sam Brannen
2024-03-20 12:34:01 +01:00
parent 2f070e59e6
commit 7f40b49f4d
2 changed files with 29 additions and 25 deletions

View File

@@ -6724,20 +6724,4 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
}
}
// NOTE: saveGeneratedClassFile() can be copied to SpelCompiler and uncommented
// 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) {
// 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) {
// throw new UncheckedIOException(ex);
// }
// }
}