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 not_supported/;
10 use base qw(Mouse::Meta::Module);
12 sub method_metaclass(){ 'Mouse::Meta::Method' } # required for get_method()
15 my($class, %args) = @_;
17 $args{attributes} ||= {};
18 $args{methods} ||= {};
21 $args{superclasses} = do {
23 \@{ $args{package} . '::ISA' };
26 #return Mouse::Meta::Class->initialize($class)->new_object(%args)
27 # if $class ne __PACKAGE__;
29 return bless \%args, $class;
32 sub create_anon_class{
34 return $self->create(undef, @_);
38 return exists $_[0]->{anon_serial_id};
41 sub roles { $_[0]->{roles} }
47 Mouse::load_class($_) for @_;
48 @{ $self->{superclasses} } = @_;
51 @{ $self->{superclasses} };
54 sub get_all_method_names {
57 return grep { $uniq{$_}++ == 0 }
58 map { Mouse::Meta::Class->initialize($_)->get_method_list() }
59 $self->linearized_isa;
65 if (@_ == 1 && blessed($_[0])) {
67 $self->{'attributes'}{$attr->name} = $attr;
71 $names = [$names] if !ref($names);
72 my $metaclass = 'Mouse::Meta::Attribute';
73 my %options = (@_ == 1 ? %{$_[0]} : @_);
75 if ( my $metaclass_name = delete $options{metaclass} ) {
76 my $new_class = Mouse::Util::resolve_metaclass_alias(
80 if ( $metaclass ne $new_class ) {
81 $metaclass = $new_class;
85 for my $name (@$names) {
86 if ($name =~ s/^\+//) {
87 $metaclass->clone_parent($self, $name, %options);
90 $metaclass->create($self, $name, %options);
96 sub compute_all_applicable_attributes { shift->get_all_attributes(@_) }
97 sub get_all_attributes {
101 for my $class ($self->linearized_isa) {
102 my $meta = $self->_metaclass_cache($class)
105 for my $name (keys %{ $meta->get_attribute_map }) {
106 next if $seen{$name}++;
107 push @attr, $meta->get_attribute($name);
114 sub linearized_isa { @{ get_linear_isa($_[0]->name) } }
118 my %args = (@_ == 1 ? %{$_[0]} : @_);
120 my $instance = bless {}, $self->name;
122 foreach my $attribute ($self->get_all_attributes) {
123 my $from = $attribute->init_arg;
124 my $key = $attribute->name;
126 if (defined($from) && exists($args{$from})) {
127 $args{$from} = $attribute->coerce_constraint($args{$from})
128 if $attribute->should_coerce;
129 $attribute->verify_against_type_constraint($args{$from});
131 $instance->{$key} = $args{$from};
133 weaken($instance->{$key})
134 if ref($instance->{$key}) && $attribute->is_weak_ref;
136 if ($attribute->has_trigger) {
137 $attribute->trigger->($instance, $args{$from});
141 if ($attribute->has_default || $attribute->has_builder) {
142 unless ($attribute->is_lazy) {
143 my $default = $attribute->default;
144 my $builder = $attribute->builder;
145 my $value = $attribute->has_builder
146 ? $instance->$builder
147 : ref($default) eq 'CODE'
148 ? $default->($instance)
151 $value = $attribute->coerce_constraint($value)
152 if $attribute->should_coerce;
153 $attribute->verify_against_type_constraint($value);
155 $instance->{$key} = $value;
157 weaken($instance->{$key})
158 if ref($instance->{$key}) && $attribute->is_weak_ref;
162 if ($attribute->is_required) {
163 $self->throw_error("Attribute (".$attribute->name.") is required");
173 my $instance = shift;
175 (blessed($instance) && $instance->isa($class->name))
176 || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($instance)");
178 $class->clone_instance($instance, @_);
182 my ($class, $instance, %params) = @_;
185 || $class->throw_error("You can only clone instances, ($instance) is not a blessed instance");
187 my $clone = bless { %$instance }, ref $instance;
189 foreach my $attr ($class->get_all_attributes()) {
190 if ( defined( my $init_arg = $attr->init_arg ) ) {
191 if (exists $params{$init_arg}) {
192 $clone->{ $attr->name } = $params{$init_arg};
204 inline_constructor => 1,
205 inline_destructor => 1,
209 $self->{is_immutable}++;
211 if ($args{inline_constructor}) {
212 $self->add_method('new' => Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self ));
215 if ($args{inline_destructor}) {
216 $self->add_method('DESTROY' => Mouse::Meta::Method::Destructor->generate_destructor_method_inline( $self ));
219 # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value
220 # at the end of a source file.
224 sub make_mutable { not_supported }
226 sub is_immutable { $_[0]->{is_immutable} }
227 sub is_mutable { !$_[0]->{is_immutable} }
229 sub _install_modifier {
230 my ( $self, $into, $type, $name, $code ) = @_;
232 # which is modifer class available?
233 my $modifier_class = do {
234 if (eval "require Class::Method::Modifiers::Fast; 1") {
235 'Class::Method::Modifiers::Fast';
236 } elsif (eval "require Class::Method::Modifiers; 1") {
237 'Class::Method::Modifiers';
239 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.");
242 my $modifier = $modifier_class->can('_install_modifier');
244 # replace this method itself :)
246 no warnings 'redefine';
247 *_install_modifier = sub {
248 my ( $self, $into, $type, $name, $code ) = @_;
255 $self->{methods}{$name}++; # register it to the method map
260 # call me. for first time.
261 $self->_install_modifier( $into, $type, $name, $code );
264 sub add_before_method_modifier {
265 my ( $self, $name, $code ) = @_;
266 $self->_install_modifier( $self->name, 'before', $name, $code );
269 sub add_around_method_modifier {
270 my ( $self, $name, $code ) = @_;
271 $self->_install_modifier( $self->name, 'around', $name, $code );
274 sub add_after_method_modifier {
275 my ( $self, $name, $code ) = @_;
276 $self->_install_modifier( $self->name, 'after', $name, $code );
279 sub add_override_method_modifier {
280 my ($self, $name, $code) = @_;
282 my $package = $self->name;
284 my $body = $package->can($name)
285 or $self->throw_error("You cannot override '$name' because it has no super method");
287 $self->add_method($name => sub { $code->($package, $body, @_) });
291 my ($self, $role_name) = @_;
294 || $self->throw_error("You must supply a role name to look for");
296 for my $class ($self->linearized_isa) {
297 my $meta = Mouse::Meta::Module::class_of($class);
298 next unless $meta && $meta->can('roles');
300 for my $role (@{ $meta->roles }) {
302 return 1 if $role->does_role($role_name);
315 Mouse::Meta::Class - hook into the Mouse MOP
319 =head2 initialize ClassName -> Mouse::Meta::Class
321 Finds or creates a Mouse::Meta::Class instance for the given ClassName. Only
322 one instance should exist for a given class.
324 =head2 new %args -> Mouse::Meta::Class
326 Creates a new Mouse::Meta::Class. Don't call this directly.
328 =head2 name -> ClassName
330 Returns the name of the owner class.
332 =head2 superclasses -> [ClassName]
334 Gets (or sets) the list of superclasses of the owner class.
336 =head2 add_attribute (Mouse::Meta::Attribute| name => spec)
338 Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
341 =head2 get_all_attributes -> (Mouse::Meta::Attribute)
343 Returns the list of all L<Mouse::Meta::Attribute> instances associated with
344 this class and its superclasses.
346 =head2 get_attribute_map -> { name => Mouse::Meta::Attribute }
348 Returns a mapping of attribute names to their corresponding
349 L<Mouse::Meta::Attribute> objects.
351 =head2 get_attribute_list -> { name => Mouse::Meta::Attribute }
353 This returns a list of attribute names which are defined in the local
354 class. If you want a list of all applicable attributes for a class,
355 use the C<get_all_attributes> method.
357 =head2 has_attribute Name -> Bool
359 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
361 =head2 get_attribute Name -> Mouse::Meta::Attribute | undef
363 Returns the L<Mouse::Meta::Attribute> with the given name.
365 =head2 linearized_isa -> [ClassNames]
367 Returns the list of classes in method dispatch order, with duplicates removed.
369 =head2 clone_object Instance -> Instance
371 Clones the given C<Instance> which must be an instance governed by this
374 =head2 clone_instance Instance, Parameters -> Instance
376 The clone_instance method has been made private.
377 The public version is deprecated.