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' };
29 sub roles { $_[0]->{roles} }
35 Mouse::load_class($_) for @_;
36 @{ $self->{superclasses} } = @_;
39 @{ $self->{superclasses} };
42 sub get_all_method_names {
45 return grep { $uniq{$_}++ == 0 }
46 map { Mouse::Meta::Class->initialize($_)->get_method_list() }
47 $self->linearized_isa;
53 if (@_ == 1 && blessed($_[0])) {
55 $self->{'attributes'}{$attr->name} = $attr;
58 $names = [$names] if !ref($names);
59 my $metaclass = 'Mouse::Meta::Attribute';
62 if ( my $metaclass_name = delete $options{metaclass} ) {
63 my $new_class = Mouse::Util::resolve_metaclass_alias(
67 if ( $metaclass ne $new_class ) {
68 $metaclass = $new_class;
72 for my $name (@$names) {
73 if ($name =~ s/^\+//) {
74 $metaclass->clone_parent($self, $name, @_);
77 $metaclass->create($self, $name, @_);
83 sub compute_all_applicable_attributes { shift->get_all_attributes(@_) }
84 sub get_all_attributes {
88 for my $class ($self->linearized_isa) {
89 my $meta = $self->_metaclass_cache($class)
92 for my $name (keys %{ $meta->get_attribute_map }) {
93 next if $seen{$name}++;
94 push @attr, $meta->get_attribute($name);
101 sub linearized_isa { @{ get_linear_isa($_[0]->name) } }
105 my $args = (@_ == 1) ? $_[0] : { @_ };
107 my $instance = bless {}, $self->name;
109 foreach my $attribute ($self->get_all_attributes) {
110 my $from = $attribute->init_arg;
111 my $key = $attribute->name;
113 if (defined($from) && exists($args->{$from})) {
114 $args->{$from} = $attribute->coerce_constraint($args->{$from})
115 if $attribute->should_coerce;
116 $attribute->verify_against_type_constraint($args->{$from});
118 $instance->{$key} = $args->{$from};
120 weaken($instance->{$key})
121 if ref($instance->{$key}) && $attribute->is_weak_ref;
123 if ($attribute->has_trigger) {
124 $attribute->trigger->($instance, $args->{$from});
128 if ($attribute->has_default || $attribute->has_builder) {
129 unless ($attribute->is_lazy) {
130 my $default = $attribute->default;
131 my $builder = $attribute->builder;
132 my $value = $attribute->has_builder
133 ? $instance->$builder
134 : ref($default) eq 'CODE'
135 ? $default->($instance)
138 $value = $attribute->coerce_constraint($value)
139 if $attribute->should_coerce;
140 $attribute->verify_against_type_constraint($value);
142 $instance->{$key} = $value;
144 weaken($instance->{$key})
145 if ref($instance->{$key}) && $attribute->is_weak_ref;
149 if ($attribute->is_required) {
150 $self->throw_error("Attribute (".$attribute->name.") is required");
160 my $instance = shift;
162 (blessed($instance) && $instance->isa($class->name))
163 || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($instance)");
165 $class->clone_instance($instance, @_);
169 my ($class, $instance, %params) = @_;
172 || $class->throw_error("You can only clone instances, ($instance) is not a blessed instance");
174 my $clone = bless { %$instance }, ref $instance;
176 foreach my $attr ($class->get_all_attributes()) {
177 if ( defined( my $init_arg = $attr->init_arg ) ) {
178 if (exists $params{$init_arg}) {
179 $clone->{ $attr->name } = $params{$init_arg};
191 inline_constructor => 1,
192 inline_destructor => 1,
196 $self->{is_immutable}++;
198 if ($args{inline_constructor}) {
199 $self->add_method('new' => Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self ));
202 if ($args{inline_destructor}) {
203 $self->add_method('DESTROY' => Mouse::Meta::Method::Destructor->generate_destructor_method_inline( $self ));
206 # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value
207 # at the end of a source file.
211 sub make_mutable { not_supported }
213 sub is_immutable { $_[0]->{is_immutable} }
214 sub is_mutable { !$_[0]->{is_immutable} }
216 sub _install_modifier {
217 my ( $self, $into, $type, $name, $code ) = @_;
219 # which is modifer class available?
220 my $modifier_class = do {
221 if (eval "require Class::Method::Modifiers::Fast; 1") {
222 'Class::Method::Modifiers::Fast';
223 } elsif (eval "require Class::Method::Modifiers; 1") {
224 'Class::Method::Modifiers';
226 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.");
229 my $modifier = $modifier_class->can('_install_modifier');
231 # replace this method itself :)
233 no warnings 'redefine';
234 *_install_modifier = sub {
235 my ( $self, $into, $type, $name, $code ) = @_;
242 $self->{methods}{$name}++; # register it to the method map
247 # call me. for first time.
248 $self->_install_modifier( $into, $type, $name, $code );
251 sub add_before_method_modifier {
252 my ( $self, $name, $code ) = @_;
253 $self->_install_modifier( $self->name, 'before', $name, $code );
256 sub add_around_method_modifier {
257 my ( $self, $name, $code ) = @_;
258 $self->_install_modifier( $self->name, 'around', $name, $code );
261 sub add_after_method_modifier {
262 my ( $self, $name, $code ) = @_;
263 $self->_install_modifier( $self->name, 'after', $name, $code );
266 sub add_override_method_modifier {
267 my ($self, $name, $code) = @_;
269 my $package = $self->name;
271 my $body = $package->can($name)
272 or $self->throw_error("You cannot override '$name' because it has no super method");
274 $self->add_method($name => sub { $code->($package, $body, @_) });
278 my ($self, $role_name) = @_;
281 || $self->throw_error("You must supply a role name to look for");
283 for my $class ($self->linearized_isa) {
284 my $meta = Mouse::Meta::Module::class_of($class);
285 next unless $meta && $meta->can('roles');
287 for my $role (@{ $meta->roles }) {
289 return 1 if $role->does_role($role_name);
297 my ($class, $package_name, %options) = @_;
299 (ref $options{superclasses} eq 'ARRAY')
300 || $class->throw_error("You must pass an ARRAY ref of superclasses")
301 if exists $options{superclasses};
303 (ref $options{attributes} eq 'ARRAY')
304 || $class->throw_error("You must pass an ARRAY ref of attributes")
305 if exists $options{attributes};
307 (ref $options{methods} eq 'HASH')
308 || $class->throw_error("You must pass a HASH ref of methods")
309 if exists $options{methods};
311 (ref $options{roles} eq 'ARRAY')
312 || $class->throw_error("You must pass an ARRAY ref of roles")
313 if exists $options{roles};
316 ( defined $package_name && $package_name )
317 || $class->throw_error("You must pass a package name");
320 ${ $package_name . '::VERSION' } = $options{version} if exists $options{version};
321 ${ $package_name . '::AUTHORITY' } = $options{authority} if exists $options{authority};
324 my %initialize_options = %options;
325 delete @initialize_options{qw(
334 my $meta = $class->initialize( $package_name => %initialize_options );
337 $meta->add_method('meta' => sub {
338 Mouse::Meta::Class->initialize(ref($_[0]) || $_[0]);
341 $meta->superclasses(@{$options{superclasses}})
342 if exists $options{superclasses};
344 # process attributes first, so that they can
345 # install accessors, but locally defined methods
346 # can then overwrite them. It is maybe a little odd, but
347 # I think this should be the order of things.
348 if (exists $options{attributes}) {
349 foreach my $attr (@{$options{attributes}}) {
350 Mouse::Meta::Attribute->create($meta, $attr->{name}, %$attr);
353 if (exists $options{methods}) {
354 foreach my $method_name (keys %{$options{methods}}) {
355 $meta->add_method($method_name, $options{methods}->{$method_name});
358 if (exists $options{roles}){
359 Mouse::Util::apply_all_roles($package_name, @{$options{roles}});
365 my $ANON_CLASS_SERIAL = 0;
366 my $ANON_CLASS_PREFIX = 'Mouse::Meta::Class::__ANON__::SERIAL::';
368 my %IMMORTAL_ANON_CLASSES;
369 sub create_anon_class {
370 my ( $class, %options ) = @_;
372 my $cache = $options{cache};
375 if($cache){ # anonymous but not mortal
376 # something like Super::Class|Super::Class::2=Role|Role::1
\r
377 $cache_key = join '=' => (
\r
378 join('|', @{$options{superclasses} || []}),
\r
379 join('|', sort @{$options{roles} || []}),
\r
381 return $IMMORTAL_ANON_CLASSES{$cache_key} if exists $IMMORTAL_ANON_CLASSES{$cache_key};
383 my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL;
384 my $meta = $class->create( $package_name, anon_class_id => $ANON_CLASS_SERIAL, %options );
387 $IMMORTAL_ANON_CLASSES{$cache_key} = $meta;
390 Mouse::Meta::Module::weaken_metaclass($package_name);
396 return exists $_[0]->{anon_class_id};
403 my $serial_id = $self->{anon_class_id};
405 return if !$serial_id;
407 my $stash = $self->namespace;
409 @{$self->{sperclasses}} = ();
411 Mouse::Meta::Module::remove_metaclass_by_name($self->name);
414 delete ${$ANON_CLASS_PREFIX}{ $serial_id . '::' };
427 Mouse::Meta::Class - hook into the Mouse MOP
431 =head2 initialize ClassName -> Mouse::Meta::Class
433 Finds or creates a Mouse::Meta::Class instance for the given ClassName. Only
434 one instance should exist for a given class.
436 =head2 new %args -> Mouse::Meta::Class
438 Creates a new Mouse::Meta::Class. Don't call this directly.
440 =head2 name -> ClassName
442 Returns the name of the owner class.
444 =head2 superclasses -> [ClassName]
446 Gets (or sets) the list of superclasses of the owner class.
448 =head2 add_attribute (Mouse::Meta::Attribute| name => spec)
450 Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
453 =head2 get_all_attributes -> (Mouse::Meta::Attribute)
455 Returns the list of all L<Mouse::Meta::Attribute> instances associated with
456 this class and its superclasses.
458 =head2 get_attribute_map -> { name => Mouse::Meta::Attribute }
460 Returns a mapping of attribute names to their corresponding
461 L<Mouse::Meta::Attribute> objects.
463 =head2 get_attribute_list -> { name => Mouse::Meta::Attribute }
465 This returns a list of attribute names which are defined in the local
466 class. If you want a list of all applicable attributes for a class,
467 use the C<get_all_attributes> method.
469 =head2 has_attribute Name -> Bool
471 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
473 =head2 get_attribute Name -> Mouse::Meta::Attribute | undef
475 Returns the L<Mouse::Meta::Attribute> with the given name.
477 =head2 linearized_isa -> [ClassNames]
479 Returns the list of classes in method dispatch order, with duplicates removed.
481 =head2 clone_object Instance -> Instance
483 Clones the given C<Instance> which must be an instance governed by this
486 =head2 clone_instance Instance, Parameters -> Instance
488 The clone_instance method has been made private.
489 The public version is deprecated.