More perltidying
[gitmo/Class-MOP.git] / lib / Class / MOP / Method / Inlined.pm
1 package Class::MOP::Method::Inlined;
2
3 use strict;
4 use warnings;
5
6 use Carp         'confess';
7 use Scalar::Util 'blessed', 'weaken', 'looks_like_number', 'refaddr';
8
9 our $VERSION   = '0.81';
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
12
13 use base 'Class::MOP::Method::Generated';
14
15 sub _expected_method_class { $_[0]{_expected_method_class} }
16
17 sub _uninlined_body {
18     my $self = shift;
19
20     if ( my $super_method = $self->associated_metaclass->find_next_method_by_name( $self->name ) ) {
21         if ( $super_method->isa(__PACKAGE__) ) {
22             return $super_method->_uninlined_body;
23         } else {
24             return $super_method->body;
25         }
26     } else {
27         return;
28     }
29 }
30
31 sub can_be_inlined {
32     my $self      = shift;
33     my $metaclass = $self->associated_metaclass;
34     my $class = $metaclass->name;
35
36     if ( my $expected_class = $self->_expected_method_class ) {
37
38         # if we are shadowing a method we first verify that it is
39         # compatible with the definition we are replacing it with
40         my $expected_method = $expected_class->can($self->name);
41
42         my $warning
43             = "Not inlining '" . $self->name . "' for $class since it is not"
44             . " inheriting the default ${expected_class}::" . $self->name . "\n"
45             . "If you are certain you don't need to inline your";
46
47         if ( $self->isa("Class::MOP::Method::Constructor") ) {
48             # FIXME kludge, refactor warning generation to a method
49             $warning .= " constructor, specify inline_constructor => 0 in your"
50                      . " call to $class->meta->make_immutable\n";
51         }
52
53         if ( my $actual_method = $class->can($self->name) ) {
54             if ( refaddr($expected_method) == refaddr($actual_method) ) {
55                 # the method is what we wanted (probably Moose::Object::new)
56                 return 1;
57             } elsif ( my $inherited_method = $metaclass->find_next_method_by_name( $self->name ) ) {
58                 # otherwise we have to check that the actual method is an
59                 # inlined version of what we're expecting
60                 if ( $inherited_method->isa(__PACKAGE__) ) {
61                     if ( refaddr($inherited_method->_uninlined_body) == refaddr($expected_method) ) {
62                         return 1;
63                     }
64                 } elsif ( refaddr($inherited_method->body) == refaddr($expected_method) ) {
65                     return 1;
66                 }
67
68                 # FIXME we can just rewrap them =P
69                 $warning .= " ('" . $self->name . "' has method modifiers which would be lost if it were inlined)\n"
70                     if $inherited_method->isa('Class::MOP::Method::Wrapped');
71             }
72         } else {
73             # This would be a rather weird case where we have no method
74             # in the inheritance chain even though we're expecting one to be
75             # there
76
77             # this returns 1 for backwards compatibility for now
78             return 1;
79         }
80
81         warn $warning;
82
83         return 0;
84     } else {
85         # there is no expected class so we just install the constructor as a
86         # new method
87         return 1;
88     }
89 }
90