Type CLASS

The runtime metadata for a script type.

Julian script types, be it built-in or user-defined, class-based or primitive, all have an engine-scoped singleton Type object that carries the type's metadata throughout the engine lifecycle. A Type object is usually the starting point of any reflection-based logic.

There are three ways to obtain Type in runtime. If type is known at the time the code is written, one can use typeof operator on a type name:

  Type t0 = typeof(MyModule.MyClass);
  Type t1 = typeof(string);
  Type t2 = typeof(int[]);


Alternatively, we can also retrieve the type of a runtime object via getType() method:

  MyClass inst = new MyClass();
  ... ...
  Type t0 = inst.getType(inst);
  Type t1 = typeof(MyClass);
  bool same = t0 == t1; // true


The third and the most flexible way is to get type dynamically by Type.load(). If the type is not loaded, load() will trigger type loading, same to what happens when a type is used for the first time. Otherwise the Type object will be immediately returned from engine's type storage.

Type usually serves as a starting point for invoking reflection API. For more details, see here.

Parent Class

All Members


TypeNameSignature
methodgetAttributespublic Attribute[] getAttributes()
methodgetConstructorspublic Constructor[] getConstructors()
methodgetExtensionspublic Type[] getExtensions()
methodgetFieldpublic Field getField(string)
methodgetFieldspublic Field[] getFields()
methodgetFullNamepublic String getFullName()
methodgetInterfacespublic Type[] getInterfaces()
methodgetMethodspublic Method[] getMethods()
methodgetMethodspublic Method[] getMethods(string)
methodgetModulepublic Module getModule()
methodgetModuleNamepublic String getModuleName()
methodgetParentpublic Type getParent()
methodgetSimpleNamepublic String getSimpleName()
methodisArraypublic bool isArray()
methodisClasspublic bool isClass()
methodisFinalpublic bool isFinal()
methodisInterfacepublic bool isInterface()
methodisPrimitivepublic bool isPrimitive()
method Sloadpublic static Type load(string)
methodtoStringpublic String toString()

Methods


public Attribute[] getAttributes()

Get all the attributes annotated on the type definition.

The result array contains each instance of Attribute placed at the type definition. For example:

  [Owner(name="Joshua", opened=2015)]
  [Protection(enabled=false)]
  class Account { }

  Attribute[] attrs = typeof(Account).getAttributes(); // length = 2: Owner and Protection

Returns

  • The attributes annotated on this type. In case of types which do not have attributes, including all non-class types, this returns a 0-sized Attribute array.

public Constructor[] getConstructors()

Get the constructors of this type, in no particular order.

For system types, this will only return constructors that are marked as visible to reflection. For user-defined types, this will return all constructors, regardless of its defined visibility, unless it's marked as invisible. See Reflected for its detailed usage.

Returns

  • All the constructors, defined explicitly or implicitly, on this type. For non-class types, this will always return a 0-sized array.

public Type[] getExtensions()

Get the extension types that are directly installed on this type.

The result array will only include those that are declared as the extension types for the current type. In particular, it doesn't include any extension types that can be recursively discovered by tracing through the type definition chain. In one example:

  static class ExtA { }
  interface A : ExtA { } // Install ExtA to A
  static class ExtB { }
  interface B : A, ExtB { } // Extend interface A from B and install ExtB to it

  Type[] types1 = typeof(B).getExtensions(); // size = 1: ExtA

Returns

  • The directly installed extension classes. In case of types which do not have extenions, this returns a 0-sized Type array.

public Field getField(string name)

Get the field of this type with specified name.

This method applies a simple filter on top of getFields(). For more information on the mechanic of method retrieval, refer to the documentation of that method.

Parameters

  • name The name of field to search.

Returns

  • The field of the given name. If not found, returns null.

public Field[] getFields()

Get the fields of this type, in no particular order.

For system types, this will only return fields that are marked as visible to reflection. For user-defined types, this will return all fields that are visible inside this type, unless it's marked as invisible. See Reflected for the usage of attribute.

Fields that are visible inside this type include all field members defined on this type, as well as all the field members inherited from ancestors.

Returns

  • All the fields, defined explicitly or implicitly, on this type.

public String getFullName()

Get the fully qualified type name. This will always return canonical name, not language-level alias for certain built-in types.

Returns

  • The fully qualified type name.

public Type[] getInterfaces()

Get the directly implemented or extended interfaces.

