1 package Mouse::Meta::Class;
5 use Scalar::Util qw/blessed weaken/;
7 use Mouse::Util qw/:meta get_linear_isa not_supported/;
9 use Mouse::Meta::Method::Constructor;
10 use Mouse::Meta::Method::Destructor;
11 use Mouse::Meta::Module;
12 our @ISA = qw(Mouse::Meta::Module);
14 sub method_metaclass(){ 'Mouse::Meta::Method' } # required for get_method()
17 my($class, %args) = @_;
19 $args{attributes} ||= {};
20 $args{methods} ||= {};
23 $args{superclasses} = do {
25 \@{ $args{package} . '::ISA' };
28 my $self = bless \%args, ref($class) || $class;
29 if($class ne __PACKAGE__){
30 $self->meta->_initialize_object($self, \%args);
35 sub create_anon_class{
37 return $self->create(undef, @_);
41 return exists $_[0]->{anon_serial_id};
44 sub roles { $_[0]->{roles} }
50 Mouse::load_class($_) for @_;
51 @{ $self->{superclasses} } = @_;
54 return @{ $self->{superclasses} };
57 sub find_method_by_name{
58 my($self, $method_name) = @_;
60 or $self->throw_error('You must define a method name to find');
61 foreach my $class( $self->linearized_isa ){
62 my $method = $self->initialize($class)->get_method($method_name);
63 return $method if defined $method;
70 return map{ $self->find_method_by_name($_) } $self->get_all_method_names;
73 sub get_all_method_names {
76 return grep { $uniq{$_}++ == 0 }
77 map { Mouse::Meta::Class->initialize($_)->get_method_list() }
78 $self->linearized_isa;
89 $attr->isa('Mouse::Meta::Attribute')
90 || $self->throw_error("Your attribute must be an instance of Mouse::Meta::Attribute (or a subclass)");
98 my %args = (@_ == 1) ? %{$_[0]} : @_;
101 or $self->throw_error('You must provide a name for the attribute');
103 if ($name =~ s/^\+//) { # inherited attributes
106 foreach my $class($self->linearized_isa){
107 my $meta = Mouse::Util::get_metaclass_by_name($class) or next;
108 $inherited_attr = $meta->get_attribute($name) and last;
111 defined($inherited_attr)
112 or $self->throw_error("Could not find an attribute by the name of '$name' to inherit from in ".$self->name);
114 $attr = $inherited_attr->clone_and_inherit_options($name, \%args);
117 my($attribute_class, @traits) = Mouse::Meta::Attribute->interpolate_class($name, \%args);
118 $args{traits} = \@traits if @traits;
120 $attr = $attribute_class->new($name, %args);
124 weaken( $attr->{associated_class} = $self );
126 $self->{attributes}{$attr->name} = $attr;
127 $attr->install_accessors();
129 if(_MOUSE_VERBOSE && !$attr->{associated_methods} && ($attr->{is} || '') ne 'bare'){
130 Carp::cluck(qq{Attribute (}.$attr->name.qq{) of class }.$self->name.qq{ has no associated methods (did you mean to provide an "is" argument?)});
135 sub compute_all_applicable_attributes {
136 Carp::cluck('compute_all_applicable_attributes() has been deprecated');
137 return shift->get_all_attributes(@_)
140 sub get_all_attributes {
144 for my $class ($self->linearized_isa) {
145 my $meta = Mouse::Util::get_metaclass_by_name($class)
148 for my $name ($meta->get_attribute_list) {
149 next if $seen{$name}++;
150 push @attr, $meta->get_attribute($name);
157 sub linearized_isa { @{ get_linear_isa($_[0]->name) } }
161 my %args = (@_ == 1 ? %{$_[0]} : @_);
163 my $object = bless {}, $self->name;
165 $self->_initialize_object($object, \%args);
169 sub _initialize_object{
170 my($self, $object, $args) = @_;
174 foreach my $attribute ($self->get_all_attributes) {
175 my $from = $attribute->init_arg;
176 my $key = $attribute->name;
178 if (defined($from) && exists($args->{$from})) {
179 $object->{$key} = $attribute->_coerce_and_verify($args->{$from});
181 weaken($object->{$key})
182 if ref($object->{$key}) && $attribute->is_weak_ref;
184 if ($attribute->has_trigger) {
185 push @triggers_queue, [ $attribute->trigger, $object->{$key} ];
189 if ($attribute->has_default || $attribute->has_builder) {
190 unless ($attribute->is_lazy) {
191 my $default = $attribute->default;
192 my $builder = $attribute->builder;
193 my $value = $builder ? $object->$builder()
194 : ref($default) eq 'CODE' ? $object->$default()
197 # XXX: we cannot use $attribute->set_value() because it invokes triggers.
198 $object->{$key} = $attribute->_coerce_and_verify($value, $object);;
200 weaken($object->{$key})
201 if ref($object->{$key}) && $attribute->is_weak_ref;
205 if ($attribute->is_required) {
206 $self->throw_error("Attribute (".$attribute->name.") is required");
212 foreach my $trigger_and_value(@triggers_queue){
213 my($trigger, $value) = @{$trigger_and_value};
214 $trigger->($object, $value);
217 if($self->is_anon_class){
218 $object->{__METACLASS__} = $self;
227 my %params = (@_ == 1) ? %{$_[0]} : @_;
229 (blessed($object) && $object->isa($class->name))
230 || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($object)");
232 my $cloned = bless { %$object }, ref $object;
233 $class->_initialize_object($cloned, \%params);
239 my ($class, $instance, %params) = @_;
241 Carp::cluck('clone_instance has been deprecated. Use clone_object instead')
243 return $class->clone_object($instance, %params);
249 inline_constructor => 1,
250 inline_destructor => 1,
251 constructor_name => 'new',
255 $self->{is_immutable}++;
257 if ($args{inline_constructor}) {
258 # generate and install
259 Mouse::Meta::Method::Constructor->_generate_constructor_method($self, \%args);
262 if ($args{inline_destructor}) {
263 # generate and install
264 Mouse::Meta::Method::Destructor->_generate_destructor_method($self, \%args);
267 # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value
268 # at the end of a source file.
272 sub make_mutable { not_supported }
274 sub is_immutable { $_[0]->{is_immutable} }
275 sub is_mutable { !$_[0]->{is_immutable} }
277 sub _install_modifier_pp{
278 my( $self, $into, $type, $name, $code ) = @_;
280 my $original = $into->can($name)
281 or $self->throw_error("The method '$name' is not found in the inheritance hierarchy for class $into");
283 my $modifier_table = $self->{modifiers}{$name};
285 if(!$modifier_table){
286 my(@before, @after, @around, $cache, $modified);
291 for my $c (@before) { $c->(@_) }
293 if(wantarray){ # list context
294 my @rval = $cache->(@_);
296 for my $c(@after){ $c->(@_) }
299 elsif(defined wantarray){ # scalar context
300 my $rval = $cache->(@_);
302 for my $c(@after){ $c->(@_) }
308 for my $c(@after){ $c->(@_) }
313 $self->{modifiers}{$name} = $modifier_table = {
314 original => $original,
320 cache => \$cache, # cache for around modifiers
323 $self->add_method($name => $modified);
326 if($type eq 'before'){
327 unshift @{$modifier_table->{before}}, $code;
329 elsif($type eq 'after'){
330 push @{$modifier_table->{after}}, $code;
333 push @{$modifier_table->{around}}, $code;
335 my $next = ${ $modifier_table->{cache} };
336 ${ $modifier_table->{cache} } = sub{ $code->($next, @_) };
342 sub _install_modifier {
343 my ( $self, $into, $type, $name, $code ) = @_;
345 # load Class::Method::Modifiers first
346 my $no_cmm_fast = do{
348 eval q{ require Class::Method::Modifiers::Fast };
354 $impl = \&_install_modifier_pp;
357 my $install_modifier = Class::Method::Modifiers::Fast->can('_install_modifier');
359 my ( $self, $into, $type, $name, $code ) = @_;
366 $self->{methods}{$name}++; # register it to the method map
371 # replace this method itself :)
373 no warnings 'redefine';
374 *_install_modifier = $impl;
377 $self->$impl( $into, $type, $name, $code );
380 sub add_before_method_modifier {
381 my ( $self, $name, $code ) = @_;
382 $self->_install_modifier( $self->name, 'before', $name, $code );
385 sub add_around_method_modifier {
386 my ( $self, $name, $code ) = @_;
387 $self->_install_modifier( $self->name, 'around', $name, $code );
390 sub add_after_method_modifier {
391 my ( $self, $name, $code ) = @_;
392 $self->_install_modifier( $self->name, 'after', $name, $code );
395 sub add_override_method_modifier {
396 my ($self, $name, $code) = @_;
398 my $package = $self->name;
400 my $body = $package->can($name)
401 or $self->throw_error("You cannot override '$name' because it has no super method");
403 $self->add_method($name => sub { $code->($package, $body, @_) });
407 my ($self, $role_name) = @_;
410 || $self->throw_error("You must supply a role name to look for");
412 for my $class ($self->linearized_isa) {
413 my $meta = Mouse::Util::get_metaclass_by_name($class);
414 next unless $meta && $meta->can('roles');
416 for my $role (@{ $meta->roles }) {
418 return 1 if $role->does_role($role_name);
431 Mouse::Meta::Class - The Mouse class metaclass
435 =head2 C<< initialize(ClassName) -> Mouse::Meta::Class >>
437 Finds or creates a C<Mouse::Meta::Class> instance for the given ClassName. Only
438 one instance should exist for a given class.
440 =head2 C<< name -> ClassName >>
442 Returns the name of the owner class.
444 =head2 C<< superclasses -> ClassNames >> C<< superclass(ClassNames) >>
446 Gets (or sets) the list of superclasses of the owner class.
448 =head2 C<< add_method(name => CodeRef) >>
450 Adds a method to the owner class.
452 =head2 C<< has_method(name) -> Bool >>
454 Returns whether we have a method with the given name.
456 =head2 C<< get_method(name) -> Mouse::Meta::Method | undef >>
458 Returns a L<Mouse::Meta::Method> with the given name.
460 Note that you can also use C<< $metaclass->name->can($name) >> for a method body.
462 =head2 C<< get_method_list -> Names >>
464 Returns a list of method names which are defined in the local class.
465 If you want a list of all applicable methods for a class, use the
466 C<get_all_methods> method.
468 =head2 C<< get_all_methods -> (Mouse::Meta::Method) >>
470 Return the list of all L<Mouse::Meta::Method> instances associated with
471 the class and its superclasses.
473 =head2 C<< add_attribute(name => spec | Mouse::Meta::Attribute) >>
475 Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
478 =head2 C<< has_attribute(Name) -> Bool >>
480 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
482 =head2 C<< get_attribute Name -> Mouse::Meta::Attribute | undef >>
484 Returns the L<Mouse::Meta::Attribute> with the given name.
486 =head2 C<< get_attribute_list -> Names >>
488 Returns a list of attribute names which are defined in the local
489 class. If you want a list of all applicable attributes for a class,
490 use the C<get_all_attributes> method.
492 =head2 C<< get_all_attributes -> (Mouse::Meta::Attribute) >>
494 Returns the list of all L<Mouse::Meta::Attribute> instances associated with
495 this class and its superclasses.
497 =head2 C<< linearized_isa -> [ClassNames] >>
499 Returns the list of classes in method dispatch order, with duplicates removed.
501 =head2 C<< new_object(Parameters) -> Instance >>
503 Creates a new instance.
505 =head2 C<< clone_object(Instance, Parameters) -> Instance >>
507 Clones the given instance which must be an instance governed by this
510 =head2 C<< throw_error(Message, Parameters) >>
512 Throws an error with the given message.
516 L<Moose::Meta::Class>