@@ -230,7 +230,7 @@ class IOUtils extends SpringIOUtils {
230230
231231
232232 @Memoized
233- public static File findApplicationDirectoryFile () {
233+ static File findApplicationDirectoryFile () {
234234 def directory = findApplicationDirectory()
235235 if (directory) {
236236 def f = new File (directory)
@@ -248,7 +248,7 @@ class IOUtils extends SpringIOUtils {
248248 * @param targetClass The target class
249249 * @return The application directory or null if it can't be found
250250 */
251- public static File findApplicationDirectoryFile (Class targetClass ) {
251+ static File findApplicationDirectoryFile (Class targetClass ) {
252252
253253 def rootResource = findRootResource(targetClass)
254254 if (rootResource != null ) {
@@ -267,8 +267,46 @@ class IOUtils extends SpringIOUtils {
267267 return null
268268 }
269269
270+ /**
271+ * Finds a source file for the given class name
272+ *
273+ * @param className The class name
274+ * @return The source file
275+ */
276+ @Memoized
277+ static File findSourceFile (String className ) {
278+ File applicationDir = BuildSettings . BASE_DIR
279+ File file = null
280+ if (applicationDir != null ) {
281+ String fileName = className. replace(' .' as char , File . separatorChar) + ' .groovy'
282+ List<File > allFiles = [ new File (applicationDir, " src/main/groovy" ) ]
283+ File [] files = new File (applicationDir, " grails-app" ). listFiles(new FileFilter () {
284+ @Override
285+ boolean accept (File f ) {
286+ return f. isDirectory() && ! f. isHidden() && ! f. name. startsWith(' .' )
287+ }
288+ })
289+ if (files != null ) {
290+ allFiles. addAll( Arrays . asList(files) )
291+ }
292+ for (File dir in allFiles) {
293+ File possibleFile = new File (dir, fileName)
294+ if (possibleFile. exists()) {
295+ file = possibleFile
296+ break
297+ }
298+ }
299+
300+ }
301+ return file
302+ }
303+
304+ /**
305+ * Finds the directory where the Application class is contained
306+ * @return The application directory
307+ */
270308 @Memoized
271- public static String findApplicationDirectory () {
309+ static String findApplicationDirectory () {
272310 if (applicationDirectory) {
273311 return applicationDirectory
274312 }
@@ -296,10 +334,10 @@ class IOUtils extends SpringIOUtils {
296334 final Class<?> mainClass = Thread . currentThread(). contextClassLoader. loadClass(mainClassName)
297335 final URL classResource = mainClass ? findClassResource(mainClass) : null
298336 if (classResource) {
299- def file = new UrlResource (classResource). getFile()
300- def path = file. canonicalPath
337+ File file = new UrlResource (classResource). getFile()
338+ String path = file. canonicalPath
301339
302- def buildClassespath = BuildSettings . BUILD_CLASSES_PATH . replace(' /' , File . separator)
340+ String buildClassespath = BuildSettings . BUILD_CLASSES_PATH . replace(' /' , File . separator)
303341 if (path. contains(buildClassespath)) {
304342 location = path. substring(0 , path. indexOf(buildClassespath) - 1 )
305343 }
@@ -312,6 +350,6 @@ class IOUtils extends SpringIOUtils {
312350 // ignore
313351 }
314352 applicationDirectory = location
315- return location;
353+ return location
316354 }
317355}
0 commit comments