1 package Mouse::Meta::Class;
2 use Mouse::Util qw/:meta get_linear_isa not_supported/; # enables strict and warnings
4 use Scalar::Util qw/blessed weaken/;
6 use Mouse::Meta::Module;
7 our @ISA = qw(Mouse::Meta::Module);
10 sub attribute_metaclass;
12 sub constructor_class;
16 my($class, %args) = @_;
18 $args{attributes} = {};
22 $args{superclasses} = do {
24 \@{ $args{package} . '::ISA' };
27 my $self = bless \%args, ref($class) || $class;
28 if(ref($self) ne __PACKAGE__){
29 $self->meta->_initialize_object($self, \%args);
34 sub create_anon_class{
36 return $self->create(undef, @_);
43 sub calculate_all_roles {
46 return grep { !$seen{ $_->name }++ }
47 map { $_->calculate_all_roles } @{ $self->roles };
54 foreach my $super(@_){
55 Mouse::Util::load_class($super);
56 my $meta = Mouse::Util::get_metaclass_by_name($super);
58 next if not defined $meta;
60 if(Mouse::Util::is_a_metarole($meta)){
61 $self->throw_error("You cannot inherit from a Mouse Role ($super)");
64 next if $self->isa(ref $meta); # _superclass_meta_is_compatible
66 $self->_reconcile_with_superclass_meta($meta);
68 @{ $self->{superclasses} } = @_;
71 return @{ $self->{superclasses} };
74 my @MetaClassTypes = qw(
81 sub _reconcile_with_superclass_meta {
82 my($self, $super_meta) = @_;
86 foreach my $metaclass_type(@MetaClassTypes){
87 my $super_c = $super_meta->$metaclass_type();
88 my $self_c = $self->$metaclass_type();
90 if(!$super_c->isa($self_c)){
91 push @incompatibles, ($metaclass_type => $super_c);
95 $super_meta->reinitialize($self, @incompatibles);
100 sub 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');
105 foreach my $class( $self->linearized_isa ){
106 my $method = $self->initialize($class)->get_method($method_name);
107 return $method if defined $method;
112 sub get_all_methods {
114 return map{ $self->find_method_by_name($_) } $self->get_all_method_names;
117 sub get_all_method_names {
120 return grep { $uniq{$_}++ == 0 }
121 map { Mouse::Meta::Class->initialize($_)->get_method_list() }
122 $self->linearized_isa;
125 sub find_attribute_by_name{
126 my($self, $name) = @_;
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;
143 $attr->isa('Mouse::Meta::Attribute')
144 || $self->throw_error("Your attribute must be an instance of Mouse::Meta::Attribute (or a subclass)");
152 my %args = (@_ == 1) ? %{$_[0]} : @_;
155 or $self->throw_error('You must provide a name for the attribute');
157 if ($name =~ s/^\+//) { # inherited attributes
158 my $inherited_attr = $self->find_attribute_by_name($name)
159 or $self->throw_error("Could not find an attribute by the name of '$name' to inherit from in ".$self->name);
161 $attr = $inherited_attr->clone_and_inherit_options(%args);
164 my($attribute_class, @traits) = $self->attribute_metaclass->interpolate_class(\%args);
165 $args{traits} = \@traits if @traits;
167 $attr = $attribute_class->new($name, %args);
171 weaken( $attr->{associated_class} = $self );
173 $self->{attributes}{$attr->name} = $attr;
174 $attr->install_accessors();
176 if(Mouse::Util::_MOUSE_VERBOSE && !$attr->{associated_methods} && ($attr->{is} || '') ne 'bare'){
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?)});
182 sub compute_all_applicable_attributes {
183 Carp::cluck('compute_all_applicable_attributes() has been deprecated')
184 if Mouse::Util::_MOUSE_VERBOSE;
185 return shift->get_all_attributes(@_)
195 my %params = (@_ == 1) ? %{$_[0]} : @_;
197 (blessed($object) && $object->isa($class->name))
198 || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($object)");
200 my $cloned = bless { %$object }, ref $object;
201 $class->_initialize_object($cloned, \%params);
207 my ($class, $instance, %params) = @_;
209 Carp::cluck('clone_instance has been deprecated. Use clone_object instead')
210 if Mouse::Util::_MOUSE_VERBOSE;
211 return $class->clone_object($instance, %params);
217 inline_constructor => 1,
218 inline_destructor => 1,
219 constructor_name => 'new',
223 $self->{is_immutable}++;
225 if ($args{inline_constructor}) {
226 my $c = $self->constructor_class;
227 Mouse::Util::load_class($c);
228 $self->add_method($args{constructor_name} =>
229 $c->_generate_constructor($self, \%args));
232 if ($args{inline_destructor}) {
233 my $c = $self->destructor_class;
234 Mouse::Util::load_class($c);
235 $self->add_method(DESTROY =>
236 $c->_generate_destructor($self, \%args));
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.
244 sub make_mutable { not_supported }
246 sub is_immutable { $_[0]->{is_immutable} }
247 sub is_mutable { !$_[0]->{is_immutable} }
249 sub _install_modifier_pp{
250 my( $self, $type, $name, $code ) = @_;
251 my $into = $self->name;
253 my $original = $into->can($name)
254 or $self->throw_error("The method '$name' is not found in the inheritance hierarchy for class $into");
256 my $modifier_table = $self->{modifiers}{$name};
258 if(!$modifier_table){
259 my(@before, @after, @around, $cache, $modified);
264 for my $c (@before) { $c->(@_) }
266 if(wantarray){ # list context
267 my @rval = $cache->(@_);
269 for my $c(@after){ $c->(@_) }
272 elsif(defined wantarray){ # scalar context
273 my $rval = $cache->(@_);
275 for my $c(@after){ $c->(@_) }
281 for my $c(@after){ $c->(@_) }
286 $self->{modifiers}{$name} = $modifier_table = {
287 original => $original,
293 cache => \$cache, # cache for around modifiers
296 $self->add_method($name => $modified);
299 if($type eq 'before'){
300 unshift @{$modifier_table->{before}}, $code;
302 elsif($type eq 'after'){
303 push @{$modifier_table->{after}}, $code;
306 push @{$modifier_table->{around}}, $code;
308 my $next = ${ $modifier_table->{cache} };
309 ${ $modifier_table->{cache} } = sub{ $code->($next, @_) };
315 sub _install_modifier {
316 my ( $self, $type, $name, $code ) = @_;
318 # load Class::Method::Modifiers first
319 my $no_cmm_fast = do{
321 eval q{ require Class::Method::Modifiers::Fast };
327 $impl = \&_install_modifier_pp;
330 my $install_modifier = Class::Method::Modifiers::Fast->can('_install_modifier');
332 my ( $self, $type, $name, $code ) = @_;
333 my $into = $self->name;
334 $install_modifier->($into, $type, $name, $code);
336 $self->add_method($name => do{
338 \&{ $into . '::' . $name };
344 # replace this method itself :)
346 no warnings 'redefine';
347 *_install_modifier = $impl;
350 $self->$impl( $type, $name, $code );
353 sub add_before_method_modifier {
354 my ( $self, $name, $code ) = @_;
355 $self->_install_modifier( 'before', $name, $code );
358 sub add_around_method_modifier {
359 my ( $self, $name, $code ) = @_;
360 $self->_install_modifier( 'around', $name, $code );
363 sub add_after_method_modifier {
364 my ( $self, $name, $code ) = @_;
365 $self->_install_modifier( 'after', $name, $code );
368 sub add_override_method_modifier {
369 my ($self, $name, $code) = @_;
371 if($self->has_method($name)){
372 $self->throw_error("Cannot add an override method if a local method is already present");
375 my $package = $self->name;
377 my $super_body = $package->can($name)
378 or $self->throw_error("You cannot override '$name' because it has no super method");
380 $self->add_method($name => sub {
381 local $Mouse::SUPER_PACKAGE = $package;
382 local $Mouse::SUPER_BODY = $super_body;
383 local @Mouse::SUPER_ARGS = @_;
390 sub 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");
396 my $super = $self->find_method_by_name($name)
397 or $self->throw_error("You cannot augment '$name' because it has no super method");
399 my $super_package = $super->package_name;
400 my $super_body = $super->body;
402 $self->add_method($name => sub{
403 local $Mouse::INNER_BODY{$super_package} = $code;
404 local $Mouse::INNER_ARGS{$super_package} = [@_];
411 my ($self, $role_name) = @_;
414 || $self->throw_error("You must supply a role name to look for");
416 for my $class ($self->linearized_isa) {
417 my $meta = Mouse::Util::get_metaclass_by_name($class)
420 for my $role (@{ $meta->roles }) {
422 return 1 if $role->does_role($role_name);
434 Mouse::Meta::Class - The Mouse class metaclass
438 This document describes Mouse version 0.40_07
442 =head2 C<< initialize(ClassName) -> Mouse::Meta::Class >>
444 Finds or creates a C<Mouse::Meta::Class> instance for the given ClassName. Only
445 one instance should exist for a given class.
447 =head2 C<< name -> ClassName >>
449 Returns the name of the owner class.
451 =head2 C<< superclasses -> ClassNames >> C<< superclass(ClassNames) >>
453 Gets (or sets) the list of superclasses of the owner class.
455 =head2 C<< add_method(name => CodeRef) >>
457 Adds a method to the owner class.
459 =head2 C<< has_method(name) -> Bool >>
461 Returns whether we have a method with the given name.
463 =head2 C<< get_method(name) -> Mouse::Meta::Method | undef >>
465 Returns a L<Mouse::Meta::Method> with the given name.
467 Note that you can also use C<< $metaclass->name->can($name) >> for a method body.
469 =head2 C<< get_method_list -> Names >>
471 Returns a list of method names which are defined in the local class.
472 If you want a list of all applicable methods for a class, use the
473 C<get_all_methods> method.
475 =head2 C<< get_all_methods -> (Mouse::Meta::Method) >>
477 Return the list of all L<Mouse::Meta::Method> instances associated with
478 the class and its superclasses.
480 =head2 C<< add_attribute(name => spec | Mouse::Meta::Attribute) >>
482 Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
485 =head2 C<< has_attribute(Name) -> Bool >>
487 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
489 =head2 C<< get_attribute Name -> Mouse::Meta::Attribute | undef >>
491 Returns the L<Mouse::Meta::Attribute> with the given name.
493 =head2 C<< get_attribute_list -> Names >>
495 Returns a list of attribute names which are defined in the local
496 class. If you want a list of all applicable attributes for a class,
497 use the C<get_all_attributes> method.
499 =head2 C<< get_all_attributes -> (Mouse::Meta::Attribute) >>
501 Returns the list of all L<Mouse::Meta::Attribute> instances associated with
502 this class and its superclasses.
504 =head2 C<< linearized_isa -> [ClassNames] >>
506 Returns the list of classes in method dispatch order, with duplicates removed.
508 =head2 C<< new_object(Parameters) -> Instance >>
510 Creates a new instance.
512 =head2 C<< clone_object(Instance, Parameters) -> Instance >>
514 Clones the given instance which must be an instance governed by this
517 =head2 C<< throw_error(Message, Parameters) >>
519 Throws an error with the given message.
521 =head3 OTHER PUBLIC METHODS
523 =head3 add_after_method_modifier
525 =head3 add_around_method_modifier
527 =head3 add_augment_method_modifier
529 =head3 add_before_method_modifier
531 =head3 add_override_method_modifier
533 =head3 attribute_metaclass
535 =head3 calculate_all_roles
537 =head3 compute_all_applicable_attributes
539 =head3 constructor_class
541 =head3 create_anon_class
543 =head3 destructor_class
547 =head3 find_attribute_by_name
549 =head3 find_method_by_name
551 =head3 get_all_method_names
559 =head3 make_immutable
563 =head3 method_metaclass
569 L<Mouse::Meta::Module>
571 L<Moose::Meta::Class>