5.8 Artifact API - Reference Documentation
Authors: Andres Almiray
Version: 1.2.0
5.8 Artifact API
The Artifact API provides introspection capabilities on the conventions set on each artifact type. The following sections explain further what you can do with this API.5.8.1 Evaluating Conventions
Every Griffon application exposes all information about its artifacts and addons via a pair of helper classesAddonManager
- used for all installed addonsArtifactManager
- used for all remaining artifacts
ArtifactManager
TheArtifactManager
class provides methods to evaluate the conventions within the project and internally stores references to all classes within a GriffonApplication using subclasses of GriffonClass class.A GriffonClass
represents a physical Griffon resources such as a controller or a service. For example to get all GriffonClass
instances you can call:app.artifactManager.allClasses.each { println it.name }
ArtifactManager
instance possesses that allow you to narrow the type of artifact you are interested in. For example if you only need to deal with controllers you can do:app.artifactManager.controllerClasses.each { println it.name }
get*Classes
- Retrieves all the classes for a particular artifact type. Exampleapp.artifactManager.getControllerClasses()
.*Classes
- Retrieves all the classes for a particular artifact type. Exampleapp.artifactManager.controllerClasses
.is*Class
- Returns true if the given class is of the given artifact type. Exampleapp.artifactManager.isControllerClass(ExampleController)
GriffonClass
interface itself provides a number of useful methods that allow you to further evaluate and work with the conventions. These include:
newInstance
- Creates a new instance of the enclosed class.getName
- Returns the logical name of the class in the application without the trailing convention part if applicablegetClazz
- Returns the artifact classgetType
- Returns the type of the artifact, i.e "controller"getTrailing
- Returns the suffix (if any) of the artifact, i.e "Controller"
AddonManager
TheAddonManager
class is responsible for holding references to all addons (which are of type griffon.core.GriffonAddon), as well as providing metainformation on each addon via an addon descriptor. The latter can be used to know at runtime the name and version of a particular addon, useful for building a dynamic About dialog for example.All addons have the same behavior which is explained in detail in the Addons section.
5.8.2 Adding Dynamic Methods at Runtime
For Griffon managed classes like controllers, models and so forth you can add methods, constructors etc. using the ExpandoMetaClass mechanism by accessing each controller's MetaClass:class ExampleAddon {
def addonPostInit(app) {
app.artifactManager.controllerClasses.each { controllerClass ->
controllerClass.metaClass.myNewMethod = {-> println "hello world" }
}
}
}
app.artifactManager
object to get a reference to all of the controller classes' MetaClass instances and then add a new method called myNewMethod
to each controller.
Alternatively, if you know before hand the class you wish add a method to you can simple reference that classes metaClass
property:class ExampleAddon { def addonPostInit(app) { String.metaClass.swapCase = {-> def sb = new StringBuffer() delegate.each { sb << (Character.isUpperCase(it as char) ? Character.toLowerCase(it as char) : Character.toUpperCase(it as char)) } sb.toString() } assert "UpAndDown" == "uPaNDdOWN".swapCase() } }
swapCase
to java.lang.String
directly by accessing its metaClass
.
5.8.3 Artifact Types
All Griffon artifacts share common behavior. This behavior is captured by an interface named griffon.core.GriffonArtifact. Additional interfaces with more explicit behavior exist per each artifact type. The following is a list of the basic types and their corresponding interface- Model -> griffon.core.GriffonModel
- View -> griffon.core.GriffonView
- Controller -> griffon.core.GriffonController
- Service -> griffon.core.GriffonService
AST injection is always enabled unless you disable it as explained in the Disable AST Injection section.Additionally to each artifact type you will find a companion GriffonClass that is specialized for each type. These specialized classes can be used to discover metadata about a particular artifact. The following is a list of the companion GriffonClass for each of the basic artifacts found in core
- Model -> griffon.core.GriffonModelClass
- View -> griffon.core.GriffonViewClass
- Controller -> griffon.core.GriffonControllerClass
- Service -> griffon.core.GriffonServiceClass
ArtifactManager
.