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);
9 our @CARP_NOT = qw(Mouse); # trust Mouse
11 sub attribute_metaclass;
14 sub constructor_class;
19 my($class, %args) = @_;
21 $args{attributes} = {};
25 $args{superclasses} = do {
27 \@{ $args{package} . '::ISA' };
30 my $self = bless \%args, ref($class) || $class;
31 if(ref($self) ne __PACKAGE__){
32 $self->meta->_initialize_object($self, \%args);
37 sub create_anon_class{
39 return $self->create(undef, @_);
46 sub calculate_all_roles {
49 return grep { !$seen{ $_->name }++ }
50 map { $_->calculate_all_roles } @{ $self->roles };
57 foreach my $super(@_){
58 Mouse::Util::load_class($super);
59 my $meta = Mouse::Util::get_metaclass_by_name($super);
61 next if not defined $meta;
63 if(Mouse::Util::is_a_metarole($meta)){
64 $self->throw_error("You cannot inherit from a Mouse Role ($super)");
67 next if $self->isa(ref $meta); # _superclass_meta_is_compatible
69 $self->_reconcile_with_superclass_meta($meta);
71 @{ $self->{superclasses} } = @_;
74 return @{ $self->{superclasses} };
76 my @MetaClassTypes = (
77 'attribute', # Mouse::Meta::Attribute
78 'method', # Mouse::Meta::Method
79 'constructor', # Mouse::Meta::Method::Constructor
80 'destructor', # Mouse::Meta::Method::Destructor
83 sub _reconcile_with_superclass_meta {
84 my($self, $other) = @_;
86 # find incompatible traits
88 foreach my $metaclass_type(@MetaClassTypes){
89 my $accessor = $self->can($metaclass_type . '_metaclass')
90 || $self->can($metaclass_type . '_class');
92 my $other_c = $other->$accessor();
93 my $self_c = $self->$accessor();
95 if(!$self_c->isa($other_c)){
96 $metaroles{$metaclass_type}
97 = [ $self_c->meta->_collect_roles($other_c->meta) ];
101 $metaroles{class} = [$self->meta->_collect_roles($other->meta)];
103 #use Data::Dumper; print Data::Dumper->new([\%metaroles], ['*metaroles'])->Indent(1)->Dump;
105 require Mouse::Util::MetaRole;
106 $_[0] = Mouse::Util::MetaRole::apply_metaroles(
108 class_metaroles => \%metaroles,
114 my ($self, $other) = @_;
116 # find common ancestor
117 my @self_lin_isa = $self->linearized_isa;
118 my @other_lin_isa = $other->linearized_isa;
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;
124 my $common_ancestor = $self_lin_isa[0] eq $other_lin_isa[0] && $self_lin_isa[0];
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);
132 return sort grep { !$seen{$_}++ } ## no critic
133 (map{ $_->name } map{ $_->meta->calculate_all_roles } @self_anon_supers),
134 (map{ $_->name } map{ $_->meta->calculate_all_roles } @other_anon_supers),
139 sub 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');
144 foreach my $class( $self->linearized_isa ){
145 my $method = $self->initialize($class)->get_method($method_name);
146 return $method if defined $method;
151 sub get_all_methods {
153 return map{ $self->find_method_by_name($_) } $self->get_all_method_names;
156 sub get_all_method_names {
159 return grep { $uniq{$_}++ == 0 }
160 map { Mouse::Meta::Class->initialize($_)->get_method_list() }
161 $self->linearized_isa;
164 sub find_attribute_by_name{
165 my($self, $name) = @_;
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;
182 $attr->isa('Mouse::Meta::Attribute')
183 || $self->throw_error("Your attribute must be an instance of Mouse::Meta::Attribute (or a subclass)");
191 my %args = (@_ == 1) ? %{$_[0]} : @_;
194 or $self->throw_error('You must provide a name for the attribute');
196 if ($name =~ s/^\+//) { # inherited attributes
197 my $inherited_attr = $self->find_attribute_by_name($name)
198 or $self->throw_error("Could not find an attribute by the name of '$name' to inherit from in ".$self->name);
200 $attr = $inherited_attr->clone_and_inherit_options(%args);
203 my($attribute_class, @traits) = $self->attribute_metaclass->interpolate_class(\%args);
204 $args{traits} = \@traits if @traits;
206 $attr = $attribute_class->new($name, %args);
210 weaken( $attr->{associated_class} = $self );
212 $self->{attributes}{$attr->name} = $attr;
213 $attr->install_accessors();
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?)});
222 sub compute_all_applicable_attributes { # DEPRECATED
223 Carp::cluck('compute_all_applicable_attributes() has been deprecated. Use get_all_attributes() instead');
225 return shift->get_all_attributes(@_)
235 my $args = $object->Mouse::Object::BUILDARGS(@_);
237 (blessed($object) && $object->isa($class->name))
238 || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($object)");
240 my $cloned = bless { %$object }, ref $object;
241 $class->_initialize_object($cloned, $args);
246 sub clone_instance { # DEPRECATED
247 my ($class, $instance, %params) = @_;
249 Carp::cluck('clone_instance() has been deprecated. Use clone_object() instead');
251 return $class->clone_object($instance, %params);
255 sub immutable_options {
256 my ( $self, @args ) = @_;
259 inline_constructor => 1,
260 inline_destructor => 1,
261 constructor_name => 'new',
269 my %args = $self->immutable_options(@_);
271 $self->{is_immutable}++;
273 $self->{strict_constructor} = $args{strict_constructor};
275 if ($args{inline_constructor}) {
276 $self->add_method($args{constructor_name} =>
277 Mouse::Util::load_class($self->constructor_class)
278 ->_generate_constructor($self, \%args));
281 if ($args{inline_destructor}) {
282 $self->add_method(DESTROY =>
283 Mouse::Util::load_class($self->destructor_class)
284 ->_generate_destructor($self, \%args));
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.
294 $self->{is_immutable} = 0;
299 sub is_mutable { !$_[0]->is_immutable }
301 sub _install_modifier_pp{
302 my( $self, $type, $name, $code ) = @_;
303 my $into = $self->name;
305 my $original = $into->can($name)
306 or $self->throw_error("The method '$name' is not found in the inheritance hierarchy for class $into");
308 my $modifier_table = $self->{modifiers}{$name};
310 if(!$modifier_table){
311 my(@before, @after, @around, $cache, $modified);
316 for my $c (@before) { $c->(@_) }
318 if(wantarray){ # list context
319 my @rval = $cache->(@_);
321 for my $c(@after){ $c->(@_) }
324 elsif(defined wantarray){ # scalar context
325 my $rval = $cache->(@_);
327 for my $c(@after){ $c->(@_) }
333 for my $c(@after){ $c->(@_) }
338 $self->{modifiers}{$name} = $modifier_table = {
339 original => $original,
345 cache => \$cache, # cache for around modifiers
348 $self->add_method($name => $modified);
351 if($type eq 'before'){
352 unshift @{$modifier_table->{before}}, $code;
354 elsif($type eq 'after'){
355 push @{$modifier_table->{after}}, $code;
358 push @{$modifier_table->{around}}, $code;
360 my $next = ${ $modifier_table->{cache} };
361 ${ $modifier_table->{cache} } = sub{ $code->($next, @_) };
367 sub _install_modifier {
368 my ( $self, $type, $name, $code ) = @_;
370 # load Class::Method::Modifiers first
371 my $no_cmm_fast = do{
373 eval q{ use Class::Method::Modifiers::Fast 0.041 () };
379 $impl = \&_install_modifier_pp;
382 my $install_modifier = Class::Method::Modifiers::Fast->can('install_modifier');
384 my ( $self, $type, $name, $code ) = @_;
385 my $into = $self->name;
386 $install_modifier->($into, $type, $name, $code);
388 $self->add_method($name => Mouse::Util::get_code_ref($into, $name));
393 # replace this method itself :)
395 no warnings 'redefine';
396 *_install_modifier = $impl;
399 $self->$impl( $type, $name, $code );
402 sub add_before_method_modifier {
403 my ( $self, $name, $code ) = @_;
404 $self->_install_modifier( 'before', $name, $code );
407 sub add_around_method_modifier {
408 my ( $self, $name, $code ) = @_;
409 $self->_install_modifier( 'around', $name, $code );
412 sub add_after_method_modifier {
413 my ( $self, $name, $code ) = @_;
414 $self->_install_modifier( 'after', $name, $code );
417 sub add_override_method_modifier {
418 my ($self, $name, $code) = @_;
420 if($self->has_method($name)){
421 $self->throw_error("Cannot add an override method if a local method is already present");
424 my $package = $self->name;
426 my $super_body = $package->can($name)
427 or $self->throw_error("You cannot override '$name' because it has no super method");
429 $self->add_method($name => sub {
430 local $Mouse::SUPER_PACKAGE = $package;
431 local $Mouse::SUPER_BODY = $super_body;
432 local @Mouse::SUPER_ARGS = @_;
439 sub 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");
445 my $super = $self->find_method_by_name($name)
446 or $self->throw_error("You cannot augment '$name' because it has no super method");
448 my $super_package = $super->package_name;
449 my $super_body = $super->body;
451 $self->add_method($name => sub{
452 local $Mouse::INNER_BODY{$super_package} = $code;
453 local $Mouse::INNER_ARGS{$super_package} = [@_];
460 my ($self, $role_name) = @_;
463 || $self->throw_error("You must supply a role name to look for");
465 $role_name = $role_name->name if ref $role_name;
467 for my $class ($self->linearized_isa) {
468 my $meta = Mouse::Util::get_metaclass_by_name($class)
471 for my $role (@{ $meta->roles }) {
473 return 1 if $role->does_role($role_name);
485 Mouse::Meta::Class - The Mouse class metaclass
489 This document describes Mouse version 0.51
493 =head2 C<< initialize(ClassName) -> Mouse::Meta::Class >>
495 Finds or creates a C<Mouse::Meta::Class> instance for the given ClassName. Only
496 one instance should exist for a given class.
498 =head2 C<< name -> ClassName >>
500 Returns the name of the owner class.
502 =head2 C<< superclasses -> ClassNames >> C<< superclass(ClassNames) >>
504 Gets (or sets) the list of superclasses of the owner class.
506 =head2 C<< add_method(name => CodeRef) >>
508 Adds a method to the owner class.
510 =head2 C<< has_method(name) -> Bool >>
512 Returns whether we have a method with the given name.
514 =head2 C<< get_method(name) -> Mouse::Meta::Method | undef >>
516 Returns a L<Mouse::Meta::Method> with the given name.
518 Note that you can also use C<< $metaclass->name->can($name) >> for a method body.
520 =head2 C<< get_method_list -> Names >>
522 Returns a list of method names which are defined in the local class.
523 If you want a list of all applicable methods for a class, use the
524 C<get_all_methods> method.
526 =head2 C<< get_all_methods -> (Mouse::Meta::Method) >>
528 Return the list of all L<Mouse::Meta::Method> instances associated with
529 the class and its superclasses.
531 =head2 C<< add_attribute(name => spec | Mouse::Meta::Attribute) >>
533 Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
536 =head2 C<< has_attribute(Name) -> Bool >>
538 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
540 =head2 C<< get_attribute Name -> Mouse::Meta::Attribute | undef >>
542 Returns the L<Mouse::Meta::Attribute> with the given name.
544 =head2 C<< get_attribute_list -> Names >>
546 Returns a list of attribute names which are defined in the local
547 class. If you want a list of all applicable attributes for a class,
548 use the C<get_all_attributes> method.
550 =head2 C<< get_all_attributes -> (Mouse::Meta::Attribute) >>
552 Returns the list of all L<Mouse::Meta::Attribute> instances associated with
553 this class and its superclasses.
555 =head2 C<< linearized_isa -> [ClassNames] >>
557 Returns the list of classes in method dispatch order, with duplicates removed.
559 =head2 C<< new_object(Parameters) -> Instance >>
561 Creates a new instance.
563 =head2 C<< clone_object(Instance, Parameters) -> Instance >>
565 Clones the given instance which must be an instance governed by this
568 =head2 C<< throw_error(Message, Parameters) >>
570 Throws an error with the given message.
574 L<Mouse::Meta::Module>
576 L<Moose::Meta::Class>