Fix _reconcile_with_superclass_meta
[gitmo/Mouse.git] / lib / Mouse / Meta / Class.pm
CommitLineData
306290e8 1package Mouse::Meta::Class;
bc69ee88 2use Mouse::Util qw/:meta get_linear_isa not_supported/; # enables strict and warnings
c3398f5b 3
cecfb973 4use Scalar::Util qw/blessed weaken/;
6d28c5cf 5
6d28c5cf 6use Mouse::Meta::Module;
f3bb863f 7our @ISA = qw(Mouse::Meta::Module);
3a63a2e7 8
e058b279 9sub method_metaclass;
10sub attribute_metaclass;
3a63a2e7 11
e058b279 12sub constructor_class;
13sub destructor_class;
380e1cd7 14
8e64d0fa 15sub _construct_meta {
88ed7189 16 my($class, %args) = @_;
c3398f5b 17
5132ec42 18 $args{attributes} = {};
19 $args{methods} = {};
20 $args{roles} = [];
8536d351 21
c3398f5b 22 $args{superclasses} = do {
23 no strict 'refs';
88ed7189 24 \@{ $args{package} . '::ISA' };
c3398f5b 25 };
26
8d40c3b8 27 my $self = bless \%args, ref($class) || $class;
7eb3a8d5 28 if(ref($self) ne __PACKAGE__){
9009aca1 29 $self->meta->_initialize_object($self, \%args);
8d40c3b8 30 }
31 return $self;
7a50b450 32}
33
34sub create_anon_class{
35 my $self = shift;
36 return $self->create(undef, @_);
37}
38
43165725 39sub is_anon_class;
c3398f5b 40
43165725 41sub roles;
c3398f5b 42
e7264861 43sub calculate_all_roles {
44 my $self = shift;
45 my %seen;
46 return grep { !$seen{ $_->name }++ }
47 map { $_->calculate_all_roles } @{ $self->roles };
48}
49
c3398f5b 50sub superclasses {
51 my $self = shift;
52
53 if (@_) {
9d0686b2 54 foreach my $super(@_){
55 Mouse::Util::load_class($super);
56 my $meta = Mouse::Util::get_metaclass_by_name($super);
b6369395 57
58 next if not defined $meta;
59
f48920c1 60 if(Mouse::Util::is_a_metarole($meta)){
9d0686b2 61 $self->throw_error("You cannot inherit from a Mouse Role ($super)");
62 }
b6369395 63
64 next if $self->isa(ref $meta); # _superclass_meta_is_compatible
65
b6369395 66 $self->_reconcile_with_superclass_meta($meta);
9d0686b2 67 }
c3398f5b 68 @{ $self->{superclasses} } = @_;
69 }
70
8e64d0fa 71 return @{ $self->{superclasses} };
72}
73
b6369395 74my @MetaClassTypes = qw(
75 attribute_metaclass
76 method_metaclass
77 constructor_class
78 destructor_class
79);
80
81sub _reconcile_with_superclass_meta {
82 my($self, $super_meta) = @_;
83
84 my @incompatibles;
29422d00 85
b6369395 86 foreach my $metaclass_type(@MetaClassTypes){
87 my $super_c = $super_meta->$metaclass_type();
88 my $self_c = $self->$metaclass_type();
89
90 if(!$super_c->isa($self_c)){
29422d00 91 push @incompatibles, ($metaclass_type => $super_c);
b6369395 92 }
93 }
94
29422d00 95 $super_meta->reinitialize($self, @incompatibles);
b6369395 96 return;
97}
98
99
8e64d0fa 100sub find_method_by_name{
101 my($self, $method_name) = @_;
102 defined($method_name)
103 or $self->throw_error('You must define a method name to find');
0126c27c 104
8e64d0fa 105 foreach my $class( $self->linearized_isa ){
106 my $method = $self->initialize($class)->get_method($method_name);
107 return $method if defined $method;
108 }
109 return undef;
110}
111
112sub get_all_methods {
113 my($self) = @_;
612d3e1a 114 return map{ $self->find_method_by_name($_) } $self->get_all_method_names;
c3398f5b 115}
116
60cfc6ad 117sub get_all_method_names {
118 my $self = shift;
119 my %uniq;
120 return grep { $uniq{$_}++ == 0 }
3a63a2e7 121 map { Mouse::Meta::Class->initialize($_)->get_method_list() }
60cfc6ad 122 $self->linearized_isa;
123}
124
2b908b79 125sub find_attribute_by_name{
126 my($self, $name) = @_;
127 my $attr;
128 foreach my $class($self->linearized_isa){
129 my $meta = Mouse::Util::get_metaclass_by_name($class) or next;
130 $attr = $meta->get_attribute($name) and last;
131 }
132 return $attr;
133}
134
87ca293b 135sub add_attribute {
c3398f5b 136 my $self = shift;
60f6eba9 137
87ca293b 138 my($attr, $name);
1b9e472d 139
87ca293b 140 if(blessed $_[0]){
141 $attr = $_[0];
1b9e472d 142
87ca293b 143 $attr->isa('Mouse::Meta::Attribute')
144 || $self->throw_error("Your attribute must be an instance of Mouse::Meta::Attribute (or a subclass)");
1b9e472d 145
87ca293b 146 $name = $attr->name;
1b9e472d 147 }
148 else{
87ca293b 149 # _process_attribute
150 $name = shift;
1b9e472d 151
87ca293b 152 my %args = (@_ == 1) ? %{$_[0]} : @_;
1b9e472d 153
87ca293b 154 defined($name)
155 or $self->throw_error('You must provide a name for the attribute');
1b9e472d 156
87ca293b 157 if ($name =~ s/^\+//) { # inherited attributes
2b908b79 158 my $inherited_attr = $self->find_attribute_by_name($name)
87ca293b 159 or $self->throw_error("Could not find an attribute by the name of '$name' to inherit from in ".$self->name);
160
8cf51b82 161 $attr = $inherited_attr->clone_and_inherit_options(%args);
87ca293b 162 }
163 else{
8cf51b82 164 my($attribute_class, @traits) = $self->attribute_metaclass->interpolate_class(\%args);
87ca293b 165 $args{traits} = \@traits if @traits;
166
2a464664 167 $attr = $attribute_class->new($name, %args);
87ca293b 168 }
169 }
1b9e472d 170
171 weaken( $attr->{associated_class} = $self );
172
173 $self->{attributes}{$attr->name} = $attr;
174 $attr->install_accessors();
175
8aba926d 176 if(Mouse::Util::_MOUSE_VERBOSE && !$attr->{associated_methods} && ($attr->{is} || '') ne 'bare'){
1b9e472d 177 Carp::cluck(qq{Attribute (}.$attr->name.qq{) of class }.$self->name.qq{ has no associated methods (did you mean to provide an "is" argument?)});
60f6eba9 178 }
1b9e472d 179 return $attr;
c3398f5b 180}
181
3a683350 182sub compute_all_applicable_attributes {
0126c27c 183 Carp::cluck('compute_all_applicable_attributes() has been deprecated')
8aba926d 184 if Mouse::Util::_MOUSE_VERBOSE;
3a683350 185 return shift->get_all_attributes(@_)
186}
187
cccb83de 188sub linearized_isa;
8536d351 189
ba153b33 190sub new_object;
1b9e472d 191
7a59f4e8 192sub clone_object {
926290ac 193 my $class = shift;
194 my $object = shift;
195 my %params = (@_ == 1) ? %{$_[0]} : @_;
7a59f4e8 196
926290ac 197 (blessed($object) && $object->isa($class->name))
198 || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($object)");
7a59f4e8 199
926290ac 200 my $cloned = bless { %$object }, ref $object;
201 $class->_initialize_object($cloned, \%params);
7a59f4e8 202
926290ac 203 return $cloned;
2cea7a5f 204}
7a59f4e8 205
2cea7a5f 206sub clone_instance {
207 my ($class, $instance, %params) = @_;
208
04493075 209 Carp::cluck('clone_instance has been deprecated. Use clone_object instead')
8aba926d 210 if Mouse::Util::_MOUSE_VERBOSE;
2cea7a5f 211 return $class->clone_object($instance, %params);
7a59f4e8 212}
213
fc1d8369 214sub make_immutable {
215 my $self = shift;
6a1d1835 216 my %args = (
7efbc77d 217 inline_constructor => 1,
e578d610 218 inline_destructor => 1,
2a464664 219 constructor_name => 'new',
6a1d1835 220 @_,
221 );
222
fc1d8369 223 $self->{is_immutable}++;
c7a6403f 224
6a1d1835 225 if ($args{inline_constructor}) {
a5c683f6 226 my $c = $self->constructor_class;
227 Mouse::Util::load_class($c);
380e1cd7 228 $self->add_method($args{constructor_name} =>
a5c683f6 229 $c->_generate_constructor($self, \%args));
c7a6403f 230 }
231
8632b6fe 232 if ($args{inline_destructor}) {
a5c683f6 233 my $c = $self->destructor_class;
234 Mouse::Util::load_class($c);
380e1cd7 235 $self->add_method(DESTROY =>
a5c683f6 236 $c->_generate_destructor($self, \%args));
8632b6fe 237 }
2276cb14 238
239 # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value
240 # at the end of a source file.
241 return 1;
fc1d8369 242}
ad958001 243
fce211ae 244sub make_mutable { not_supported }
ad958001 245
6cfa1e5e 246sub is_immutable { $_[0]->{is_immutable} }
247sub is_mutable { !$_[0]->{is_immutable} }
84ef660f 248
3fbade18 249sub _install_modifier_pp{
8d59c723 250 my( $self, $type, $name, $code ) = @_;
251 my $into = $self->name;
3fbade18 252
253 my $original = $into->can($name)
254 or $self->throw_error("The method '$name' is not found in the inheritance hierarchy for class $into");
255
256 my $modifier_table = $self->{modifiers}{$name};
257
258 if(!$modifier_table){
259 my(@before, @after, @around, $cache, $modified);
260
261 $cache = $original;
262
263 $modified = sub {
264 for my $c (@before) { $c->(@_) }
265
266 if(wantarray){ # list context
267 my @rval = $cache->(@_);
268
269 for my $c(@after){ $c->(@_) }
270 return @rval;
271 }
272 elsif(defined wantarray){ # scalar context
273 my $rval = $cache->(@_);
274
275 for my $c(@after){ $c->(@_) }
276 return $rval;
277 }
278 else{ # void context
279 $cache->(@_);
280
281 for my $c(@after){ $c->(@_) }
282 return;
283 }
284 };
285
286 $self->{modifiers}{$name} = $modifier_table = {
287 original => $original,
288
289 before => \@before,
290 after => \@after,
291 around => \@around,
292
293 cache => \$cache, # cache for around modifiers
294 };
295
296 $self->add_method($name => $modified);
297 }
298
299 if($type eq 'before'){
300 unshift @{$modifier_table->{before}}, $code;
301 }
302 elsif($type eq 'after'){
303 push @{$modifier_table->{after}}, $code;
304 }
305 else{ # around
306 push @{$modifier_table->{around}}, $code;
307
308 my $next = ${ $modifier_table->{cache} };
309 ${ $modifier_table->{cache} } = sub{ $code->($next, @_) };
310 }
311
312 return;
313}
314
4859d490 315sub _install_modifier {
8d59c723 316 my ( $self, $type, $name, $code ) = @_;
4f5b44a0 317
3fbade18 318 # load Class::Method::Modifiers first
7ca5c5fb 319 my $no_cmm_fast = do{
3fbade18 320 local $@;
321 eval q{ require Class::Method::Modifiers::Fast };
322 $@;
4f5b44a0 323 };
4f5b44a0 324
3fbade18 325 my $impl;
326 if($no_cmm_fast){
327 $impl = \&_install_modifier_pp;
328 }
329 else{
330 my $install_modifier = Class::Method::Modifiers::Fast->can('_install_modifier');
331 $impl = sub {
8d59c723 332 my ( $self, $type, $name, $code ) = @_;
333 my $into = $self->name;
95ecd6f1 334 $install_modifier->($into, $type, $name, $code);
335
8d59c723 336 $self->add_method($name => do{
337 no strict 'refs';
338 \&{ $into . '::' . $name };
339 });
6cfa1e5e 340 return;
4f5b44a0 341 };
1b79a118 342 }
4f5b44a0 343
3fbade18 344 # replace this method itself :)
345 {
346 no warnings 'redefine';
347 *_install_modifier = $impl;
348 }
349
8d59c723 350 $self->$impl( $type, $name, $code );
4859d490 351}
352
50dc6ee5 353sub add_before_method_modifier {
4859d490 354 my ( $self, $name, $code ) = @_;
8d59c723 355 $self->_install_modifier( 'before', $name, $code );
50dc6ee5 356}
357
358sub add_around_method_modifier {
4859d490 359 my ( $self, $name, $code ) = @_;
8d59c723 360 $self->_install_modifier( 'around', $name, $code );
50dc6ee5 361}
362
363sub add_after_method_modifier {
4859d490 364 my ( $self, $name, $code ) = @_;
8d59c723 365 $self->_install_modifier( 'after', $name, $code );
50dc6ee5 366}
367
67199842 368sub add_override_method_modifier {
369 my ($self, $name, $code) = @_;
370
768804c0 371 if($self->has_method($name)){
372 $self->throw_error("Cannot add an override method if a local method is already present");
85bd3f44 373 }
374
6cfa1e5e 375 my $package = $self->name;
67199842 376
85bd3f44 377 my $super_body = $package->can($name)
6cfa1e5e 378 or $self->throw_error("You cannot override '$name' because it has no super method");
67199842 379
85bd3f44 380 $self->add_method($name => sub {
381 local $Mouse::SUPER_PACKAGE = $package;
382 local $Mouse::SUPER_BODY = $super_body;
383 local @Mouse::SUPER_ARGS = @_;
384
385 $code->(@_);
386 });
387 return;
67199842 388}
389
768804c0 390sub add_augment_method_modifier {
391 my ($self, $name, $code) = @_;
392 if($self->has_method($name)){
393 $self->throw_error("Cannot add an augment method if a local method is already present");
394 }
395
396 my $super = $self->find_method_by_name($name)
397 or $self->throw_error("You cannot augment '$name' because it has no super method");
398
399 my $super_package = $super->package_name;
400 my $super_body = $super->body;
401
402 $self->add_method($name => sub{
403 local $Mouse::INNER_BODY{$super_package} = $code;
404 local $Mouse::INNER_ARGS{$super_package} = [@_];
405 $super_body->(@_);
406 });
407 return;
408}
409
47f36c05 410sub does_role {
411 my ($self, $role_name) = @_;
ad958001 412
47f36c05 413 (defined $role_name)
fce211ae 414 || $self->throw_error("You must supply a role name to look for");
ad958001 415
f7fec86c 416 for my $class ($self->linearized_isa) {
95ecd6f1 417 my $meta = Mouse::Util::get_metaclass_by_name($class)
418 or next;
3a63a2e7 419
420 for my $role (@{ $meta->roles }) {
ff687069 421
3a63a2e7 422 return 1 if $role->does_role($role_name);
f7fec86c 423 }
47f36c05 424 }
ad958001 425
47f36c05 426 return 0;
427}
428
c3398f5b 4291;
c3398f5b 430__END__
431
432=head1 NAME
433
1820fffe 434Mouse::Meta::Class - The Mouse class metaclass
c3398f5b 435
a25ca8d6 436=head1 VERSION
437
8aba926d 438This document describes Mouse version 0.40_07
a25ca8d6 439
c3398f5b 440=head1 METHODS
441
612d3e1a 442=head2 C<< initialize(ClassName) -> Mouse::Meta::Class >>
c3398f5b 443
31c5194b 444Finds or creates a C<Mouse::Meta::Class> instance for the given ClassName. Only
306290e8 445one instance should exist for a given class.
c3398f5b 446
612d3e1a 447=head2 C<< name -> ClassName >>
c3398f5b 448
449Returns the name of the owner class.
450
612d3e1a 451=head2 C<< superclasses -> ClassNames >> C<< superclass(ClassNames) >>
c3398f5b 452
453Gets (or sets) the list of superclasses of the owner class.
454
31c5194b 455=head2 C<< add_method(name => CodeRef) >>
c3398f5b 456
31c5194b 457Adds a method to the owner class.
c3398f5b 458
31c5194b 459=head2 C<< has_method(name) -> Bool >>
72b88a88 460
31c5194b 461Returns whether we have a method with the given name.
72b88a88 462
31c5194b 463=head2 C<< get_method(name) -> Mouse::Meta::Method | undef >>
c68b4110 464
31c5194b 465Returns a L<Mouse::Meta::Method> with the given name.
466
467Note that you can also use C<< $metaclass->name->can($name) >> for a method body.
468
469=head2 C<< get_method_list -> Names >>
470
471Returns a list of method names which are defined in the local class.
472If you want a list of all applicable methods for a class, use the
473C<get_all_methods> method.
474
475=head2 C<< get_all_methods -> (Mouse::Meta::Method) >>
476
477Return the list of all L<Mouse::Meta::Method> instances associated with
478the class and its superclasses.
479
480=head2 C<< add_attribute(name => spec | Mouse::Meta::Attribute) >>
481
482Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
483class.
c68b4110 484
612d3e1a 485=head2 C<< has_attribute(Name) -> Bool >>
66eea168 486
487Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
488
31c5194b 489=head2 C<< get_attribute Name -> Mouse::Meta::Attribute | undef >>
c3398f5b 490
306290e8 491Returns the L<Mouse::Meta::Attribute> with the given name.
c3398f5b 492
31c5194b 493=head2 C<< get_attribute_list -> Names >>
494
495Returns a list of attribute names which are defined in the local
496class. If you want a list of all applicable attributes for a class,
497use the C<get_all_attributes> method.
498
499=head2 C<< get_all_attributes -> (Mouse::Meta::Attribute) >>
500
501Returns the list of all L<Mouse::Meta::Attribute> instances associated with
502this class and its superclasses.
503
612d3e1a 504=head2 C<< linearized_isa -> [ClassNames] >>
c3398f5b 505
506Returns the list of classes in method dispatch order, with duplicates removed.
507
612d3e1a 508=head2 C<< new_object(Parameters) -> Instance >>
2cea7a5f 509
612d3e1a 510Creates a new instance.
2cea7a5f 511
612d3e1a 512=head2 C<< clone_object(Instance, Parameters) -> Instance >>
f7b11a21 513
31c5194b 514Clones the given instance which must be an instance governed by this
f7b11a21 515metaclass.
516
31c5194b 517=head2 C<< throw_error(Message, Parameters) >>
518
519Throws an error with the given message.
520
1820fffe 521=head1 SEE ALSO
f7b11a21 522
1820fffe 523L<Moose::Meta::Class>
f7b11a21 524
31c5194b 525L<Class::MOP::Class>
526
c3398f5b 527=cut
528