Commit | Line | Data |
306290e8 |
1 | package Mouse::Meta::Class; |
bc76ed1c |
2 | use Mouse::Util qw/:meta/; # enables strict and warnings |
c3398f5b |
3 | |
cecfb973 |
4 | use Scalar::Util qw/blessed weaken/; |
6d28c5cf |
5 | |
6d28c5cf |
6 | use Mouse::Meta::Module; |
f3bb863f |
7 | our @ISA = qw(Mouse::Meta::Module); |
3a63a2e7 |
8 | |
3b48bcc8 |
9 | our @CARP_NOT = qw(Mouse); # trust Mouse |
10 | |
e058b279 |
11 | sub attribute_metaclass; |
d9659f80 |
12 | sub method_metaclass; |
3a63a2e7 |
13 | |
e058b279 |
14 | sub constructor_class; |
15 | sub destructor_class; |
380e1cd7 |
16 | |
d9659f80 |
17 | |
8e64d0fa |
18 | sub _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 | |
37 | sub create_anon_class{ |
38 | my $self = shift; |
39 | return $self->create(undef, @_); |
40 | } |
41 | |
43165725 |
42 | sub is_anon_class; |
c3398f5b |
43 | |
43165725 |
44 | sub roles; |
c3398f5b |
45 | |
e7264861 |
46 | sub 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 |
53 | sub superclasses { |
54 | my $self = shift; |
55 | |
56 | if (@_) { |
9d0686b2 |
57 | foreach my $super(@_){ |
58 | Mouse::Util::load_class($super); |
b6369395 |
59 | |
902174eb |
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); |
67 | } |
68 | } |
69 | next; |
70 | } |
b6369395 |
71 | |
f48920c1 |
72 | if(Mouse::Util::is_a_metarole($meta)){ |
9d0686b2 |
73 | $self->throw_error("You cannot inherit from a Mouse Role ($super)"); |
74 | } |
b6369395 |
75 | |
902174eb |
76 | # checks and fixes in metaclass compatiility |
b6369395 |
77 | next if $self->isa(ref $meta); # _superclass_meta_is_compatible |
b6369395 |
78 | $self->_reconcile_with_superclass_meta($meta); |
9d0686b2 |
79 | } |
c3398f5b |
80 | @{ $self->{superclasses} } = @_; |
81 | } |
82 | |
8e64d0fa |
83 | return @{ $self->{superclasses} }; |
84 | } |
902174eb |
85 | |
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 concider to use MouseX::Foreign"); |
91 | return; |
92 | } |
93 | |
c0b18afd |
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 |
99 | ); |
8e64d0fa |
100 | |
b6369395 |
101 | sub _reconcile_with_superclass_meta { |
c0b18afd |
102 | my($self, $other) = @_; |
b6369395 |
103 | |
733f404b |
104 | # find incompatible traits |
c0b18afd |
105 | my %metaroles; |
b6369395 |
106 | foreach my $metaclass_type(@MetaClassTypes){ |
c0b18afd |
107 | my $accessor = $self->can($metaclass_type . '_metaclass') |
108 | || $self->can($metaclass_type . '_class'); |
b6369395 |
109 | |
c0b18afd |
110 | my $other_c = $other->$accessor(); |
111 | my $self_c = $self->$accessor(); |
b6369395 |
112 | |
c0b18afd |
113 | if(!$self_c->isa($other_c)){ |
114 | $metaroles{$metaclass_type} |
115 | = [ $self_c->meta->_collect_roles($other_c->meta) ]; |
d9659f80 |
116 | } |
117 | } |
118 | |
c0b18afd |
119 | $metaroles{class} = [$self->meta->_collect_roles($other->meta)]; |
120 | |
121 | #use Data::Dumper; print Data::Dumper->new([\%metaroles], ['*metaroles'])->Indent(1)->Dump; |
d9659f80 |
122 | |
123 | require Mouse::Util::MetaRole; |
c0b18afd |
124 | $_[0] = Mouse::Util::MetaRole::apply_metaroles( |
125 | for => $self, |
126 | class_metaroles => \%metaroles, |
d9659f80 |
127 | ); |
b6369395 |
128 | return; |
129 | } |
130 | |
c0b18afd |
131 | sub _collect_roles { |
132 | my ($self, $other) = @_; |
133 | |
134 | # find common ancestor |
135 | my @self_lin_isa = $self->linearized_isa; |
136 | my @other_lin_isa = $other->linearized_isa; |
137 | |
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; |
141 | |
142 | my $common_ancestor = $self_lin_isa[0] eq $other_lin_isa[0] && $self_lin_isa[0]; |
143 | |
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); |
147 | } |
148 | |
149 | my %seen; |
a4b15169 |
150 | return sort grep { !$seen{$_}++ } ## no critic |
c0b18afd |
151 | (map{ $_->name } map{ $_->meta->calculate_all_roles } @self_anon_supers), |
152 | (map{ $_->name } map{ $_->meta->calculate_all_roles } @other_anon_supers), |
153 | ; |
154 | } |
155 | |
156 | |
8e64d0fa |
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'); |
0126c27c |
161 | |
8e64d0fa |
162 | foreach my $class( $self->linearized_isa ){ |
163 | my $method = $self->initialize($class)->get_method($method_name); |
164 | return $method if defined $method; |
165 | } |
166 | return undef; |
167 | } |
168 | |
169 | sub get_all_methods { |
170 | my($self) = @_; |
612d3e1a |
171 | return map{ $self->find_method_by_name($_) } $self->get_all_method_names; |
c3398f5b |
172 | } |
173 | |
60cfc6ad |
174 | sub get_all_method_names { |
175 | my $self = shift; |
176 | my %uniq; |
177 | return grep { $uniq{$_}++ == 0 } |
3a63a2e7 |
178 | map { Mouse::Meta::Class->initialize($_)->get_method_list() } |
60cfc6ad |
179 | $self->linearized_isa; |
180 | } |
181 | |
2b908b79 |
182 | sub find_attribute_by_name{ |
183 | my($self, $name) = @_; |
184 | my $attr; |
185 | foreach my $class($self->linearized_isa){ |
186 | my $meta = Mouse::Util::get_metaclass_by_name($class) or next; |
187 | $attr = $meta->get_attribute($name) and last; |
188 | } |
189 | return $attr; |
190 | } |
191 | |
87ca293b |
192 | sub add_attribute { |
c3398f5b |
193 | my $self = shift; |
60f6eba9 |
194 | |
87ca293b |
195 | my($attr, $name); |
1b9e472d |
196 | |
87ca293b |
197 | if(blessed $_[0]){ |
198 | $attr = $_[0]; |
1b9e472d |
199 | |
87ca293b |
200 | $attr->isa('Mouse::Meta::Attribute') |
201 | || $self->throw_error("Your attribute must be an instance of Mouse::Meta::Attribute (or a subclass)"); |
1b9e472d |
202 | |
87ca293b |
203 | $name = $attr->name; |
1b9e472d |
204 | } |
205 | else{ |
87ca293b |
206 | # _process_attribute |
207 | $name = shift; |
1b9e472d |
208 | |
87ca293b |
209 | my %args = (@_ == 1) ? %{$_[0]} : @_; |
1b9e472d |
210 | |
87ca293b |
211 | defined($name) |
212 | or $self->throw_error('You must provide a name for the attribute'); |
1b9e472d |
213 | |
87ca293b |
214 | if ($name =~ s/^\+//) { # inherited attributes |
2b908b79 |
215 | my $inherited_attr = $self->find_attribute_by_name($name) |
87ca293b |
216 | or $self->throw_error("Could not find an attribute by the name of '$name' to inherit from in ".$self->name); |
217 | |
8cf51b82 |
218 | $attr = $inherited_attr->clone_and_inherit_options(%args); |
87ca293b |
219 | } |
220 | else{ |
8cf51b82 |
221 | my($attribute_class, @traits) = $self->attribute_metaclass->interpolate_class(\%args); |
87ca293b |
222 | $args{traits} = \@traits if @traits; |
223 | |
2a464664 |
224 | $attr = $attribute_class->new($name, %args); |
87ca293b |
225 | } |
226 | } |
1b9e472d |
227 | |
228 | weaken( $attr->{associated_class} = $self ); |
229 | |
df7e4729 |
230 | # install accessors first |
231 | $attr->install_accessors(); |
232 | |
233 | # then register the attribute to the metaclass |
beb51b30 |
234 | $attr->{insertion_order} = keys %{ $self->{attributes} }; |
1b9e472d |
235 | $self->{attributes}{$attr->name} = $attr; |
1b9e472d |
236 | |
3b48bcc8 |
237 | if(!$attr->{associated_methods} && ($attr->{is} || '') ne 'bare'){ |
238 | Carp::carp(qq{Attribute ($name) of class }.$self->name |
239 | .qq{ has no associated methods (did you mean to provide an "is" argument?)}); |
60f6eba9 |
240 | } |
74de0f8b |
241 | |
a3a09e38 |
242 | if(!Mouse::Util::MOUSE_XS) { |
74de0f8b |
243 | # in Mouse::PurePerl, attribute initialization code is cached, so it |
244 | # must be clear here. See _initialize_object() in Mouse::PurePerl. |
a3a09e38 |
245 | delete $self->{_initialize_object}; |
246 | } |
1b9e472d |
247 | return $attr; |
c3398f5b |
248 | } |
249 | |
cccb83de |
250 | sub linearized_isa; |
8536d351 |
251 | |
ba153b33 |
252 | sub new_object; |
13b99908 |
253 | sub clone_object; |
1b9e472d |
254 | |
cfef75f0 |
255 | sub 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 | |
cfef75f0 |
266 | sub make_immutable { |
267 | my $self = shift; |
268 | my %args = $self->immutable_options(@_); |
6a1d1835 |
269 | |
fc1d8369 |
270 | $self->{is_immutable}++; |
c7a6403f |
271 | |
6a1d1835 |
272 | if ($args{inline_constructor}) { |
380e1cd7 |
273 | $self->add_method($args{constructor_name} => |
637d4f17 |
274 | Mouse::Util::load_class($self->constructor_class) |
275 | ->_generate_constructor($self, \%args)); |
c7a6403f |
276 | } |
277 | |
8632b6fe |
278 | if ($args{inline_destructor}) { |
380e1cd7 |
279 | $self->add_method(DESTROY => |
637d4f17 |
280 | Mouse::Util::load_class($self->destructor_class) |
281 | ->_generate_destructor($self, \%args)); |
8632b6fe |
282 | } |
2276cb14 |
283 | |
89681b0b |
284 | # Moose's make_immutable returns true allowing calling code to skip |
285 | # setting an explicit true value at the end of a source file. |
2276cb14 |
286 | return 1; |
fc1d8369 |
287 | } |
ad958001 |
288 | |
32490a72 |
289 | sub make_mutable { |
290 | my($self) = @_; |
291 | $self->{is_immutable} = 0; |
292 | return; |
293 | } |
ad958001 |
294 | |
80efe911 |
295 | sub is_immutable; |
296 | sub is_mutable { !$_[0]->is_immutable } |
84ef660f |
297 | |
8d5287a7 |
298 | sub _install_modifier { |
8d59c723 |
299 | my( $self, $type, $name, $code ) = @_; |
300 | my $into = $self->name; |
3fbade18 |
301 | |
302 | my $original = $into->can($name) |
083e49a5 |
303 | or $self->throw_error("The method '$name' was not found in the inheritance hierarchy for $into"); |
3fbade18 |
304 | |
305 | my $modifier_table = $self->{modifiers}{$name}; |
306 | |
307 | if(!$modifier_table){ |
d686eec5 |
308 | my(@before, @after, @around); |
309 | my $cache = $original; |
1b490891 |
310 | my $modified = sub { |
d686eec5 |
311 | if(@before) { |
312 | for my $c (@before) { $c->(@_) } |
313 | } |
314 | unless(@after) { |
1b490891 |
315 | return $cache->(@_); |
316 | } |
317 | |
3fbade18 |
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, |
d686eec5 |
343 | around => \@around, |
3fbade18 |
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 | |
50dc6ee5 |
367 | sub add_before_method_modifier { |
4859d490 |
368 | my ( $self, $name, $code ) = @_; |
8d59c723 |
369 | $self->_install_modifier( 'before', $name, $code ); |
50dc6ee5 |
370 | } |
371 | |
372 | sub add_around_method_modifier { |
4859d490 |
373 | my ( $self, $name, $code ) = @_; |
8d59c723 |
374 | $self->_install_modifier( 'around', $name, $code ); |
50dc6ee5 |
375 | } |
376 | |
377 | sub add_after_method_modifier { |
4859d490 |
378 | my ( $self, $name, $code ) = @_; |
8d59c723 |
379 | $self->_install_modifier( 'after', $name, $code ); |
50dc6ee5 |
380 | } |
381 | |
67199842 |
382 | sub add_override_method_modifier { |
383 | my ($self, $name, $code) = @_; |
384 | |
768804c0 |
385 | if($self->has_method($name)){ |
386 | $self->throw_error("Cannot add an override method if a local method is already present"); |
85bd3f44 |
387 | } |
388 | |
6cfa1e5e |
389 | my $package = $self->name; |
67199842 |
390 | |
85bd3f44 |
391 | my $super_body = $package->can($name) |
6cfa1e5e |
392 | or $self->throw_error("You cannot override '$name' because it has no super method"); |
67199842 |
393 | |
85bd3f44 |
394 | $self->add_method($name => sub { |
395 | local $Mouse::SUPER_PACKAGE = $package; |
396 | local $Mouse::SUPER_BODY = $super_body; |
397 | local @Mouse::SUPER_ARGS = @_; |
398 | |
399 | $code->(@_); |
400 | }); |
401 | return; |
67199842 |
402 | } |
403 | |
768804c0 |
404 | sub add_augment_method_modifier { |
405 | my ($self, $name, $code) = @_; |
406 | if($self->has_method($name)){ |
407 | $self->throw_error("Cannot add an augment method if a local method is already present"); |
408 | } |
409 | |
410 | my $super = $self->find_method_by_name($name) |
411 | or $self->throw_error("You cannot augment '$name' because it has no super method"); |
412 | |
413 | my $super_package = $super->package_name; |
414 | my $super_body = $super->body; |
415 | |
416 | $self->add_method($name => sub{ |
417 | local $Mouse::INNER_BODY{$super_package} = $code; |
418 | local $Mouse::INNER_ARGS{$super_package} = [@_]; |
419 | $super_body->(@_); |
420 | }); |
421 | return; |
422 | } |
423 | |
47f36c05 |
424 | sub does_role { |
425 | my ($self, $role_name) = @_; |
ad958001 |
426 | |
47f36c05 |
427 | (defined $role_name) |
fce211ae |
428 | || $self->throw_error("You must supply a role name to look for"); |
ad958001 |
429 | |
f3e11122 |
430 | $role_name = $role_name->name if ref $role_name; |
431 | |
f7fec86c |
432 | for my $class ($self->linearized_isa) { |
95ecd6f1 |
433 | my $meta = Mouse::Util::get_metaclass_by_name($class) |
434 | or next; |
3a63a2e7 |
435 | |
436 | for my $role (@{ $meta->roles }) { |
ff687069 |
437 | |
3a63a2e7 |
438 | return 1 if $role->does_role($role_name); |
f7fec86c |
439 | } |
47f36c05 |
440 | } |
ad958001 |
441 | |
47f36c05 |
442 | return 0; |
443 | } |
444 | |
c3398f5b |
445 | 1; |
c3398f5b |
446 | __END__ |
447 | |
448 | =head1 NAME |
449 | |
1820fffe |
450 | Mouse::Meta::Class - The Mouse class metaclass |
c3398f5b |
451 | |
a25ca8d6 |
452 | =head1 VERSION |
453 | |
43c1bb1a |
454 | This document describes Mouse version 0.71 |
a25ca8d6 |
455 | |
503ed648 |
456 | =head1 DESCRIPTION |
457 | |
458 | This class is a meta object protocol for Mouse classes, |
459 | which is a subset of Moose::Meta:::Class. |
460 | |
c3398f5b |
461 | =head1 METHODS |
462 | |
612d3e1a |
463 | =head2 C<< initialize(ClassName) -> Mouse::Meta::Class >> |
c3398f5b |
464 | |
31c5194b |
465 | Finds or creates a C<Mouse::Meta::Class> instance for the given ClassName. Only |
306290e8 |
466 | one instance should exist for a given class. |
c3398f5b |
467 | |
612d3e1a |
468 | =head2 C<< name -> ClassName >> |
c3398f5b |
469 | |
470 | Returns the name of the owner class. |
471 | |
612d3e1a |
472 | =head2 C<< superclasses -> ClassNames >> C<< superclass(ClassNames) >> |
c3398f5b |
473 | |
474 | Gets (or sets) the list of superclasses of the owner class. |
475 | |
31c5194b |
476 | =head2 C<< add_method(name => CodeRef) >> |
c3398f5b |
477 | |
31c5194b |
478 | Adds a method to the owner class. |
c3398f5b |
479 | |
31c5194b |
480 | =head2 C<< has_method(name) -> Bool >> |
72b88a88 |
481 | |
31c5194b |
482 | Returns whether we have a method with the given name. |
72b88a88 |
483 | |
31c5194b |
484 | =head2 C<< get_method(name) -> Mouse::Meta::Method | undef >> |
c68b4110 |
485 | |
31c5194b |
486 | Returns a L<Mouse::Meta::Method> with the given name. |
487 | |
488 | Note that you can also use C<< $metaclass->name->can($name) >> for a method body. |
489 | |
490 | =head2 C<< get_method_list -> Names >> |
491 | |
492 | Returns a list of method names which are defined in the local class. |
493 | If you want a list of all applicable methods for a class, use the |
494 | C<get_all_methods> method. |
495 | |
496 | =head2 C<< get_all_methods -> (Mouse::Meta::Method) >> |
497 | |
498 | Return the list of all L<Mouse::Meta::Method> instances associated with |
499 | the class and its superclasses. |
500 | |
501 | =head2 C<< add_attribute(name => spec | Mouse::Meta::Attribute) >> |
502 | |
503 | Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner |
504 | class. |
c68b4110 |
505 | |
612d3e1a |
506 | =head2 C<< has_attribute(Name) -> Bool >> |
66eea168 |
507 | |
508 | Returns whether we have a L<Mouse::Meta::Attribute> with the given name. |
509 | |
31c5194b |
510 | =head2 C<< get_attribute Name -> Mouse::Meta::Attribute | undef >> |
c3398f5b |
511 | |
306290e8 |
512 | Returns the L<Mouse::Meta::Attribute> with the given name. |
c3398f5b |
513 | |
31c5194b |
514 | =head2 C<< get_attribute_list -> Names >> |
515 | |
516 | Returns a list of attribute names which are defined in the local |
517 | class. If you want a list of all applicable attributes for a class, |
518 | use the C<get_all_attributes> method. |
519 | |
520 | =head2 C<< get_all_attributes -> (Mouse::Meta::Attribute) >> |
521 | |
522 | Returns the list of all L<Mouse::Meta::Attribute> instances associated with |
523 | this class and its superclasses. |
524 | |
612d3e1a |
525 | =head2 C<< linearized_isa -> [ClassNames] >> |
c3398f5b |
526 | |
527 | Returns the list of classes in method dispatch order, with duplicates removed. |
528 | |
612d3e1a |
529 | =head2 C<< new_object(Parameters) -> Instance >> |
2cea7a5f |
530 | |
612d3e1a |
531 | Creates a new instance. |
2cea7a5f |
532 | |
612d3e1a |
533 | =head2 C<< clone_object(Instance, Parameters) -> Instance >> |
f7b11a21 |
534 | |
31c5194b |
535 | Clones the given instance which must be an instance governed by this |
f7b11a21 |
536 | metaclass. |
537 | |
31c5194b |
538 | =head2 C<< throw_error(Message, Parameters) >> |
539 | |
540 | Throws an error with the given message. |
541 | |
1820fffe |
542 | =head1 SEE ALSO |
f7b11a21 |
543 | |
6b82419e |
544 | L<Mouse::Meta::Module> |
545 | |
1820fffe |
546 | L<Moose::Meta::Class> |
f7b11a21 |
547 | |
31c5194b |
548 | L<Class::MOP::Class> |
549 | |
c3398f5b |
550 | =cut |
551 | |