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);
59 my $meta = Mouse::Util::get_metaclass_by_name($super);
60 next if $self->verify_superclass($super, $meta);
61 $self->_reconcile_with_superclass_meta($meta);
63 return @{ $self->{superclasses} } = @_;
66 return @{ $self->{superclasses} };
69 sub verify_superclass {
70 my($self, $super, $super_meta) = @_;
72 if(defined $super_meta) {
73 if(Mouse::Util::is_a_metarole($super_meta)){
74 $self->throw_error("You cannot inherit from a Mouse Role ($super)");
78 # The metaclass of $super is not initialized.
79 # i.e. it might be Mouse::Object, a mixin package (e.g. Exporter),
80 # or a foreign class including Moose classes.
81 # See also Mouse::Foreign::Meta::Role::Class.
82 my $mm = $super->can('meta');
83 if(!($mm && $mm == \&Mouse::Util::meta)) {
84 if($super->can('new') or $super->can('DESTROY')) {
85 $self->inherit_from_foreign_class($super);
91 return $self->isa(ref $super_meta); # checks metaclass compatibility
94 sub inherit_from_foreign_class {
95 my($class, $super) = @_;
96 if($ENV{PERL_MOUSE_STRICT}) {
97 Carp::carp("You inherit from non-Mouse class ($super),"
98 . " but it is unlikely to work correctly."
99 . " Please consider using MouseX::Foreign");
104 my @MetaClassTypes = (
105 'attribute', # Mouse::Meta::Attribute
106 'method', # Mouse::Meta::Method
107 'constructor', # Mouse::Meta::Method::Constructor
108 'destructor', # Mouse::Meta::Method::Destructor
111 sub _reconcile_with_superclass_meta {
112 my($self, $other) = @_;
114 # find incompatible traits
116 foreach my $metaclass_type(@MetaClassTypes){
117 my $accessor = $self->can($metaclass_type . '_metaclass')
118 || $self->can($metaclass_type . '_class');
120 my $other_c = $other->$accessor();
121 my $self_c = $self->$accessor();
123 if(!$self_c->isa($other_c)){
124 $metaroles{$metaclass_type}
125 = [ $self_c->meta->_collect_roles($other_c->meta) ];
129 $metaroles{class} = [$self->meta->_collect_roles($other->meta)];
131 #use Data::Dumper; print Data::Dumper->new([\%metaroles], ['*metaroles'])->Indent(1)->Dump;
133 require Mouse::Util::MetaRole;
134 $_[0] = Mouse::Util::MetaRole::apply_metaroles(
136 class_metaroles => \%metaroles,
142 my ($self, $other) = @_;
144 # find common ancestor
145 my @self_lin_isa = $self->linearized_isa;
146 my @other_lin_isa = $other->linearized_isa;
148 my(@self_anon_supers, @other_anon_supers);
149 push @self_anon_supers, shift @self_lin_isa while $self_lin_isa[0]->meta->is_anon_class;
150 push @other_anon_supers, shift @other_lin_isa while $other_lin_isa[0]->meta->is_anon_class;
152 my $common_ancestor = $self_lin_isa[0] eq $other_lin_isa[0] && $self_lin_isa[0];
154 if(!$common_ancestor){
155 $self->throw_error(sprintf '%s cannot have %s as a super class because of their metaclass incompatibility',
156 $self->name, $other->name);
160 return sort grep { !$seen{$_}++ } ## no critic
161 (map{ $_->name } map{ $_->meta->calculate_all_roles } @self_anon_supers),
162 (map{ $_->name } map{ $_->meta->calculate_all_roles } @other_anon_supers),
167 sub find_method_by_name {
168 my($self, $method_name) = @_;
169 defined($method_name)
170 or $self->throw_error('You must define a method name to find');
172 foreach my $class( $self->linearized_isa ){
173 my $method = $self->initialize($class)->get_method($method_name);
174 return $method if defined $method;
179 sub get_all_methods {
181 return map{ $self->find_method_by_name($_) } $self->get_all_method_names;
184 sub get_all_method_names {
187 return grep { $uniq{$_}++ == 0 }
188 map { Mouse::Meta::Class->initialize($_)->get_method_list() }
189 $self->linearized_isa;
192 sub find_attribute_by_name {
193 my($self, $name) = @_;
195 or $self->throw_error('You must define an attribute name to find');
196 foreach my $attr($self->get_all_attributes) {
197 return $attr if $attr->name eq $name;
207 if(Scalar::Util::blessed($_[0])){
210 $attr->isa('Mouse::Meta::Attribute')
211 || $self->throw_error("Your attribute must be an instance of Mouse::Meta::Attribute (or a subclass)");
219 my %args = (@_ == 1) ? %{$_[0]} : @_;
222 or $self->throw_error('You must provide a name for the attribute');
224 if ($name =~ s/^\+//) { # inherited attributes
225 my $inherited_attr = $self->find_attribute_by_name($name)
226 or $self->throw_error("Could not find an attribute by the name of '$name' to inherit from in ".$self->name);
228 $attr = $inherited_attr->clone_and_inherit_options(%args);
231 my($attribute_class, @traits) = $self->attribute_metaclass->interpolate_class(\%args);
232 $args{traits} = \@traits if @traits;
234 $attr = $attribute_class->new($name, %args);
238 Scalar::Util::weaken( $attr->{associated_class} = $self );
240 # install accessors first
241 $attr->install_accessors();
243 # then register the attribute to the metaclass
244 $attr->{insertion_order} = keys %{ $self->{attributes} };
245 $self->{attributes}{$name} = $attr;
246 $self->_invalidate_metaclass_cache();
248 if(!$attr->{associated_methods} && ($attr->{is} || '') ne 'bare'){
249 Carp::carp(qq{Attribute ($name) of class }.$self->name
250 .qq{ has no associated methods (did you mean to provide an "is" argument?)});
255 sub _calculate_all_attributes {
259 foreach my $class($self->linearized_isa) {
260 my $meta = Mouse::Util::get_metaclass_by_name($class) or next;
261 my @attrs = grep { !$seen{$_->name}++ } values %{$meta->{attributes}};
263 $b->{insertion_order} <=> $a->{insertion_order}
265 push @all_attrs, @attrs;
267 return [reverse @all_attrs];
275 sub immutable_options {
276 my ( $self, @args ) = @_;
279 inline_constructor => 1,
280 inline_destructor => 1,
281 constructor_name => 'new',
288 my %args = $self->immutable_options(@_);
290 $self->{is_immutable}++;
292 if ($args{inline_constructor}) {
293 $self->add_method($args{constructor_name} =>
294 Mouse::Util::load_class($self->constructor_class)
295 ->_generate_constructor($self, \%args));
298 if ($args{inline_destructor}) {
299 $self->add_method(DESTROY =>
300 Mouse::Util::load_class($self->destructor_class)
301 ->_generate_destructor($self, \%args));
304 # Moose's make_immutable returns true allowing calling code to skip
305 # setting an explicit true value at the end of a source file.
311 $self->{is_immutable} = 0;
316 sub is_mutable { !$_[0]->is_immutable }
318 sub _install_modifier {
319 my( $self, $type, $name, $code ) = @_;
320 my $into = $self->name;
322 my $original = $into->can($name)
323 or $self->throw_error("The method '$name' was not found in the inheritance hierarchy for $into");
325 my $modifier_table = $self->{modifiers}{$name};
327 if(!$modifier_table){
328 my(@before, @after, @around);
329 my $cache = $original;
332 for my $c (@before) { $c->(@_) }
338 if(wantarray){ # list context
339 my @rval = $cache->(@_);
341 for my $c(@after){ $c->(@_) }
344 elsif(defined wantarray){ # scalar context
345 my $rval = $cache->(@_);
347 for my $c(@after){ $c->(@_) }
353 for my $c(@after){ $c->(@_) }
358 $self->{modifiers}{$name} = $modifier_table = {
359 original => $original,
365 cache => \$cache, # cache for around modifiers
368 $self->add_method($name => $modified);
371 if($type eq 'before'){
372 unshift @{$modifier_table->{before}}, $code;
374 elsif($type eq 'after'){
375 push @{$modifier_table->{after}}, $code;
378 push @{$modifier_table->{around}}, $code;
380 my $next = ${ $modifier_table->{cache} };
381 ${ $modifier_table->{cache} } = sub{ $code->($next, @_) };
387 sub add_before_method_modifier {
388 my ( $self, $name, $code ) = @_;
389 $self->_install_modifier( 'before', $name, $code );
392 sub add_around_method_modifier {
393 my ( $self, $name, $code ) = @_;
394 $self->_install_modifier( 'around', $name, $code );
397 sub add_after_method_modifier {
398 my ( $self, $name, $code ) = @_;
399 $self->_install_modifier( 'after', $name, $code );
402 sub add_override_method_modifier {
403 my ($self, $name, $code) = @_;
405 if($self->has_method($name)){
406 $self->throw_error("Cannot add an override method if a local method is already present");
409 my $package = $self->name;
411 my $super_body = $package->can($name)
412 or $self->throw_error("You cannot override '$name' because it has no super method");
414 $self->add_method($name => sub {
415 local $Mouse::SUPER_PACKAGE = $package;
416 local $Mouse::SUPER_BODY = $super_body;
417 local @Mouse::SUPER_ARGS = @_;
423 sub add_augment_method_modifier {
424 my ($self, $name, $code) = @_;
425 if($self->has_method($name)){
426 $self->throw_error("Cannot add an augment method if a local method is already present");
429 my $super = $self->find_method_by_name($name)
430 or $self->throw_error("You cannot augment '$name' because it has no super method");
432 my $super_package = $super->package_name;
433 my $super_body = $super->body;
435 $self->add_method($name => sub {
436 local $Mouse::INNER_BODY{$super_package} = $code;
437 local $Mouse::INNER_ARGS{$super_package} = [@_];
444 my ($self, $role_name) = @_;
447 || $self->throw_error("You must supply a role name to look for");
449 $role_name = $role_name->name if ref $role_name;
451 for my $class ($self->linearized_isa) {
452 my $meta = Mouse::Util::get_metaclass_by_name($class)
455 for my $role (@{ $meta->roles }) {
457 return 1 if $role->does_role($role_name);
469 Mouse::Meta::Class - The Mouse class metaclass
473 This document describes Mouse version 0.90
477 This class is a meta object protocol for Mouse classes,
478 which is a subset of Moose::Meta:::Class.
482 =head2 C<< initialize(ClassName) -> Mouse::Meta::Class >>
484 Finds or creates a C<Mouse::Meta::Class> instance for the given ClassName. Only
485 one instance should exist for a given class.
487 =head2 C<< name -> ClassName >>
489 Returns the name of the owner class.
491 =head2 C<< superclasses -> ClassNames >> C<< superclass(ClassNames) >>
493 Gets (or sets) the list of superclasses of the owner class.
495 =head2 C<< add_method(name => CodeRef) >>
497 Adds a method to the owner class.
499 =head2 C<< has_method(name) -> Bool >>
501 Returns whether we have a method with the given name.
503 =head2 C<< get_method(name) -> Mouse::Meta::Method | undef >>
505 Returns a L<Mouse::Meta::Method> with the given name.
507 Note that you can also use C<< $metaclass->name->can($name) >> for a method body.
509 =head2 C<< get_method_list -> Names >>
511 Returns a list of method names which are defined in the local class.
512 If you want a list of all applicable methods for a class, use the
513 C<get_all_methods> method.
515 =head2 C<< get_all_methods -> (Mouse::Meta::Method) >>
517 Return the list of all L<Mouse::Meta::Method> instances associated with
518 the class and its superclasses.
520 =head2 C<< add_attribute(name => spec | Mouse::Meta::Attribute) >>
522 Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
525 =head2 C<< has_attribute(Name) -> Bool >>
527 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
529 =head2 C<< get_attribute Name -> Mouse::Meta::Attribute | undef >>
531 Returns the L<Mouse::Meta::Attribute> with the given name.
533 =head2 C<< get_attribute_list -> Names >>
535 Returns a list of attribute names which are defined in the local
536 class. If you want a list of all applicable attributes for a class,
537 use the C<get_all_attributes> method.
539 =head2 C<< get_all_attributes -> (Mouse::Meta::Attribute) >>
541 Returns the list of all L<Mouse::Meta::Attribute> instances associated with
542 this class and its superclasses.
544 =head2 C<< linearized_isa -> [ClassNames] >>
546 Returns the list of classes in method dispatch order, with duplicates removed.
548 =head2 C<< new_object(Parameters) -> Instance >>
550 Creates a new instance.
552 =head2 C<< clone_object(Instance, Parameters) -> Instance >>
554 Clones the given instance which must be an instance governed by this
557 =head2 C<< throw_error(Message, Parameters) >>
559 Throws an error with the given message.
563 L<Mouse::Meta::Module>
565 L<Moose::Meta::Class>