Skip to content

Commit 46fc5c0

Browse files
committed
Work around Groovy compiler bug that can name classes incorrectly
If a source URL is added to a CompilationUnit and that source URL does not contain any slashes, the resulting ClassNode in the AST will be incorrectly named. For example, a URL of 'file:foo.groovy' will produce a ClassNode named 'file:foo'. The expected name is 'foo'. This commit works around this problem by adding any URL sources with a file protocol to the compilation unit as File instances. Any URL sources that do not have a file protocol continue to be added as URL instances. Such URLs are still prone to this bug should we be dealing with one that contains no slashes. A fix for the underlying Groovy bug will address this possibility. Fixes #594
1 parent 8491f8e commit 46fc5c0

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompiler.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import groovy.lang.GroovyClassLoader.ClassCollector;
2121
import groovy.lang.GroovyCodeSource;
2222

23+
import java.io.File;
2324
import java.io.IOException;
2425
import java.lang.reflect.Field;
2526
import java.net.URL;
@@ -184,7 +185,13 @@ public Class<?>[] compile(String... sources) throws CompilationFailedException,
184185
for (String source : sources) {
185186
List<String> paths = ResourceUtils.getUrls(source, this.loader);
186187
for (String path : paths) {
187-
compilationUnit.addSource(new URL(path));
188+
URL url = new URL(path);
189+
if ("file".equals(url.getProtocol())) {
190+
compilationUnit.addSource(new File(url.getFile()));
191+
}
192+
else {
193+
compilationUnit.addSource(url);
194+
}
188195
}
189196
}
190197

0 commit comments

Comments
 (0)