1 package Mouse::Meta::Class;
5 use Mouse::Meta::Method::Constructor;
6 use Mouse::Meta::Method::Destructor;
7 use Scalar::Util qw/blessed weaken/;
8 use Mouse::Util qw/get_linear_isa version authority identifier/;
14 # because Mouse doesn't introspect existing classes, we're forced to
15 # only pay attention to other Mouse classes
16 sub _metaclass_cache {
19 return $METACLASS_CACHE{$name};
23 my $class = blessed($_[0]) || $_[0];
26 $METACLASS_CACHE{$name} = $class->new(name => $name)
27 if !exists($METACLASS_CACHE{$name});
28 return $METACLASS_CACHE{$name};
31 # Means of accessing all the metaclasses that have
32 # been initialized thus far
33 sub get_all_metaclasses { %METACLASS_CACHE }
34 sub get_all_metaclass_instances { values %METACLASS_CACHE }
35 sub get_all_metaclass_names { keys %METACLASS_CACHE }
36 sub get_metaclass_by_name { $METACLASS_CACHE{$_[0]} }
37 sub store_metaclass_by_name { $METACLASS_CACHE{$_[0]} = $_[1] }
38 sub weaken_metaclass { weaken($METACLASS_CACHE{$_[0]}) }
39 sub does_metaclass_exist { exists $METACLASS_CACHE{$_[0]} && defined $METACLASS_CACHE{$_[0]} }
40 sub remove_metaclass_by_name { $METACLASS_CACHE{$_[0]} = undef }
47 $args{attributes} = {};
48 $args{superclasses} = do {
50 \@{ $args{name} . '::ISA' };
57 sub name { $_[0]->{name} }
63 Mouse::load_class($_) for @_;
64 @{ $self->{superclasses} } = @_;
67 @{ $self->{superclasses} };
75 my $pkg = $self->name;
78 no warnings 'redefine';
79 $self->{'methods'}->{$name}++; # Moose stores meta object here.
80 *{ $pkg . '::' . $name } = $code;
86 $self->name->can($name);
89 # copied from Class::Inspector
90 my $get_methods_for_class = sub {
95 # Get all the CODE symbol table entries
97 grep !/^(?:has|with|around|before|after|augment|inner|blessed|extends|confess|override|super)$/,
98 grep { defined &{"${name}::$_"} }
100 push @functions, keys %{$self->{'methods'}->{$name}} if $self;
101 wantarray ? @functions : \@functions;
104 sub get_method_list {
106 $get_methods_for_class->($self, $self->name);
109 sub get_all_method_names {
112 return grep { $uniq{$_}++ == 0 }
113 map { $get_methods_for_class->(undef, $_) }
114 $self->linearized_isa;
120 if (@_ == 1 && blessed($_[0])) {
122 $self->{'attributes'}{$attr->name} = $attr;
124 my $names = shift @_;
125 $names = [$names] if !ref($names);
126 my $metaclass = 'Mouse::Meta::Attribute';
129 if ( my $metaclass_name = delete $options{metaclass} ) {
130 my $new_class = Mouse::Util::resolve_metaclass_alias(
134 if ( $metaclass ne $new_class ) {
135 $metaclass = $new_class;
139 for my $name (@$names) {
140 if ($name =~ s/^\+//) {
141 $metaclass->clone_parent($self, $name, @_);
144 $metaclass->create($self, $name, @_);
150 sub compute_all_applicable_attributes { shift->get_all_attributes(@_) }
151 sub get_all_attributes {
155 for my $class ($self->linearized_isa) {
156 my $meta = $self->_metaclass_cache($class)
159 for my $name (keys %{ $meta->get_attribute_map }) {
160 next if $seen{$name}++;
161 push @attr, $meta->get_attribute($name);
168 sub get_attribute_map { $_[0]->{attributes} }
169 sub has_attribute { exists $_[0]->{attributes}->{$_[1]} }
170 sub get_attribute { $_[0]->{attributes}->{$_[1]} }
171 sub get_attribute_list {
173 keys %{$self->get_attribute_map};
176 sub linearized_isa { @{ get_linear_isa($_[0]->name) } }
180 my $instance = shift;
182 (blessed($instance) && $instance->isa($class->name))
183 || confess "You must pass an instance of the metaclass (" . $class->name . "), not ($instance)";
185 $class->clone_instance($instance, @_);
189 my ($class, $instance, %params) = @_;
192 || confess "You can only clone instances, ($instance) is not a blessed instance";
194 my $clone = bless { %$instance }, ref $instance;
196 foreach my $attr ($class->get_all_attributes()) {
197 if ( defined( my $init_arg = $attr->init_arg ) ) {
198 if (exists $params{$init_arg}) {
199 $clone->{ $attr->name } = $params{$init_arg};
211 inline_constructor => 1,
212 inline_destructor => 1,
216 my $name = $self->name;
217 $self->{is_immutable}++;
219 if ($args{inline_constructor}) {
220 $self->add_method('new' => Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self ));
223 if ($args{inline_destructor}) {
224 $self->add_method('DESTROY' => Mouse::Meta::Method::Destructor->generate_destructor_method_inline( $self ));
227 # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value
228 # at the end of a source file.
232 sub make_mutable { confess "Mouse does not currently support 'make_mutable'" }
234 sub is_immutable { $_[0]->{is_immutable} }
236 sub attribute_metaclass { "Mouse::Meta::Class" }
238 sub _install_modifier {
239 my ( $self, $into, $type, $name, $code ) = @_;
241 # which is modifer class available?
242 my $modifier_class = do {
243 if (eval "require Class::Method::Modifiers::Fast; 1") {
244 'Class::Method::Modifiers::Fast';
245 } elsif (eval "require Class::Method::Modifiers; 1") {
246 'Class::Method::Modifiers';
248 Carp::croak("Method modifiers require the use of Class::Method::Modifiers or Class::Method::Modifiers::Fast. Please install it from CPAN and file a bug report with this application.");
251 my $modifier = $modifier_class->can('_install_modifier');
253 # replace this method itself :)
256 no warnings 'redefine';
257 *{__PACKAGE__ . '::_install_modifier'} = sub {
258 my ( $self, $into, $type, $name, $code ) = @_;
268 # call me. for first time.
269 $self->_install_modifier( $into, $type, $name, $code );
272 sub add_before_method_modifier {
273 my ( $self, $name, $code ) = @_;
274 $self->_install_modifier( $self->name, 'before', $name, $code );
277 sub add_around_method_modifier {
278 my ( $self, $name, $code ) = @_;
279 $self->_install_modifier( $self->name, 'around', $name, $code );
282 sub add_after_method_modifier {
283 my ( $self, $name, $code ) = @_;
284 $self->_install_modifier( $self->name, 'after', $name, $code );
287 sub add_override_method_modifier {
288 my ($self, $name, $code) = @_;
290 my $pkg = $self->name;
291 my $method = "${pkg}::${name}";
293 # Class::Method::Modifiers won't do this for us, so do it ourselves
295 my $body = $pkg->can($name)
296 or confess "You cannot override '$method' because it has no super method";
299 *$method = sub { $code->($pkg, $body, @_) };
303 sub roles { $_[0]->{roles} }
306 my ($self, $role_name) = @_;
309 || confess "You must supply a role name to look for";
311 for my $class ($self->linearized_isa) {
312 next unless $class->can('meta') and $class->meta->can('roles');
313 for my $role (@{ $class->meta->roles }) {
314 return 1 if $role->name eq $role_name;
322 my ($self, $package_name, %options) = @_;
324 (ref $options{superclasses} eq 'ARRAY')
325 || confess "You must pass an ARRAY ref of superclasses"
326 if exists $options{superclasses};
328 (ref $options{attributes} eq 'ARRAY')
329 || confess "You must pass an ARRAY ref of attributes"
330 if exists $options{attributes};
332 (ref $options{methods} eq 'HASH')
333 || confess "You must pass a HASH ref of methods"
334 if exists $options{methods};
337 ( defined $package_name && $package_name )
338 || confess "You must pass a package name";
340 my $code = "package $package_name;";
341 $code .= "\$$package_name\:\:VERSION = '" . $options{version} . "';"
342 if exists $options{version};
343 $code .= "\$$package_name\:\:AUTHORITY = '" . $options{authority} . "';"
344 if exists $options{authority};
347 confess "creation of $package_name failed : $@" if $@;
350 my %initialize_options = %options;
351 delete @initialize_options{qw(
359 my $meta = $self->initialize( $package_name => %initialize_options );
362 $meta->add_method('meta' => sub {
363 $self->initialize(ref($_[0]) || $_[0]);
366 $meta->superclasses(@{$options{superclasses}})
367 if exists $options{superclasses};
369 # process attributes first, so that they can
370 # install accessors, but locally defined methods
371 # can then overwrite them. It is maybe a little odd, but
372 # I think this should be the order of things.
373 if (exists $options{attributes}) {
374 foreach my $attr (@{$options{attributes}}) {
375 Mouse::Meta::Attribute->create($meta, $attr->{name}, %$attr);
378 if (exists $options{methods}) {
379 foreach my $method_name (keys %{$options{methods}}) {
380 $meta->add_method($method_name, $options{methods}->{$method_name});
387 my $ANON_CLASS_SERIAL = 0;
388 my $ANON_CLASS_PREFIX = 'Mouse::Meta::Class::__ANON__::SERIAL::';
389 sub create_anon_class {
390 my ( $class, %options ) = @_;
391 my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL;
392 return $class->create( $package_name, %options );
402 Mouse::Meta::Class - hook into the Mouse MOP
406 =head2 initialize ClassName -> Mouse::Meta::Class
408 Finds or creates a Mouse::Meta::Class instance for the given ClassName. Only
409 one instance should exist for a given class.
411 =head2 new %args -> Mouse::Meta::Class
413 Creates a new Mouse::Meta::Class. Don't call this directly.
415 =head2 name -> ClassName
417 Returns the name of the owner class.
419 =head2 superclasses -> [ClassName]
421 Gets (or sets) the list of superclasses of the owner class.
423 =head2 add_attribute (Mouse::Meta::Attribute| name => spec)
425 Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
428 =head2 get_all_attributes -> (Mouse::Meta::Attribute)
430 Returns the list of all L<Mouse::Meta::Attribute> instances associated with
431 this class and its superclasses.
433 =head2 get_attribute_map -> { name => Mouse::Meta::Attribute }
435 Returns a mapping of attribute names to their corresponding
436 L<Mouse::Meta::Attribute> objects.
438 =head2 get_attribute_list -> { name => Mouse::Meta::Attribute }
440 This returns a list of attribute names which are defined in the local
441 class. If you want a list of all applicable attributes for a class,
442 use the C<get_all_attributes> method.
444 =head2 has_attribute Name -> Bool
446 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
448 =head2 get_attribute Name -> Mouse::Meta::Attribute | undef
450 Returns the L<Mouse::Meta::Attribute> with the given name.
452 =head2 linearized_isa -> [ClassNames]
454 Returns the list of classes in method dispatch order, with duplicates removed.
456 =head2 clone_object Instance -> Instance
458 Clones the given C<Instance> which must be an instance governed by this
461 =head2 clone_instance Instance, Parameters -> Instance
463 The clone_instance method has been made private.
464 The public version is deprecated.