classafBeanUtils::BeanFactory
sys::Obj afBeanUtils::BeanFactory
@
Js@
Deprecated { msg="Use BeanBuilder instead" }
Creates Lists, Maps and other Objects, optionally setting field values. Ctors may be of any scope: public
, protected
, internal
and even private
.
Fields are either set post construction or via an it-block ctor argument (which must be the last method parameter).
const
types must provide an it-block ctor if fields are to be set.
- add
Adds a ctor argument.
- create
Obj create(Method? ctor := null)
Creates an instance of the object, optionally using the given ctor.
If no ctor is given, a suitable one is picked that matches the arguments accumulated by the factory.
- defaultValue
static Obj? defaultValue(Type type, Bool force := false)
Returns a default value for the given type. Use as a replacement for Type.make().
Returned objects are not guaranteed to be immutable. Call
toImmutable()
on returned object if you needconst
Lists and Maps.The default type is determined by the following algorithm:
- If the type is nullable (and
force == false
) returnnull
- If the type is a Map, an empty map is returned
- If the type is a List, an empty list is returned (with zero capacity)
- If one exists, a public no-args ctor is called to create the object
- If it exists, the value of the type's
defVal
slot is returned (must be a static field or method with zero params) ArgErr
is thrown
This method differs from Type.make() for the following reasons:
null
is returned if type is nullable.- Can create Lists and Maps
- The public no-args ctor can be called anything.
- If the type is nullable (and
- make
new make(Type type, Obj?[]? ctorArgs := null, [Field:Obj?]? fieldVals := null)
Makes a factory for the given type.
- set
@
Operator
This set(Field field, Obj? val)Sets a field on the type to be instantiated.
- setByName
This setByName(Str fieldName, Obj? val)
Sets a field on the type to be instantiated.
- type
Type type { private set }
The type this factory will create