make sure accessors are properly removed on reinitialize
Jesse Luehrs [Sun, 26 Sep 2010 11:33:01 +0000 (06:33 -0500)]
lib/Class/MOP/Class.pm
t/010_self_introspection.t
t/049_metaclass_reinitialize.t

index 24621c8..511a499 100644 (file)
@@ -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
 
 {
index e3e9c1b..a8de140 100644 (file)
@@ -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
index 6861b04..3a56567 100644 (file)
@@ -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;