The result array will only include those that are declared as the interface types for the current type. In particular, it doesn't include any interfaces that can be recursively discovered by tracing through the type definition chain. In one example:

  interface A { }
  interface B : A { }
  interface C { }
  class MyClass : B, C { }

  Type[] types0 = typeof(MyClass).getInterfaces(); // size = 2: B and C
  Type[] types1 = typeof(B).getInterfaces(); // size = 1: A

Returns

  • The directly implemented or extended interfaces. In case of types which do not have interfaces, including all non-class types, this returns a 0-sized Type array.

public Method[] getMethods()

Get the methods of this type, in no particular order.

For system types, this will only return methods that are marked as visible to reflection. For user-defined types, this will return all methods that are visible inside this type, unless it's marked as invisible. See Reflected for the usage of attribute.

Methods that are visible inside this type include all method members defined on this type, as well as all the method members inherited from ancestors yet not hidden by a new member with same signature. So if a protected member is not overridden, it will be showing up in the result array. If it's been overridden in this type, the latest definition will be included in the array instead:

 class Parent {
   void fun1(){ }
   void fun2(){ }
 }
 class Child : Parent {
   void fun1(){ } // Overrides fun1
 }

 // Returns an array of size = 2, including Parent.fun2() and Child.fun1()
 Method[] methods = typeof(Child).getMethods();

Returns

  • All the methods, defined explicitly or implicitly, on this type. For types that are neither class nor interface, this will always return a 0-sized array.

public Method[] getMethods(string name)

Get the methods of this type with specified name, in no particular order.

This method applies a simple filter on top of getMethods(). For more information on the mechanic of method retrieval, refer to the documentation of that method. For more refined lookup, recommend using IIterable's extension API.

Parameters

  • name The name of method to search.

Returns

  • All the methods of the specified name, defined explicitly or implicitly, on this type.

public Module getModule()

Get the module this type belongs to. Note primitive and some built-in types such as Object do not belong to any module.

Returns

  • The module this type belongs to. Null if the type doesn't belong to any module.

public String getModuleName()

Get the module name of this type, i.e. excluding the part for module. For a type with fully qualified name "A.B.C", this method will return "A.B".

Returns

  • The module name. For some built-in types which do not have a module this returns null.

public Type getParent()

Get the parent type of this type. A class type, other than Object, must have one parent. Calling this method on Object, interface, or other non-class types always returns null.

Returns

  • The parent type of this type.

public String getSimpleName()

Get the simple name of this type, i.e. excluding the part for module. For a type with fully qualified name "A.B.C", this method will return "C". This will always return canonical name, not language-level alias for certain built-in types.

Returns

  • The simple type name. For some built-in types this name is same to full name returned by getFullName().

public bool isArray()

Check whether the type is an Array type. Array type is also a class type.

Returns

  • True if this type is a class type, inheriting, or exactly is, Array.

public bool isClass()

Check whether the type is a class type. Interface types, primitive types, Any and Void type is not class type; all the others are.

Returns

  • True if this type is a class type, inheriting, or exactly is, Object.

public bool isFinal()

Check whether the type is finalized and thus cannot be inherited.

Returns

  • True if this type is finalized; false if a class can inherit from it.

public bool isInterface()

Check whether the type is an interface type.

Returns

  • True if this type is an interface type.

public bool isPrimitive()

Check whether the type is primitive. A type is primitive if it's int, byte, float, bool or char. Any other types, including string, any and array type with primitive elements, are not primitive.

Returns

  • True if this type is one of the five primitive types.

public static Type load(string typeName)

Load a type with specified name. If the type is not loaded, this method will trigger loading of the entire dependency closure of the required type. Otherwise the Type object will be immediately returned from engine's type storage. This method is thread-safe.

Some examples:

  Type.load("int");
  Type.load("Integer");
  Type.load("byte[]");
  Type.load("MyModule.MyClass");
  Type.load("UrClass[][]");

Required Policies

  • System.Reflection/load

Parameters

  • typeName The fully qualified type name, including dimension info, written in pairs of square brackets, at the end. The syntax is completely same to type identifier that appears at the start of a variable definition, so language-alias can be used (int instead of Integer).

Returns

  • The loaded type.

Throws

  • System.Reflection.ReflectedInvocationException This is a wrapper exception for the real cause of failure, which can be type definition error, syntax error on the type name, type not found, etc. So always call getCause() to inspect the original error.

public String toString()

Get a string representation of this type.

Returns

  • A string in form of [TYPE|full-name]. Example: [TYPE|MyModule.MyClass]