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