deb-murano/doc/source/admin/appdev-guide/murano_pl/reflection.rst
zhurong 9cb54690e9 Adapt murano documentation for new standards
This patch set makes the following changes:
 * using the new standards
 * Update doc/source/index.rst with new links
 * Move content of install-guide to the doc/source/install

Change-Id: Ic16671191832d949d2ea0626dcbf334ba60c56ef
2017-07-11 11:38:08 +08:00

6.1 KiB

Reflection capabilities in MuranoPL.

Reflection provides objects that describes MuranoPL classes and packages.

The first important function is typeinfo . Usage:

$typeInfo: typeinfo($someObject)

Now $typeInfo variable contains instance of type of $someObject (MuranoClass instance).

MuranoPL provide following abilities to reflection:

Types

Property Description
name name of MuranoPL class
version version (SemVer) of MuranoPL class.
ancestors list of class ancestors
properties list of class properties. See properties_reflection
package package information. See package_reflection
methods list of methods. See methods_reflection
type reference to type, which can be used as argument in engine functions

Example

- $typeInfo: typeinfo($)
...
# log name, version and package name of this class
- $log.info("This is "{class_name}/{version} from {package}",
     class_name => $typeInfo.name,
     version => str($typeInfo.version),
     package => $typeInfo.package.name))
- $log.info("Ancestors:")
- For: ancestor
  In: $typeInfo.ancestors
  Do:
    #log all ancestors names
    - $log.info("{ancestor_name}", ancestor_name => $ancestor.name)
# log full class version
- $log.info("{version}", version => str($typeInfo.version))
# create object with same class
- $newObject = new($typeInfo.type)

Properties

Property introspection

Property Description
name name of property
hasDefault boolean value. True, if property has default value, False otherwise
usage Usage property's field. See property_usage for details
declaringType type - owner of declared property

Property access

Methods Description
$property.setValue($target, $value) set value of $property for object $target to $value
$property.getValue($target) get value of $property for object $target

Example

- $typeInfo: typeinfo($)
...
# select first property
- $selectedPropety: $typeInfo.properties.first()
# log property name
- $log.info("Hi, my name is {p_name}, p_name => $selectedProperty.name)
# set new property value
- $selectedProperty.setValue($, "new_value")
# log new property value using reflection
- $log.info("My new value is {value}", value => $selectedProperty.getValue($))
# also, if property static, $target can be null
- $log.info("Static property value is {value},
    value => $staticProperty.getValue(null))

Packages

Property Description
types list of types, declared in package
name package name
version package version

Example

- $typeInfo: typeinfo($)
...
- $packageRef: $typeInfo.package
- $log.info("This is package {p_name}/{p_version}",
    p_name => $packageRef.name,
    p_version => str($packageRef.version))
- $log.info("Types in package:")
- For: type_
  In: $packageRef.types
  Do:
    - $log.info("{typename}", typename => type_.name)

Methods

Methods properties

Property Description
name method's name
declaringType type - owner of declared method
arguments list of method's arguments. See arguments_reflection

Method invoking

Methods Description
$method.invoke($target, $arg1, ... $argN, kwarg1 => value1, ..., kwargN => valueN) call $target's method $method with $arg1, ..., $argN positional arguments and kwarg1, .... kwargN named arguments

Example

- $typeInfo: typeinfo($)
...
# select single method by name
- $selectedMethod: $typeInfo.methods.where($.name = sampleMethodName).single()
# log method name
- $log.info("Method name: {m_name}", m_name => $selectedMethod.name)
# log method arguments names
- For: argument
  In: $selectedMethod.arguments
  Do:
     - $log.info("{name}", name => $argument.name)
# call method with positional argument 'bar' and named `baz` == 'baz'
- $selectedMethod.invoke($, 'bar', baz => baz)

Method arguments

Property Description
name argument's name
hasDefault True if argument has default value, False otherwise
declaringMethod method - owner of argument
usage argument's usage type. See method_arguments for details
- $firstArgument: $selectedMethod.arguments.first()
# store argument's name
- $argName: $firstArgument.name
# store owner's name
- $methodName: $firstArgument.declaringMethod.name
- $log.info("Hi, my name is {a_name} ! My owner is {m_name}",
    a_name => $argName,
    m_name => $methodName)