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::Meta::Module::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 { shift->get_all_attributes(@_) }
135 sub get_all_attributes {
139 for my $class ($self->linearized_isa) {
140 my $meta = $self->_metaclass_cache($class)
143 for my $name (keys %{ $meta->get_attribute_map }) {
144 next if $seen{$name}++;
145 push @attr, $meta->get_attribute($name);
152 sub linearized_isa { @{ get_linear_isa($_[0]->name) } }
156 my %args = (@_ == 1 ? %{$_[0]} : @_);
158 my $instance = bless {}, $self->name;
160 $self->_initialize_instance($instance, \%args);
164 sub _initialize_instance{
165 my($self, $instance, $args) = @_;
169 foreach my $attribute ($self->get_all_attributes) {
170 my $from = $attribute->init_arg;
171 my $key = $attribute->name;
173 if (defined($from) && exists($args->{$from})) {
174 $instance->{$key} = $attribute->_coerce_and_verify($args->{$from});
176 weaken($instance->{$key})
177 if ref($instance->{$key}) && $attribute->is_weak_ref;
179 if ($attribute->has_trigger) {
180 push @triggers_queue, [ $attribute->trigger, $instance->{$from} ];
184 if ($attribute->has_default || $attribute->has_builder) {
185 unless ($attribute->is_lazy) {
186 my $default = $attribute->default;
187 my $builder = $attribute->builder;
188 my $value = $attribute->has_builder
189 ? $instance->$builder
190 : ref($default) eq 'CODE'
191 ? $default->($instance)
194 $value = $attribute->coerce_constraint($value)
195 if $attribute->should_coerce;
196 $attribute->verify_against_type_constraint($value);
198 $instance->{$key} = $value;
200 weaken($instance->{$key})
201 if ref($instance->{$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->($instance, $value);
217 if($self->is_anon_class){
218 $instance->{__METACLASS__} = $self;
226 my $instance = shift;
227 my %params = (@_ == 1) ? %{$_[0]} : @_;
229 (blessed($instance) && $instance->isa($class->name))
230 || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($instance)");
232 my $clone = bless { %$instance }, ref $instance;
234 foreach my $attr ($class->get_all_attributes()) {
235 if ( defined( my $init_arg = $attr->init_arg ) ) {
236 if (exists $params{$init_arg}) {
237 $clone->{ $attr->name } = $params{$init_arg};
246 my ($class, $instance, %params) = @_;
248 Carp::cluck('clone_instance has been deprecated. Use clone_object instead')
250 return $class->clone_object($instance, %params);
256 inline_constructor => 1,
257 inline_destructor => 1,
261 $self->{is_immutable}++;
263 if ($args{inline_constructor}) {
264 $self->add_method('new' => Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self ));
267 if ($args{inline_destructor}) {
268 $self->add_method('DESTROY' => Mouse::Meta::Method::Destructor->generate_destructor_method_inline( $self ));
271 # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value
272 # at the end of a source file.
276 sub make_mutable { not_supported }
278 sub is_immutable { $_[0]->{is_immutable} }
279 sub is_mutable { !$_[0]->{is_immutable} }
281 sub _install_modifier_pp{
282 my( $self, $into, $type, $name, $code ) = @_;
284 my $original = $into->can($name)
285 or $self->throw_error("The method '$name' is not found in the inheritance hierarchy for class $into");
287 my $modifier_table = $self->{modifiers}{$name};
289 if(!$modifier_table){
290 my(@before, @after, @around, $cache, $modified);
295 for my $c (@before) { $c->(@_) }
297 if(wantarray){ # list context
298 my @rval = $cache->(@_);
300 for my $c(@after){ $c->(@_) }
303 elsif(defined wantarray){ # scalar context
304 my $rval = $cache->(@_);
306 for my $c(@after){ $c->(@_) }
312 for my $c(@after){ $c->(@_) }
317 $self->{modifiers}{$name} = $modifier_table = {
318 original => $original,
324 cache => \$cache, # cache for around modifiers
327 $self->add_method($name => $modified);
330 if($type eq 'before'){
331 unshift @{$modifier_table->{before}}, $code;
333 elsif($type eq 'after'){
334 push @{$modifier_table->{after}}, $code;
337 push @{$modifier_table->{around}}, $code;
339 my $next = ${ $modifier_table->{cache} };
340 ${ $modifier_table->{cache} } = sub{ $code->($next, @_) };
346 sub _install_modifier {
347 my ( $self, $into, $type, $name, $code ) = @_;
349 # load Class::Method::Modifiers first
350 my $no_cmm_fast = do{
352 eval q{ require Class::Method::Modifiers::Fast };
358 $impl = \&_install_modifier_pp;
361 my $install_modifier = Class::Method::Modifiers::Fast->can('_install_modifier');
363 my ( $self, $into, $type, $name, $code ) = @_;
370 $self->{methods}{$name}++; # register it to the method map
375 # replace this method itself :)
377 no warnings 'redefine';
378 *_install_modifier = $impl;
381 $self->$impl( $into, $type, $name, $code );
384 sub add_before_method_modifier {
385 my ( $self, $name, $code ) = @_;
386 $self->_install_modifier( $self->name, 'before', $name, $code );
389 sub add_around_method_modifier {
390 my ( $self, $name, $code ) = @_;
391 $self->_install_modifier( $self->name, 'around', $name, $code );
394 sub add_after_method_modifier {
395 my ( $self, $name, $code ) = @_;
396 $self->_install_modifier( $self->name, 'after', $name, $code );
399 sub add_override_method_modifier {
400 my ($self, $name, $code) = @_;
402 my $package = $self->name;
404 my $body = $package->can($name)
405 or $self->throw_error("You cannot override '$name' because it has no super method");
407 $self->add_method($name => sub { $code->($package, $body, @_) });
411 my ($self, $role_name) = @_;
414 || $self->throw_error("You must supply a role name to look for");
416 for my $class ($self->linearized_isa) {
417 my $meta = Mouse::Meta::Module::class_of($class);
418 next unless $meta && $meta->can('roles');
420 for my $role (@{ $meta->roles }) {
422 return 1 if $role->does_role($role_name);
435 Mouse::Meta::Class - The Mouse class metaclass
439 =head2 C<< initialize(ClassName) -> Mouse::Meta::Class >>
441 Finds or creates a C<Mouse::Meta::Class> instance for the given ClassName. Only
442 one instance should exist for a given class.
444 =head2 C<< name -> ClassName >>
446 Returns the name of the owner class.
448 =head2 C<< superclasses -> ClassNames >> C<< superclass(ClassNames) >>
450 Gets (or sets) the list of superclasses of the owner class.
452 =head2 C<< add_method(name => CodeRef) >>
454 Adds a method to the owner class.
456 =head2 C<< has_method(name) -> Bool >>
458 Returns whether we have a method with the given name.
460 =head2 C<< get_method(name) -> Mouse::Meta::Method | undef >>
462 Returns a L<Mouse::Meta::Method> with the given name.
464 Note that you can also use C<< $metaclass->name->can($name) >> for a method body.
466 =head2 C<< get_method_list -> Names >>
468 Returns a list of method names which are defined in the local class.
469 If you want a list of all applicable methods for a class, use the
470 C<get_all_methods> method.
472 =head2 C<< get_all_methods -> (Mouse::Meta::Method) >>
474 Return the list of all L<Mouse::Meta::Method> instances associated with
475 the class and its superclasses.
477 =head2 C<< add_attribute(name => spec | Mouse::Meta::Attribute) >>
479 Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
482 =head2 C<< has_attribute(Name) -> Bool >>
484 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
486 =head2 C<< get_attribute Name -> Mouse::Meta::Attribute | undef >>
488 Returns the L<Mouse::Meta::Attribute> with the given name.
490 =head2 C<< get_attribute_list -> Names >>
492 Returns a list of attribute names which are defined in the local
493 class. If you want a list of all applicable attributes for a class,
494 use the C<get_all_attributes> method.
496 =head2 C<< get_all_attributes -> (Mouse::Meta::Attribute) >>
498 Returns the list of all L<Mouse::Meta::Attribute> instances associated with
499 this class and its superclasses.
501 =head2 C<< linearized_isa -> [ClassNames] >>
503 Returns the list of classes in method dispatch order, with duplicates removed.
505 =head2 C<< new_object(Parameters) -> Instance >>
507 Creates a new instance.
509 =head2 C<< clone_object(Instance, Parameters) -> Instance >>
511 Clones the given instance which must be an instance governed by this
514 =head2 C<< throw_error(Message, Parameters) >>
516 Throws an error with the given message.
520 L<Moose::Meta::Class>