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 get_code_info/;
11 use base qw(Mouse::Meta::Module);
16 # because Mouse doesn't introspect existing classes, we're forced to
17 # only pay attention to other Mouse classes
18 sub _metaclass_cache {
21 return $METACLASS_CACHE{$name};
25 my($class, $package_name, @args) = @_;
27 ($package_name && !ref($package_name))
\r
28 || confess("You must pass a package name and it cannot be blessed");
\r
30 return $METACLASS_CACHE{$package_name}
31 ||= $class->_construct_class_instance(package => $package_name, @args);
35 my($class_or_instance) = @_;
36 return undef unless defined $class_or_instance;
37 return $METACLASS_CACHE{ blessed($class_or_instance) || $class_or_instance };
40 # Means of accessing all the metaclasses that have
41 # been initialized thus far
42 sub get_all_metaclasses { %METACLASS_CACHE }
43 sub get_all_metaclass_instances { values %METACLASS_CACHE }
44 sub get_all_metaclass_names { keys %METACLASS_CACHE }
45 sub get_metaclass_by_name { $METACLASS_CACHE{$_[0]} }
46 sub store_metaclass_by_name { $METACLASS_CACHE{$_[0]} = $_[1] }
47 sub weaken_metaclass { weaken($METACLASS_CACHE{$_[0]}) }
48 sub does_metaclass_exist { exists $METACLASS_CACHE{$_[0]} && defined $METACLASS_CACHE{$_[0]} }
49 sub remove_metaclass_by_name { $METACLASS_CACHE{$_[0]} = undef }
52 sub _construct_class_instance {
53 my($class, %args) = @_;
55 $args{attributes} = {};
56 $args{superclasses} = do {
58 \@{ $args{package} . '::ISA' };
61 $args{methods} ||= {};
66 sub name { $_[0]->{package} }
67 sub _method_map{ $_[0]->{methods} }
70 my $name = $_[0]->{package};
72 return \%{ $name . '::' };
79 Mouse::load_class($_) for @_;
80 @{ $self->{superclasses} } = @_;
83 @{ $self->{superclasses} };
86 sub get_all_method_names {
89 return grep { $uniq{$_}++ == 0 }
90 map { Mouse::Meta::Class->initialize($_)->get_method_list() }
91 $self->linearized_isa;
97 if (@_ == 1 && blessed($_[0])) {
99 $self->{'attributes'}{$attr->name} = $attr;
101 my $names = shift @_;
102 $names = [$names] if !ref($names);
103 my $metaclass = 'Mouse::Meta::Attribute';
106 if ( my $metaclass_name = delete $options{metaclass} ) {
107 my $new_class = Mouse::Util::resolve_metaclass_alias(
111 if ( $metaclass ne $new_class ) {
112 $metaclass = $new_class;
116 for my $name (@$names) {
117 if ($name =~ s/^\+//) {
118 $metaclass->clone_parent($self, $name, @_);
121 $metaclass->create($self, $name, @_);
127 sub compute_all_applicable_attributes { shift->get_all_attributes(@_) }
128 sub get_all_attributes {
132 for my $class ($self->linearized_isa) {
133 my $meta = $self->_metaclass_cache($class)
136 for my $name (keys %{ $meta->get_attribute_map }) {
137 next if $seen{$name}++;
138 push @attr, $meta->get_attribute($name);
145 sub get_attribute_map { $_[0]->{attributes} }
146 sub has_attribute { exists $_[0]->{attributes}->{$_[1]} }
147 sub get_attribute { $_[0]->{attributes}->{$_[1]} }
148 sub get_attribute_list {
150 keys %{$self->get_attribute_map};
153 sub linearized_isa { @{ get_linear_isa($_[0]->name) } }
157 my $instance = shift;
159 (blessed($instance) && $instance->isa($class->name))
160 || confess "You must pass an instance of the metaclass (" . $class->name . "), not ($instance)";
162 $class->clone_instance($instance, @_);
166 my ($class, $instance, %params) = @_;
169 || confess "You can only clone instances, ($instance) is not a blessed instance";
171 my $clone = bless { %$instance }, ref $instance;
173 foreach my $attr ($class->get_all_attributes()) {
174 if ( defined( my $init_arg = $attr->init_arg ) ) {
175 if (exists $params{$init_arg}) {
176 $clone->{ $attr->name } = $params{$init_arg};
188 inline_constructor => 1,
189 inline_destructor => 1,
193 my $name = $self->name;
194 $self->{is_immutable}++;
196 if ($args{inline_constructor}) {
197 $self->add_method('new' => Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self ));
200 if ($args{inline_destructor}) {
201 $self->add_method('DESTROY' => Mouse::Meta::Method::Destructor->generate_destructor_method_inline( $self ));
204 # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value
205 # at the end of a source file.
209 sub make_mutable { confess "Mouse does not currently support 'make_mutable'" }
211 sub is_immutable { $_[0]->{is_immutable} }
213 sub attribute_metaclass { "Mouse::Meta::Class" }
215 sub _install_modifier {
216 my ( $self, $into, $type, $name, $code ) = @_;
218 # which is modifer class available?
219 my $modifier_class = do {
220 if (eval "require Class::Method::Modifiers::Fast; 1") {
221 'Class::Method::Modifiers::Fast';
222 } elsif (eval "require Class::Method::Modifiers; 1") {
223 'Class::Method::Modifiers';
225 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.");
228 my $modifier = $modifier_class->can('_install_modifier');
230 # replace this method itself :)
233 no warnings 'redefine';
234 *{__PACKAGE__ . '::_install_modifier'} = sub {
235 my ( $self, $into, $type, $name, $code ) = @_;
245 # call me. for first time.
246 $self->_install_modifier( $into, $type, $name, $code );
249 sub add_before_method_modifier {
250 my ( $self, $name, $code ) = @_;
251 $self->_install_modifier( $self->name, 'before', $name, $code );
254 sub add_around_method_modifier {
255 my ( $self, $name, $code ) = @_;
256 $self->_install_modifier( $self->name, 'around', $name, $code );
259 sub add_after_method_modifier {
260 my ( $self, $name, $code ) = @_;
261 $self->_install_modifier( $self->name, 'after', $name, $code );
264 sub add_override_method_modifier {
265 my ($self, $name, $code) = @_;
267 my $pkg = $self->name;
268 my $method = "${pkg}::${name}";
270 # Class::Method::Modifiers won't do this for us, so do it ourselves
272 my $body = $pkg->can($name)
273 or confess "You cannot override '$method' because it has no super method";
276 *$method = sub { $code->($pkg, $body, @_) };
280 sub roles { $_[0]->{roles} }
283 my ($self, $role_name) = @_;
286 || confess "You must supply a role name to look for";
288 for my $class ($self->linearized_isa) {
289 my $meta = class_of($class);
290 next unless $meta && $meta->can('roles');
292 for my $role (@{ $meta->roles }) {
293 return 1 if $role->does_role($role_name);
301 my ($class, $package_name, %options) = @_;
303 (ref $options{superclasses} eq 'ARRAY')
304 || confess "You must pass an ARRAY ref of superclasses"
305 if exists $options{superclasses};
307 (ref $options{attributes} eq 'ARRAY')
308 || confess "You must pass an ARRAY ref of attributes"
309 if exists $options{attributes};
311 (ref $options{methods} eq 'HASH')
312 || confess "You must pass a HASH ref of methods"
313 if exists $options{methods};
316 ( defined $package_name && $package_name )
317 || confess "You must pass a package name";
319 my $code = "package $package_name;";
320 $code .= "\$$package_name\:\:VERSION = '" . $options{version} . "';"
321 if exists $options{version};
322 $code .= "\$$package_name\:\:AUTHORITY = '" . $options{authority} . "';"
323 if exists $options{authority};
326 confess "creation of $package_name failed : $@" if $@;
329 my %initialize_options = %options;
330 delete @initialize_options{qw(
338 my $meta = $class->initialize( $package_name => %initialize_options );
341 $meta->add_method('meta' => sub {
342 Mouse::Meta::Class->initialize(ref($_[0]) || $_[0]);
345 $meta->superclasses(@{$options{superclasses}})
346 if exists $options{superclasses};
348 # process attributes first, so that they can
349 # install accessors, but locally defined methods
350 # can then overwrite them. It is maybe a little odd, but
351 # I think this should be the order of things.
352 if (exists $options{attributes}) {
353 foreach my $attr (@{$options{attributes}}) {
354 Mouse::Meta::Attribute->create($meta, $attr->{name}, %$attr);
357 if (exists $options{methods}) {
358 foreach my $method_name (keys %{$options{methods}}) {
359 $meta->add_method($method_name, $options{methods}->{$method_name});
366 my $ANON_CLASS_SERIAL = 0;
367 my $ANON_CLASS_PREFIX = 'Mouse::Meta::Class::__ANON__::SERIAL::';
368 sub create_anon_class {
369 my ( $class, %options ) = @_;
370 my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL;
371 return $class->create( $package_name, %options );
381 Mouse::Meta::Class - hook into the Mouse MOP
385 =head2 initialize ClassName -> Mouse::Meta::Class
387 Finds or creates a Mouse::Meta::Class instance for the given ClassName. Only
388 one instance should exist for a given class.
390 =head2 new %args -> Mouse::Meta::Class
392 Creates a new Mouse::Meta::Class. Don't call this directly.
394 =head2 name -> ClassName
396 Returns the name of the owner class.
398 =head2 superclasses -> [ClassName]
400 Gets (or sets) the list of superclasses of the owner class.
402 =head2 add_attribute (Mouse::Meta::Attribute| name => spec)
404 Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
407 =head2 get_all_attributes -> (Mouse::Meta::Attribute)
409 Returns the list of all L<Mouse::Meta::Attribute> instances associated with
410 this class and its superclasses.
412 =head2 get_attribute_map -> { name => Mouse::Meta::Attribute }
414 Returns a mapping of attribute names to their corresponding
415 L<Mouse::Meta::Attribute> objects.
417 =head2 get_attribute_list -> { name => Mouse::Meta::Attribute }
419 This returns a list of attribute names which are defined in the local
420 class. If you want a list of all applicable attributes for a class,
421 use the C<get_all_attributes> method.
423 =head2 has_attribute Name -> Bool
425 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
427 =head2 get_attribute Name -> Mouse::Meta::Attribute | undef
429 Returns the L<Mouse::Meta::Attribute> with the given name.
431 =head2 linearized_isa -> [ClassNames]
433 Returns the list of classes in method dispatch order, with duplicates removed.
435 =head2 clone_object Instance -> Instance
437 Clones the given C<Instance> which must be an instance governed by this
440 =head2 clone_instance Instance, Parameters -> Instance
442 The clone_instance method has been made private.
443 The public version is deprecated.