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(); # XS
14 sub destructor_class() { 'Mouse::Meta::Method::Destructor' }
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 $self->add_method($args{constructor_name} =>
203 $self->constructor_class->_generate_constructor($self, \%args));
206 if ($args{inline_destructor}) {
207 $self->add_method(DESTROY =>
208 $self->destructor_class->_generate_destructor($self, \%args));
211 # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value
212 # at the end of a source file.
216 sub make_mutable { not_supported }
218 sub is_immutable { $_[0]->{is_immutable} }
219 sub is_mutable { !$_[0]->{is_immutable} }
221 sub _install_modifier_pp{
222 my( $self, $type, $name, $code ) = @_;
223 my $into = $self->name;
225 my $original = $into->can($name)
226 or $self->throw_error("The method '$name' is not found in the inheritance hierarchy for class $into");
228 my $modifier_table = $self->{modifiers}{$name};
230 if(!$modifier_table){
231 my(@before, @after, @around, $cache, $modified);
236 for my $c (@before) { $c->(@_) }
238 if(wantarray){ # list context
239 my @rval = $cache->(@_);
241 for my $c(@after){ $c->(@_) }
244 elsif(defined wantarray){ # scalar context
245 my $rval = $cache->(@_);
247 for my $c(@after){ $c->(@_) }
253 for my $c(@after){ $c->(@_) }
258 $self->{modifiers}{$name} = $modifier_table = {
259 original => $original,
265 cache => \$cache, # cache for around modifiers
268 $self->add_method($name => $modified);
271 if($type eq 'before'){
272 unshift @{$modifier_table->{before}}, $code;
274 elsif($type eq 'after'){
275 push @{$modifier_table->{after}}, $code;
278 push @{$modifier_table->{around}}, $code;
280 my $next = ${ $modifier_table->{cache} };
281 ${ $modifier_table->{cache} } = sub{ $code->($next, @_) };
287 sub _install_modifier {
288 my ( $self, $type, $name, $code ) = @_;
290 # load Class::Method::Modifiers first
291 my $no_cmm_fast = do{
293 eval q{ require Class::Method::Modifiers::Fast };
299 $impl = \&_install_modifier_pp;
302 my $install_modifier = Class::Method::Modifiers::Fast->can('_install_modifier');
304 my ( $self, $type, $name, $code ) = @_;
305 my $into = $self->name;
306 $install_modifier->($into, $type, $name, $code);
308 $self->add_method($name => do{
310 \&{ $into . '::' . $name };
316 # replace this method itself :)
318 no warnings 'redefine';
319 *_install_modifier = $impl;
322 $self->$impl( $type, $name, $code );
325 sub add_before_method_modifier {
326 my ( $self, $name, $code ) = @_;
327 $self->_install_modifier( 'before', $name, $code );
330 sub add_around_method_modifier {
331 my ( $self, $name, $code ) = @_;
332 $self->_install_modifier( 'around', $name, $code );
335 sub add_after_method_modifier {
336 my ( $self, $name, $code ) = @_;
337 $self->_install_modifier( 'after', $name, $code );
340 sub add_override_method_modifier {
341 my ($self, $name, $code) = @_;
343 if($self->has_method($name)){
344 $self->throw_error("Cannot add an override method if a local method is already present");
347 my $package = $self->name;
349 my $super_body = $package->can($name)
350 or $self->throw_error("You cannot override '$name' because it has no super method");
352 $self->add_method($name => sub {
353 local $Mouse::SUPER_PACKAGE = $package;
354 local $Mouse::SUPER_BODY = $super_body;
355 local @Mouse::SUPER_ARGS = @_;
362 sub add_augment_method_modifier {
363 my ($self, $name, $code) = @_;
364 if($self->has_method($name)){
365 $self->throw_error("Cannot add an augment method if a local method is already present");
368 my $super = $self->find_method_by_name($name)
369 or $self->throw_error("You cannot augment '$name' because it has no super method");
371 my $super_package = $super->package_name;
372 my $super_body = $super->body;
374 $self->add_method($name => sub{
375 local $Mouse::INNER_BODY{$super_package} = $code;
376 local $Mouse::INNER_ARGS{$super_package} = [@_];
383 my ($self, $role_name) = @_;
386 || $self->throw_error("You must supply a role name to look for");
388 for my $class ($self->linearized_isa) {
389 my $meta = Mouse::Util::get_metaclass_by_name($class)
392 for my $role (@{ $meta->roles }) {
394 return 1 if $role->does_role($role_name);
406 Mouse::Meta::Class - The Mouse class metaclass
410 This document describes Mouse version 0.40_05
414 =head2 C<< initialize(ClassName) -> Mouse::Meta::Class >>
416 Finds or creates a C<Mouse::Meta::Class> instance for the given ClassName. Only
417 one instance should exist for a given class.
419 =head2 C<< name -> ClassName >>
421 Returns the name of the owner class.
423 =head2 C<< superclasses -> ClassNames >> C<< superclass(ClassNames) >>
425 Gets (or sets) the list of superclasses of the owner class.
427 =head2 C<< add_method(name => CodeRef) >>
429 Adds a method to the owner class.
431 =head2 C<< has_method(name) -> Bool >>
433 Returns whether we have a method with the given name.
435 =head2 C<< get_method(name) -> Mouse::Meta::Method | undef >>
437 Returns a L<Mouse::Meta::Method> with the given name.
439 Note that you can also use C<< $metaclass->name->can($name) >> for a method body.
441 =head2 C<< get_method_list -> Names >>
443 Returns a list of method names which are defined in the local class.
444 If you want a list of all applicable methods for a class, use the
445 C<get_all_methods> method.
447 =head2 C<< get_all_methods -> (Mouse::Meta::Method) >>
449 Return the list of all L<Mouse::Meta::Method> instances associated with
450 the class and its superclasses.
452 =head2 C<< add_attribute(name => spec | Mouse::Meta::Attribute) >>
454 Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
457 =head2 C<< has_attribute(Name) -> Bool >>
459 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
461 =head2 C<< get_attribute Name -> Mouse::Meta::Attribute | undef >>
463 Returns the L<Mouse::Meta::Attribute> with the given name.
465 =head2 C<< get_attribute_list -> Names >>
467 Returns a list of attribute names which are defined in the local
468 class. If you want a list of all applicable attributes for a class,
469 use the C<get_all_attributes> method.
471 =head2 C<< get_all_attributes -> (Mouse::Meta::Attribute) >>
473 Returns the list of all L<Mouse::Meta::Attribute> instances associated with
474 this class and its superclasses.
476 =head2 C<< linearized_isa -> [ClassNames] >>
478 Returns the list of classes in method dispatch order, with duplicates removed.
480 =head2 C<< new_object(Parameters) -> Instance >>
482 Creates a new instance.
484 =head2 C<< clone_object(Instance, Parameters) -> Instance >>
486 Clones the given instance which must be an instance governed by this
489 =head2 C<< throw_error(Message, Parameters) >>
491 Throws an error with the given message.
495 L<Moose::Meta::Class>