Skip to content

Commit

Permalink
Automatically discover and populate package names to scan at compile …
Browse files Browse the repository at this point in the history
…time
  • Loading branch information
graemerocher committed Dec 15, 2014
1 parent 6f13c30 commit fad4047
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 29 deletions.

This file was deleted.

63 changes: 63 additions & 0 deletions grails-bootstrap/src/main/groovy/grails/io/ResourceUtils.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2014 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package grails.io

import grails.util.BuildSettings
import groovy.transform.CompileStatic
import org.grails.io.support.GrailsResourceUtils



/**
* Utility methods for interacting with resources
*
* @author Graeme Rocher
* @since 3.0
*/
@CompileStatic
class ResourceUtils extends GrailsResourceUtils {

/**
* Obtains the package names for the project. Works at development time only, do not use at runtime.
*
* @return The project package names
*/
static Iterable<String> getProjectPackageNames() {
Set<String> packageNames = []
def baseDir = BuildSettings.BASE_DIR
if(baseDir) {
def rootDir = new File(baseDir, "grails-app")
rootDir.eachDir { File dir ->
def dirName = dir.name
if (!dir.hidden && !dirName.startsWith('.') && !['conf', 'i18n', 'assets', 'views'].contains(dirName)) {
populatePackages(dir, packageNames, "")
}
}
}
return packageNames
}

protected static populatePackages(File rootDir, Collection<String> packageNames, String prefix) {
rootDir.eachDir { File dir ->
def dirName = dir.name
if (!dir.hidden && !dirName.startsWith('.')) {
packageNames << "${prefix}${dirName}".toString()

populatePackages(dir, packageNames, "${dirName}.")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ class GrailsAutoConfiguration implements GrailsApplicationLifeCycle, ResourceLoa
*/
Collection<Class> classes() {
def readerFactory = new CachingMetadataReaderFactory(resourcePatternResolver)
def packages = packages()
def packages = packageNames().unique()
Collection<Class> classes = [] as Set
for (pkg in packages) {
if(pkg == null) continue
String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
ClassUtils.convertClassNameToResourcePath(pkg.name) + Settings.CLASS_RESOURCE_PATTERN;
ClassUtils.convertClassNameToResourcePath(pkg) + Settings.CLASS_RESOURCE_PATTERN;

classes.addAll scanUsingPattern(pattern, readerFactory)
}
Expand Down Expand Up @@ -74,6 +74,13 @@ class GrailsAutoConfiguration implements GrailsApplicationLifeCycle, ResourceLoa
[ getClass().package ]
}

/**
* @return The package names to scan. Delegates to {@link #packages()} by default
*/
Collection<String> packageNames() {
packages().collect { Package p -> p.name }
}

@Override
void setResourceLoader(ResourceLoader resourceLoader) {
this.resourcePatternResolver = new PathMatchingResourcePatternResolver(resourceLoader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@ package org.grails.compiler.injection
import grails.compiler.ast.AstTransformer
import grails.compiler.ast.GrailsArtefactClassInjector
import grails.dev.Support
import grails.io.ResourceUtils
import groovy.transform.CompileDynamic
import groovy.transform.CompileStatic
import org.codehaus.groovy.ast.AnnotationNode
import org.codehaus.groovy.ast.ClassHelper
import org.codehaus.groovy.ast.ClassNode
import org.codehaus.groovy.ast.MethodNode
import org.codehaus.groovy.ast.expr.ClassExpression
import org.codehaus.groovy.ast.expr.ConstantExpression
import org.codehaus.groovy.ast.expr.Expression
import org.codehaus.groovy.ast.expr.ListExpression
import org.codehaus.groovy.ast.expr.MethodCallExpression
import org.codehaus.groovy.ast.stmt.BlockStatement
import org.codehaus.groovy.ast.stmt.ExpressionStatement
import org.codehaus.groovy.ast.stmt.ReturnStatement
import org.codehaus.groovy.ast.stmt.Statement
import org.codehaus.groovy.classgen.GeneratorContext
import org.codehaus.groovy.control.SourceUnit
Expand All @@ -36,6 +41,8 @@ import org.grails.io.support.GrailsResourceUtils
import org.grails.io.support.UrlResource
import org.springframework.util.ClassUtils

import java.lang.reflect.Modifier

/**
* Injector for the 'Application' class
*
Expand Down Expand Up @@ -78,6 +85,17 @@ class ApplicationClassInjector implements GrailsArtefactClassInjector {
List<Statement> statements = [ methodCallStatement ]
classNode.addStaticInitializerStatements(statements, false)

def packageNamesMethod = classNode.getMethod('packageNames', GrailsASTUtils.ZERO_PARAMETERS)

if(packageNamesMethod == null || packageNamesMethod.declaringClass != classNode) {
def collectionClassNode = GrailsASTUtils.replaceGenericsPlaceholders(ClassHelper.make(Collection), [E: ClassHelper.make(String)])

def packageNamesBody = new BlockStatement()
def packageNames = ResourceUtils.projectPackageNames.collect() { String str -> new ConstantExpression(str) }
packageNamesBody.addStatement(new ReturnStatement(new ExpressionStatement(new ListExpression(packageNames.toList()))))
classNode.addMethod("packageNames", Modifier.PUBLIC, collectionClassNode, ZERO_PARAMETERS, null, packageNamesBody)
}

def classLoader = getClass().classLoader
if(ClassUtils.isPresent('javax.servlet.ServletContext', classLoader)) {
GrailsASTUtils.addAnnotationOrGetExisting(classNode, ClassHelper.make(classLoader.loadClass('org.springframework.web.servlet.config.annotation.EnableWebMvc')))
Expand Down

0 comments on commit fad4047

Please sign in to comment.