Skip to content

Commit 86518a2

Browse files
committed
populate main class cache per path. Fixes #9845
1 parent b45aac3 commit 86518a2

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

grails-bootstrap/src/main/groovy/org/grails/io/support/MainClassFinder.groovy

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import groovyjarjarasm.asm.Opcodes
1010
import groovyjarjarasm.asm.Type
1111

1212
import java.nio.file.Paths
13+
import java.util.concurrent.ConcurrentHashMap
1314

1415
/**
1516
* @author Graeme Rocher
@@ -24,7 +25,7 @@ class MainClassFinder {
2425

2526
private static final String MAIN_METHOD_NAME = "main"
2627

27-
static String mainClassName = null
28+
static final Map<String, String> mainClasses = new ConcurrentHashMap<>()
2829

2930
/**
3031
* Searches for the main class relative to the give path that is within the project tree
@@ -33,7 +34,11 @@ class MainClassFinder {
3334
* @return The name of the main class
3435
*/
3536
static String searchMainClass(URI path) {
36-
if(mainClassName) return mainClassName
37+
38+
def pathStr = path.toString()
39+
if(mainClasses.containsKey(pathStr)) {
40+
return mainClasses.get(pathStr)
41+
}
3742

3843
try {
3944
File file = path ? Paths.get(path).toFile() : null
@@ -60,6 +65,9 @@ class MainClassFinder {
6065
mainClass = findMainClass(dir)
6166
if (mainClass) break
6267
}
68+
if(mainClass != null) {
69+
mainClasses.put(pathStr, mainClass)
70+
}
6371
return mainClass
6472
} catch (Throwable e) {
6573
return null
@@ -83,18 +91,24 @@ class MainClassFinder {
8391
}
8492

8593
static String findMainClass(File rootFolder = BuildSettings.CLASSES_DIR) {
86-
if(mainClassName) return mainClassName
8794
if( rootFolder == null) {
8895
// try current directory
8996
rootFolder = new File("build/classes/main")
9097
}
98+
99+
100+
def rootFolderPath = rootFolder.canonicalPath
101+
if(mainClasses.containsKey(rootFolderPath)) {
102+
return mainClasses.get(rootFolderPath)
103+
}
104+
91105
if (!rootFolder.exists()) {
92106
return null // nothing to do
93107
}
94108
if (!rootFolder.isDirectory()) {
95109
throw new IllegalArgumentException("Invalid root folder '$rootFolder'")
96110
}
97-
String prefix = "$rootFolder.absolutePath/"
111+
String prefix = "${rootFolderPath}/"
98112
def stack = new ArrayDeque<File>()
99113
stack.push rootFolder
100114

@@ -106,7 +120,8 @@ class MainClassFinder {
106120
def classReader = new ClassReader(inputStream)
107121

108122
if (isMainClass(classReader)) {
109-
mainClassName = classReader.getClassName().replace('/', '.').replace('\\', '.')
123+
def mainClassName = classReader.getClassName().replace('/', '.').replace('\\', '.')
124+
mainClasses.put(rootFolderPath, mainClassName)
110125
return mainClassName
111126
}
112127
} finally {

0 commit comments

Comments
 (0)