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::Method::Destructor;
7 use Mouse::Meta::Module;
8 our @ISA = qw(Mouse::Meta::Module);
10 sub method_metaclass() { 'Mouse::Meta::Method' }
11 sub attribute_metaclass() { 'Mouse::Meta::Attribute' }
13 sub constructor_class();
14 sub destructor_class();
17 my($class, %args) = @_;
19 $args{attributes} = {};
23 $args{superclasses} = do {
25 \@{ $args{package} . '::ISA' };
28 my $self = bless \%args, ref($class) || $class;
29 if(ref($self) ne __PACKAGE__){
30 $self->meta->_initialize_object($self, \%args);
35 sub create_anon_class{
37 return $self->create(undef, @_);
44 sub calculate_all_roles {
47 return grep { !$seen{ $_->name }++ }
48 map { $_->calculate_all_roles } @{ $self->roles };
55 foreach my $super(@_){
56 Mouse::Util::load_class($super);
57 my $meta = Mouse::Util::get_metaclass_by_name($super);
58 if(Mouse::Util::is_a_metarole($meta)){
59 $self->throw_error("You cannot inherit from a Mouse Role ($super)");
62 @{ $self->{superclasses} } = @_;
65 return @{ $self->{superclasses} };
68 sub find_method_by_name{
69 my($self, $method_name) = @_;
71 or $self->throw_error('You must define a method name to find');
73 foreach my $class( $self->linearized_isa ){
74 my $method = $self->initialize($class)->get_method($method_name);
75 return $method if defined $method;
82 return map{ $self->find_method_by_name($_) } $self->get_all_method_names;
85 sub get_all_method_names {
88 return grep { $uniq{$_}++ == 0 }
89 map { Mouse::Meta::Class->initialize($_)->get_method_list() }
90 $self->linearized_isa;
93 sub find_attribute_by_name{
94 my($self, $name) = @_;
96 foreach my $class($self->linearized_isa){
97 my $meta = Mouse::Util::get_metaclass_by_name($class) or next;
98 $attr = $meta->get_attribute($name) and last;
111 $attr->isa('Mouse::Meta::Attribute')
112 || $self->throw_error("Your attribute must be an instance of Mouse::Meta::Attribute (or a subclass)");
120 my %args = (@_ == 1) ? %{$_[0]} : @_;
123 or $self->throw_error('You must provide a name for the attribute');
125 if ($name =~ s/^\+//) { # inherited attributes
126 my $inherited_attr = $self->find_attribute_by_name($name)
127 or $self->throw_error("Could not find an attribute by the name of '$name' to inherit from in ".$self->name);
129 $attr = $inherited_attr->clone_and_inherit_options(%args);
132 my($attribute_class, @traits) = $self->attribute_metaclass->interpolate_class(\%args);
133 $args{traits} = \@traits if @traits;
135 $attr = $attribute_class->new($name, %args);
139 weaken( $attr->{associated_class} = $self );
141 $self->{attributes}{$attr->name} = $attr;
142 $attr->install_accessors();
144 if(_MOUSE_VERBOSE && !$attr->{associated_methods} && ($attr->{is} || '') ne 'bare'){
145 Carp::cluck(qq{Attribute (}.$attr->name.qq{) of class }.$self->name.qq{ has no associated methods (did you mean to provide an "is" argument?)});
150 sub compute_all_applicable_attributes {
151 Carp::cluck('compute_all_applicable_attributes() has been deprecated')
153 return shift->get_all_attributes(@_)
160 my %args = (@_ == 1 ? %{$_[0]} : @_);
162 my $object = bless {}, $self->name;
164 $self->_initialize_object($object, \%args);
171 my %params = (@_ == 1) ? %{$_[0]} : @_;
173 (blessed($object) && $object->isa($class->name))
174 || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($object)");
176 my $cloned = bless { %$object }, ref $object;
177 $class->_initialize_object($cloned, \%params);
183 my ($class, $instance, %params) = @_;
185 Carp::cluck('clone_instance has been deprecated. Use clone_object instead')
187 return $class->clone_object($instance, %params);
193 inline_constructor => 1,
194 inline_destructor => 1,
195 constructor_name => 'new',
199 $self->{is_immutable}++;
201 if ($args{inline_constructor}) {
202 my $c = $self->constructor_class;
203 Mouse::Util::load_class($c);
204 $self->add_method($args{constructor_name} =>
205 $c->_generate_constructor($self, \%args));
208 if ($args{inline_destructor}) {
209 my $c = $self->destructor_class;
210 Mouse::Util::load_class($c);
211 $self->add_method(DESTROY =>
212 $c->_generate_destructor($self, \%args));
215 # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value
216 # at the end of a source file.
220 sub make_mutable { not_supported }
222 sub is_immutable { $_[0]->{is_immutable} }
223 sub is_mutable { !$_[0]->{is_immutable} }
225 sub _install_modifier_pp{
226 my( $self, $type, $name, $code ) = @_;
227 my $into = $self->name;
229 my $original = $into->can($name)
230 or $self->throw_error("The method '$name' is not found in the inheritance hierarchy for class $into");
232 my $modifier_table = $self->{modifiers}{$name};
234 if(!$modifier_table){
235 my(@before, @after, @around, $cache, $modified);
240 for my $c (@before) { $c->(@_) }
242 if(wantarray){ # list context
243 my @rval = $cache->(@_);
245 for my $c(@after){ $c->(@_) }
248 elsif(defined wantarray){ # scalar context
249 my $rval = $cache->(@_);
251 for my $c(@after){ $c->(@_) }
257 for my $c(@after){ $c->(@_) }
262 $self->{modifiers}{$name} = $modifier_table = {
263 original => $original,
269 cache => \$cache, # cache for around modifiers
272 $self->add_method($name => $modified);
275 if($type eq 'before'){
276 unshift @{$modifier_table->{before}}, $code;
278 elsif($type eq 'after'){
279 push @{$modifier_table->{after}}, $code;
282 push @{$modifier_table->{around}}, $code;
284 my $next = ${ $modifier_table->{cache} };
285 ${ $modifier_table->{cache} } = sub{ $code->($next, @_) };
291 sub _install_modifier {
292 my ( $self, $type, $name, $code ) = @_;
294 # load Class::Method::Modifiers first
295 my $no_cmm_fast = do{
297 eval q{ require Class::Method::Modifiers::Fast };
303 $impl = \&_install_modifier_pp;
306 my $install_modifier = Class::Method::Modifiers::Fast->can('_install_modifier');
308 my ( $self, $type, $name, $code ) = @_;
309 my $into = $self->name;
310 $install_modifier->($into, $type, $name, $code);
312 $self->add_method($name => do{
314 \&{ $into . '::' . $name };
320 # replace this method itself :)
322 no warnings 'redefine';
323 *_install_modifier = $impl;
326 $self->$impl( $type, $name, $code );
329 sub add_before_method_modifier {
330 my ( $self, $name, $code ) = @_;
331 $self->_install_modifier( 'before', $name, $code );
334 sub add_around_method_modifier {
335 my ( $self, $name, $code ) = @_;
336 $self->_install_modifier( 'around', $name, $code );
339 sub add_after_method_modifier {
340 my ( $self, $name, $code ) = @_;
341 $self->_install_modifier( 'after', $name, $code );
344 sub add_override_method_modifier {
345 my ($self, $name, $code) = @_;
347 if($self->has_method($name)){
348 $self->throw_error("Cannot add an override method if a local method is already present");
351 my $package = $self->name;
353 my $super_body = $package->can($name)
354 or $self->throw_error("You cannot override '$name' because it has no super method");
356 $self->add_method($name => sub {
357 local $Mouse::SUPER_PACKAGE = $package;
358 local $Mouse::SUPER_BODY = $super_body;
359 local @Mouse::SUPER_ARGS = @_;
366 sub add_augment_method_modifier {
367 my ($self, $name, $code) = @_;
368 if($self->has_method($name)){
369 $self->throw_error("Cannot add an augment method if a local method is already present");
372 my $super = $self->find_method_by_name($name)
373 or $self->throw_error("You cannot augment '$name' because it has no super method");
375 my $super_package = $super->package_name;
376 my $super_body = $super->body;
378 $self->add_method($name => sub{
379 local $Mouse::INNER_BODY{$super_package} = $code;
380 local $Mouse::INNER_ARGS{$super_package} = [@_];
387 my ($self, $role_name) = @_;
390 || $self->throw_error("You must supply a role name to look for");
392 for my $class ($self->linearized_isa) {
393 my $meta = Mouse::Util::get_metaclass_by_name($class)
396 for my $role (@{ $meta->roles }) {
398 return 1 if $role->does_role($role_name);
410 Mouse::Meta::Class - The Mouse class metaclass
414 This document describes Mouse version 0.40_05
418 =head2 C<< initialize(ClassName) -> Mouse::Meta::Class >>
420 Finds or creates a C<Mouse::Meta::Class> instance for the given ClassName. Only
421 one instance should exist for a given class.
423 =head2 C<< name -> ClassName >>
425 Returns the name of the owner class.
427 =head2 C<< superclasses -> ClassNames >> C<< superclass(ClassNames) >>
429 Gets (or sets) the list of superclasses of the owner class.
431 =head2 C<< add_method(name => CodeRef) >>
433 Adds a method to the owner class.
435 =head2 C<< has_method(name) -> Bool >>
437 Returns whether we have a method with the given name.
439 =head2 C<< get_method(name) -> Mouse::Meta::Method | undef >>
441 Returns a L<Mouse::Meta::Method> with the given name.
443 Note that you can also use C<< $metaclass->name->can($name) >> for a method body.
445 =head2 C<< get_method_list -> Names >>
447 Returns a list of method names which are defined in the local class.
448 If you want a list of all applicable methods for a class, use the
449 C<get_all_methods> method.
451 =head2 C<< get_all_methods -> (Mouse::Meta::Method) >>
453 Return the list of all L<Mouse::Meta::Method> instances associated with
454 the class and its superclasses.
456 =head2 C<< add_attribute(name => spec | Mouse::Meta::Attribute) >>
458 Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
461 =head2 C<< has_attribute(Name) -> Bool >>
463 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
465 =head2 C<< get_attribute Name -> Mouse::Meta::Attribute | undef >>
467 Returns the L<Mouse::Meta::Attribute> with the given name.
469 =head2 C<< get_attribute_list -> Names >>
471 Returns a list of attribute names which are defined in the local
472 class. If you want a list of all applicable attributes for a class,
473 use the C<get_all_attributes> method.
475 =head2 C<< get_all_attributes -> (Mouse::Meta::Attribute) >>
477 Returns the list of all L<Mouse::Meta::Attribute> instances associated with
478 this class and its superclasses.
480 =head2 C<< linearized_isa -> [ClassNames] >>
482 Returns the list of classes in method dispatch order, with duplicates removed.
484 =head2 C<< new_object(Parameters) -> Instance >>
486 Creates a new instance.
488 =head2 C<< clone_object(Instance, Parameters) -> Instance >>
490 Clones the given instance which must be an instance governed by this
493 =head2 C<< throw_error(Message, Parameters) >>
495 Throws an error with the given message.
499 L<Moose::Meta::Class>