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 sub method_metaclass() { 'Mouse::Meta::Method' }
10 sub attribute_metaclass() { 'Mouse::Meta::Attribute' }
12 sub constructor_class();
13 sub destructor_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);
57 if(Mouse::Util::is_a_metarole($meta)){
58 $self->throw_error("You cannot inherit from a Mouse Role ($super)");
61 @{ $self->{superclasses} } = @_;
64 return @{ $self->{superclasses} };
67 sub find_method_by_name{
68 my($self, $method_name) = @_;
70 or $self->throw_error('You must define a method name to find');
72 foreach my $class( $self->linearized_isa ){
73 my $method = $self->initialize($class)->get_method($method_name);
74 return $method if defined $method;
81 return map{ $self->find_method_by_name($_) } $self->get_all_method_names;
84 sub get_all_method_names {
87 return grep { $uniq{$_}++ == 0 }
88 map { Mouse::Meta::Class->initialize($_)->get_method_list() }
89 $self->linearized_isa;
92 sub find_attribute_by_name{
93 my($self, $name) = @_;
95 foreach my $class($self->linearized_isa){
96 my $meta = Mouse::Util::get_metaclass_by_name($class) or next;
97 $attr = $meta->get_attribute($name) and last;
110 $attr->isa('Mouse::Meta::Attribute')
111 || $self->throw_error("Your attribute must be an instance of Mouse::Meta::Attribute (or a subclass)");
119 my %args = (@_ == 1) ? %{$_[0]} : @_;
122 or $self->throw_error('You must provide a name for the attribute');
124 if ($name =~ s/^\+//) { # inherited attributes
125 my $inherited_attr = $self->find_attribute_by_name($name)
126 or $self->throw_error("Could not find an attribute by the name of '$name' to inherit from in ".$self->name);
128 $attr = $inherited_attr->clone_and_inherit_options(%args);
131 my($attribute_class, @traits) = $self->attribute_metaclass->interpolate_class(\%args);
132 $args{traits} = \@traits if @traits;
134 $attr = $attribute_class->new($name, %args);
138 weaken( $attr->{associated_class} = $self );
140 $self->{attributes}{$attr->name} = $attr;
141 $attr->install_accessors();
143 if(_MOUSE_VERBOSE && !$attr->{associated_methods} && ($attr->{is} || '') ne 'bare'){
144 Carp::cluck(qq{Attribute (}.$attr->name.qq{) of class }.$self->name.qq{ has no associated methods (did you mean to provide an "is" argument?)});
149 sub compute_all_applicable_attributes {
150 Carp::cluck('compute_all_applicable_attributes() has been deprecated')
152 return shift->get_all_attributes(@_)
159 my %args = (@_ == 1 ? %{$_[0]} : @_);
161 my $object = bless {}, $self->name;
163 $self->_initialize_object($object, \%args);
170 my %params = (@_ == 1) ? %{$_[0]} : @_;
172 (blessed($object) && $object->isa($class->name))
173 || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($object)");
175 my $cloned = bless { %$object }, ref $object;
176 $class->_initialize_object($cloned, \%params);
182 my ($class, $instance, %params) = @_;
184 Carp::cluck('clone_instance has been deprecated. Use clone_object instead')
186 return $class->clone_object($instance, %params);
192 inline_constructor => 1,
193 inline_destructor => 1,
194 constructor_name => 'new',
198 $self->{is_immutable}++;
200 if ($args{inline_constructor}) {
201 my $c = $self->constructor_class;
202 Mouse::Util::load_class($c);
203 $self->add_method($args{constructor_name} =>
204 $c->_generate_constructor($self, \%args));
207 if ($args{inline_destructor}) {
208 my $c = $self->destructor_class;
209 Mouse::Util::load_class($c);
210 $self->add_method(DESTROY =>
211 $c->_generate_destructor($self, \%args));
214 # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value
215 # at the end of a source file.
219 sub make_mutable { not_supported }
221 sub is_immutable { $_[0]->{is_immutable} }
222 sub is_mutable { !$_[0]->{is_immutable} }
224 sub _install_modifier_pp{
225 my( $self, $type, $name, $code ) = @_;
226 my $into = $self->name;
228 my $original = $into->can($name)
229 or $self->throw_error("The method '$name' is not found in the inheritance hierarchy for class $into");
231 my $modifier_table = $self->{modifiers}{$name};
233 if(!$modifier_table){
234 my(@before, @after, @around, $cache, $modified);
239 for my $c (@before) { $c->(@_) }
241 if(wantarray){ # list context
242 my @rval = $cache->(@_);
244 for my $c(@after){ $c->(@_) }
247 elsif(defined wantarray){ # scalar context
248 my $rval = $cache->(@_);
250 for my $c(@after){ $c->(@_) }
256 for my $c(@after){ $c->(@_) }
261 $self->{modifiers}{$name} = $modifier_table = {
262 original => $original,
268 cache => \$cache, # cache for around modifiers
271 $self->add_method($name => $modified);
274 if($type eq 'before'){
275 unshift @{$modifier_table->{before}}, $code;
277 elsif($type eq 'after'){
278 push @{$modifier_table->{after}}, $code;
281 push @{$modifier_table->{around}}, $code;
283 my $next = ${ $modifier_table->{cache} };
284 ${ $modifier_table->{cache} } = sub{ $code->($next, @_) };
290 sub _install_modifier {
291 my ( $self, $type, $name, $code ) = @_;
293 # load Class::Method::Modifiers first
294 my $no_cmm_fast = do{
296 eval q{ require Class::Method::Modifiers::Fast };
302 $impl = \&_install_modifier_pp;
305 my $install_modifier = Class::Method::Modifiers::Fast->can('_install_modifier');
307 my ( $self, $type, $name, $code ) = @_;
308 my $into = $self->name;
309 $install_modifier->($into, $type, $name, $code);
311 $self->add_method($name => do{
313 \&{ $into . '::' . $name };
319 # replace this method itself :)
321 no warnings 'redefine';
322 *_install_modifier = $impl;
325 $self->$impl( $type, $name, $code );
328 sub add_before_method_modifier {
329 my ( $self, $name, $code ) = @_;
330 $self->_install_modifier( 'before', $name, $code );
333 sub add_around_method_modifier {
334 my ( $self, $name, $code ) = @_;
335 $self->_install_modifier( 'around', $name, $code );
338 sub add_after_method_modifier {
339 my ( $self, $name, $code ) = @_;
340 $self->_install_modifier( 'after', $name, $code );
343 sub add_override_method_modifier {
344 my ($self, $name, $code) = @_;
346 if($self->has_method($name)){
347 $self->throw_error("Cannot add an override method if a local method is already present");
350 my $package = $self->name;
352 my $super_body = $package->can($name)
353 or $self->throw_error("You cannot override '$name' because it has no super method");
355 $self->add_method($name => sub {
356 local $Mouse::SUPER_PACKAGE = $package;
357 local $Mouse::SUPER_BODY = $super_body;
358 local @Mouse::SUPER_ARGS = @_;
365 sub add_augment_method_modifier {
366 my ($self, $name, $code) = @_;
367 if($self->has_method($name)){
368 $self->throw_error("Cannot add an augment method if a local method is already present");
371 my $super = $self->find_method_by_name($name)
372 or $self->throw_error("You cannot augment '$name' because it has no super method");
374 my $super_package = $super->package_name;
375 my $super_body = $super->body;
377 $self->add_method($name => sub{
378 local $Mouse::INNER_BODY{$super_package} = $code;
379 local $Mouse::INNER_ARGS{$super_package} = [@_];
386 my ($self, $role_name) = @_;
389 || $self->throw_error("You must supply a role name to look for");
391 for my $class ($self->linearized_isa) {
392 my $meta = Mouse::Util::get_metaclass_by_name($class)
395 for my $role (@{ $meta->roles }) {
397 return 1 if $role->does_role($role_name);
409 Mouse::Meta::Class - The Mouse class metaclass
413 This document describes Mouse version 0.40_06
417 =head2 C<< initialize(ClassName) -> Mouse::Meta::Class >>
419 Finds or creates a C<Mouse::Meta::Class> instance for the given ClassName. Only
420 one instance should exist for a given class.
422 =head2 C<< name -> ClassName >>
424 Returns the name of the owner class.
426 =head2 C<< superclasses -> ClassNames >> C<< superclass(ClassNames) >>
428 Gets (or sets) the list of superclasses of the owner class.
430 =head2 C<< add_method(name => CodeRef) >>
432 Adds a method to the owner class.
434 =head2 C<< has_method(name) -> Bool >>
436 Returns whether we have a method with the given name.
438 =head2 C<< get_method(name) -> Mouse::Meta::Method | undef >>
440 Returns a L<Mouse::Meta::Method> with the given name.
442 Note that you can also use C<< $metaclass->name->can($name) >> for a method body.
444 =head2 C<< get_method_list -> Names >>
446 Returns a list of method names which are defined in the local class.
447 If you want a list of all applicable methods for a class, use the
448 C<get_all_methods> method.
450 =head2 C<< get_all_methods -> (Mouse::Meta::Method) >>
452 Return the list of all L<Mouse::Meta::Method> instances associated with
453 the class and its superclasses.
455 =head2 C<< add_attribute(name => spec | Mouse::Meta::Attribute) >>
457 Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
460 =head2 C<< has_attribute(Name) -> Bool >>
462 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
464 =head2 C<< get_attribute Name -> Mouse::Meta::Attribute | undef >>
466 Returns the L<Mouse::Meta::Attribute> with the given name.
468 =head2 C<< get_attribute_list -> Names >>
470 Returns a list of attribute names which are defined in the local
471 class. If you want a list of all applicable attributes for a class,
472 use the C<get_all_attributes> method.
474 =head2 C<< get_all_attributes -> (Mouse::Meta::Attribute) >>
476 Returns the list of all L<Mouse::Meta::Attribute> instances associated with
477 this class and its superclasses.
479 =head2 C<< linearized_isa -> [ClassNames] >>
481 Returns the list of classes in method dispatch order, with duplicates removed.
483 =head2 C<< new_object(Parameters) -> Instance >>
485 Creates a new instance.
487 =head2 C<< clone_object(Instance, Parameters) -> Instance >>
489 Clones the given instance which must be an instance governed by this
492 =head2 C<< throw_error(Message, Parameters) >>
494 Throws an error with the given message.
498 L<Moose::Meta::Class>