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 #return Mouse::Meta::Class->initialize($class)->new_object(%args)
29 # if $class ne __PACKAGE__;
31 return bless \%args, ref($class) || $class;
34 sub create_anon_class{
36 return $self->create(undef, @_);
40 return exists $_[0]->{anon_serial_id};
43 sub roles { $_[0]->{roles} }
49 Mouse::load_class($_) for @_;
50 @{ $self->{superclasses} } = @_;
53 return @{ $self->{superclasses} };
56 sub find_method_by_name{
57 my($self, $method_name) = @_;
59 or $self->throw_error('You must define a method name to find');
60 foreach my $class( $self->linearized_isa ){
61 my $method = $self->initialize($class)->get_method($method_name);
62 return $method if defined $method;
69 return map{ $self->find_method_by_name($_) } $self->get_all_method_names;
72 sub get_all_method_names {
75 return grep { $uniq{$_}++ == 0 }
76 map { Mouse::Meta::Class->initialize($_)->get_method_list() }
77 $self->linearized_isa;
88 $attr->isa('Mouse::Meta::Attribute')
89 || $self->throw_error("Your attribute must be an instance of Mouse::Meta::Attribute (or a subclass)");
97 my %args = (@_ == 1) ? %{$_[0]} : @_;
100 or $self->throw_error('You must provide a name for the attribute');
102 if ($name =~ s/^\+//) { # inherited attributes
105 foreach my $class($self->linearized_isa){
106 my $meta = Mouse::Util::get_metaclass_by_name($class) or next;
107 $inherited_attr = $meta->get_attribute($name) and last;
110 defined($inherited_attr)
111 or $self->throw_error("Could not find an attribute by the name of '$name' to inherit from in ".$self->name);
113 $attr = $inherited_attr->clone_and_inherit_options($name, \%args);
116 my($attribute_class, @traits) = Mouse::Meta::Attribute->interpolate_class($name, \%args);
117 $args{traits} = \@traits if @traits;
119 $attr = $attribute_class->new($name, \%args);
123 weaken( $attr->{associated_class} = $self );
125 $self->{attributes}{$attr->name} = $attr;
126 $attr->install_accessors();
128 if(_MOUSE_VERBOSE && !$attr->{associated_methods} && ($attr->{is} || '') ne 'bare'){
129 Carp::cluck(qq{Attribute (}.$attr->name.qq{) of class }.$self->name.qq{ has no associated methods (did you mean to provide an "is" argument?)});
134 sub compute_all_applicable_attributes {
135 Carp::cluck('compute_all_applicable_attributes() has been deprecated');
136 return shift->get_all_attributes(@_)
139 sub get_all_attributes {
143 for my $class ($self->linearized_isa) {
144 my $meta = Mouse::Util::get_metaclass_by_name($class)
147 for my $name ($meta->get_attribute_list) {
148 next if $seen{$name}++;
149 push @attr, $meta->get_attribute($name);
156 sub linearized_isa { @{ get_linear_isa($_[0]->name) } }
160 my %args = (@_ == 1 ? %{$_[0]} : @_);
162 my $instance = bless {}, $self->name;
164 $self->_initialize_instance($instance, \%args);
168 sub _initialize_instance{
169 my($self, $instance, $args) = @_;
173 foreach my $attribute ($self->get_all_attributes) {
174 my $from = $attribute->init_arg;
175 my $key = $attribute->name;
177 if (defined($from) && exists($args->{$from})) {
178 $instance->{$key} = $attribute->_coerce_and_verify($args->{$from});
180 weaken($instance->{$key})
181 if ref($instance->{$key}) && $attribute->is_weak_ref;
183 if ($attribute->has_trigger) {
184 push @triggers_queue, [ $attribute->trigger, $instance->{$from} ];
188 if ($attribute->has_default || $attribute->has_builder) {
189 unless ($attribute->is_lazy) {
190 my $default = $attribute->default;
191 my $builder = $attribute->builder;
192 my $value = $builder ? $instance->$builder()
193 : ref($default) eq 'CODE' ? $instance->$default()
196 # XXX: we cannot use $attribute->set_value() because it invokes triggers.
197 $instance->{$key} = $attribute->_coerce_and_verify($value, $instance);;
199 weaken($instance->{$key})
200 if ref($instance->{$key}) && $attribute->is_weak_ref;
204 if ($attribute->is_required) {
205 $self->throw_error("Attribute (".$attribute->name.") is required");
211 foreach my $trigger_and_value(@triggers_queue){
212 my($trigger, $value) = @{$trigger_and_value};
213 $trigger->($instance, $value);
216 if($self->is_anon_class){
217 $instance->{__METACLASS__} = $self;
225 my $instance = shift;
226 my %params = (@_ == 1) ? %{$_[0]} : @_;
228 (blessed($instance) && $instance->isa($class->name))
229 || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($instance)");
231 my $clone = bless { %$instance }, ref $instance;
233 foreach my $attr ($class->get_all_attributes()) {
234 if ( defined( my $init_arg = $attr->init_arg ) ) {
235 if (exists $params{$init_arg}) {
236 $clone->{ $attr->name } = $params{$init_arg};
245 my ($class, $instance, %params) = @_;
247 Carp::cluck('clone_instance has been deprecated. Use clone_object instead')
249 return $class->clone_object($instance, %params);
255 inline_constructor => 1,
256 inline_destructor => 1,
260 $self->{is_immutable}++;
262 if ($args{inline_constructor}) {
263 $self->add_method('new' => Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self ));
266 if ($args{inline_destructor}) {
267 $self->add_method('DESTROY' => Mouse::Meta::Method::Destructor->generate_destructor_method_inline( $self ));
270 # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value
271 # at the end of a source file.
275 sub make_mutable { not_supported }
277 sub is_immutable { $_[0]->{is_immutable} }
278 sub is_mutable { !$_[0]->{is_immutable} }
280 sub _install_modifier_pp{
281 my( $self, $into, $type, $name, $code ) = @_;
283 my $original = $into->can($name)
284 or $self->throw_error("The method '$name' is not found in the inheritance hierarchy for class $into");
286 my $modifier_table = $self->{modifiers}{$name};
288 if(!$modifier_table){
289 my(@before, @after, @around, $cache, $modified);
294 for my $c (@before) { $c->(@_) }
296 if(wantarray){ # list context
297 my @rval = $cache->(@_);
299 for my $c(@after){ $c->(@_) }
302 elsif(defined wantarray){ # scalar context
303 my $rval = $cache->(@_);
305 for my $c(@after){ $c->(@_) }
311 for my $c(@after){ $c->(@_) }
316 $self->{modifiers}{$name} = $modifier_table = {
317 original => $original,
323 cache => \$cache, # cache for around modifiers
326 $self->add_method($name => $modified);
329 if($type eq 'before'){
330 unshift @{$modifier_table->{before}}, $code;
332 elsif($type eq 'after'){
333 push @{$modifier_table->{after}}, $code;
336 push @{$modifier_table->{around}}, $code;
338 my $next = ${ $modifier_table->{cache} };
339 ${ $modifier_table->{cache} } = sub{ $code->($next, @_) };
345 sub _install_modifier {
346 my ( $self, $into, $type, $name, $code ) = @_;
348 # load Class::Method::Modifiers first
349 my $no_cmm_fast = do{
351 eval q{ require Class::Method::Modifiers::Fast };
357 $impl = \&_install_modifier_pp;
360 my $install_modifier = Class::Method::Modifiers::Fast->can('_install_modifier');
362 my ( $self, $into, $type, $name, $code ) = @_;
369 $self->{methods}{$name}++; # register it to the method map
374 # replace this method itself :)
376 no warnings 'redefine';
377 *_install_modifier = $impl;
380 $self->$impl( $into, $type, $name, $code );
383 sub add_before_method_modifier {
384 my ( $self, $name, $code ) = @_;
385 $self->_install_modifier( $self->name, 'before', $name, $code );
388 sub add_around_method_modifier {
389 my ( $self, $name, $code ) = @_;
390 $self->_install_modifier( $self->name, 'around', $name, $code );
393 sub add_after_method_modifier {
394 my ( $self, $name, $code ) = @_;
395 $self->_install_modifier( $self->name, 'after', $name, $code );
398 sub add_override_method_modifier {
399 my ($self, $name, $code) = @_;
401 my $package = $self->name;
403 my $body = $package->can($name)
404 or $self->throw_error("You cannot override '$name' because it has no super method");
406 $self->add_method($name => sub { $code->($package, $body, @_) });
410 my ($self, $role_name) = @_;
413 || $self->throw_error("You must supply a role name to look for");
415 for my $class ($self->linearized_isa) {
416 my $meta = Mouse::Util::get_metaclass_by_name($class);
417 next unless $meta && $meta->can('roles');
419 for my $role (@{ $meta->roles }) {
421 return 1 if $role->does_role($role_name);
434 Mouse::Meta::Class - The Mouse class metaclass
438 =head2 C<< initialize(ClassName) -> Mouse::Meta::Class >>
440 Finds or creates a C<Mouse::Meta::Class> instance for the given ClassName. Only
441 one instance should exist for a given class.
443 =head2 C<< name -> ClassName >>
445 Returns the name of the owner class.
447 =head2 C<< superclasses -> ClassNames >> C<< superclass(ClassNames) >>
449 Gets (or sets) the list of superclasses of the owner class.
451 =head2 C<< add_method(name => CodeRef) >>
453 Adds a method to the owner class.
455 =head2 C<< has_method(name) -> Bool >>
457 Returns whether we have a method with the given name.
459 =head2 C<< get_method(name) -> Mouse::Meta::Method | undef >>
461 Returns a L<Mouse::Meta::Method> with the given name.
463 Note that you can also use C<< $metaclass->name->can($name) >> for a method body.
465 =head2 C<< get_method_list -> Names >>
467 Returns a list of method names which are defined in the local class.
468 If you want a list of all applicable methods for a class, use the
469 C<get_all_methods> method.
471 =head2 C<< get_all_methods -> (Mouse::Meta::Method) >>
473 Return the list of all L<Mouse::Meta::Method> instances associated with
474 the class and its superclasses.
476 =head2 C<< add_attribute(name => spec | Mouse::Meta::Attribute) >>
478 Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
481 =head2 C<< has_attribute(Name) -> Bool >>
483 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
485 =head2 C<< get_attribute Name -> Mouse::Meta::Attribute | undef >>
487 Returns the L<Mouse::Meta::Attribute> with the given name.
489 =head2 C<< get_attribute_list -> Names >>
491 Returns a list of attribute names which are defined in the local
492 class. If you want a list of all applicable attributes for a class,
493 use the C<get_all_attributes> method.
495 =head2 C<< get_all_attributes -> (Mouse::Meta::Attribute) >>
497 Returns the list of all L<Mouse::Meta::Attribute> instances associated with
498 this class and its superclasses.
500 =head2 C<< linearized_isa -> [ClassNames] >>
502 Returns the list of classes in method dispatch order, with duplicates removed.
504 =head2 C<< new_object(Parameters) -> Instance >>
506 Creates a new instance.
508 =head2 C<< clone_object(Instance, Parameters) -> Instance >>
510 Clones the given instance which must be an instance governed by this
513 =head2 C<< throw_error(Message, Parameters) >>
515 Throws an error with the given message.
519 L<Moose::Meta::Class>