2 package Mouse::Meta::Class;
6 use Mouse::Meta::Method::Constructor;
7 use Mouse::Util qw/get_linear_isa blessed/;
13 # because Mouse doesn't introspect existing classes, we're forced to
14 # only pay attention to other Mouse classes
15 sub _metaclass_cache {
18 return $METACLASS_CACHE{$name};
24 $METACLASS_CACHE{$name} = $class->new(name => $name)
25 if !exists($METACLASS_CACHE{$name});
26 return $METACLASS_CACHE{$name};
34 $args{attributes} = {};
35 $args{superclasses} = do {
37 \@{ $args{name} . '::ISA' };
43 sub name { $_[0]->{name} }
49 Mouse::load_class($_) for @_;
50 @{ $self->{superclasses} } = @_;
53 @{ $self->{superclasses} };
61 my $pkg = $self->name;
64 *{ $pkg . '::' . $name } = $code;
67 # copied from Class::Inspector
70 my $name = $self->name;
73 # Get all the CODE symbol table entries
74 my @functions = grep !/^meta$/,
75 grep { /\A[^\W\d]\w*\z/o }
76 grep { defined &{"${name}::$_"} }
78 wantarray ? @functions : \@functions;
85 $self->{'attributes'}{$attr->name} = $attr;
88 sub compute_all_applicable_attributes {
92 for my $class ($self->linearized_isa) {
93 my $meta = $self->_metaclass_cache($class)
96 for my $name (keys %{ $meta->get_attribute_map }) {
97 next if $seen{$name}++;
98 push @attr, $meta->get_attribute($name);
105 sub get_attribute_map { $_[0]->{attributes} }
106 sub has_attribute { exists $_[0]->{attributes}->{$_[1]} }
107 sub get_attribute { $_[0]->{attributes}->{$_[1]} }
109 sub linearized_isa { @{ get_linear_isa($_[0]->name) } }
113 my $instance = shift;
115 (blessed($instance) && $instance->isa($class->name))
116 || confess "You must pass an instance of the metaclass (" . $class->name . "), not ($instance)";
118 $class->clone_instance($instance, @_);
122 my ($class, $instance, %params) = @_;
125 || confess "You can only clone instances, ($instance) is not a blessed instance";
127 my $clone = bless { %$instance }, ref $instance;
129 foreach my $attr ($class->compute_all_applicable_attributes()) {
130 if ( defined( my $init_arg = $attr->init_arg ) ) {
131 if (exists $params{$init_arg}) {
132 $clone->{ $attr->name } = $params{$init_arg};
143 my $name = $self->name;
144 $self->{is_immutable}++;
146 *{"$name\::new"} = Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self );
149 Carp::croak "Mouse::Meta::Class->make_mutable does not supported by Mouse";
151 sub is_immutable { $_[0]->{is_immutable} }
153 sub attribute_metaclass { "Mouse::Meta::Class" }
155 sub add_before_method_modifier {
156 my ($self, $name, $code) = @_;
157 require Class::Method::Modifiers;
158 Class::Method::Modifiers::_install_modifier(
166 sub add_around_method_modifier {
167 my ($self, $name, $code) = @_;
168 require Class::Method::Modifiers;
169 Class::Method::Modifiers::_install_modifier(
177 sub add_after_method_modifier {
178 my ($self, $name, $code) = @_;
179 require Class::Method::Modifiers;
180 Class::Method::Modifiers::_install_modifier(
194 Mouse::Meta::Class - hook into the Mouse MOP
198 =head2 initialize ClassName -> Mouse::Meta::Class
200 Finds or creates a Mouse::Meta::Class instance for the given ClassName. Only
201 one instance should exist for a given class.
203 =head2 new %args -> Mouse::Meta::Class
205 Creates a new Mouse::Meta::Class. Don't call this directly.
207 =head2 name -> ClassName
209 Returns the name of the owner class.
211 =head2 superclasses -> [ClassName]
213 Gets (or sets) the list of superclasses of the owner class.
215 =head2 add_attribute Mouse::Meta::Attribute
217 Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
220 =head2 compute_all_applicable_attributes -> (Mouse::Meta::Attribute)
222 Returns the list of all L<Mouse::Meta::Attribute> instances associated with
223 this class and its superclasses.
225 =head2 get_attribute_map -> { name => Mouse::Meta::Attribute }
227 Returns a mapping of attribute names to their corresponding
228 L<Mouse::Meta::Attribute> objects.
230 =head2 has_attribute Name -> Boool
232 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
234 =head2 get_attribute Name -> Mouse::Meta::Attribute | undef
236 Returns the L<Mouse::Meta::Attribute> with the given name.
238 =head2 linearized_isa -> [ClassNames]
240 Returns the list of classes in method dispatch order, with duplicates removed.
242 =head2 clone_object Instance -> Instance
244 Clones the given C<Instance> which must be an instance governed by this
247 =head2 clone_instance Instance, Parameters -> Instance
249 Clones the given C<Instance> and sets any additional parameters.