Commit | Line | Data |
c0e30cf5 |
1 | |
2 | package Moose::Meta::Class; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
0addec44 |
7 | use Class::MOP; |
648e79ae |
8 | |
590e8894 |
9 | use Carp qw( confess ); |
2e7f6cf4 |
10 | use Data::OptList; |
f8b6827f |
11 | use List::Util qw( first ); |
349cda54 |
12 | use List::MoreUtils qw( any all uniq first_index ); |
21f1e231 |
13 | use Scalar::Util 'weaken', 'blessed'; |
a15dff8d |
14 | |
74862722 |
15 | use Moose::Meta::Method::Overridden; |
3f9e4b0a |
16 | use Moose::Meta::Method::Augmented; |
77f14411 |
17 | use Moose::Error::Default; |
0fa70d03 |
18 | use Moose::Meta::Class::Immutable::Trait; |
19 | use Moose::Meta::Method::Constructor; |
20 | use Moose::Meta::Method::Destructor; |
699a2e32 |
21 | use Moose::Meta::Method::Meta; |
61907a02 |
22 | use Moose::Util; |
d2782813 |
23 | use Class::MOP::MiniTrait; |
8ee73eeb |
24 | |
c0e30cf5 |
25 | use base 'Class::MOP::Class'; |
26 | |
d2782813 |
27 | Class::MOP::MiniTrait::apply(__PACKAGE__, 'Moose::Meta::Object::Trait'); |
28 | |
598340d5 |
29 | __PACKAGE__->meta->add_attribute('roles' => ( |
ef333f17 |
30 | reader => 'roles', |
31 | default => sub { [] } |
32 | )); |
33 | |
a9b63d79 |
34 | __PACKAGE__->meta->add_attribute('role_applications' => ( |
639f9a1a |
35 | reader => '_get_role_applications', |
a9b63d79 |
36 | default => sub { [] } |
37 | )); |
38 | |
0fa70d03 |
39 | __PACKAGE__->meta->add_attribute( |
40 | Class::MOP::Attribute->new('immutable_trait' => ( |
41 | accessor => "immutable_trait", |
42 | default => 'Moose::Meta::Class::Immutable::Trait', |
43 | )) |
44 | ); |
45 | |
e606ae5f |
46 | __PACKAGE__->meta->add_attribute('constructor_class' => ( |
47 | accessor => 'constructor_class', |
e0001338 |
48 | default => 'Moose::Meta::Method::Constructor', |
e606ae5f |
49 | )); |
50 | |
51 | __PACKAGE__->meta->add_attribute('destructor_class' => ( |
52 | accessor => 'destructor_class', |
e0001338 |
53 | default => 'Moose::Meta::Method::Destructor', |
e606ae5f |
54 | )); |
55 | |
11c86f15 |
56 | __PACKAGE__->meta->add_attribute('error_class' => ( |
bf6fa6b3 |
57 | accessor => 'error_class', |
58 | default => 'Moose::Error::Default', |
11c86f15 |
59 | )); |
60 | |
590868a3 |
61 | sub initialize { |
62 | my $class = shift; |
63 | my $pkg = shift; |
d03bd989 |
64 | return Class::MOP::get_metaclass_by_name($pkg) |
685f7e44 |
65 | || $class->SUPER::initialize($pkg, |
66 | 'attribute_metaclass' => 'Moose::Meta::Attribute', |
67 | 'method_metaclass' => 'Moose::Meta::Method', |
68 | 'instance_metaclass' => 'Moose::Meta::Instance', |
69 | @_ |
d03bd989 |
70 | ); |
ac2dc464 |
71 | } |
590868a3 |
72 | |
61bdd94f |
73 | sub create { |
7d4035ae |
74 | my ($class, $package_name, %options) = @_; |
d03bd989 |
75 | |
61bdd94f |
76 | (ref $options{roles} eq 'ARRAY') |
7d4035ae |
77 | || $class->throw_error("You must pass an ARRAY ref of roles", data => $options{roles}) |
61bdd94f |
78 | if exists $options{roles}; |
310ba883 |
79 | my $roles = delete $options{roles}; |
dd37a5be |
80 | |
7d4035ae |
81 | my $new_meta = $class->SUPER::create($package_name, %options); |
dd37a5be |
82 | |
310ba883 |
83 | if ($roles) { |
7d4035ae |
84 | Moose::Util::apply_all_roles( $new_meta, @$roles ); |
61bdd94f |
85 | } |
d03bd989 |
86 | |
7d4035ae |
87 | return $new_meta; |
61bdd94f |
88 | } |
89 | |
17594769 |
90 | my %ANON_CLASSES; |
91 | |
92 | sub create_anon_class { |
93 | my ($self, %options) = @_; |
94 | |
95 | my $cache_ok = delete $options{cache}; |
d03bd989 |
96 | |
cf600c83 |
97 | my $cache_key |
98 | = _anon_cache_key( $options{superclasses}, $options{roles} ); |
d03bd989 |
99 | |
6d5cbd2b |
100 | if ($cache_ok && defined $ANON_CLASSES{$cache_key}) { |
17594769 |
101 | return $ANON_CLASSES{$cache_key}; |
102 | } |
d03bd989 |
103 | |
dcc8dc06 |
104 | $options{weaken} = !$cache_ok |
105 | unless exists $options{weaken}; |
106 | |
17594769 |
107 | my $new_class = $self->SUPER::create_anon_class(%options); |
108 | |
dcc8dc06 |
109 | if ($cache_ok) { |
110 | $ANON_CLASSES{$cache_key} = $new_class; |
111 | weaken($ANON_CLASSES{$cache_key}); |
112 | } |
17594769 |
113 | |
114 | return $new_class; |
115 | } |
116 | |
699a2e32 |
117 | sub _meta_method_class { 'Moose::Meta::Method::Meta' } |
118 | |
cf600c83 |
119 | sub _anon_cache_key { |
120 | # Makes something like Super::Class|Super::Class::2=Role|Role::1 |
121 | return join '=' => ( |
122 | join( '|', @{ $_[0] || [] } ), |
123 | join( '|', sort @{ $_[1] || [] } ), |
124 | ); |
125 | } |
126 | |
127 | sub reinitialize { |
128 | my $self = shift; |
129 | my $pkg = shift; |
130 | |
131 | my $meta = blessed $pkg ? $pkg : Class::MOP::class_of($pkg); |
132 | |
133 | my $cache_key; |
134 | |
135 | my %existing_classes; |
136 | if ($meta) { |
137 | %existing_classes = map { $_ => $meta->$_() } qw( |
138 | attribute_metaclass |
139 | method_metaclass |
140 | wrapped_method_metaclass |
141 | instance_metaclass |
142 | constructor_class |
143 | destructor_class |
144 | error_class |
145 | ); |
146 | |
147 | $cache_key = _anon_cache_key( |
148 | [ $meta->superclasses ], |
149 | [ map { $_->name } @{ $meta->roles } ], |
150 | ) if $meta->is_anon_class; |
151 | } |
152 | |
153 | my $new_meta = $self->SUPER::reinitialize( |
154 | $pkg, |
155 | %existing_classes, |
156 | @_, |
157 | ); |
158 | |
159 | return $new_meta unless defined $cache_key; |
160 | |
161 | my $new_cache_key = _anon_cache_key( |
162 | [ $meta->superclasses ], |
163 | [ map { $_->name } @{ $meta->roles } ], |
164 | ); |
165 | |
166 | delete $ANON_CLASSES{$cache_key}; |
167 | $ANON_CLASSES{$new_cache_key} = $new_meta; |
dcc8dc06 |
168 | weaken($ANON_CLASSES{$new_cache_key}); |
cf600c83 |
169 | |
170 | return $new_meta; |
171 | } |
172 | |
ef333f17 |
173 | sub add_role { |
174 | my ($self, $role) = @_; |
175 | (blessed($role) && $role->isa('Moose::Meta::Role')) |
11c86f15 |
176 | || $self->throw_error("Roles must be instances of Moose::Meta::Role", data => $role); |
ef333f17 |
177 | push @{$self->roles} => $role; |
178 | } |
179 | |
639f9a1a |
180 | sub role_applications { |
181 | my ($self) = @_; |
182 | |
183 | return @{$self->_get_role_applications}; |
184 | } |
185 | |
a9b63d79 |
186 | sub add_role_application { |
187 | my ($self, $application) = @_; |
188 | (blessed($application) && $application->isa('Moose::Meta::Role::Application::ToClass')) |
189 | || $self->throw_error("Role applications must be instances of Moose::Meta::Role::Application::ToClass", data => $application); |
639f9a1a |
190 | push @{$self->_get_role_applications} => $application; |
a9b63d79 |
191 | } |
192 | |
b8aeb4dc |
193 | sub calculate_all_roles { |
194 | my $self = shift; |
195 | my %seen; |
196 | grep { !$seen{$_->name}++ } map { $_->calculate_all_roles } @{ $self->roles }; |
197 | } |
198 | |
9f83eb5d |
199 | sub calculate_all_roles_with_inheritance { |
200 | my $self = shift; |
201 | my %seen; |
202 | grep { !$seen{$_->name}++ } |
203 | map { Class::MOP::class_of($_)->can('calculate_all_roles') |
204 | ? Class::MOP::class_of($_)->calculate_all_roles |
205 | : () } |
206 | $self->linearized_isa; |
207 | } |
208 | |
ef333f17 |
209 | sub does_role { |
210 | my ($self, $role_name) = @_; |
322abb07 |
211 | |
ef333f17 |
212 | (defined $role_name) |
11c86f15 |
213 | || $self->throw_error("You must supply a role name to look for"); |
322abb07 |
214 | |
9c429218 |
215 | foreach my $class ($self->class_precedence_list) { |
322abb07 |
216 | my $meta = Class::MOP::class_of($class); |
3d0f5a27 |
217 | # when a Moose metaclass is itself extended with a role, |
218 | # this check needs to be done since some items in the |
219 | # class_precedence_list might in fact be Class::MOP |
220 | # based still. |
322abb07 |
221 | next unless $meta && $meta->can('roles'); |
222 | foreach my $role (@{$meta->roles}) { |
9c429218 |
223 | return 1 if $role->does_role($role_name); |
224 | } |
ef333f17 |
225 | } |
226 | return 0; |
227 | } |
228 | |
d79e62fd |
229 | sub excludes_role { |
230 | my ($self, $role_name) = @_; |
ebfc4d0f |
231 | |
d79e62fd |
232 | (defined $role_name) |
11c86f15 |
233 | || $self->throw_error("You must supply a role name to look for"); |
ebfc4d0f |
234 | |
ac2dc464 |
235 | foreach my $class ($self->class_precedence_list) { |
ebfc4d0f |
236 | my $meta = Class::MOP::class_of($class); |
237 | # when a Moose metaclass is itself extended with a role, |
238 | # this check needs to be done since some items in the |
239 | # class_precedence_list might in fact be Class::MOP |
240 | # based still. |
241 | next unless $meta && $meta->can('roles'); |
242 | foreach my $role (@{$meta->roles}) { |
9c429218 |
243 | return 1 if $role->excludes_role($role_name); |
244 | } |
d79e62fd |
245 | } |
246 | return 0; |
247 | } |
248 | |
8c9d74e7 |
249 | sub new_object { |
7d4035ae |
250 | my $self = shift; |
e606ae5f |
251 | my $params = @_ == 1 ? $_[0] : {@_}; |
7d4035ae |
252 | my $object = $self->SUPER::new_object($params); |
1308deb4 |
253 | |
7d4035ae |
254 | foreach my $attr ( $self->get_all_attributes() ) { |
1308deb4 |
255 | |
256 | next unless $attr->can('has_trigger') && $attr->has_trigger; |
257 | |
258 | my $init_arg = $attr->init_arg; |
259 | |
260 | next unless defined $init_arg; |
261 | |
262 | next unless exists $params->{$init_arg}; |
263 | |
264 | $attr->trigger->( |
7d4035ae |
265 | $object, |
1308deb4 |
266 | ( |
267 | $attr->should_coerce |
7d4035ae |
268 | ? $attr->get_read_method_ref->($object) |
1308deb4 |
269 | : $params->{$init_arg} |
270 | ), |
1308deb4 |
271 | ); |
8c9d74e7 |
272 | } |
1308deb4 |
273 | |
7d4035ae |
274 | $object->BUILDALL($params) if $object->can('BUILDALL'); |
a19ae3d7 |
275 | |
7d4035ae |
276 | return $object; |
8c9d74e7 |
277 | } |
278 | |
e3225a0f |
279 | sub _generate_fallback_constructor { |
280 | my $self = shift; |
281 | my ($class) = @_; |
282 | return $class . '->Moose::Object::new(@_)' |
283 | } |
284 | |
285 | sub _inline_params { |
286 | my $self = shift; |
287 | my ($params, $class) = @_; |
288 | return ( |
289 | 'my ' . $params . ' = ', |
290 | $self->_inline_BUILDARGS($class, '@_'), |
291 | ';', |
292 | ); |
293 | } |
294 | |
295 | sub _inline_BUILDARGS { |
296 | my $self = shift; |
297 | my ($class, $args) = @_; |
298 | |
299 | my $buildargs = $self->find_method_by_name("BUILDARGS"); |
300 | |
301 | if ($args eq '@_' |
302 | && (!$buildargs or $buildargs->body == \&Moose::Object::BUILDARGS)) { |
303 | return ( |
304 | 'do {', |
305 | 'my $params;', |
306 | 'if (scalar @_ == 1) {', |
307 | 'if (!defined($_[0]) || ref($_[0]) ne \'HASH\') {', |
308 | $self->_inline_throw_error( |
309 | '"Single parameters to new() must be a HASH ref"', |
310 | 'data => $_[0]', |
311 | ) . ';', |
312 | '}', |
313 | '$params = { %{ $_[0] } };', |
314 | '}', |
315 | 'elsif (@_ % 2) {', |
316 | 'Carp::carp(', |
317 | '"The new() method for ' . $class . ' expects a ' |
318 | . 'hash reference or a key/value list. You passed an ' |
319 | . 'odd number of arguments"', |
320 | ');', |
321 | '$params = {@_, undef};', |
322 | '}', |
323 | 'else {', |
324 | '$params = {@_};', |
325 | '}', |
326 | '$params;', |
327 | '}', |
328 | ); |
329 | } |
330 | else { |
331 | return $class . '->BUILDARGS(' . $args . ')'; |
332 | } |
333 | } |
334 | |
335 | sub _inline_slot_initializer { |
336 | my $self = shift; |
ec86bdff |
337 | my ($attr, $idx) = @_; |
e3225a0f |
338 | |
ec86bdff |
339 | return ( |
340 | '## ' . $attr->name, |
341 | $self->_inline_check_required_attr($attr), |
342 | $self->SUPER::_inline_slot_initializer(@_), |
343 | ); |
e3225a0f |
344 | } |
345 | |
346 | sub _inline_check_required_attr { |
347 | my $self = shift; |
348 | my ($attr) = @_; |
349 | |
350 | return unless defined $attr->init_arg; |
351 | return unless $attr->can('is_required') && $attr->is_required; |
352 | return if $attr->has_default || $attr->has_builder; |
353 | |
354 | return ( |
355 | 'if (!exists $params->{\'' . $attr->init_arg . '\'}) {', |
356 | $self->_inline_throw_error( |
357 | '"Attribute (' . quotemeta($attr->name) . ') is required"' |
358 | ) . ';', |
359 | '}', |
360 | ); |
361 | } |
362 | |
7106ce79 |
363 | # XXX: these two are duplicated from cmop, because we have to pass the tc stuff |
364 | # through to _inline_set_value - this should probably be fixed, but i'm not |
365 | # quite sure how. -doy |
e3225a0f |
366 | sub _inline_init_attr_from_constructor { |
367 | my $self = shift; |
ec86bdff |
368 | my ($attr, $idx) = @_; |
369 | |
370 | my @initial_value = $attr->_inline_set_value( |
371 | '$instance', |
372 | '$params->{\'' . $attr->init_arg . '\'}', |
373 | '$type_constraint_bodies[' . $idx . ']', |
374 | '$type_constraints[' . $idx . ']', |
375 | 'for constructor', |
e3225a0f |
376 | ); |
377 | |
ec86bdff |
378 | push @initial_value, ( |
379 | '$attrs->[' . $idx . ']->set_initial_value(', |
380 | '$instance,', |
381 | $attr->_inline_instance_get('$instance'), |
382 | ');', |
383 | ) if $attr->has_initializer; |
e3225a0f |
384 | |
ec86bdff |
385 | return @initial_value; |
e3225a0f |
386 | } |
387 | |
ec86bdff |
388 | sub _inline_init_attr_from_default { |
e3225a0f |
389 | my $self = shift; |
ec86bdff |
390 | my ($attr, $idx) = @_; |
e3225a0f |
391 | |
545c6012 |
392 | return if $attr->can('is_lazy') && $attr->is_lazy; |
ec86bdff |
393 | my $default = $self->_inline_default_value($attr, $idx); |
394 | return unless $default; |
e3225a0f |
395 | |
ec86bdff |
396 | my @initial_value = ( |
397 | 'my $default = ' . $default . ';', |
398 | $attr->_inline_set_value( |
399 | '$instance', |
400 | '$default', |
401 | '$type_constraint_bodies[' . $idx . ']', |
402 | '$type_constraints[' . $idx . ']', |
403 | 'for constructor', |
404 | ), |
e3225a0f |
405 | ); |
406 | |
ec86bdff |
407 | push @initial_value, ( |
408 | '$attrs->[' . $idx . ']->set_initial_value(', |
409 | '$instance,', |
410 | $attr->_inline_instance_get('$instance'), |
411 | ');', |
412 | ) if $attr->has_initializer; |
e3225a0f |
413 | |
ec86bdff |
414 | return @initial_value; |
e3225a0f |
415 | } |
416 | |
417 | sub _inline_extra_init { |
418 | my $self = shift; |
419 | return ( |
420 | $self->_inline_triggers, |
421 | $self->_inline_BUILDALL, |
422 | ); |
423 | } |
424 | |
425 | sub _inline_triggers { |
426 | my $self = shift; |
427 | my @trigger_calls; |
428 | |
ec86bdff |
429 | my @attrs = sort { $a->name cmp $b->name } $self->get_all_attributes; |
e3225a0f |
430 | for my $i (0 .. $#attrs) { |
431 | my $attr = $attrs[$i]; |
432 | |
433 | next unless $attr->can('has_trigger') && $attr->has_trigger; |
434 | |
435 | my $init_arg = $attr->init_arg; |
436 | next unless defined $init_arg; |
437 | |
438 | push @trigger_calls, |
439 | 'if (exists $params->{\'' . $init_arg . '\'}) {', |
440 | '$attrs->[' . $i . ']->trigger->(', |
441 | '$instance,', |
442 | $attr->_inline_instance_get('$instance') . ',', |
443 | ');', |
444 | '}'; |
445 | } |
446 | |
447 | return @trigger_calls; |
448 | } |
449 | |
450 | sub _inline_BUILDALL { |
451 | my $self = shift; |
452 | |
453 | my @methods = reverse $self->find_all_methods_by_name('BUILD'); |
454 | my @BUILD_calls; |
455 | |
456 | foreach my $method (@methods) { |
457 | push @BUILD_calls, |
458 | '$instance->' . $method->{class} . '::BUILD($params);'; |
459 | } |
460 | |
461 | return @BUILD_calls; |
462 | } |
463 | |
e2eef3a5 |
464 | sub superclasses { |
465 | my $self = shift; |
2e7f6cf4 |
466 | my $supers = Data::OptList::mkopt(\@_); |
467 | foreach my $super (@{ $supers }) { |
468 | my ($name, $opts) = @{ $super }; |
469 | Class::MOP::load_class($name, $opts); |
470 | my $meta = Class::MOP::class_of($name); |
471 | $self->throw_error("You cannot inherit from a Moose Role ($name)") |
e2eef3a5 |
472 | if $meta && $meta->isa('Moose::Meta::Role') |
473 | } |
2e7f6cf4 |
474 | return $self->SUPER::superclasses(map { $_->[0] } @{ $supers }); |
e2eef3a5 |
475 | } |
476 | |
093b12c2 |
477 | ### --------------------------------------------- |
478 | |
a2eec5e7 |
479 | sub add_attribute { |
480 | my $self = shift; |
28af3424 |
481 | my $attr = |
e472c9a5 |
482 | (blessed $_[0] && $_[0]->isa('Class::MOP::Attribute') |
d03bd989 |
483 | ? $_[0] |
28af3424 |
484 | : $self->_process_attribute(@_)); |
485 | $self->SUPER::add_attribute($attr); |
486 | # it may be a Class::MOP::Attribute, theoretically, which doesn't have |
487 | # 'bare' and doesn't implement this method |
9340e346 |
488 | if ($attr->can('_check_associated_methods')) { |
489 | $attr->_check_associated_methods; |
28af3424 |
490 | } |
491 | return $attr; |
a2eec5e7 |
492 | } |
493 | |
78cd1d3b |
494 | sub add_override_method_modifier { |
495 | my ($self, $name, $method, $_super_package) = @_; |
18c2ec0e |
496 | |
d05cd563 |
497 | (!$self->has_method($name)) |
11c86f15 |
498 | || $self->throw_error("Cannot add an override method if a local method is already present"); |
18c2ec0e |
499 | |
74862722 |
500 | $self->add_method($name => Moose::Meta::Method::Overridden->new( |
3f9e4b0a |
501 | method => $method, |
502 | class => $self, |
503 | package => $_super_package, # need this for roles |
504 | name => $name, |
18c2ec0e |
505 | )); |
78cd1d3b |
506 | } |
507 | |
508 | sub add_augment_method_modifier { |
ac2dc464 |
509 | my ($self, $name, $method) = @_; |
d05cd563 |
510 | (!$self->has_method($name)) |
11c86f15 |
511 | || $self->throw_error("Cannot add an augment method if a local method is already present"); |
3f9e4b0a |
512 | |
513 | $self->add_method($name => Moose::Meta::Method::Augmented->new( |
514 | method => $method, |
515 | class => $self, |
516 | name => $name, |
517 | )); |
78cd1d3b |
518 | } |
519 | |
1341f10c |
520 | ## Private Utility methods ... |
521 | |
05d9eaf6 |
522 | sub _find_next_method_by_name_which_is_not_overridden { |
523 | my ($self, $name) = @_; |
68efb014 |
524 | foreach my $method ($self->find_all_methods_by_name($name)) { |
ac2dc464 |
525 | return $method->{code} |
74862722 |
526 | if blessed($method->{code}) && !$method->{code}->isa('Moose::Meta::Method::Overridden'); |
05d9eaf6 |
527 | } |
528 | return undef; |
529 | } |
530 | |
f6df97ae |
531 | ## Metaclass compatibility |
f8b6827f |
532 | |
f6df97ae |
533 | sub _base_metaclasses { |
534 | my $self = shift; |
535 | my %metaclasses = $self->SUPER::_base_metaclasses; |
536 | for my $class (keys %metaclasses) { |
537 | $metaclasses{$class} =~ s/^Class::MOP/Moose::Meta/; |
1341f10c |
538 | } |
f6df97ae |
539 | return ( |
540 | %metaclasses, |
541 | error_class => 'Moose::Error::Default', |
f8b6827f |
542 | ); |
f8b6827f |
543 | } |
544 | |
f6df97ae |
545 | sub _fix_class_metaclass_incompatibility { |
546 | my $self = shift; |
547 | my ($super_meta) = @_; |
f8b6827f |
548 | |
f6df97ae |
549 | $self->SUPER::_fix_class_metaclass_incompatibility(@_); |
f8b6827f |
550 | |
88f2e008 |
551 | if ($self->_class_metaclass_can_be_made_compatible($super_meta)) { |
590e8894 |
552 | ($self->is_pristine) |
553 | || confess "Can't fix metaclass incompatibility for " |
554 | . $self->name |
555 | . " because it is not pristine."; |
a907317a |
556 | my $super_meta_name = $super_meta->_real_ref_name; |
61907a02 |
557 | my $class_meta_subclass_meta_name = Moose::Util::_reconcile_roles_for_metaclass(blessed($self), $super_meta_name); |
8450b001 |
558 | my $new_self = $class_meta_subclass_meta_name->reinitialize( |
cf7febc7 |
559 | $self->name, |
560 | ); |
6a52b083 |
561 | |
8450b001 |
562 | $self->_replace_self( $new_self, $class_meta_subclass_meta_name ); |
f8b6827f |
563 | } |
f6df97ae |
564 | } |
f8b6827f |
565 | |
f6df97ae |
566 | sub _fix_single_metaclass_incompatibility { |
567 | my $self = shift; |
568 | my ($metaclass_type, $super_meta) = @_; |
f8b6827f |
569 | |
f6df97ae |
570 | $self->SUPER::_fix_single_metaclass_incompatibility(@_); |
f8b6827f |
571 | |
88f2e008 |
572 | if ($self->_single_metaclass_can_be_made_compatible($super_meta, $metaclass_type)) { |
590e8894 |
573 | ($self->is_pristine) |
574 | || confess "Can't fix metaclass incompatibility for " |
575 | . $self->name |
576 | . " because it is not pristine."; |
7f6c8567 |
577 | my $super_meta_name = $super_meta->_real_ref_name; |
61907a02 |
578 | my $class_specific_meta_subclass_meta_name = Moose::Util::_reconcile_roles_for_metaclass($self->$metaclass_type, $super_meta->$metaclass_type); |
cf7febc7 |
579 | my $new_self = $super_meta->reinitialize( |
580 | $self->name, |
8450b001 |
581 | $metaclass_type => $class_specific_meta_subclass_meta_name, |
cf7febc7 |
582 | ); |
6a52b083 |
583 | |
7f6c8567 |
584 | $self->_replace_self( $new_self, $super_meta_name ); |
f6df97ae |
585 | } |
f8b6827f |
586 | } |
587 | |
6a52b083 |
588 | sub _replace_self { |
589 | my $self = shift; |
590 | my ( $new_self, $new_class) = @_; |
591 | |
592 | %$self = %$new_self; |
593 | bless $self, $new_class; |
594 | |
595 | # We need to replace the cached metaclass instance or else when it goes |
596 | # out of scope Class::MOP::Class destroy's the namespace for the |
597 | # metaclass's class, causing much havoc. |
dcc8dc06 |
598 | my $weaken = Class::MOP::metaclass_is_weak( $self->name ); |
6a52b083 |
599 | Class::MOP::store_metaclass_by_name( $self->name, $self ); |
dcc8dc06 |
600 | Class::MOP::weaken_metaclass( $self->name ) if $weaken; |
6a52b083 |
601 | } |
602 | |
1341f10c |
603 | sub _process_attribute { |
a3738e5b |
604 | my ( $self, $name, @args ) = @_; |
7e59b803 |
605 | |
606 | @args = %{$args[0]} if scalar @args == 1 && ref($args[0]) eq 'HASH'; |
d9bb6c63 |
607 | |
f9b5f5f8 |
608 | if (($name || '') =~ /^\+(.*)/) { |
7e59b803 |
609 | return $self->_process_inherited_attribute($1, @args); |
1341f10c |
610 | } |
611 | else { |
7e59b803 |
612 | return $self->_process_new_attribute($name, @args); |
613 | } |
614 | } |
615 | |
616 | sub _process_new_attribute { |
617 | my ( $self, $name, @args ) = @_; |
7e59b803 |
618 | |
d5c30e52 |
619 | $self->attribute_metaclass->interpolate_class_and_new($name, @args); |
1341f10c |
620 | } |
621 | |
622 | sub _process_inherited_attribute { |
623 | my ($self, $attr_name, %options) = @_; |
624 | my $inherited_attr = $self->find_attribute_by_name($attr_name); |
625 | (defined $inherited_attr) |
329c5dd4 |
626 | || $self->throw_error("Could not find an attribute by the name of '$attr_name' to inherit from in ${\$self->name}", data => $attr_name); |
1341f10c |
627 | if ($inherited_attr->isa('Moose::Meta::Attribute')) { |
d7d8a8c7 |
628 | return $inherited_attr->clone_and_inherit_options(%options); |
1341f10c |
629 | } |
630 | else { |
631 | # NOTE: |
632 | # kind of a kludge to handle Class::MOP::Attributes |
d7d8a8c7 |
633 | return $inherited_attr->Moose::Meta::Attribute::clone_and_inherit_options(%options); |
ac2dc464 |
634 | } |
1341f10c |
635 | } |
636 | |
17c594b1 |
637 | # reinitialization support |
638 | |
639 | sub _restore_metaobjects_from { |
640 | my $self = shift; |
641 | my ($old_meta) = @_; |
642 | |
643 | $self->SUPER::_restore_metaobjects_from($old_meta); |
644 | |
645 | for my $role ( @{ $old_meta->roles } ) { |
646 | $self->add_role($role); |
647 | } |
648 | |
649 | for my $application ( @{ $old_meta->_get_role_applications } ) { |
650 | $application->class($self); |
651 | $self->add_role_application ($application); |
652 | } |
653 | } |
654 | |
948cd189 |
655 | ## Immutability |
656 | |
657 | sub _immutable_options { |
658 | my ( $self, @args ) = @_; |
659 | |
660 | $self->SUPER::_immutable_options( |
661 | inline_destructor => 1, |
948cd189 |
662 | |
663 | # Moose always does this when an attribute is created |
664 | inline_accessors => 0, |
665 | |
666 | @args, |
667 | ); |
668 | } |
669 | |
5cf3dbcf |
670 | ## ------------------------------------------------- |
671 | |
bf6fa6b3 |
672 | our $error_level; |
11c86f15 |
673 | |
674 | sub throw_error { |
675 | my ( $self, @args ) = @_; |
bf6fa6b3 |
676 | local $error_level = ($error_level || 0) + 1; |
11c86f15 |
677 | $self->raise_error($self->create_error(@args)); |
678 | } |
679 | |
e3225a0f |
680 | sub _inline_throw_error { |
681 | my ( $self, $msg, $args ) = @_; |
682 | "\$meta->throw_error($msg" . ($args ? ", $args" : "") . ")"; # FIXME makes deparsing *REALLY* hard |
683 | } |
684 | |
11c86f15 |
685 | sub raise_error { |
686 | my ( $self, @args ) = @_; |
687 | die @args; |
688 | } |
689 | |
690 | sub create_error { |
691 | my ( $self, @args ) = @_; |
692 | |
18748ad6 |
693 | require Carp::Heavy; |
694 | |
bf6fa6b3 |
695 | local $error_level = ($error_level || 0 ) + 1; |
18748ad6 |
696 | |
11c86f15 |
697 | if ( @args % 2 == 1 ) { |
698 | unshift @args, "message"; |
699 | } |
700 | |
fcab1742 |
701 | my %args = ( metaclass => $self, last_error => $@, @args ); |
11c86f15 |
702 | |
bf6fa6b3 |
703 | $args{depth} += $error_level; |
11c86f15 |
704 | |
bf6fa6b3 |
705 | my $class = ref $self ? $self->error_class : "Moose::Error::Default"; |
11c86f15 |
706 | |
a810a01f |
707 | Class::MOP::load_class($class); |
708 | |
11c86f15 |
709 | $class->new( |
bf6fa6b3 |
710 | Carp::caller_info($args{depth}), |
711 | %args |
11c86f15 |
712 | ); |
713 | } |
714 | |
c0e30cf5 |
715 | 1; |
716 | |
ad46f524 |
717 | # ABSTRACT: The Moose metaclass |
718 | |
c0e30cf5 |
719 | __END__ |
720 | |
721 | =pod |
722 | |
c0e30cf5 |
723 | =head1 DESCRIPTION |
724 | |
70bb0f97 |
725 | This class is a subclass of L<Class::MOP::Class> that provides |
726 | additional Moose-specific functionality. |
e522431d |
727 | |
7854b409 |
728 | To really understand this class, you will need to start with the |
729 | L<Class::MOP::Class> documentation. This class can be understood as a |
730 | set of additional features on top of the basic feature provided by |
731 | that parent class. |
6ba6d68c |
732 | |
d4b1449e |
733 | =head1 INHERITANCE |
734 | |
735 | C<Moose::Meta::Class> is a subclass of L<Class::MOP::Class>. |
736 | |
c0e30cf5 |
737 | =head1 METHODS |
738 | |
739 | =over 4 |
740 | |
70bb0f97 |
741 | =item B<< Moose::Meta::Class->initialize($package_name, %options) >> |
590868a3 |
742 | |
70bb0f97 |
743 | This overrides the parent's method in order to provide its own |
744 | defaults for the C<attribute_metaclass>, C<instance_metaclass>, and |
745 | C<method_metaclass> options. |
61bdd94f |
746 | |
70bb0f97 |
747 | These all default to the appropriate Moose class. |
61bdd94f |
748 | |
70bb0f97 |
749 | =item B<< Moose::Meta::Class->create($package_name, %options) >> |
17594769 |
750 | |
70bb0f97 |
751 | This overrides the parent's method in order to accept a C<roles> |
9e25a72a |
752 | option. This should be an array reference containing roles |
753 | that the class does, each optionally followed by a hashref of options |
754 | (C<-excludes> and C<-alias>). |
17594769 |
755 | |
70bb0f97 |
756 | my $metaclass = Moose::Meta::Class->create( 'New::Class', roles => [...] ); |
17594769 |
757 | |
70bb0f97 |
758 | =item B<< Moose::Meta::Class->create_anon_class >> |
17594769 |
759 | |
70bb0f97 |
760 | This overrides the parent's method to accept a C<roles> option, just |
761 | as C<create> does. |
5cf3dbcf |
762 | |
70bb0f97 |
763 | It also accepts a C<cache> option. If this is true, then the anonymous |
764 | class will be cached based on its superclasses and roles. If an |
765 | existing anonymous class in the cache has the same superclasses and |
766 | roles, it will be reused. |
ac2dc464 |
767 | |
70bb0f97 |
768 | my $metaclass = Moose::Meta::Class->create_anon_class( |
769 | superclasses => ['Foo'], |
770 | roles => [qw/Some Roles Go Here/], |
771 | cache => 1, |
772 | ); |
ac2dc464 |
773 | |
2e7f6cf4 |
774 | Each entry in both the C<superclasses> and the C<roles> option can be |
b2d54db8 |
775 | followed by a hash reference with arguments. The C<superclasses> |
2e7f6cf4 |
776 | option can be supplied with a L<-version|Class::MOP/Class Loading |
777 | Options> option that ensures the loaded superclass satisfies the |
778 | required version. The C<role> option also takes the C<-version> as an |
779 | argument, but the option hash reference can also contain any other |
780 | role relevant values like exclusions or parameterized role arguments. |
781 | |
70bb0f97 |
782 | =item B<< $metaclass->make_immutable(%options) >> |
ac2dc464 |
783 | |
70bb0f97 |
784 | This overrides the parent's method to add a few options. Specifically, |
785 | it uses the Moose-specific constructor and destructor classes, and |
786 | enables inlining the destructor. |
8c9d74e7 |
787 | |
dcdceb38 |
788 | Since Moose always inlines attributes, it sets the C<inline_accessors> option |
789 | to false. |
790 | |
70bb0f97 |
791 | =item B<< $metaclass->new_object(%params) >> |
a15dff8d |
792 | |
70bb0f97 |
793 | This overrides the parent's method in order to add support for |
794 | attribute triggers. |
6ba6d68c |
795 | |
2e7f6cf4 |
796 | =item B<< $metaclass->superclasses(@superclasses) >> |
797 | |
6b958a3e |
798 | This is the accessor allowing you to read or change the parents of |
2e7f6cf4 |
799 | the class. |
800 | |
801 | Each superclass can be followed by a hash reference containing a |
802 | L<-version|Class::MOP/Class Loading Options> value. If the version |
803 | requirement is not satisfied an error will be thrown. |
804 | |
70bb0f97 |
805 | =item B<< $metaclass->add_override_method_modifier($name, $sub) >> |
ef1d5f4b |
806 | |
70bb0f97 |
807 | This adds an C<override> method modifier to the package. |
e9ec68d6 |
808 | |
70bb0f97 |
809 | =item B<< $metaclass->add_augment_method_modifier($name, $sub) >> |
e9ec68d6 |
810 | |
70bb0f97 |
811 | This adds an C<augment> method modifier to the package. |
78cd1d3b |
812 | |
70bb0f97 |
813 | =item B<< $metaclass->calculate_all_roles >> |
02a0fb52 |
814 | |
70bb0f97 |
815 | This will return a unique array of C<Moose::Meta::Role> instances |
816 | which are attached to this class. |
78cd1d3b |
817 | |
9f83eb5d |
818 | =item B<< $metaclass->calculate_all_roles_with_inheritance >> |
819 | |
820 | This will return a unique array of C<Moose::Meta::Role> instances |
821 | which are attached to this class, and each of this class's ancestors. |
822 | |
70bb0f97 |
823 | =item B<< $metaclass->add_role($role) >> |
02a0fb52 |
824 | |
70bb0f97 |
825 | This takes a L<Moose::Meta::Role> object, and adds it to the class's |
826 | list of roles. This I<does not> actually apply the role to the class. |
2b14ac61 |
827 | |
b90dd4ef |
828 | =item B<< $metaclass->role_applications >> |
829 | |
639f9a1a |
830 | Returns a list of L<Moose::Meta::Role::Application::ToClass> |
b90dd4ef |
831 | objects, which contain the arguments to role application. |
832 | |
833 | =item B<< $metaclass->add_role_application($application) >> |
834 | |
835 | This takes a L<Moose::Meta::Role::Application::ToClass> object, and |
836 | adds it to the class's list of role applications. This I<does not> |
837 | actually apply any role to the class; it is only for tracking role |
838 | applications. |
839 | |
560c498d |
840 | =item B<< $metaclass->does_role($role) >> |
ef333f17 |
841 | |
560c498d |
842 | This returns a boolean indicating whether or not the class does the specified |
843 | role. The role provided can be either a role name or a L<Moose::Meta::Role> |
844 | object. This tests both the class and its parents. |
02a0fb52 |
845 | |
70bb0f97 |
846 | =item B<< $metaclass->excludes_role($role_name) >> |
ef333f17 |
847 | |
70bb0f97 |
848 | A class excludes a role if it has already composed a role which |
849 | excludes the named role. This tests both the class and its parents. |
02a0fb52 |
850 | |
70bb0f97 |
851 | =item B<< $metaclass->add_attribute($attr_name, %params|$params) >> |
ef333f17 |
852 | |
70bb0f97 |
853 | This overrides the parent's method in order to allow the parameters to |
854 | be provided as a hash reference. |
02a0fb52 |
855 | |
9f9fdd08 |
856 | =item B<< $metaclass->constructor_class($class_name) >> |
d79e62fd |
857 | |
9f9fdd08 |
858 | =item B<< $metaclass->destructor_class($class_name) >> |
e606ae5f |
859 | |
948cd189 |
860 | These are the names of classes used when making a class immutable. These |
90a49845 |
861 | default to L<Moose::Meta::Method::Constructor> and |
862 | L<Moose::Meta::Method::Destructor> respectively. These accessors are |
863 | read-write, so you can use them to change the class name. |
e606ae5f |
864 | |
70bb0f97 |
865 | =item B<< $metaclass->error_class($class_name) >> |
8b1d510f |
866 | |
70bb0f97 |
867 | The name of the class used to throw errors. This defaults to |
8b1d510f |
868 | L<Moose::Error::Default>, which generates an error with a stacktrace |
869 | just like C<Carp::confess>. |
870 | |
70bb0f97 |
871 | =item B<< $metaclass->throw_error($message, %extra) >> |
11c86f15 |
872 | |
873 | Throws the error created by C<create_error> using C<raise_error> |
874 | |
c0e30cf5 |
875 | =back |
876 | |
877 | =head1 BUGS |
878 | |
d4048ef3 |
879 | See L<Moose/BUGS> for details on reporting bugs. |
c0e30cf5 |
880 | |
8a7a9c53 |
881 | =cut |
1a563243 |
882 | |