sort some things to allow for some better caching
[gitmo/Moose.git] / lib / Moose / Meta / Class.pm
1
2 package Moose::Meta::Class;
3
4 use strict;
5 use warnings;
6
7 use Class::MOP;
8
9 use Carp qw( confess );
10 use Data::OptList;
11 use List::Util qw( first );
12 use List::MoreUtils qw( any all uniq first_index );
13 use Scalar::Util 'blessed';
14
15 use Moose::Meta::Method::Overridden;
16 use Moose::Meta::Method::Augmented;
17 use Moose::Error::Default;
18 use Moose::Meta::Class::Immutable::Trait;
19 use Moose::Meta::Method::Constructor;
20 use Moose::Meta::Method::Destructor;
21 use Moose::Meta::Method::Meta;
22 use Moose::Util;
23 use Class::MOP::MiniTrait;
24
25 use base 'Class::MOP::Class';
26
27 Class::MOP::MiniTrait::apply(__PACKAGE__, 'Moose::Meta::Object::Trait');
28
29 __PACKAGE__->meta->add_attribute('roles' => (
30     reader  => 'roles',
31     default => sub { [] }
32 ));
33
34 __PACKAGE__->meta->add_attribute('role_applications' => (
35     reader  => '_get_role_applications',
36     default => sub { [] }
37 ));
38
39 __PACKAGE__->meta->add_attribute(
40     Class::MOP::Attribute->new('immutable_trait' => (
41         accessor => "immutable_trait",
42         default  => 'Moose::Meta::Class::Immutable::Trait',
43     ))
44 );
45
46 __PACKAGE__->meta->add_attribute('constructor_class' => (
47     accessor => 'constructor_class',
48     default  => 'Moose::Meta::Method::Constructor',
49 ));
50
51 __PACKAGE__->meta->add_attribute('destructor_class' => (
52     accessor => 'destructor_class',
53     default  => 'Moose::Meta::Method::Destructor',
54 ));
55
56 __PACKAGE__->meta->add_attribute('error_class' => (
57     accessor => 'error_class',
58     default  => 'Moose::Error::Default',
59 ));
60
61 sub initialize {
62     my $class = shift;
63     my @args = @_;
64     unshift @args, 'package' if @args % 2;
65     my %opts = @args;
66     my $package = delete $opts{package};
67     return Class::MOP::get_metaclass_by_name($package)
68         || $class->SUPER::initialize($package,
69                 'attribute_metaclass' => 'Moose::Meta::Attribute',
70                 'method_metaclass'    => 'Moose::Meta::Method',
71                 'instance_metaclass'  => 'Moose::Meta::Instance',
72                 %opts,
73             );
74 }
75
76 sub create {
77     my $class = shift;
78     my @args = @_;
79
80     unshift @args, 'package' if @args % 2 == 1;
81     my %options = @args;
82
83     (ref $options{roles} eq 'ARRAY')
84         || $class->throw_error("You must pass an ARRAY ref of roles", data => $options{roles})
85             if exists $options{roles};
86
87     my $package = delete $options{package};
88     my $roles   = delete $options{roles};
89
90     my $new_meta = $class->SUPER::create($package, %options);
91
92     if ($roles) {
93         Moose::Util::apply_all_roles( $new_meta, @$roles );
94     }
95
96     return $new_meta;
97 }
98
99 sub _meta_method_class { 'Moose::Meta::Method::Meta' }
100
101 sub _anon_package_prefix { 'Moose::Meta::Class::__ANON__::SERIAL::' }
102
103 sub _anon_cache_key {
104     my $class = shift;
105     my %options = @_;
106
107     my $superclass_key = join('|',
108         map { $_->[0] } @{ Data::OptList::mkopt($options{superclasses} || []) }
109     );
110
111     my $roles = Data::OptList::mkopt(($options{roles} || []), {
112         moniker  => 'role',
113         val_test => sub { ref($_[0]) eq 'HASH' },
114     });
115
116     my @role_keys;
117     for my $role_spec (@$roles) {
118         my ($role, $params) = @$role_spec;
119         $params = { %$params } if $params;
120
121         my $key = blessed($role) ? $role->name : $role;
122
123         if ($params && %$params) {
124             my $alias    = delete $params->{'-alias'}
125                         || delete $params->{'alias'}
126                         || {};
127             my $excludes = delete $params->{'-excludes'}
128                         || delete $params->{'excludes'}
129                         || [];
130             $excludes = [$excludes] unless ref($excludes) eq 'ARRAY';
131
132             if (%$params) {
133                 # disable this warning until 2.02
134                 # warn "Roles with parameters cannot be cached. Consider "
135                 #    . "applying the parameters before calling "
136                 #    . "create_anon_class, or using 'weaken => 0' instead";
137                 return;
138             }
139
140             $key .= '<' . join('+', 'a', join('%', sort %$alias),
141                                     'e', join('%', sort @$excludes)) . '>';
142         }
143
144         push @role_keys, $key;
145     }
146
147     my $role_key = join('|', sort @role_keys);
148
149     # Makes something like Super::Class|Super::Class::2=Role|Role::1
150     return join('=', $superclass_key, $role_key);
151 }
152
153 sub reinitialize {
154     my $self = shift;
155     my $pkg  = shift;
156
157     my $meta = blessed $pkg ? $pkg : Class::MOP::class_of($pkg);
158
159     my %existing_classes;
160     if ($meta) {
161         %existing_classes = map { $_ => $meta->$_() } qw(
162             attribute_metaclass
163             method_metaclass
164             wrapped_method_metaclass
165             instance_metaclass
166             constructor_class
167             destructor_class
168             error_class
169         );
170     }
171
172     return $self->SUPER::reinitialize(
173         $pkg,
174         %existing_classes,
175         @_,
176     );
177 }
178
179 sub add_role {
180     my ($self, $role) = @_;
181     (blessed($role) && $role->isa('Moose::Meta::Role'))
182         || $self->throw_error("Roles must be instances of Moose::Meta::Role", data => $role);
183     push @{$self->roles} => $role;
184 }
185
186 sub role_applications {
187     my ($self) = @_;
188
189     return @{$self->_get_role_applications};
190 }
191
192 sub add_role_application {
193     my ($self, $application) = @_;
194     (blessed($application) && $application->isa('Moose::Meta::Role::Application::ToClass'))
195         || $self->throw_error("Role applications must be instances of Moose::Meta::Role::Application::ToClass", data => $application);
196     push @{$self->_get_role_applications} => $application;
197 }
198
199 sub calculate_all_roles {
200     my $self = shift;
201     my %seen;
202     grep { !$seen{$_->name}++ } map { $_->calculate_all_roles } @{ $self->roles };
203 }
204
205 sub calculate_all_roles_with_inheritance {
206     my $self = shift;
207     my %seen;
208     grep { !$seen{$_->name}++ }
209          map { Class::MOP::class_of($_)->can('calculate_all_roles')
210                    ? Class::MOP::class_of($_)->calculate_all_roles
211                    : () }
212              $self->linearized_isa;
213 }
214
215 sub does_role {
216     my ($self, $role_name) = @_;
217
218     (defined $role_name)
219         || $self->throw_error("You must supply a role name to look for");
220
221     foreach my $class ($self->class_precedence_list) {
222         my $meta = Class::MOP::class_of($class);
223         # when a Moose metaclass is itself extended with a role,
224         # this check needs to be done since some items in the
225         # class_precedence_list might in fact be Class::MOP
226         # based still.
227         next unless $meta && $meta->can('roles');
228         foreach my $role (@{$meta->roles}) {
229             return 1 if $role->does_role($role_name);
230         }
231     }
232     return 0;
233 }
234
235 sub excludes_role {
236     my ($self, $role_name) = @_;
237
238     (defined $role_name)
239         || $self->throw_error("You must supply a role name to look for");
240
241     foreach my $class ($self->class_precedence_list) {
242         my $meta = Class::MOP::class_of($class);
243         # when a Moose metaclass is itself extended with a role,
244         # this check needs to be done since some items in the
245         # class_precedence_list might in fact be Class::MOP
246         # based still.
247         next unless $meta && $meta->can('roles');
248         foreach my $role (@{$meta->roles}) {
249             return 1 if $role->excludes_role($role_name);
250         }
251     }
252     return 0;
253 }
254
255 sub new_object {
256     my $self   = shift;
257     my $params = @_ == 1 ? $_[0] : {@_};
258     my $object = $self->SUPER::new_object($params);
259
260     foreach my $attr ( $self->get_all_attributes() ) {
261
262         next unless $attr->can('has_trigger') && $attr->has_trigger;
263
264         my $init_arg = $attr->init_arg;
265
266         next unless defined $init_arg;
267
268         next unless exists $params->{$init_arg};
269
270         $attr->trigger->(
271             $object,
272             (
273                   $attr->should_coerce
274                 ? $attr->get_read_method_ref->($object)
275                 : $params->{$init_arg}
276             ),
277         );
278     }
279
280     $object->BUILDALL($params) if $object->can('BUILDALL');
281
282     return $object;
283 }
284
285 sub _generate_fallback_constructor {
286     my $self = shift;
287     my ($class) = @_;
288     return $class . '->Moose::Object::new(@_)'
289 }
290
291 sub _inline_params {
292     my $self = shift;
293     my ($params, $class) = @_;
294     return (
295         'my ' . $params . ' = ',
296         $self->_inline_BUILDARGS($class, '@_'),
297         ';',
298     );
299 }
300
301 sub _inline_BUILDARGS {
302     my $self = shift;
303     my ($class, $args) = @_;
304
305     my $buildargs = $self->find_method_by_name("BUILDARGS");
306
307     if ($args eq '@_'
308      && (!$buildargs or $buildargs->body == \&Moose::Object::BUILDARGS)) {
309         return (
310             'do {',
311                 'my $params;',
312                 'if (scalar @_ == 1) {',
313                     'if (!defined($_[0]) || ref($_[0]) ne \'HASH\') {',
314                         $self->_inline_throw_error(
315                             '"Single parameters to new() must be a HASH ref"',
316                             'data => $_[0]',
317                         ) . ';',
318                     '}',
319                     '$params = { %{ $_[0] } };',
320                 '}',
321                 'elsif (@_ % 2) {',
322                     'Carp::carp(',
323                         '"The new() method for ' . $class . ' expects a '
324                       . 'hash reference or a key/value list. You passed an '
325                       . 'odd number of arguments"',
326                     ');',
327                     '$params = {@_, undef};',
328                 '}',
329                 'else {',
330                     '$params = {@_};',
331                 '}',
332                 '$params;',
333             '}',
334         );
335     }
336     else {
337         return $class . '->BUILDARGS(' . $args . ')';
338     }
339 }
340
341 sub _inline_slot_initializer {
342     my $self  = shift;
343     my ($attr, $idx) = @_;
344
345     return (
346         '## ' . $attr->name,
347         $self->_inline_check_required_attr($attr),
348         $self->SUPER::_inline_slot_initializer(@_),
349     );
350 }
351
352 sub _inline_check_required_attr {
353     my $self = shift;
354     my ($attr) = @_;
355
356     return unless defined $attr->init_arg;
357     return unless $attr->can('is_required') && $attr->is_required;
358     return if $attr->has_default || $attr->has_builder;
359
360     return (
361         'if (!exists $params->{\'' . $attr->init_arg . '\'}) {',
362             $self->_inline_throw_error(
363                 '"Attribute (' . quotemeta($attr->name) . ') is required"'
364             ) . ';',
365         '}',
366     );
367 }
368
369 # XXX: these two are duplicated from cmop, because we have to pass the tc stuff
370 # through to _inline_set_value - this should probably be fixed, but i'm not
371 # quite sure how. -doy
372 sub _inline_init_attr_from_constructor {
373     my $self = shift;
374     my ($attr, $idx) = @_;
375
376     my @initial_value = $attr->_inline_set_value(
377         '$instance',
378         '$params->{\'' . $attr->init_arg . '\'}',
379         '$type_constraint_bodies[' . $idx . ']',
380         '$type_constraints[' . $idx . ']',
381         'for constructor',
382     );
383
384     push @initial_value, (
385         '$attrs->[' . $idx . ']->set_initial_value(',
386             '$instance,',
387             $attr->_inline_instance_get('$instance'),
388         ');',
389     ) if $attr->has_initializer;
390
391     return @initial_value;
392 }
393
394 sub _inline_init_attr_from_default {
395     my $self = shift;
396     my ($attr, $idx) = @_;
397
398     return if $attr->can('is_lazy') && $attr->is_lazy;
399     my $default = $self->_inline_default_value($attr, $idx);
400     return unless $default;
401
402     my @initial_value = (
403         'my $default = ' . $default . ';',
404         $attr->_inline_set_value(
405             '$instance',
406             '$default',
407             '$type_constraint_bodies[' . $idx . ']',
408             '$type_constraints[' . $idx . ']',
409             'for constructor',
410         ),
411     );
412
413     push @initial_value, (
414         '$attrs->[' . $idx . ']->set_initial_value(',
415             '$instance,',
416             $attr->_inline_instance_get('$instance'),
417         ');',
418     ) if $attr->has_initializer;
419
420     return @initial_value;
421 }
422
423 sub _inline_extra_init {
424     my $self = shift;
425     return (
426         $self->_inline_triggers,
427         $self->_inline_BUILDALL,
428     );
429 }
430
431 sub _inline_triggers {
432     my $self = shift;
433     my @trigger_calls;
434
435     my @attrs = sort { $a->name cmp $b->name } $self->get_all_attributes;
436     for my $i (0 .. $#attrs) {
437         my $attr = $attrs[$i];
438
439         next unless $attr->can('has_trigger') && $attr->has_trigger;
440
441         my $init_arg = $attr->init_arg;
442         next unless defined $init_arg;
443
444         push @trigger_calls,
445             'if (exists $params->{\'' . $init_arg . '\'}) {',
446                 '$attrs->[' . $i . ']->trigger->(',
447                     '$instance,',
448                     $attr->_inline_instance_get('$instance') . ',',
449                 ');',
450             '}';
451     }
452
453     return @trigger_calls;
454 }
455
456 sub _inline_BUILDALL {
457     my $self = shift;
458
459     my @methods = reverse $self->find_all_methods_by_name('BUILD');
460     my @BUILD_calls;
461
462     foreach my $method (@methods) {
463         push @BUILD_calls,
464             '$instance->' . $method->{class} . '::BUILD($params);';
465     }
466
467     return @BUILD_calls;
468 }
469
470 sub superclasses {
471     my $self = shift;
472     my $supers = Data::OptList::mkopt(\@_);
473     foreach my $super (@{ $supers }) {
474         my ($name, $opts) = @{ $super };
475         Class::MOP::load_class($name, $opts);
476         my $meta = Class::MOP::class_of($name);
477         $self->throw_error("You cannot inherit from a Moose Role ($name)")
478             if $meta && $meta->isa('Moose::Meta::Role')
479     }
480     return $self->SUPER::superclasses(map { $_->[0] } @{ $supers });
481 }
482
483 ### ---------------------------------------------
484
485 sub add_attribute {
486     my $self = shift;
487     my $attr =
488         (blessed $_[0] && $_[0]->isa('Class::MOP::Attribute')
489             ? $_[0]
490             : $self->_process_attribute(@_));
491     $self->SUPER::add_attribute($attr);
492     # it may be a Class::MOP::Attribute, theoretically, which doesn't have
493     # 'bare' and doesn't implement this method
494     if ($attr->can('_check_associated_methods')) {
495         $attr->_check_associated_methods;
496     }
497     return $attr;
498 }
499
500 sub add_override_method_modifier {
501     my ($self, $name, $method, $_super_package) = @_;
502
503     (!$self->has_method($name))
504         || $self->throw_error("Cannot add an override method if a local method is already present");
505
506     $self->add_method($name => Moose::Meta::Method::Overridden->new(
507         method  => $method,
508         class   => $self,
509         package => $_super_package, # need this for roles
510         name    => $name,
511     ));
512 }
513
514 sub add_augment_method_modifier {
515     my ($self, $name, $method) = @_;
516     (!$self->has_method($name))
517         || $self->throw_error("Cannot add an augment method if a local method is already present");
518
519     $self->add_method($name => Moose::Meta::Method::Augmented->new(
520         method  => $method,
521         class   => $self,
522         name    => $name,
523     ));
524 }
525
526 ## Private Utility methods ...
527
528 sub _find_next_method_by_name_which_is_not_overridden {
529     my ($self, $name) = @_;
530     foreach my $method ($self->find_all_methods_by_name($name)) {
531         return $method->{code}
532             if blessed($method->{code}) && !$method->{code}->isa('Moose::Meta::Method::Overridden');
533     }
534     return undef;
535 }
536
537 ## Metaclass compatibility
538
539 sub _base_metaclasses {
540     my $self = shift;
541     my %metaclasses = $self->SUPER::_base_metaclasses;
542     for my $class (keys %metaclasses) {
543         $metaclasses{$class} =~ s/^Class::MOP/Moose::Meta/;
544     }
545     return (
546         %metaclasses,
547         error_class => 'Moose::Error::Default',
548     );
549 }
550
551 sub _fix_class_metaclass_incompatibility {
552     my $self = shift;
553     my ($super_meta) = @_;
554
555     $self->SUPER::_fix_class_metaclass_incompatibility(@_);
556
557     if ($self->_class_metaclass_can_be_made_compatible($super_meta)) {
558         ($self->is_pristine)
559             || confess "Can't fix metaclass incompatibility for "
560                      . $self->name
561                      . " because it is not pristine.";
562         my $super_meta_name = $super_meta->_real_ref_name;
563         my $class_meta_subclass_meta_name = Moose::Util::_reconcile_roles_for_metaclass(blessed($self), $super_meta_name);
564         my $new_self = $class_meta_subclass_meta_name->reinitialize(
565             $self->name,
566         );
567
568         $self->_replace_self( $new_self, $class_meta_subclass_meta_name );
569     }
570 }
571
572 sub _fix_single_metaclass_incompatibility {
573     my $self = shift;
574     my ($metaclass_type, $super_meta) = @_;
575
576     $self->SUPER::_fix_single_metaclass_incompatibility(@_);
577
578     if ($self->_single_metaclass_can_be_made_compatible($super_meta, $metaclass_type)) {
579         ($self->is_pristine)
580             || confess "Can't fix metaclass incompatibility for "
581                      . $self->name
582                      . " because it is not pristine.";
583         my $super_meta_name = $super_meta->_real_ref_name;
584         my $class_specific_meta_subclass_meta_name = Moose::Util::_reconcile_roles_for_metaclass($self->$metaclass_type, $super_meta->$metaclass_type);
585         my $new_self = $super_meta->reinitialize(
586             $self->name,
587             $metaclass_type => $class_specific_meta_subclass_meta_name,
588         );
589
590         $self->_replace_self( $new_self, $super_meta_name );
591     }
592 }
593
594 sub _replace_self {
595     my $self      = shift;
596     my ( $new_self, $new_class)   = @_;
597
598     %$self = %$new_self;
599     bless $self, $new_class;
600
601     # We need to replace the cached metaclass instance or else when it goes
602     # out of scope Class::MOP::Class destroy's the namespace for the
603     # metaclass's class, causing much havoc.
604     my $weaken = Class::MOP::metaclass_is_weak( $self->name );
605     Class::MOP::store_metaclass_by_name( $self->name, $self );
606     Class::MOP::weaken_metaclass( $self->name ) if $weaken;
607 }
608
609 sub _process_attribute {
610     my ( $self, $name, @args ) = @_;
611
612     @args = %{$args[0]} if scalar @args == 1 && ref($args[0]) eq 'HASH';
613
614     if (($name || '') =~ /^\+(.*)/) {
615         return $self->_process_inherited_attribute($1, @args);
616     }
617     else {
618         return $self->_process_new_attribute($name, @args);
619     }
620 }
621
622 sub _process_new_attribute {
623     my ( $self, $name, @args ) = @_;
624
625     $self->attribute_metaclass->interpolate_class_and_new($name, @args);
626 }
627
628 sub _process_inherited_attribute {
629     my ($self, $attr_name, %options) = @_;
630     my $inherited_attr = $self->find_attribute_by_name($attr_name);
631     (defined $inherited_attr)
632         || $self->throw_error("Could not find an attribute by the name of '$attr_name' to inherit from in ${\$self->name}", data => $attr_name);
633     if ($inherited_attr->isa('Moose::Meta::Attribute')) {
634         return $inherited_attr->clone_and_inherit_options(%options);
635     }
636     else {
637         # NOTE:
638         # kind of a kludge to handle Class::MOP::Attributes
639         return $inherited_attr->Moose::Meta::Attribute::clone_and_inherit_options(%options);
640     }
641 }
642
643 # reinitialization support
644
645 sub _restore_metaobjects_from {
646     my $self = shift;
647     my ($old_meta) = @_;
648
649     $self->SUPER::_restore_metaobjects_from($old_meta);
650
651     for my $role ( @{ $old_meta->roles } ) {
652         $self->add_role($role);
653     }
654
655     for my $application ( @{ $old_meta->_get_role_applications } ) {
656         $application->class($self);
657         $self->add_role_application ($application);
658     }
659 }
660
661 ## Immutability
662
663 sub _immutable_options {
664     my ( $self, @args ) = @_;
665
666     $self->SUPER::_immutable_options(
667         inline_destructor => 1,
668
669         # Moose always does this when an attribute is created
670         inline_accessors => 0,
671
672         @args,
673     );
674 }
675
676 ## -------------------------------------------------
677
678 our $error_level;
679
680 sub throw_error {
681     my ( $self, @args ) = @_;
682     local $error_level = ($error_level || 0) + 1;
683     $self->raise_error($self->create_error(@args));
684 }
685
686 sub _inline_throw_error {
687     my ( $self, $msg, $args ) = @_;
688     "\$meta->throw_error($msg" . ($args ? ", $args" : "") . ")"; # FIXME makes deparsing *REALLY* hard
689 }
690
691 sub raise_error {
692     my ( $self, @args ) = @_;
693     die @args;
694 }
695
696 sub create_error {
697     my ( $self, @args ) = @_;
698
699     require Carp::Heavy;
700
701     local $error_level = ($error_level || 0 ) + 1;
702
703     if ( @args % 2 == 1 ) {
704         unshift @args, "message";
705     }
706
707     my %args = ( metaclass => $self, last_error => $@, @args );
708
709     $args{depth} += $error_level;
710
711     my $class = ref $self ? $self->error_class : "Moose::Error::Default";
712
713     Class::MOP::load_class($class);
714
715     $class->new(
716         Carp::caller_info($args{depth}),
717         %args
718     );
719 }
720
721 1;
722
723 # ABSTRACT: The Moose metaclass
724
725 __END__
726
727 =pod
728
729 =head1 DESCRIPTION
730
731 This class is a subclass of L<Class::MOP::Class> that provides
732 additional Moose-specific functionality.
733
734 To really understand this class, you will need to start with the
735 L<Class::MOP::Class> documentation. This class can be understood as a
736 set of additional features on top of the basic feature provided by
737 that parent class.
738
739 =head1 INHERITANCE
740
741 C<Moose::Meta::Class> is a subclass of L<Class::MOP::Class>.
742
743 =head1 METHODS
744
745 =over 4
746
747 =item B<< Moose::Meta::Class->initialize($package_name, %options) >>
748
749 This overrides the parent's method in order to provide its own
750 defaults for the C<attribute_metaclass>, C<instance_metaclass>, and
751 C<method_metaclass> options.
752
753 These all default to the appropriate Moose class.
754
755 =item B<< Moose::Meta::Class->create($package_name, %options) >>
756
757 This overrides the parent's method in order to accept a C<roles>
758 option. This should be an array reference containing roles
759 that the class does, each optionally followed by a hashref of options
760 (C<-excludes> and C<-alias>).
761
762   my $metaclass = Moose::Meta::Class->create( 'New::Class', roles => [...] );
763
764 =item B<< Moose::Meta::Class->create_anon_class >>
765
766 This overrides the parent's method to accept a C<roles> option, just
767 as C<create> does.
768
769 It also accepts a C<cache> option. If this is true, then the anonymous
770 class will be cached based on its superclasses and roles. If an
771 existing anonymous class in the cache has the same superclasses and
772 roles, it will be reused.
773
774   my $metaclass = Moose::Meta::Class->create_anon_class(
775       superclasses => ['Foo'],
776       roles        => [qw/Some Roles Go Here/],
777       cache        => 1,
778   );
779
780 Each entry in both the C<superclasses> and the C<roles> option can be
781 followed by a hash reference with arguments. The C<superclasses>
782 option can be supplied with a L<-version|Class::MOP/Class Loading
783 Options> option that ensures the loaded superclass satisfies the
784 required version. The C<role> option also takes the C<-version> as an
785 argument, but the option hash reference can also contain any other
786 role relevant values like exclusions or parameterized role arguments.
787
788 =item B<< $metaclass->make_immutable(%options) >>
789
790 This overrides the parent's method to add a few options. Specifically,
791 it uses the Moose-specific constructor and destructor classes, and
792 enables inlining the destructor.
793
794 Since Moose always inlines attributes, it sets the C<inline_accessors> option
795 to false.
796
797 =item B<< $metaclass->new_object(%params) >>
798
799 This overrides the parent's method in order to add support for
800 attribute triggers.
801
802 =item B<< $metaclass->superclasses(@superclasses) >>
803
804 This is the accessor allowing you to read or change the parents of
805 the class.
806
807 Each superclass can be followed by a hash reference containing a
808 L<-version|Class::MOP/Class Loading Options> value. If the version
809 requirement is not satisfied an error will be thrown.
810
811 =item B<< $metaclass->add_override_method_modifier($name, $sub) >>
812
813 This adds an C<override> method modifier to the package.
814
815 =item B<< $metaclass->add_augment_method_modifier($name, $sub) >>
816
817 This adds an C<augment> method modifier to the package.
818
819 =item B<< $metaclass->calculate_all_roles >>
820
821 This will return a unique array of C<Moose::Meta::Role> instances
822 which are attached to this class.
823
824 =item B<< $metaclass->calculate_all_roles_with_inheritance >>
825
826 This will return a unique array of C<Moose::Meta::Role> instances
827 which are attached to this class, and each of this class's ancestors.
828
829 =item B<< $metaclass->add_role($role) >>
830
831 This takes a L<Moose::Meta::Role> object, and adds it to the class's
832 list of roles. This I<does not> actually apply the role to the class.
833
834 =item B<< $metaclass->role_applications >>
835
836 Returns a list of L<Moose::Meta::Role::Application::ToClass>
837 objects, which contain the arguments to role application.
838
839 =item B<< $metaclass->add_role_application($application) >>
840
841 This takes a L<Moose::Meta::Role::Application::ToClass> object, and
842 adds it to the class's list of role applications. This I<does not>
843 actually apply any role to the class; it is only for tracking role
844 applications.
845
846 =item B<< $metaclass->does_role($role) >>
847
848 This returns a boolean indicating whether or not the class does the specified
849 role. The role provided can be either a role name or a L<Moose::Meta::Role>
850 object. This tests both the class and its parents.
851
852 =item B<< $metaclass->excludes_role($role_name) >>
853
854 A class excludes a role if it has already composed a role which
855 excludes the named role. This tests both the class and its parents.
856
857 =item B<< $metaclass->add_attribute($attr_name, %params|$params) >>
858
859 This overrides the parent's method in order to allow the parameters to
860 be provided as a hash reference.
861
862 =item B<< $metaclass->constructor_class($class_name) >>
863
864 =item B<< $metaclass->destructor_class($class_name) >>
865
866 These are the names of classes used when making a class immutable. These
867 default to L<Moose::Meta::Method::Constructor> and
868 L<Moose::Meta::Method::Destructor> respectively. These accessors are
869 read-write, so you can use them to change the class name.
870
871 =item B<< $metaclass->error_class($class_name) >>
872
873 The name of the class used to throw errors. This defaults to
874 L<Moose::Error::Default>, which generates an error with a stacktrace
875 just like C<Carp::confess>.
876
877 =item B<< $metaclass->throw_error($message, %extra) >>
878
879 Throws the error created by C<create_error> using C<raise_error>
880
881 =back
882
883 =head1 BUGS
884
885 See L<Moose/BUGS> for details on reporting bugs.
886
887 =cut
888