From: Jesse Luehrs Date: Sun, 26 Sep 2010 11:33:01 +0000 (-0500) Subject: make sure accessors are properly removed on reinitialize X-Git-Tag: 1.09~30 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b26fab6dadbb855db6c465b976dd965088499b65;p=gitmo%2FClass-MOP.git make sure accessors are properly removed on reinitialize --- diff --git a/lib/Class/MOP/Class.pm b/lib/Class/MOP/Class.pm index 24621c8..511a499 100644 --- a/lib/Class/MOP/Class.pm +++ b/lib/Class/MOP/Class.pm @@ -53,9 +53,11 @@ sub reinitialize { my $old_metaclass = blessed($options{package}) ? $options{package} : Class::MOP::get_metaclass_by_name($options{package}); + $old_metaclass->_remove_generated_metaobjects + if $old_metaclass && $old_metaclass->isa('Class::MOP::Class'); my $new_metaclass = $class->SUPER::reinitialize(@args); $new_metaclass->_restore_metaobjects_from($old_metaclass) - if $old_metaclass; + if $old_metaclass && $old_metaclass->isa('Class::MOP::Class'); return $new_metaclass; } @@ -485,6 +487,14 @@ sub _restore_metaobjects_from { } } +sub _remove_generated_metaobjects { + my $self = shift; + + for my $attr (map { $self->get_attribute($_) } $self->get_attribute_list) { + $attr->remove_accessors; + } +} + ## ANON classes { diff --git a/t/010_self_introspection.t b/t/010_self_introspection.t index e3e9c1b..a8de140 100644 --- a/t/010_self_introspection.t +++ b/t/010_self_introspection.t @@ -83,6 +83,7 @@ my @class_mop_class_methods = qw( _get_compatible_single_metaclass_by_subclassing _get_compatible_single_metaclass _make_metaobject_compatible + _remove_generated_metaobjects _restore_metaobjects_from add_meta_instance_dependencies remove_meta_instance_dependencies update_meta_instance_dependencies diff --git a/t/049_metaclass_reinitialize.t b/t/049_metaclass_reinitialize.t index 6861b04..3a56567 100644 --- a/t/049_metaclass_reinitialize.t +++ b/t/049_metaclass_reinitialize.t @@ -140,4 +140,30 @@ throws_ok { ); } qr/compatible/; +{ + package Quuux::Meta::Attribute; + use base 'Class::MOP::Attribute'; + + sub install_accessors {} +} + +{ + package Quuux; + use metaclass; + sub foo {} + Quuux->meta->add_attribute('bar', reader => 'bar'); +} + +$meta = Quuux->meta; +check_meta_sanity($meta, 'Quuux'); +ok($meta->has_method('bar')); +lives_ok { + $meta = $meta->reinitialize( + 'Quuux', + attribute_metaclass => 'Quuux::Meta::Attribute', + ); +}; +check_meta_sanity($meta, 'Quuux'); +ok(!$meta->has_method('bar')); + done_testing;