Move having methods to a new superclass - Class::MOP::HasMethods
[gitmo/Class-MOP.git] / lib / Class / MOP / Class.pm
1
2 package Class::MOP::Class;
3
4 use strict;
5 use warnings;
6
7 use Class::MOP::Instance;
8 use Class::MOP::Method::Wrapped;
9 use Class::MOP::Method::Accessor;
10 use Class::MOP::Method::Constructor;
11
12 use Carp         'confess';
13 use Scalar::Util 'blessed', 'reftype', 'weaken';
14 use Sub::Name    'subname';
15 use Devel::GlobalDestruction 'in_global_destruction';
16 use Try::Tiny;
17
18 our $VERSION   = '0.95';
19 $VERSION = eval $VERSION;
20 our $AUTHORITY = 'cpan:STEVAN';
21
22 use base 'Class::MOP::Module';
23
24 # Creation
25
26 sub initialize {
27     my $class = shift;
28
29     my $package_name;
30     
31     if ( @_ % 2 ) {
32         $package_name = shift;
33     } else {
34         my %options = @_;
35         $package_name = $options{package};
36     }
37
38     ($package_name && !ref($package_name))
39         || confess "You must pass a package name and it cannot be blessed";
40
41     return Class::MOP::get_metaclass_by_name($package_name)
42         || $class->_construct_class_instance(package => $package_name, @_);
43 }
44
45 # NOTE: (meta-circularity)
46 # this is a special form of _construct_instance
47 # (see below), which is used to construct class
48 # meta-object instances for any Class::MOP::*
49 # class. All other classes will use the more
50 # normal &construct_instance.
51 sub _construct_class_instance {
52     my $class        = shift;
53     my $options      = @_ == 1 ? $_[0] : {@_};
54     my $package_name = $options->{package};
55     (defined $package_name && $package_name)
56         || confess "You must pass a package name";
57     # NOTE:
58     # return the metaclass if we have it cached,
59     # and it is still defined (it has not been
60     # reaped by DESTROY yet, which can happen
61     # annoyingly enough during global destruction)
62
63     if (defined(my $meta = Class::MOP::get_metaclass_by_name($package_name))) {
64         return $meta;
65     }
66
67     # NOTE:
68     # we need to deal with the possibility
69     # of class immutability here, and then
70     # get the name of the class appropriately
71     $class = (ref($class)
72                     ? ($class->is_immutable
73                         ? $class->_get_mutable_metaclass_name()
74                         : ref($class))
75                     : $class);
76
77     # now create the metaclass
78     my $meta;
79     if ($class eq 'Class::MOP::Class') {
80         $meta = $class->_new($options);
81     }
82     else {
83         # NOTE:
84         # it is safe to use meta here because
85         # class will always be a subclass of
86         # Class::MOP::Class, which defines meta
87         $meta = $class->meta->_construct_instance($options)
88     }
89
90     # and check the metaclass compatibility
91     $meta->_check_metaclass_compatibility();  
92
93     Class::MOP::store_metaclass_by_name($package_name, $meta);
94
95     # NOTE:
96     # we need to weaken any anon classes
97     # so that they can call DESTROY properly
98     Class::MOP::weaken_metaclass($package_name) if $meta->is_anon_class;
99
100     $meta;
101 }
102
103 sub _new {
104     my $class = shift;
105
106     return Class::MOP::Class->initialize($class)->new_object(@_)
107         if $class ne __PACKAGE__;
108
109     my $options = @_ == 1 ? $_[0] : {@_};
110
111     return bless {
112         # inherited from Class::MOP::Package
113         'package' => $options->{package},
114
115         # NOTE:
116         # since the following attributes will
117         # actually be loaded from the symbol
118         # table, and actually bypass the instance
119         # entirely, we can just leave these things
120         # listed here for reference, because they
121         # should not actually have a value associated
122         # with the slot.
123         'namespace' => \undef,
124         'methods'   => {},
125
126         # inherited from Class::MOP::Module
127         'version'   => \undef,
128         'authority' => \undef,
129
130         # defined in Class::MOP::Class
131         'superclasses' => \undef,
132
133         'attributes' => {},
134         'attribute_metaclass' =>
135             ( $options->{'attribute_metaclass'} || 'Class::MOP::Attribute' ),
136         'method_metaclass' =>
137             ( $options->{'method_metaclass'} || 'Class::MOP::Method' ),
138         'wrapped_method_metaclass' => (
139             $options->{'wrapped_method_metaclass'}
140                 || 'Class::MOP::Method::Wrapped'
141         ),
142         'instance_metaclass' =>
143             ( $options->{'instance_metaclass'} || 'Class::MOP::Instance' ),
144         'immutable_trait' => (
145             $options->{'immutable_trait'}
146                 || 'Class::MOP::Class::Immutable::Trait'
147         ),
148         'constructor_name' => ( $options->{constructor_name} || 'new' ),
149         'constructor_class' => (
150             $options->{constructor_class} || 'Class::MOP::Method::Constructor'
151         ),
152         'destructor_class' => $options->{destructor_class},
153     }, $class;
154 }
155
156 sub reset_package_cache_flag  { (shift)->{'_package_cache_flag'} = undef } 
157 sub update_package_cache_flag {
158     my $self = shift;
159     # NOTE:
160     # we can manually update the cache number 
161     # since we are actually adding the method
162     # to our cache as well. This avoids us 
163     # having to regenerate the method_map.
164     # - SL    
165     $self->{'_package_cache_flag'} = Class::MOP::check_package_cache_flag($self->name);    
166 }
167
168 sub _check_metaclass_compatibility {
169     my $self = shift;
170
171     # this is always okay ...
172     return if ref($self)                eq 'Class::MOP::Class'   &&
173               $self->instance_metaclass eq 'Class::MOP::Instance';
174
175     my @class_list = $self->linearized_isa;
176     shift @class_list; # shift off $self->name
177
178     foreach my $superclass_name (@class_list) {
179         my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name) || next;
180
181         # NOTE:
182         # we need to deal with the possibility
183         # of class immutability here, and then
184         # get the name of the class appropriately
185         my $super_meta_type
186             = $super_meta->is_immutable
187             ? $super_meta->_get_mutable_metaclass_name()
188             : ref($super_meta);
189
190         ($self->isa($super_meta_type))
191             || confess "The metaclass of " . $self->name . " ("
192                        . (ref($self)) . ")" .  " is not compatible with the " .
193                        "metaclass of its superclass, ".$superclass_name . " ("
194                        . ($super_meta_type) . ")";
195         # NOTE:
196         # we also need to check that instance metaclasses
197         # are compatibile in the same the class.
198         ($self->instance_metaclass->isa($super_meta->instance_metaclass))
199             || confess "The instance metaclass for " . $self->name . " (" . ($self->instance_metaclass) . ")" .
200                        " is not compatible with the " .
201                        "instance metaclass of its superclass, " . $superclass_name . " (" . ($super_meta->instance_metaclass) . ")";
202     }
203 }
204
205 ## ANON classes
206
207 {
208     # NOTE:
209     # this should be sufficient, if you have a
210     # use case where it is not, write a test and
211     # I will change it.
212     my $ANON_CLASS_SERIAL = 0;
213
214     # NOTE:
215     # we need a sufficiently annoying prefix
216     # this should suffice for now, this is
217     # used in a couple of places below, so
218     # need to put it up here for now.
219     my $ANON_CLASS_PREFIX = 'Class::MOP::Class::__ANON__::SERIAL::';
220
221     sub is_anon_class {
222         my $self = shift;
223         no warnings 'uninitialized';
224         $self->name =~ /^$ANON_CLASS_PREFIX/o;
225     }
226
227     sub create_anon_class {
228         my ($class, %options) = @_;
229         my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL;
230         return $class->create($package_name, %options);
231     }
232
233     # NOTE:
234     # this will only get called for
235     # anon-classes, all other calls
236     # are assumed to occur during
237     # global destruction and so don't
238     # really need to be handled explicitly
239     sub DESTROY {
240         my $self = shift;
241
242         return if in_global_destruction(); # it'll happen soon anyway and this just makes things more complicated
243
244         no warnings 'uninitialized';
245         my $name = $self->name;
246         return unless $name =~ /^$ANON_CLASS_PREFIX/o;
247         # Moose does a weird thing where it replaces the metaclass for
248         # class when fixing metaclass incompatibility. In that case,
249         # we don't want to clean out the namespace now. We can detect
250         # that because Moose will explicitly update the singleton
251         # cache in Class::MOP.
252         my $current_meta = Class::MOP::get_metaclass_by_name($name);
253         return if $current_meta ne $self;
254
255         my ($serial_id) = ($name =~ /^$ANON_CLASS_PREFIX(\d+)/o);
256         no strict 'refs';
257         @{$name . '::ISA'} = ();
258         %{$name . '::'}    = ();
259         delete ${$ANON_CLASS_PREFIX}{$serial_id . '::'};
260
261         Class::MOP::remove_metaclass_by_name($name);
262     }
263
264 }
265
266 # creating classes with MOP ...
267
268 sub create {
269     my ( $class, @args ) = @_;
270
271     unshift @args, 'package' if @args % 2 == 1;
272
273     my (%options) = @args;
274     my $package_name = $options{package};
275
276     (ref $options{superclasses} eq 'ARRAY')
277         || confess "You must pass an ARRAY ref of superclasses"
278             if exists $options{superclasses};
279             
280     (ref $options{attributes} eq 'ARRAY')
281         || confess "You must pass an ARRAY ref of attributes"
282             if exists $options{attributes};      
283             
284     (ref $options{methods} eq 'HASH')
285         || confess "You must pass a HASH ref of methods"
286             if exists $options{methods};                  
287
288     my (%initialize_options) = @args;
289     delete @initialize_options{qw(
290         package
291         superclasses
292         attributes
293         methods
294         version
295         authority
296     )};
297     my $meta = $class->initialize( $package_name => %initialize_options );
298
299     $meta->_instantiate_module( $options{version}, $options{authority} );
300
301     # FIXME totally lame
302     $meta->add_method('meta' => sub {
303         $class->initialize(ref($_[0]) || $_[0]);
304     });
305
306     $meta->superclasses(@{$options{superclasses}})
307         if exists $options{superclasses};
308     # NOTE:
309     # process attributes first, so that they can
310     # install accessors, but locally defined methods
311     # can then overwrite them. It is maybe a little odd, but
312     # I think this should be the order of things.
313     if (exists $options{attributes}) {
314         foreach my $attr (@{$options{attributes}}) {
315             $meta->add_attribute($attr);
316         }
317     }
318     if (exists $options{methods}) {
319         foreach my $method_name (keys %{$options{methods}}) {
320             $meta->add_method($method_name, $options{methods}->{$method_name});
321         }
322     }
323     return $meta;
324 }
325
326 ## Attribute readers
327
328 # NOTE:
329 # all these attribute readers will be bootstrapped
330 # away in the Class::MOP bootstrap section
331
332 sub _attribute_map           { $_[0]->{'attributes'}                  }
333 sub attribute_metaclass      { $_[0]->{'attribute_metaclass'}         }
334 sub instance_metaclass       { $_[0]->{'instance_metaclass'}          }
335 sub immutable_trait          { $_[0]->{'immutable_trait'}             }
336 sub constructor_class        { $_[0]->{'constructor_class'}           }
337 sub constructor_name         { $_[0]->{'constructor_name'}            }
338 sub destructor_class         { $_[0]->{'destructor_class'}            }
339
340 # Instance Construction & Cloning
341
342 sub new_object {
343     my $class = shift;
344
345     # NOTE:
346     # we need to protect the integrity of the
347     # Class::MOP::Class singletons here, so we
348     # delegate this to &construct_class_instance
349     # which will deal with the singletons
350     return $class->_construct_class_instance(@_)
351         if $class->name->isa('Class::MOP::Class');
352     return $class->_construct_instance(@_);
353 }
354
355 sub _construct_instance {
356     my $class = shift;
357     my $params = @_ == 1 ? $_[0] : {@_};
358     my $meta_instance = $class->get_meta_instance();
359     # FIXME:
360     # the code below is almost certainly incorrect
361     # but this is foreign inheritance, so we might
362     # have to kludge it in the end.
363     my $instance = $params->{__INSTANCE__} || $meta_instance->create_instance();
364     foreach my $attr ($class->get_all_attributes()) {
365         $attr->initialize_instance_slot($meta_instance, $instance, $params);
366     }
367     # NOTE:
368     # this will only work for a HASH instance type
369     if ($class->is_anon_class) {
370         (reftype($instance) eq 'HASH')
371             || confess "Currently only HASH based instances are supported with instance of anon-classes";
372         # NOTE:
373         # At some point we should make this official
374         # as a reserved slot name, but right now I am
375         # going to keep it here.
376         # my $RESERVED_MOP_SLOT = '__MOP__';
377         $instance->{'__MOP__'} = $class;
378     }
379     return $instance;
380 }
381
382
383 sub get_meta_instance {
384     my $self = shift;
385     $self->{'_meta_instance'} ||= $self->_create_meta_instance();
386 }
387
388 sub _create_meta_instance {
389     my $self = shift;
390     
391     my $instance = $self->instance_metaclass->new(
392         associated_metaclass => $self,
393         attributes => [ $self->get_all_attributes() ],
394     );
395
396     $self->add_meta_instance_dependencies()
397         if $instance->is_dependent_on_superclasses();
398
399     return $instance;
400 }
401
402 sub clone_object {
403     my $class    = shift;
404     my $instance = shift;
405     (blessed($instance) && $instance->isa($class->name))
406         || confess "You must pass an instance of the metaclass (" . (ref $class ? $class->name : $class) . "), not ($instance)";
407
408     # NOTE:
409     # we need to protect the integrity of the
410     # Class::MOP::Class singletons here, they
411     # should not be cloned.
412     return $instance if $instance->isa('Class::MOP::Class');
413     $class->_clone_instance($instance, @_);
414 }
415
416 sub _clone_instance {
417     my ($class, $instance, %params) = @_;
418     (blessed($instance))
419         || confess "You can only clone instances, ($instance) is not a blessed instance";
420     my $meta_instance = $class->get_meta_instance();
421     my $clone = $meta_instance->clone_instance($instance);
422     foreach my $attr ($class->get_all_attributes()) {
423         if ( defined( my $init_arg = $attr->init_arg ) ) {
424             if (exists $params{$init_arg}) {
425                 $attr->set_value($clone, $params{$init_arg});
426             }
427         }
428     }
429     return $clone;
430 }
431
432 sub rebless_instance {
433     my ($self, $instance, %params) = @_;
434
435     my $old_metaclass = Class::MOP::class_of($instance);
436
437     my $old_class = $old_metaclass ? $old_metaclass->name : blessed($instance);
438     $self->name->isa($old_class)
439         || confess "You may rebless only into a subclass of ($old_class), of which (". $self->name .") isn't.";
440
441     $old_metaclass->rebless_instance_away($instance, $self, %params)
442         if $old_metaclass;
443
444     my $meta_instance = $self->get_meta_instance();
445
446     # rebless!
447     # we use $_[1] here because of t/306_rebless_overload.t regressions on 5.8.8
448     $meta_instance->rebless_instance_structure($_[1], $self);
449
450     foreach my $attr ( $self->get_all_attributes ) {
451         if ( $attr->has_value($instance) ) {
452             if ( defined( my $init_arg = $attr->init_arg ) ) {
453                 $params{$init_arg} = $attr->get_value($instance)
454                     unless exists $params{$init_arg};
455             } 
456             else {
457                 $attr->set_value($instance, $attr->get_value($instance));
458             }
459         }
460     }
461
462     foreach my $attr ($self->get_all_attributes) {
463         $attr->initialize_instance_slot($meta_instance, $instance, \%params);
464     }
465     
466     $instance;
467 }
468
469 sub rebless_instance_away {
470     # this intentionally does nothing, it is just a hook
471 }
472
473 # Inheritance
474
475 sub superclasses {
476     my $self     = shift;
477     my $var_spec = { sigil => '@', type => 'ARRAY', name => 'ISA' };
478     if (@_) {
479         my @supers = @_;
480         @{$self->get_package_symbol($var_spec)} = @supers;
481
482         # NOTE:
483         # on 5.8 and below, we need to call
484         # a method to get Perl to detect
485         # a cycle in the class hierarchy
486         my $class = $self->name;
487         $class->isa($class);
488
489         # NOTE:
490         # we need to check the metaclass
491         # compatibility here so that we can
492         # be sure that the superclass is
493         # not potentially creating an issues
494         # we don't know about
495
496         $self->_check_metaclass_compatibility();
497         $self->_superclasses_updated();
498     }
499     @{$self->get_package_symbol($var_spec)};
500 }
501
502 sub _superclasses_updated {
503     my $self = shift;
504     $self->update_meta_instance_dependencies();
505 }
506
507 sub subclasses {
508     my $self = shift;
509     my $super_class = $self->name;
510
511     return @{ $super_class->mro::get_isarev() };
512 }
513
514 sub direct_subclasses {
515     my $self = shift;
516     my $super_class = $self->name;
517
518     return grep {
519         grep {
520             $_ eq $super_class
521         } Class::MOP::Class->initialize($_)->superclasses
522     } $self->subclasses;
523 }
524
525 sub linearized_isa {
526     return @{ mro::get_linear_isa( (shift)->name ) };
527 }
528
529 sub class_precedence_list {
530     my $self = shift;
531     my $name = $self->name;
532
533     unless (Class::MOP::IS_RUNNING_ON_5_10()) { 
534         # NOTE:
535         # We need to check for circular inheritance here
536         # if we are are not on 5.10, cause 5.8 detects it 
537         # late. This will do nothing if all is well, and 
538         # blow up otherwise. Yes, it's an ugly hack, better
539         # suggestions are welcome.        
540         # - SL
541         ($name || return)->isa('This is a test for circular inheritance') 
542     }
543
544     # if our mro is c3, we can 
545     # just grab the linear_isa
546     if (mro::get_mro($name) eq 'c3') {
547         return @{ mro::get_linear_isa($name) }
548     }
549     else {
550         # NOTE:
551         # we can't grab the linear_isa for dfs
552         # since it has all the duplicates 
553         # already removed.
554         return (
555             $name,
556             map {
557                 $self->initialize($_)->class_precedence_list()
558             } $self->superclasses()
559         );
560     }
561 }
562
563 ## Methods
564
565 {
566     my $fetch_and_prepare_method = sub {
567         my ($self, $method_name) = @_;
568         my $wrapped_metaclass = $self->wrapped_method_metaclass;
569         # fetch it locally
570         my $method = $self->get_method($method_name);
571         # if we dont have local ...
572         unless ($method) {
573             # try to find the next method
574             $method = $self->find_next_method_by_name($method_name);
575             # die if it does not exist
576             (defined $method)
577                 || confess "The method '$method_name' was not found in the inheritance hierarchy for " . $self->name;
578             # and now make sure to wrap it
579             # even if it is already wrapped
580             # because we need a new sub ref
581             $method = $wrapped_metaclass->wrap($method,
582                 package_name => $self->name,
583                 name         => $method_name,
584             );
585         }
586         else {
587             # now make sure we wrap it properly
588             $method = $wrapped_metaclass->wrap($method,
589                 package_name => $self->name,
590                 name         => $method_name,
591             ) unless $method->isa($wrapped_metaclass);
592         }
593         $self->add_method($method_name => $method);
594         return $method;
595     };
596
597     sub add_before_method_modifier {
598         my ($self, $method_name, $method_modifier) = @_;
599         (defined $method_name && length $method_name)
600             || confess "You must pass in a method name";
601         my $method = $fetch_and_prepare_method->($self, $method_name);
602         $method->add_before_modifier(
603             subname(':before' => $method_modifier)
604         );
605     }
606
607     sub add_after_method_modifier {
608         my ($self, $method_name, $method_modifier) = @_;
609         (defined $method_name && length $method_name)
610             || confess "You must pass in a method name";
611         my $method = $fetch_and_prepare_method->($self, $method_name);
612         $method->add_after_modifier(
613             subname(':after' => $method_modifier)
614         );
615     }
616
617     sub add_around_method_modifier {
618         my ($self, $method_name, $method_modifier) = @_;
619         (defined $method_name && length $method_name)
620             || confess "You must pass in a method name";
621         my $method = $fetch_and_prepare_method->($self, $method_name);
622         $method->add_around_modifier(
623             subname(':around' => $method_modifier)
624         );
625     }
626
627     # NOTE:
628     # the methods above used to be named like this:
629     #    ${pkg}::${method}:(before|after|around)
630     # but this proved problematic when using one modifier
631     # to wrap multiple methods (something which is likely
632     # to happen pretty regularly IMO). So instead of naming
633     # it like this, I have chosen to just name them purely
634     # with their modifier names, like so:
635     #    :(before|after|around)
636     # The fact is that in a stack trace, it will be fairly
637     # evident from the context what method they are attached
638     # to, and so don't need the fully qualified name.
639 }
640
641 sub find_method_by_name {
642     my ($self, $method_name) = @_;
643     (defined $method_name && length $method_name)
644         || confess "You must define a method name to find";
645     foreach my $class ($self->linearized_isa) {
646         my $method = $self->initialize($class)->get_method($method_name);
647         return $method if defined $method;
648     }
649     return;
650 }
651
652 sub get_all_methods {
653     my $self = shift;
654
655     my %methods;
656     for my $class ( reverse $self->linearized_isa ) {
657         my $meta = $self->initialize($class);
658
659         $methods{$_} = $meta->get_method($_)
660             for $meta->get_method_list;
661     }
662
663     return values %methods;
664 }
665
666 sub get_all_method_names {
667     my $self = shift;
668     my %uniq;
669     return grep { !$uniq{$_}++ } map { $self->initialize($_)->get_method_list } $self->linearized_isa;
670 }
671
672 sub find_all_methods_by_name {
673     my ($self, $method_name) = @_;
674     (defined $method_name && length $method_name)
675         || confess "You must define a method name to find";
676     my @methods;
677     foreach my $class ($self->linearized_isa) {
678         # fetch the meta-class ...
679         my $meta = $self->initialize($class);
680         push @methods => {
681             name  => $method_name,
682             class => $class,
683             code  => $meta->get_method($method_name)
684         } if $meta->has_method($method_name);
685     }
686     return @methods;
687 }
688
689 sub find_next_method_by_name {
690     my ($self, $method_name) = @_;
691     (defined $method_name && length $method_name)
692         || confess "You must define a method name to find";
693     my @cpl = $self->linearized_isa;
694     shift @cpl; # discard ourselves
695     foreach my $class (@cpl) {
696         my $method = $self->initialize($class)->get_method($method_name);
697         return $method if defined $method;
698     }
699     return;
700 }
701
702 ## Attributes
703
704 sub add_attribute {
705     my $self      = shift;
706     # either we have an attribute object already
707     # or we need to create one from the args provided
708     my $attribute = blessed($_[0]) ? $_[0] : $self->attribute_metaclass->new(@_);
709     # make sure it is derived from the correct type though
710     ($attribute->isa('Class::MOP::Attribute'))
711         || confess "Your attribute must be an instance of Class::MOP::Attribute (or a subclass)";
712
713     # first we attach our new attribute
714     # because it might need certain information
715     # about the class which it is attached to
716     $attribute->attach_to_class($self);
717
718     my $attr_name = $attribute->name;
719
720     # then we remove attributes of a conflicting
721     # name here so that we can properly detach
722     # the old attr object, and remove any
723     # accessors it would have generated
724     if ( $self->has_attribute($attr_name) ) {
725         $self->remove_attribute($attr_name);
726     } else {
727         $self->invalidate_meta_instances();
728     }
729     
730     # get our count of previously inserted attributes and
731     # increment by one so this attribute knows its order
732     my $order = (scalar keys %{$self->_attribute_map});
733     $attribute->_set_insertion_order($order);
734
735     # then onto installing the new accessors
736     $self->_attribute_map->{$attr_name} = $attribute;
737
738     # invalidate package flag here
739     try {
740         local $SIG{__DIE__};
741         $attribute->install_accessors();
742     }
743     catch {
744         $self->remove_attribute($attr_name);
745         die $_;
746     };
747
748     return $attribute;
749 }
750
751 sub update_meta_instance_dependencies {
752     my $self = shift;
753
754     if ( $self->{meta_instance_dependencies} ) {
755         return $self->add_meta_instance_dependencies;
756     }
757 }
758
759 sub add_meta_instance_dependencies {
760     my $self = shift;
761
762     $self->remove_meta_instance_dependencies;
763
764     my @attrs = $self->get_all_attributes();
765
766     my %seen;
767     my @classes = grep { not $seen{$_->name}++ } map { $_->associated_class } @attrs;
768
769     foreach my $class ( @classes ) { 
770         $class->add_dependent_meta_instance($self);
771     }
772
773     $self->{meta_instance_dependencies} = \@classes;
774 }
775
776 sub remove_meta_instance_dependencies {
777     my $self = shift;
778
779     if ( my $classes = delete $self->{meta_instance_dependencies} ) {
780         foreach my $class ( @$classes ) {
781             $class->remove_dependent_meta_instance($self);
782         }
783
784         return $classes;
785     }
786
787     return;
788
789 }
790
791 sub add_dependent_meta_instance {
792     my ( $self, $metaclass ) = @_;
793     push @{ $self->{dependent_meta_instances} }, $metaclass;
794 }
795
796 sub remove_dependent_meta_instance {
797     my ( $self, $metaclass ) = @_;
798     my $name = $metaclass->name;
799     @$_ = grep { $_->name ne $name } @$_ for $self->{dependent_meta_instances};
800 }
801
802 sub invalidate_meta_instances {
803     my $self = shift;
804     $_->invalidate_meta_instance() for $self, @{ $self->{dependent_meta_instances} };
805 }
806
807 sub invalidate_meta_instance {
808     my $self = shift;
809     undef $self->{_meta_instance};
810 }
811
812 sub has_attribute {
813     my ($self, $attribute_name) = @_;
814     (defined $attribute_name)
815         || confess "You must define an attribute name";
816     exists $self->_attribute_map->{$attribute_name};
817 }
818
819 sub get_attribute {
820     my ($self, $attribute_name) = @_;
821     (defined $attribute_name)
822         || confess "You must define an attribute name";
823     return $self->_attribute_map->{$attribute_name}
824     # NOTE:
825     # this will return undef anyway, so no need ...
826     #    if $self->has_attribute($attribute_name);
827     #return;
828 }
829
830 sub remove_attribute {
831     my ($self, $attribute_name) = @_;
832     (defined $attribute_name)
833         || confess "You must define an attribute name";
834     my $removed_attribute = $self->_attribute_map->{$attribute_name};
835     return unless defined $removed_attribute;
836     delete $self->_attribute_map->{$attribute_name};
837     $self->invalidate_meta_instances();
838     $removed_attribute->remove_accessors();
839     $removed_attribute->detach_from_class();
840     return $removed_attribute;
841 }
842
843 sub get_attribute_list {
844     my $self = shift;
845     keys %{$self->_attribute_map};
846 }
847
848 sub get_all_attributes {
849     my $self = shift;
850     my %attrs = map { %{ $self->initialize($_)->_attribute_map } } reverse $self->linearized_isa;
851     return values %attrs;
852 }
853
854 sub find_attribute_by_name {
855     my ($self, $attr_name) = @_;
856     foreach my $class ($self->linearized_isa) {
857         # fetch the meta-class ...
858         my $meta = $self->initialize($class);
859         return $meta->get_attribute($attr_name)
860             if $meta->has_attribute($attr_name);
861     }
862     return;
863 }
864
865 # check if we can reinitialize
866 sub is_pristine {
867     my $self = shift;
868
869     # if any local attr is defined
870     return if $self->get_attribute_list;
871
872     # or any non-declared methods
873     for my $method ( map { $self->get_method($_) } $self->get_method_list ) {
874         return if $method->isa("Class::MOP::Method::Generated");
875         # FIXME do we need to enforce this too? return unless $method->isa( $self->method_metaclass );
876     }
877
878     return 1;
879 }
880
881 ## Class closing
882
883 sub is_mutable   { 1 }
884 sub is_immutable { 0 }
885
886 sub immutable_options { %{ $_[0]{__immutable}{options} || {} } }
887
888 sub _immutable_options {
889     my ( $self, @args ) = @_;
890
891     return (
892         inline_accessors   => 1,
893         inline_constructor => 1,
894         inline_destructor  => 0,
895         debug              => 0,
896         immutable_trait    => $self->immutable_trait,
897         constructor_name   => $self->constructor_name,
898         constructor_class  => $self->constructor_class,
899         destructor_class   => $self->destructor_class,
900         @args,
901     );
902 }
903
904 sub make_immutable {
905     my ( $self, @args ) = @_;
906
907     if ( $self->is_mutable ) {
908         $self->_initialize_immutable( $self->_immutable_options(@args) );
909         $self->_rebless_as_immutable(@args);
910         return $self;
911     }
912     else {
913         return;
914     }
915 }
916
917 sub make_mutable {
918     my $self = shift;
919
920     if ( $self->is_immutable ) {
921         my @args = $self->immutable_options;
922         $self->_rebless_as_mutable();
923         $self->_remove_inlined_code(@args);
924         delete $self->{__immutable};
925         return $self;
926     }
927     else {
928         return;
929     }
930 }
931
932 sub _rebless_as_immutable {
933     my ( $self, @args ) = @_;
934
935     $self->{__immutable}{original_class} = ref $self;
936
937     bless $self => $self->_immutable_metaclass(@args);
938 }
939
940 sub _immutable_metaclass {
941     my ( $self, %args ) = @_;
942
943     if ( my $class = $args{immutable_metaclass} ) {
944         return $class;
945     }
946
947     my $trait = $args{immutable_trait} = $self->immutable_trait
948         || confess "no immutable trait specified for $self";
949
950     my $meta      = $self->meta;
951     my $meta_attr = $meta->find_attribute_by_name("immutable_trait");
952
953     my $class_name;
954
955     if ( $meta_attr and $trait eq $meta_attr->default ) {
956         # if the trait is the same as the default we try and pick a
957         # predictable name for the immutable metaclass
958         $class_name = 'Class::MOP::Class::Immutable::' . ref($self);
959     }
960     else {
961         $class_name = join '::', 'Class::MOP::Class::Immutable::CustomTrait',
962             $trait, 'ForMetaClass', ref($self);
963     }
964
965     return $class_name
966         if Class::MOP::is_class_loaded($class_name);
967
968     # If the metaclass is a subclass of CMOP::Class which has had
969     # metaclass roles applied (via Moose), then we want to make sure
970     # that we preserve that anonymous class (see Fey::ORM for an
971     # example of where this matters).
972     my $meta_name
973         = $meta->is_immutable
974         ? $meta->_get_mutable_metaclass_name
975         : ref $meta;
976
977     my $immutable_meta = $meta_name->create(
978         $class_name,
979         superclasses => [ ref $self ],
980     );
981
982     Class::MOP::load_class($trait);
983     for my $meth ( Class::MOP::Class->initialize($trait)->get_all_methods ) {
984         my $meth_name = $meth->name;
985
986         if ( $immutable_meta->find_method_by_name( $meth_name ) ) {
987             $immutable_meta->add_around_method_modifier( $meth_name, $meth->body );
988         }
989         else {
990             $immutable_meta->add_method( $meth_name, $meth->clone );
991         }
992     }
993
994     $immutable_meta->make_immutable(
995         inline_constructor => 0,
996         inline_accessors   => 0,
997     );
998
999     return $class_name;
1000 }
1001
1002 sub _remove_inlined_code {
1003     my $self = shift;
1004
1005     $self->remove_method( $_->name ) for $self->_inlined_methods;
1006
1007     delete $self->{__immutable}{inlined_methods};
1008 }
1009
1010 sub _inlined_methods { @{ $_[0]{__immutable}{inlined_methods} || [] } }
1011
1012 sub _add_inlined_method {
1013     my ( $self, $method ) = @_;
1014
1015     push @{ $self->{__immutable}{inlined_methods} ||= [] }, $method;
1016 }
1017
1018 sub _initialize_immutable {
1019     my ( $self, %args ) = @_;
1020
1021     $self->{__immutable}{options} = \%args;
1022     $self->_install_inlined_code(%args);
1023 }
1024
1025 sub _install_inlined_code {
1026     my ( $self, %args ) = @_;
1027
1028     # FIXME
1029     $self->_inline_accessors(%args)   if $args{inline_accessors};
1030     $self->_inline_constructor(%args) if $args{inline_constructor};
1031     $self->_inline_destructor(%args)  if $args{inline_destructor};
1032 }
1033
1034 sub _rebless_as_mutable {
1035     my $self = shift;
1036
1037     bless $self, $self->_get_mutable_metaclass_name;
1038
1039     return $self;
1040 }
1041
1042 sub _inline_accessors {
1043     my $self = shift;
1044
1045     foreach my $attr_name ( $self->get_attribute_list ) {
1046         $self->get_attribute($attr_name)->install_accessors(1);
1047     }
1048 }
1049
1050 sub _inline_constructor {
1051     my ( $self, %args ) = @_;
1052
1053     my $name = $args{constructor_name};
1054     # A class may not even have a constructor, and that's okay.
1055     return unless defined $name;
1056
1057     if ( $self->has_method($name) && !$args{replace_constructor} ) {
1058         my $class = $self->name;
1059         warn "Not inlining a constructor for $class since it defines"
1060             . " its own constructor.\n"
1061             . "If you are certain you don't need to inline your"
1062             . " constructor, specify inline_constructor => 0 in your"
1063             . " call to $class->meta->make_immutable\n";
1064         return;
1065     }
1066
1067     my $constructor_class = $args{constructor_class};
1068
1069     Class::MOP::load_class($constructor_class);
1070
1071     my $constructor = $constructor_class->new(
1072         options      => \%args,
1073         metaclass    => $self,
1074         is_inline    => 1,
1075         package_name => $self->name,
1076         name         => $name,
1077     );
1078
1079     if ( $args{replace_constructor} or $constructor->can_be_inlined ) {
1080         $self->add_method( $name => $constructor );
1081         $self->_add_inlined_method($constructor);
1082     }
1083 }
1084
1085 sub _inline_destructor {
1086     my ( $self, %args ) = @_;
1087
1088     ( exists $args{destructor_class} && defined $args{destructor_class} )
1089         || confess "The 'inline_destructor' option is present, but "
1090         . "no destructor class was specified";
1091
1092     if ( $self->has_method('DESTROY') && ! $args{replace_destructor} ) {
1093         my $class = $self->name;
1094         warn "Not inlining a destructor for $class since it defines"
1095             . " its own destructor.\n";
1096         return;
1097     }
1098
1099     my $destructor_class = $args{destructor_class};
1100
1101     Class::MOP::load_class($destructor_class);
1102
1103     return unless $destructor_class->is_needed($self);
1104
1105     my $destructor = $destructor_class->new(
1106         options      => \%args,
1107         metaclass    => $self,
1108         package_name => $self->name,
1109         name         => 'DESTROY'
1110     );
1111
1112     if ( $args{replace_destructor} or $destructor->can_be_inlined ) {
1113         $self->add_method( 'DESTROY' => $destructor );
1114         $self->_add_inlined_method($destructor);
1115     }
1116 }
1117
1118 1;
1119
1120 __END__
1121
1122 =pod
1123
1124 =head1 NAME
1125
1126 Class::MOP::Class - Class Meta Object
1127
1128 =head1 SYNOPSIS
1129
1130   # assuming that class Foo
1131   # has been defined, you can
1132
1133   # use this for introspection ...
1134
1135   # add a method to Foo ...
1136   Foo->meta->add_method( 'bar' => sub {...} )
1137
1138   # get a list of all the classes searched
1139   # the method dispatcher in the correct order
1140   Foo->meta->class_precedence_list()
1141
1142   # remove a method from Foo
1143   Foo->meta->remove_method('bar');
1144
1145   # or use this to actually create classes ...
1146
1147   Class::MOP::Class->create(
1148       'Bar' => (
1149           version      => '0.01',
1150           superclasses => ['Foo'],
1151           attributes   => [
1152               Class::MOP::Attribute->new('$bar'),
1153               Class::MOP::Attribute->new('$baz'),
1154           ],
1155           methods => {
1156               calculate_bar => sub {...},
1157               construct_baz => sub {...}
1158           }
1159       )
1160   );
1161
1162 =head1 DESCRIPTION
1163
1164 The Class Protocol is the largest and most complex part of the
1165 Class::MOP meta-object protocol. It controls the introspection and
1166 manipulation of Perl 5 classes, and it can create them as well. The
1167 best way to understand what this module can do is to read the
1168 documentation for each of its methods.
1169
1170 =head1 INHERITANCE
1171
1172 C<Class::MOP::Class> is a subclass of L<Class::MOP::Module>.
1173
1174 =head1 METHODS
1175
1176 =head2 Class construction
1177
1178 These methods all create new C<Class::MOP::Class> objects. These
1179 objects can represent existing classes or they can be used to create
1180 new classes from scratch.
1181
1182 The metaclass object for a given class is a singleton. If you attempt
1183 to create a metaclass for the same class twice, you will just get the
1184 existing object.
1185
1186 =over 4
1187
1188 =item B<< Class::MOP::Class->create($package_name, %options) >>
1189
1190 This method creates a new C<Class::MOP::Class> object with the given
1191 package name. It accepts a number of options:
1192
1193 =over 8
1194
1195 =item * version
1196
1197 An optional version number for the newly created package.
1198
1199 =item * authority
1200
1201 An optional authority for the newly created package.
1202
1203 =item * superclasses
1204
1205 An optional array reference of superclass names.
1206
1207 =item * methods
1208
1209 An optional hash reference of methods for the class. The keys of the
1210 hash reference are method names and values are subroutine references.
1211
1212 =item * attributes
1213
1214 An optional array reference of L<Class::MOP::Attribute> objects.
1215
1216 =back
1217
1218 =item B<< Class::MOP::Class->create_anon_class(%options) >>
1219
1220 This method works just like C<< Class::MOP::Class->create >> but it
1221 creates an "anonymous" class. In fact, the class does have a name, but
1222 that name is a unique name generated internally by this module.
1223
1224 It accepts the same C<superclasses>, C<methods>, and C<attributes>
1225 parameters that C<create> accepts.
1226
1227 Anonymous classes are destroyed once the metaclass they are attached
1228 to goes out of scope, and will be removed from Perl's internal symbol
1229 table.
1230
1231 All instances of an anonymous class keep a special reference to the
1232 metaclass object, which prevents the metaclass from going out of scope
1233 while any instances exist.
1234
1235 This only works if the instance is based on a hash reference, however.
1236
1237 =item B<< Class::MOP::Class->initialize($package_name, %options) >>
1238
1239 This method will initialize a C<Class::MOP::Class> object for the
1240 named package. Unlike C<create>, this method I<will not> create a new
1241 class.
1242
1243 The purpose of this method is to retrieve a C<Class::MOP::Class>
1244 object for introspecting an existing class.
1245
1246 If an existing C<Class::MOP::Class> object exists for the named
1247 package, it will be returned, and any options provided will be
1248 ignored!
1249
1250 If the object does not yet exist, it will be created.
1251
1252 The valid options that can be passed to this method are
1253 C<attribute_metaclass>, C<method_metaclass>,
1254 C<wrapped_method_metaclass>, and C<instance_metaclass>. These are all
1255 optional, and default to the appropriate class in the C<Class::MOP>
1256 distribution.
1257
1258 =back
1259
1260 =head2 Object instance construction and cloning
1261
1262 These methods are all related to creating and/or cloning object
1263 instances.
1264
1265 =over 4
1266
1267 =item B<< $metaclass->clone_object($instance, %params) >>
1268
1269 This method clones an existing object instance. Any parameters you
1270 provide are will override existing attribute values in the object.
1271
1272 This is a convenience method for cloning an object instance, then
1273 blessing it into the appropriate package.
1274
1275 You could implement a clone method in your class, using this method:
1276
1277   sub clone {
1278       my ($self, %params) = @_;
1279       $self->meta->clone_object($self, %params);
1280   }
1281
1282 =item B<< $metaclass->rebless_instance($instance, %params) >>
1283
1284 This method changes the class of C<$instance> to the metaclass's class.
1285
1286 You can only rebless an instance into a subclass of its current
1287 class. If you pass any additional parameters, these will be treated
1288 like constructor parameters and used to initialize the object's
1289 attributes. Any existing attributes that are already set will be
1290 overwritten.
1291
1292 Before reblessing the instance, this method will call
1293 C<rebless_instance_away> on the instance's current metaclass. This method
1294 will be passed the instance, the new metaclass, and any parameters
1295 specified to C<rebless_instance>. By default, C<rebless_instance_away>
1296 does nothing; it is merely a hook.
1297
1298 =item B<< $metaclass->new_object(%params) >>
1299
1300 This method is used to create a new object of the metaclass's
1301 class. Any parameters you provide are used to initialize the
1302 instance's attributes. A special C<__INSTANCE__> key can be passed to
1303 provide an already generated instance, rather than having Class::MOP
1304 generate it for you. This is mostly useful for using Class::MOP with
1305 foreign classes which generate instances using their own constructors.
1306
1307 =item B<< $metaclass->instance_metaclass >>
1308
1309 Returns the class name of the instance metaclass. See
1310 L<Class::MOP::Instance> for more information on the instance
1311 metaclass.
1312
1313 =item B<< $metaclass->get_meta_instance >>
1314
1315 Returns an instance of the C<instance_metaclass> to be used in the
1316 construction of a new instance of the class.
1317
1318 =back
1319
1320 =head2 Informational predicates
1321
1322 These are a few predicate methods for asking information about the
1323 class itself.
1324
1325 =over 4
1326
1327 =item B<< $metaclass->is_anon_class >>
1328
1329 This returns true if the class was created by calling C<<
1330 Class::MOP::Class->create_anon_class >>.
1331
1332 =item B<< $metaclass->is_mutable >>
1333
1334 This returns true if the class is still mutable.
1335
1336 =item B<< $metaclass->is_immutable >>
1337
1338 This returns true if the class has been made immutable.
1339
1340 =item B<< $metaclass->is_pristine >>
1341
1342 A class is I<not> pristine if it has non-inherited attributes or if it
1343 has any generated methods.
1344
1345 =back
1346
1347 =head2 Inheritance Relationships
1348
1349 =over 4
1350
1351 =item B<< $metaclass->superclasses(@superclasses) >>
1352
1353 This is a read-write accessor which represents the superclass
1354 relationships of the metaclass's class.
1355
1356 This is basically sugar around getting and setting C<@ISA>.
1357
1358 =item B<< $metaclass->class_precedence_list >>
1359
1360 This returns a list of all of the class's ancestor classes. The
1361 classes are returned in method dispatch order.
1362
1363 =item B<< $metaclass->linearized_isa >>
1364
1365 This returns a list based on C<class_precedence_list> but with all
1366 duplicates removed.
1367
1368 =item B<< $metaclass->subclasses >>
1369
1370 This returns a list of all subclasses for this class, even indirect
1371 subclasses.
1372
1373 =item B<< $metaclass->direct_subclasses >>
1374
1375 This returns a list of immediate subclasses for this class, which does not
1376 include indirect subclasses.
1377
1378 =back
1379
1380 =head2 Method introspection
1381
1382 See L<Class::MOP::Package/Method introspection and creation> for
1383 methods that operate only on the current class.  Class::MOP::Class adds
1384 introspection capabilities that take inheritance into account.
1385
1386 =over 4
1387
1388 =item B<< $metaclass->get_all_methods >>
1389
1390 This will traverse the inheritance hierarchy and return a list of all
1391 the L<Class::MOP::Method> objects for this class and its parents.
1392
1393 =item B<< $metaclass->find_method_by_name($method_name) >>
1394
1395 This will return a L<Class::MOP::Method> for the specified
1396 C<$method_name>. If the class does not have the specified method, it
1397 returns C<undef>
1398
1399 Unlike C<get_method>, this method I<will> look for the named method in
1400 superclasses.
1401
1402 =item B<< $metaclass->get_all_method_names >>
1403
1404 This will return a list of method I<names> for all of this class's
1405 methods, including inherited methods.
1406
1407 =item B<< $metaclass->find_all_methods_by_name($method_name) >>
1408
1409 This method looks for the named method in the class and all of its
1410 parents. It returns every matching method it finds in the inheritance
1411 tree, so it returns a list of methods.
1412
1413 Each method is returned as a hash reference with three keys. The keys
1414 are C<name>, C<class>, and C<code>. The C<code> key has a
1415 L<Class::MOP::Method> object as its value.
1416
1417 The list of methods is distinct.
1418
1419 =item B<< $metaclass->find_next_method_by_name($method_name) >>
1420
1421 This method returns the first method in any superclass matching the
1422 given name. It is effectively the method that C<SUPER::$method_name>
1423 would dispatch to.
1424
1425 =back
1426
1427 =head2 Attribute introspection and creation
1428
1429 Because Perl 5 does not have a core concept of attributes in classes,
1430 we can only return information about attributes which have been added
1431 via this class's methods. We cannot discover information about
1432 attributes which are defined in terms of "regular" Perl 5 methods.
1433
1434 =over 4
1435
1436 =item B<< $metaclass->get_attribute($attribute_name) >>
1437
1438 This will return a L<Class::MOP::Attribute> for the specified
1439 C<$attribute_name>. If the class does not have the specified
1440 attribute, it returns C<undef>.
1441
1442 NOTE that get_attribute does not search superclasses, for that you
1443 need to use C<find_attribute_by_name>.
1444
1445 =item B<< $metaclass->has_attribute($attribute_name) >>
1446
1447 Returns a boolean indicating whether or not the class defines the
1448 named attribute. It does not include attributes inherited from parent
1449 classes.
1450
1451 =item B<< $metaclass->get_attribute_list >>
1452
1453 This will return a list of attributes I<names> for all attributes
1454 defined in this class.
1455
1456 =item B<< $metaclass->get_all_attributes >>
1457
1458 This will traverse the inheritance hierarchy and return a list of all
1459 the L<Class::MOP::Attribute> objects for this class and its parents.
1460
1461 =item B<< $metaclass->find_attribute_by_name($attribute_name) >>
1462
1463 This will return a L<Class::MOP::Attribute> for the specified
1464 C<$attribute_name>. If the class does not have the specified
1465 attribute, it returns C<undef>.
1466
1467 Unlike C<get_attribute>, this attribute I<will> look for the named
1468 attribute in superclasses.
1469
1470 =item B<< $metaclass->add_attribute(...) >>
1471
1472 This method accepts either an existing L<Class::MOP::Attribute>
1473 object or parameters suitable for passing to that class's C<new>
1474 method.
1475
1476 The attribute provided will be added to the class.
1477
1478 Any accessor methods defined by the attribute will be added to the
1479 class when the attribute is added.
1480
1481 If an attribute of the same name already exists, the old attribute
1482 will be removed first.
1483
1484 =item B<< $metaclass->remove_attribute($attribute_name) >>
1485
1486 This will remove the named attribute from the class, and
1487 L<Class::MOP::Attribute> object.
1488
1489 Removing an attribute also removes any accessor methods defined by the
1490 attribute.
1491
1492 However, note that removing an attribute will only affect I<future>
1493 object instances created for this class, not existing instances.
1494
1495 =item B<< $metaclass->attribute_metaclass >>
1496
1497 Returns the class name of the attribute metaclass for this class. By
1498 default, this is L<Class::MOP::Attribute>.
1499
1500 =back
1501
1502 =head2 Class Immutability
1503
1504 Making a class immutable "freezes" the class definition. You can no
1505 longer call methods which alter the class, such as adding or removing
1506 methods or attributes.
1507
1508 Making a class immutable lets us optimize the class by inlining some
1509 methods, and also allows us to optimize some methods on the metaclass
1510 object itself.
1511
1512 After immutabilization, the metaclass object will cache most informational
1513 methods that returns information about methods or attributes. Methods which
1514 would alter the class, such as C<add_attribute> and C<add_method>, will
1515 throw an error on an immutable metaclass object.
1516
1517 The immutabilization system in L<Moose> takes much greater advantage
1518 of the inlining features than Class::MOP itself does.
1519
1520 =over 4
1521
1522 =item B<< $metaclass->make_immutable(%options) >>
1523
1524 This method will create an immutable transformer and use it to make
1525 the class and its metaclass object immutable.
1526
1527 This method accepts the following options:
1528
1529 =over 8
1530
1531 =item * inline_accessors
1532
1533 =item * inline_constructor
1534
1535 =item * inline_destructor
1536
1537 These are all booleans indicating whether the specified method(s)
1538 should be inlined.
1539
1540 By default, accessors and the constructor are inlined, but not the
1541 destructor.
1542
1543 =item * immutable_trait
1544
1545 The name of a class which will be used as a parent class for the
1546 metaclass object being made immutable. This "trait" implements the
1547 post-immutability functionality of the metaclass (but not the
1548 transformation itself).
1549
1550 This defaults to L<Class::MOP::Class::Immutable::Trait>.
1551
1552 =item * constructor_name
1553
1554 This is the constructor method name. This defaults to "new".
1555
1556 =item * constructor_class
1557
1558 The name of the method metaclass for constructors. It will be used to
1559 generate the inlined constructor. This defaults to
1560 "Class::MOP::Method::Constructor".
1561
1562 =item * replace_constructor
1563
1564 This is a boolean indicating whether an existing constructor should be
1565 replaced when inlining a constructor. This defaults to false.
1566
1567 =item * destructor_class
1568
1569 The name of the method metaclass for destructors. It will be used to
1570 generate the inlined destructor. This defaults to
1571 "Class::MOP::Method::Denstructor".
1572
1573 =item * replace_destructor
1574
1575 This is a boolean indicating whether an existing destructor should be
1576 replaced when inlining a destructor. This defaults to false.
1577
1578 =back
1579
1580 =item B<< $metaclass->immutable_options >>
1581
1582 Returns a hash of the options used when making the class immutable, including
1583 both defaults and anything supplied by the user in the call to C<<
1584 $metaclass->make_immutable >>. This is useful if you need to temporarily make
1585 a class mutable and then restore immutability as it was before.
1586
1587 =item B<< $metaclass->make_mutable >>
1588
1589 Calling this method reverse the immutabilization transformation.
1590
1591 =back
1592
1593 =head2 Method Modifiers
1594
1595 Method modifiers are hooks which allow a method to be wrapped with
1596 I<before>, I<after> and I<around> method modifiers. Every time a
1597 method is called, its modifiers are also called.
1598
1599 A class can modify its own methods, as well as methods defined in
1600 parent classes.
1601
1602 =head3 How method modifiers work?
1603
1604 Method modifiers work by wrapping the original method and then
1605 replacing it in the class's symbol table. The wrappers will handle
1606 calling all the modifiers in the appropriate order and preserving the
1607 calling context for the original method.
1608
1609 The return values of C<before> and C<after> modifiers are
1610 ignored. This is because their purpose is B<not> to filter the input
1611 and output of the primary method (this is done with an I<around>
1612 modifier).
1613
1614 This may seem like an odd restriction to some, but doing this allows
1615 for simple code to be added at the beginning or end of a method call
1616 without altering the function of the wrapped method or placing any
1617 extra responsibility on the code of the modifier.
1618
1619 Of course if you have more complex needs, you can use the C<around>
1620 modifier which allows you to change both the parameters passed to the
1621 wrapped method, as well as its return value.
1622
1623 Before and around modifiers are called in last-defined-first-called
1624 order, while after modifiers are called in first-defined-first-called
1625 order. So the call tree might looks something like this:
1626
1627   before 2
1628    before 1
1629     around 2
1630      around 1
1631       primary
1632      around 1
1633     around 2
1634    after 1
1635   after 2
1636
1637 =head3 What is the performance impact?
1638
1639 Of course there is a performance cost associated with method
1640 modifiers, but we have made every effort to make that cost directly
1641 proportional to the number of modifier features you use.
1642
1643 The wrapping method does its best to B<only> do as much work as it
1644 absolutely needs to. In order to do this we have moved some of the
1645 performance costs to set-up time, where they are easier to amortize.
1646
1647 All this said, our benchmarks have indicated the following:
1648
1649   simple wrapper with no modifiers             100% slower
1650   simple wrapper with simple before modifier   400% slower
1651   simple wrapper with simple after modifier    450% slower
1652   simple wrapper with simple around modifier   500-550% slower
1653   simple wrapper with all 3 modifiers          1100% slower
1654
1655 These numbers may seem daunting, but you must remember, every feature
1656 comes with some cost. To put things in perspective, just doing a
1657 simple C<AUTOLOAD> which does nothing but extract the name of the
1658 method called and return it costs about 400% over a normal method
1659 call.
1660
1661 =over 4
1662
1663 =item B<< $metaclass->add_before_method_modifier($method_name, $code) >>
1664
1665 This wraps the specified method with the supplied subroutine
1666 reference. The modifier will be called as a method itself, and will
1667 receive the same arguments as are passed to the method.
1668
1669 When the modifier exits, the wrapped method will be called.
1670
1671 The return value of the modifier will be ignored.
1672
1673 =item B<< $metaclass->add_after_method_modifier($method_name, $code) >>
1674
1675 This wraps the specified method with the supplied subroutine
1676 reference. The modifier will be called as a method itself, and will
1677 receive the same arguments as are passed to the method.
1678
1679 When the wrapped methods exits, the modifier will be called.
1680
1681 The return value of the modifier will be ignored.
1682
1683 =item B<< $metaclass->add_around_method_modifier($method_name, $code) >>
1684
1685 This wraps the specified method with the supplied subroutine
1686 reference.
1687
1688 The first argument passed to the modifier will be a subroutine
1689 reference to the wrapped method. The second argument is the object,
1690 and after that come any arguments passed when the method is called.
1691
1692 The around modifier can choose to call the original method, as well as
1693 what arguments to pass if it does so.
1694
1695 The return value of the modifier is what will be seen by the caller.
1696
1697 =back
1698
1699 =head2 Introspection
1700
1701 =over 4
1702
1703 =item B<< Class::MOP::Class->meta >>
1704
1705 This will return a L<Class::MOP::Class> instance for this class.
1706
1707 It should also be noted that L<Class::MOP> will actually bootstrap
1708 this module by installing a number of attribute meta-objects into its
1709 metaclass.
1710
1711 =back
1712
1713 =head1 AUTHORS
1714
1715 Stevan Little E<lt>stevan@iinteractive.comE<gt>
1716
1717 =head1 COPYRIGHT AND LICENSE
1718
1719 Copyright 2006-2009 by Infinity Interactive, Inc.
1720
1721 L<http://www.iinteractive.com>
1722
1723 This library is free software; you can redistribute it and/or modify
1724 it under the same terms as Perl itself.
1725
1726 =cut