api / org.gradle.api.model / ObjectFactory

ObjectFactory

@Incubating interface ObjectFactory

A factory for creating various kinds of model objects.

An instance of the factory can be injected into a task or plugin by annotating a public constructor or method with javax.inject.Inject. It is also available via org.gradle.api.Project#getObjects().

Since
4.0

Functions

listProperty

abstract fun <T : Any> listProperty(elementType: Class<T>): ListProperty<T>

Creates a ListProperty implementation to hold a List of the given element type. The property with have an empty list as its initial value.

The implementation will return immutable List values from its query methods.

named

abstract fun <T : Named> named(type: Class<T>, name: String): T

Creates a simple immutable Named object of the given type and name.

The given type can be an interface that extends Named or 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.

newInstance

abstract fun <T : Any> newInstance(type: Class<out T>, vararg parameters: Any): T

Create a new instance of T, using parameters as the construction parameters.

The type must be a non-abstract class.

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 org.gradle.api.Project object.

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:

  • ObjectFactory.
  • org.gradle.api.file.ProjectLayout.
  • org.gradle.api.provider.ProviderFactory.

property

abstract fun <T : Any> property(valueType: Class<T>): Property<T>

Creates a Property implementation to hold values of the given type.

The property will have a value equal to the default value of that type as defined by the Java language specification. Please see Oracle's Java manual for more information.

Any other data type than the standard Java data types returns a property with no value defined.

setProperty

abstract fun <T : Any> setProperty(elementType: Class<T>): SetProperty<T>

Creates a SetProperty implementation to hold a Set of the given element type. The property with have an empty set as its initial value.

The implementation will return immutable Set values from its query methods.

Extension Functions

listProperty

fun <T> ObjectFactory.listProperty(): ListProperty<T>

Creates a ListProperty that holds values of the given type T.

property

fun <T> ObjectFactory.property(): Property<T>

Creates a Property that holds values of the given type T.