Obtaining Information About Classes with Meta-Classes

What Are Meta-Classes

Meta-classes are classes that contain information about class definitions. Each block in a class definition has an associated meta-class that defines the attributes for that block. Each attribute corresponds to a property in the meta-class. An instance of a meta-class has values assigned to each property that correspond to the values of the attributes of the associated class block.

Meta-classes enable introspection of class definitions, which is useful for programmatic inspection of classes and objects. Such techniques are used by tools such as property inspectors, debuggers, and so on.

The Meta Package

The meta package is a package of meta-classes that are involved in the definition of classes and class components. The class name indicates the component described by the meta-class:

meta.package
meta.class
meta.property
meta.method
meta.event

Each meta-class has properties, methods, and events that contain information about the class or class component. See meta.package, meta.class, meta.property, meta.method and meta.event for more information on these meta-classes.

Creating Meta-Class Objects

You can create meta-class objects from class instances or from the class definition directly.

mobj = ?classname; % create meta-class object from class name
obj = myClass;
mobj = metaclass(obj); % create meta-class object from class instance

Note that the metaclass function returns the meta.class object (i.e., an object of the meta.class class). You can obtain other meta-class objects (meta.property, meta.method, etc.) from the meta.class object.

Using Meta-Class Objects

Here is how you can use meta-class objects:

The following examples show these techniques.

Inspecting a Class

Consider the following class and the information you can obtain from its meta-class instances. The EmployeeData class is a handle class with two properties, one of which defines a set access function. The capability to glean information about the class programmatically without requiring an instance of the class can be useful when implementing tools, such as inspectors, code debuggers, and so on.

classdef EmployeeData < handle
   properties 
      EmployeeName
   end 
   properties (GetAccess = private)
      EmployeeNumber
   end
   methods 
      function obj = EmployeeData(name,ss)
         obj.EmployeeName = name;
         obj.EmployeeNumber = ss;
      end
      function obj = set.EmployeeName(obj,name)
         if ischar(name)
            obj.EmployeeName = name;
         else
            error('Employee name must be a text string')
         end
      end
   end
end

Inspecting the Class Definition

Using the EmployeeData class defined above, you can create a meta.class object using the ? operator:

mboj = ?EmployeeData;

You can determine what classes EmployeeData is derived from:

a = mobj.SuperClasses; % a is cell array of meta.class objects
a{1}.Name
ans =
   handle

Inspecting Properties.   Suppose you want to know the names of the properties defined by this class. First obtain a cell array of meta.properties objects from the meta.class Properties property.

mpCell = mobj.Properties;

The length of mpCell indicates there are two meta.property objects, one for each property:

length(mpCell)
ans =
     2

Now get a meta.property object from the cell array:

prop1 = mpCell{1}
prop1 =
   meta.property % prop1 is a meta.property object
prop1.Name
ans =
   EmployeeName % first object is EmployName property's meta.property

You can now query any of the meta.property object's properties for the EmployName class. You can determine the setting of all property attributes and even obtain a function handle to the property's set access function:

setmeth = prop1.SetMethod
setmeth = 
   @D:\MyDir\@EmployeeData\EmployeeData.m>EmployeeData.set.EmployeeName

Querying the meta.property class SetMethod property returns a function handle to the set access method defined in the EmployeeData class.

Inspecting an Instance of a Class

Suppose you create a EmployeeData object:

EdObj = EmployeeData('My Name',1234567);
mEdObj = metaclass(EdObj);
mpCell = mEdobj.Properties;
eval(['EdObj.',mpCell{1}.Name])
ans =
   My Name
eval(['EdObj.',mpCell{2}.Name])
??? Getting the 'EmployeeNumber' property of the 'EmployeeData' class is
not allowed.
mpCell{2}.GetAccess
ans =
   private
  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS