1 package Mouse::Meta::Class;
2 use Mouse::Util qw/:meta/; # enables strict and warnings
6 use Mouse::Meta::Module;
7 our @ISA = qw(Mouse::Meta::Module);
9 our @CARP_NOT = qw(Mouse); # trust Mouse
11 sub attribute_metaclass;
14 sub constructor_class;
19 my($class, %args) = @_;
21 $args{attributes} = {};
25 $args{superclasses} = do {
27 \@{ $args{package} . '::ISA' };
30 my $self = bless \%args, ref($class) || $class;
31 if(ref($self) ne __PACKAGE__){
32 $self->meta->_initialize_object($self, \%args);
37 sub create_anon_class{
39 return $self->create(undef, @_);
46 sub calculate_all_roles {
49 return grep { !$seen{ $_->name }++ }
50 map { $_->calculate_all_roles } @{ $self->roles };
57 foreach my $super(@_){
58 Mouse::Util::load_class($super);
60 my $meta = Mouse::Util::get_metaclass_by_name($super);
61 unless(defined $meta) {
62 # checks if $super is a foreign class (i.e. non-Mouse class)
63 my $mm = $super->can('meta');
64 if(!($mm && $mm == \&Mouse::Util::meta)) {
65 if($super->can('new') or $super->can('DESTROY')) {
66 $self->inherit_from_foreign_class($super);
72 if(Mouse::Util::is_a_metarole($meta)){
73 $self->throw_error("You cannot inherit from a Mouse Role ($super)");
76 # checks and fixes in metaclass compatiility
77 next if $self->isa(ref $meta); # _superclass_meta_is_compatible
78 $self->_reconcile_with_superclass_meta($meta);
80 @{ $self->{superclasses} } = @_;
83 return @{ $self->{superclasses} };
86 sub inherit_from_foreign_class {
87 my($class, $super) = @_;
88 Carp::carp("You inherit from non-Mouse class ($super),"
89 . " but it is unlikely to work correctly."
90 . " Please consider using MouseX::Foreign");
94 my @MetaClassTypes = (
95 'attribute', # Mouse::Meta::Attribute
96 'method', # Mouse::Meta::Method
97 'constructor', # Mouse::Meta::Method::Constructor
98 'destructor', # Mouse::Meta::Method::Destructor
101 sub _reconcile_with_superclass_meta {
102 my($self, $other) = @_;
104 # find incompatible traits
106 foreach my $metaclass_type(@MetaClassTypes){
107 my $accessor = $self->can($metaclass_type . '_metaclass')
108 || $self->can($metaclass_type . '_class');
110 my $other_c = $other->$accessor();
111 my $self_c = $self->$accessor();
113 if(!$self_c->isa($other_c)){
114 $metaroles{$metaclass_type}
115 = [ $self_c->meta->_collect_roles($other_c->meta) ];
119 $metaroles{class} = [$self->meta->_collect_roles($other->meta)];
121 #use Data::Dumper; print Data::Dumper->new([\%metaroles], ['*metaroles'])->Indent(1)->Dump;
123 require Mouse::Util::MetaRole;
124 $_[0] = Mouse::Util::MetaRole::apply_metaroles(
126 class_metaroles => \%metaroles,
132 my ($self, $other) = @_;
134 # find common ancestor
135 my @self_lin_isa = $self->linearized_isa;
136 my @other_lin_isa = $other->linearized_isa;
138 my(@self_anon_supers, @other_anon_supers);
139 push @self_anon_supers, shift @self_lin_isa while $self_lin_isa[0]->meta->is_anon_class;
140 push @other_anon_supers, shift @other_lin_isa while $other_lin_isa[0]->meta->is_anon_class;
142 my $common_ancestor = $self_lin_isa[0] eq $other_lin_isa[0] && $self_lin_isa[0];
144 if(!$common_ancestor){
145 $self->throw_error(sprintf '%s cannot have %s as a super class because of their metaclass incompatibility',
146 $self->name, $other->name);
150 return sort grep { !$seen{$_}++ } ## no critic
151 (map{ $_->name } map{ $_->meta->calculate_all_roles } @self_anon_supers),
152 (map{ $_->name } map{ $_->meta->calculate_all_roles } @other_anon_supers),
157 sub find_method_by_name {
158 my($self, $method_name) = @_;
159 defined($method_name)
160 or $self->throw_error('You must define a method name to find');
162 foreach my $class( $self->linearized_isa ){
163 my $method = $self->initialize($class)->get_method($method_name);
164 return $method if defined $method;
169 sub get_all_methods {
171 return map{ $self->find_method_by_name($_) } $self->get_all_method_names;
174 sub get_all_method_names {
177 return grep { $uniq{$_}++ == 0 }
178 map { Mouse::Meta::Class->initialize($_)->get_method_list() }
179 $self->linearized_isa;
182 sub find_attribute_by_name {
183 my($self, $name) = @_;
185 or $self->throw_error('You must define an attribute name to find');
186 foreach my $attr($self->get_all_attributes) {
187 return $attr if $attr->name eq $name;
197 if(Scalar::Util::blessed($_[0])){
200 $attr->isa('Mouse::Meta::Attribute')
201 || $self->throw_error("Your attribute must be an instance of Mouse::Meta::Attribute (or a subclass)");
209 my %args = (@_ == 1) ? %{$_[0]} : @_;
212 or $self->throw_error('You must provide a name for the attribute');
214 if ($name =~ s/^\+//) { # inherited attributes
215 my $inherited_attr = $self->find_attribute_by_name($name)
216 or $self->throw_error("Could not find an attribute by the name of '$name' to inherit from in ".$self->name);
218 $attr = $inherited_attr->clone_and_inherit_options(%args);
221 my($attribute_class, @traits) = $self->attribute_metaclass->interpolate_class(\%args);
222 $args{traits} = \@traits if @traits;
224 $attr = $attribute_class->new($name, %args);
228 Scalar::Util::weaken( $attr->{associated_class} = $self );
230 # install accessors first
231 $attr->install_accessors();
233 # then register the attribute to the metaclass
234 $attr->{insertion_order} = keys %{ $self->{attributes} };
235 $self->{attributes}{$name} = $attr;
236 delete $self->{_mouse_cache}; # clears internal cache
238 if(!$attr->{associated_methods} && ($attr->{is} || '') ne 'bare'){
239 Carp::carp(qq{Attribute ($name) of class }.$self->name
240 .qq{ has no associated methods (did you mean to provide an "is" argument?)});
245 sub _calculate_all_attributes {
249 foreach my $class($self->linearized_isa) {
250 my $meta = Mouse::Util::get_metaclass_by_name($class) or next;
251 my @attrs = grep { !$seen{$_->name}++ } values %{$meta->{attributes}};
253 $b->{insertion_order} <=> $a->{insertion_order}
255 push @all_attrs, @attrs;
257 return [reverse @all_attrs];
265 sub immutable_options {
266 my ( $self, @args ) = @_;
269 inline_constructor => 1,
270 inline_destructor => 1,
271 constructor_name => 'new',
278 my %args = $self->immutable_options(@_);
280 $self->{is_immutable}++;
282 if ($args{inline_constructor}) {
283 $self->add_method($args{constructor_name} =>
284 Mouse::Util::load_class($self->constructor_class)
285 ->_generate_constructor($self, \%args));
288 if ($args{inline_destructor}) {
289 $self->add_method(DESTROY =>
290 Mouse::Util::load_class($self->destructor_class)
291 ->_generate_destructor($self, \%args));
294 # Moose's make_immutable returns true allowing calling code to skip
295 # setting an explicit true value at the end of a source file.
301 $self->{is_immutable} = 0;
306 sub is_mutable { !$_[0]->is_immutable }
308 sub _install_modifier {
309 my( $self, $type, $name, $code ) = @_;
310 my $into = $self->name;
312 my $original = $into->can($name)
313 or $self->throw_error("The method '$name' was not found in the inheritance hierarchy for $into");
315 my $modifier_table = $self->{modifiers}{$name};
317 if(!$modifier_table){
318 my(@before, @after, @around);
319 my $cache = $original;
322 for my $c (@before) { $c->(@_) }
328 if(wantarray){ # list context
329 my @rval = $cache->(@_);
331 for my $c(@after){ $c->(@_) }
334 elsif(defined wantarray){ # scalar context
335 my $rval = $cache->(@_);
337 for my $c(@after){ $c->(@_) }
343 for my $c(@after){ $c->(@_) }
348 $self->{modifiers}{$name} = $modifier_table = {
349 original => $original,
355 cache => \$cache, # cache for around modifiers
358 $self->add_method($name => $modified);
361 if($type eq 'before'){
362 unshift @{$modifier_table->{before}}, $code;
364 elsif($type eq 'after'){
365 push @{$modifier_table->{after}}, $code;
368 push @{$modifier_table->{around}}, $code;
370 my $next = ${ $modifier_table->{cache} };
371 ${ $modifier_table->{cache} } = sub{ $code->($next, @_) };
377 sub add_before_method_modifier {
378 my ( $self, $name, $code ) = @_;
379 $self->_install_modifier( 'before', $name, $code );
382 sub add_around_method_modifier {
383 my ( $self, $name, $code ) = @_;
384 $self->_install_modifier( 'around', $name, $code );
387 sub add_after_method_modifier {
388 my ( $self, $name, $code ) = @_;
389 $self->_install_modifier( 'after', $name, $code );
392 sub add_override_method_modifier {
393 my ($self, $name, $code) = @_;
395 if($self->has_method($name)){
396 $self->throw_error("Cannot add an override method if a local method is already present");
399 my $package = $self->name;
401 my $super_body = $package->can($name)
402 or $self->throw_error("You cannot override '$name' because it has no super method");
404 $self->add_method($name => sub {
405 local $Mouse::SUPER_PACKAGE = $package;
406 local $Mouse::SUPER_BODY = $super_body;
407 local @Mouse::SUPER_ARGS = @_;
414 sub add_augment_method_modifier {
415 my ($self, $name, $code) = @_;
416 if($self->has_method($name)){
417 $self->throw_error("Cannot add an augment method if a local method is already present");
420 my $super = $self->find_method_by_name($name)
421 or $self->throw_error("You cannot augment '$name' because it has no super method");
423 my $super_package = $super->package_name;
424 my $super_body = $super->body;
426 $self->add_method($name => sub{
427 local $Mouse::INNER_BODY{$super_package} = $code;
428 local $Mouse::INNER_ARGS{$super_package} = [@_];
435 my ($self, $role_name) = @_;
438 || $self->throw_error("You must supply a role name to look for");
440 $role_name = $role_name->name if ref $role_name;
442 for my $class ($self->linearized_isa) {
443 my $meta = Mouse::Util::get_metaclass_by_name($class)
446 for my $role (@{ $meta->roles }) {
448 return 1 if $role->does_role($role_name);
460 Mouse::Meta::Class - The Mouse class metaclass
464 This document describes Mouse version 0.74
468 This class is a meta object protocol for Mouse classes,
469 which is a subset of Moose::Meta:::Class.
473 =head2 C<< initialize(ClassName) -> Mouse::Meta::Class >>
475 Finds or creates a C<Mouse::Meta::Class> instance for the given ClassName. Only
476 one instance should exist for a given class.
478 =head2 C<< name -> ClassName >>
480 Returns the name of the owner class.
482 =head2 C<< superclasses -> ClassNames >> C<< superclass(ClassNames) >>
484 Gets (or sets) the list of superclasses of the owner class.
486 =head2 C<< add_method(name => CodeRef) >>
488 Adds a method to the owner class.
490 =head2 C<< has_method(name) -> Bool >>
492 Returns whether we have a method with the given name.
494 =head2 C<< get_method(name) -> Mouse::Meta::Method | undef >>
496 Returns a L<Mouse::Meta::Method> with the given name.
498 Note that you can also use C<< $metaclass->name->can($name) >> for a method body.
500 =head2 C<< get_method_list -> Names >>
502 Returns a list of method names which are defined in the local class.
503 If you want a list of all applicable methods for a class, use the
504 C<get_all_methods> method.
506 =head2 C<< get_all_methods -> (Mouse::Meta::Method) >>
508 Return the list of all L<Mouse::Meta::Method> instances associated with
509 the class and its superclasses.
511 =head2 C<< add_attribute(name => spec | Mouse::Meta::Attribute) >>
513 Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
516 =head2 C<< has_attribute(Name) -> Bool >>
518 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
520 =head2 C<< get_attribute Name -> Mouse::Meta::Attribute | undef >>
522 Returns the L<Mouse::Meta::Attribute> with the given name.
524 =head2 C<< get_attribute_list -> Names >>
526 Returns a list of attribute names which are defined in the local
527 class. If you want a list of all applicable attributes for a class,
528 use the C<get_all_attributes> method.
530 =head2 C<< get_all_attributes -> (Mouse::Meta::Attribute) >>
532 Returns the list of all L<Mouse::Meta::Attribute> instances associated with
533 this class and its superclasses.
535 =head2 C<< linearized_isa -> [ClassNames] >>
537 Returns the list of classes in method dispatch order, with duplicates removed.
539 =head2 C<< new_object(Parameters) -> Instance >>
541 Creates a new instance.
543 =head2 C<< clone_object(Instance, Parameters) -> Instance >>
545 Clones the given instance which must be an instance governed by this
548 =head2 C<< throw_error(Message, Parameters) >>
550 Throws an error with the given message.
554 L<Mouse::Meta::Module>
556 L<Moose::Meta::Class>