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

Source

@Operator
This add(Obj? arg)

Adds a ctor argument.

create

Source

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

Source

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 need const Lists and Maps.

The default type is determined by the following algorithm:

  1. If the type is nullable (and force == false) return null
  2. If the type is a Map, an empty map is returned
  3. If the type is a List, an empty list is returned (with zero capacity)
  4. If one exists, a public no-args ctor is called to create the object
  5. If it exists, the value of the type's defVal slot is returned (must be a static field or method with zero params)
  6. 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.
make

Source

new make(Type type, Obj?[]? ctorArgs := null, [Field:Obj?]? fieldVals := null)

Makes a factory for the given type.

set

Source

@Operator
This set(Field field, Obj? val)

Sets a field on the type to be instantiated.

setByName

Source

This setByName(Str fieldName, Obj? val)

Sets a field on the type to be instantiated.

type

Source

Type type { private set }

The type this factory will create