Before moving onto looking at providing runtime configuration based on conventions you first need to understand how to evaluated those conventions from a plug-in. Essentially every plugin has an implicit
application
variable which is an instance of the api:org.codehaus.groovy.grails.commons.GrailsApplication interface.
The
GrailsApplication
interface provides methods to evaluate the conventions within the project and internally stores references to all classes within a GrailsApplication using the api:org.codehaus.groovy.grails.commons.GrailsClass interface.
A
GrailsClass
represents a physical Grails resources such as a controller or a tag library. For example to get all
GrailsClass
instances you can do:
application.allClasses.each { println it.name }
There are a few "magic" properties that the
GrailsApplication
instance possesses that allow you to narrow the type of artefact you are interested in. For example if you only want to controllers you can do:
application.controllerClasses.each { println it.name }
The dynamic method conventions are as follows:
*Classes
- Retrieves all the classes for a particular artefact name. Example application.controllerClasses
.
get*Class
- Retrieves a named class for a particular artefact. Example application.getControllerClass("ExampleController")
is*Class
- Returns true if the given class is of the given artefact type. Example application.isControllerClass(ExampleController.class)
The
GrailsClass
interface itself provides a number of useful methods that allow you to further evaluate and work with the conventions. These include:
getPropertyValue
- Gets the initial value of the given property on the class
hasProperty
- Returns true if the class has the specified property
newInstance
- Creates a new instance of this class.
getName
- Returns the logical name of the class in the application without the trailing convention part if applicable
getShortName
- Returns the short name of the class without package prefix
getFullName
- Returns the full name of the class in the application with the trailing convention part and with the package name
getPropertyName
- Returns the name of the class as a property name
getLogicalPropertyName
- Returns the logical property name of the class in the application without the trailing convention part if applicable
getNaturalName
- Returns the name of the property in natural terms (eg. 'lastName' becomes 'Last Name')
getPackageName
- Returns the package name
For a full reference refer to the api:org.codehaus.groovy.grails.commons.GrailsClass.