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