Interface ObjectFactory
An instance of the factory can be injected into a task, plugin or other object by annotating a public constructor or property getter method with javax.inject.Inject.
It is also available via Project.getObjects().
- Since:
- 4.0
-
Method Summary
Modifier and TypeMethodDescriptionCreates a newDependencyCollectorused for declaring dependencies.Creates a newDirectoryPropertythat uses the project directory to resolve relative paths, if required.<T> NamedDomainObjectContainer<T> domainObjectContainer(Class<T> elementType) Creates a newNamedDomainObjectContainerfor managing named objects of the specified type.<T> NamedDomainObjectContainer<T> domainObjectContainer(Class<T> elementType, NamedDomainObjectFactory<T> factory) Creates a newNamedDomainObjectContainerfor managing named objects of the specified type.<T> DomainObjectSet<T> domainObjectSet(Class<T> elementType) Creates a newDomainObjectSetfor managing objects of the specified type.Creates a newConfigurableFileCollection.Creates a newRegularFilePropertythat uses the project directory to resolve relative paths, if required.fileTree()Creates a newConfigurableFileTree.<T> ListProperty<T> listProperty(Class<T> elementType) <K,V> MapProperty <K, V> mapProperty(Class<K> keyType, Class<V> valueType) <T extends Named>
TCreates a simple immutableNamedobject of the given type and name.<T> NamedDomainObjectList<T> namedDomainObjectList(Class<T> elementType) Creates a newNamedDomainObjectListfor managing named objects of the specified type.<T> NamedDomainObjectSet<T> namedDomainObjectSet(Class<T> elementType) Creates a newNamedDomainObjectSetfor managing named objects of the specified type.<T> TnewInstance(Class<? extends T> type, Object... parameters) Create a new instance of T, usingparametersas the construction parameters.polymorphicDomainObjectContainer(Class<T> elementType) Creates a newExtensiblePolymorphicDomainObjectContainerfor managing named objects of the specified type.<T> Property<T> Creates aPropertyimplementation to hold values of the given type.<T> SetProperty<T> setProperty(Class<T> elementType) sourceDirectorySet(String name, String displayName) Creates aSourceDirectorySet.
-
Method Details
-
named
Creates a simple immutableNamedobject of the given type and name.The given type can be an interface that extends
Namedor an abstract class that 'implements'Named. An abstract class, if provided:- Must provide a zero-args constructor that is not private.
- Must not define or inherit any instance fields.
- Should not provide an implementation for
Named.getName()and should define this method as abstract. Any implementation will be overridden. - Must not define or inherit any other abstract methods.
An interface, if provided, must not define or inherit any other methods.
Objects created using this method are not decorated or extensible.
- Throws:
ObjectInstantiationException- On failure to create the new instance.- Since:
- 4.0
-
newInstance
<T> T newInstance(Class<? extends T> type, Object... parameters) throws ObjectInstantiationException Create a new instance of T, usingparametersas the construction parameters.The type must be non-final, and can be a class, abstract class or interface.
Objects created using this method are decorated and extensible, meaning that they have DSL support mixed in and can be extended using the `extensions` property, similar to the
Projectobject.An @Inject annotation is required on any constructor that accepts parameters because JSR-330 semantics for dependency injection are used. In addition to those parameters provided as an argument to this method, the following services are also available for injection:
- Throws:
ObjectInstantiationException- On failure to create the new instance.- Since:
- 4.2
-
sourceDirectorySet
Creates aSourceDirectorySet.- Parameters:
name- A short name for the set.displayName- A human consumable display name for the set.- Since:
- 5.0
-
fileCollection
ConfigurableFileCollection fileCollection()Creates a newConfigurableFileCollection. The collection is initially empty.- Since:
- 5.3
-
fileTree
ConfigurableFileTree fileTree()Creates a newConfigurableFileTree. The tree will have no base dir specified.- Since:
- 6.0
-
domainObjectContainer
Creates a new
NamedDomainObjectContainerfor managing named objects of the specified type.The specified element type must have a public constructor which takes the name as a String parameter. The type must be non-final and a class or abstract class.
Interfaces are supported if they declare a read-only
nameproperty of type String, and are otherwise empty or consist entirely of managed properties.All objects MUST expose their name as a bean property called "name". The name must be constant for the life of the object.
The objects created by the container are decorated and extensible, and have services available for injection. See
newInstance(Class, Object...)for more details.- Type Parameters:
T- The type of objects for the container to contain.- Parameters:
elementType- The type of objects for the container to contain.- Returns:
- The container. Never returns null.
- Since:
- 5.5
-
domainObjectContainer
<T> NamedDomainObjectContainer<T> domainObjectContainer(Class<T> elementType, NamedDomainObjectFactory<T> factory) Creates a new
NamedDomainObjectContainerfor managing named objects of the specified type. The given factory is used to create object instances.All objects MUST expose their name as a bean property named "name". The name must be constant for the life of the object.
- Type Parameters:
T- The type of objects for the container to contain.- Parameters:
elementType- The type of objects for the container to contain.factory- The factory to use to create object instances.- Returns:
- The container. Never returns null.
- Since:
- 5.5
-
polymorphicDomainObjectContainer
<T> ExtensiblePolymorphicDomainObjectContainer<T> polymorphicDomainObjectContainer(Class<T> elementType) Creates a new
ExtensiblePolymorphicDomainObjectContainerfor managing named objects of the specified type.The returned container will not have any factories or bindings registered.
- Type Parameters:
T- The type of objects for the container to contain.- Parameters:
elementType- The type of objects for the container to contain.- Returns:
- The container.
- Since:
- 6.1
-
domainObjectSet
Creates a newDomainObjectSetfor managing objects of the specified type.- Type Parameters:
T- The type of objects for the domain object set to contain.- Parameters:
elementType- The type of objects for the domain object set to contain.- Returns:
- The domain object set. Never returns null.
- Since:
- 5.5
-
namedDomainObjectSet
Creates a newNamedDomainObjectSetfor managing named objects of the specified type.All objects MUST expose their name as a bean property called "name". The name must be constant for the life of the object.
- Type Parameters:
T- The type of objects for the domain object set to contain.- Parameters:
elementType- The type of objects for the domain object set to contain.- Returns:
- The domain object set.
- Since:
- 6.1
-
namedDomainObjectList
Creates a newNamedDomainObjectListfor managing named objects of the specified type.All objects MUST expose their name as a bean property called "name". The name must be constant for the life of the object.
- Type Parameters:
T- The type of objects for the domain object set to contain.- Parameters:
elementType- The type of objects for the domain object set to contain.- Returns:
- The domain object list.
- Since:
- 6.1
-
property
Creates aPropertyimplementation to hold values of the given type. The property has no initial value.For certain types, there are more specialized property factory methods available:
- For
Listproperties, you should uselistProperty(Class). - For
Setproperties, you should usesetProperty(Class). - For
Mapproperties, you should usemapProperty(Class, Class). - For
Directoryproperties, you should usedirectoryProperty(). - For
RegularFileproperties, you should usefileProperty(). - For
Fileproperties, you should usefileProperty()for regular files anddirectoryProperty()for directories.
- Parameters:
valueType- The type of the property.- Returns:
- The property. Never returns null.
- Since:
- 4.3
- For
-
listProperty
Creates aListPropertyimplementation to hold aListof the given element typeT. The property has an empty list as its initial value.The implementation will return immutable
Listvalues from its query methods.- Type Parameters:
T- The type of element.- Parameters:
elementType- The type of element.- Returns:
- The property. Never returns null.
- Since:
- 4.3
-
setProperty
Creates aSetPropertyimplementation to hold aSetof the given element typeT. The property has an empty set as its initial value.The implementation will return immutable
Setvalues from its query methods.- Type Parameters:
T- The type of element.- Parameters:
elementType- The type of element.- Returns:
- The property. Never returns null.
- Since:
- 4.5
-
mapProperty
Creates aMapPropertyimplementation to hold aMapof the given key typeKand value typeV. The property has an empty map as its initial value.The implementation will return immutable
Mapvalues from its query methods.- Type Parameters:
K- the type of key.V- the type of value.- Parameters:
keyType- the type of key.valueType- the type of value.- Returns:
- the property. Never returns null.
- Since:
- 5.1
-
directoryProperty
DirectoryProperty directoryProperty()Creates a newDirectoryPropertythat uses the project directory to resolve relative paths, if required. The property has no initial value.- Since:
- 5.0
-
fileProperty
RegularFileProperty fileProperty()Creates a newRegularFilePropertythat uses the project directory to resolve relative paths, if required. The property has no initial value.- Since:
- 5.0
-
dependencyCollector
Creates a newDependencyCollectorused for declaring dependencies. Avoid this method if possible and use managed object instantiation instead.- Since:
- 8.6
-