File Coverage

blib/lib/Class/MOP/Method/Inlined.pm
Criterion Covered Total %
statement 38 40 95.0
branch 18 22 81.8
condition 4 6 66.6
subroutine 6 6 100.0
pod 1 1 100.0
total 67 75 89.3


line stmt bran cond sub pod time code
1             package Class::MOP::Method::Inlined;
2             our $VERSION = '2.2203';
3              
4 462     462   193431 use strict;
  462         983  
  462         12087  
5 462     462   2047 use warnings;
  462         834  
  462         10695  
6              
7 462     462   2206 use Scalar::Util 'refaddr';
  462         904  
  462         19119  
8              
9 462     462   2481 use parent 'Class::MOP::Method::Generated';
  462         1128  
  462         2490  
10              
11             sub _uninlined_body {
12 892     892   1556 my $self = shift;
13              
14 892 50       2342 my $super_method
15             = $self->associated_metaclass->find_next_method_by_name( $self->name )
16             or return;
17              
18 892 50       4090 if ( $super_method->isa(__PACKAGE__) ) {
19 0         0 return $super_method->_uninlined_body;
20             }
21             else {
22 892         7000 return $super_method->body;
23             }
24             }
25              
26             sub can_be_inlined {
27 13331     13331 1 24085 my $self = shift;
28 13331         29728 my $metaclass = $self->associated_metaclass;
29 13331         45202 my $class = $metaclass->name;
30              
31             # If we don't find an inherited method, this is a rather weird
32             # case where we have no method in the inheritance chain even
33             # though we're expecting one to be there
34 13331         46420 my $inherited_method
35             = $metaclass->find_next_method_by_name( $self->name );
36              
37 13331 100 100     31128 if ( $inherited_method
38             && $inherited_method->isa('Class::MOP::Method::Wrapped') ) {
39 4         130 warn "Not inlining '"
40             . $self->name
41             . "' for $class since it "
42             . "has method modifiers which would be lost if it were inlined\n";
43              
44 4         190 return 0;
45             }
46              
47 13327 100       75384 my $expected_class = $self->_expected_method_class
48             or return 1;
49              
50             # if we are shadowing a method we first verify that it is
51             # compatible with the definition we are replacing it with
52 761         6339 my $expected_method = $expected_class->can( $self->name );
53              
54 761 100       2587 if ( ! $expected_method ) {
55 1         64 warn "Not inlining '"
56             . $self->name
57             . "' for $class since ${expected_class}::"
58             . $self->name
59             . " is not defined\n";
60              
61 1         33 return 0;
62             }
63              
64 760 100       8269 my $actual_method = $class->can( $self->name )
65             or return 1;
66              
67             # the method is what we wanted (probably Moose::Object::new)
68 757 100       5856 return 1
69             if refaddr($expected_method) == refaddr($actual_method);
70              
71             # otherwise we have to check that the actual method is an inlined
72             # version of what we're expecting
73 449 100       2142 if ( $inherited_method->isa(__PACKAGE__) ) {
    50          
74 446 50 33     2727 if ( $inherited_method->_uninlined_body
75             && refaddr( $inherited_method->_uninlined_body )
76             == refaddr($expected_method) ) {
77 446         2395 return 1;
78             }
79             }
80             elsif ( refaddr( $inherited_method->body )
81             == refaddr($expected_method) ) {
82 0         0 return 1;
83             }
84              
85 3         25 my $warning
86             = "Not inlining '"
87             . $self->name
88             . "' for $class since it is not"
89             . " inheriting the default ${expected_class}::"
90             . $self->name . "\n";
91              
92 3 100       13 if ( $self->isa("Class::MOP::Method::Constructor") ) {
93              
94             # FIXME kludge, refactor warning generation to a method
95 2         10 $warning
96             .= "If you are certain you don't need to inline your"
97             . " constructor, specify inline_constructor => 0 in your"
98             . " call to $class->meta->make_immutable\n";
99             }
100              
101 3         113 warn $warning;
102              
103 3         103 return 0;
104             }
105              
106             1;
107              
108             # ABSTRACT: Method base class for methods which have been inlined
109              
110             __END__
111              
112             =pod
113              
114             =encoding UTF-8
115              
116             =head1 NAME
117              
118             Class::MOP::Method::Inlined - Method base class for methods which have been inlined
119              
120             =head1 VERSION
121              
122             version 2.2203
123              
124             =head1 DESCRIPTION
125              
126             This is a L<Class::MOP::Method::Generated> subclass for methods which
127             can be inlined.
128              
129             =head1 METHODS
130              
131             =head2 $metamethod->can_be_inlined
132              
133             This method returns true if the method in question can be inlined in
134             the associated metaclass.
135              
136             If it cannot be inlined, it spits out a warning and returns false.
137              
138             =head1 AUTHORS
139              
140             =over 4
141              
142             =item *
143              
144             Stevan Little <stevan@cpan.org>
145              
146             =item *
147              
148             Dave Rolsky <autarch@urth.org>
149              
150             =item *
151              
152             Jesse Luehrs <doy@cpan.org>
153              
154             =item *
155              
156             Shawn M Moore <sartak@cpan.org>
157              
158             =item *
159              
160             יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
161              
162             =item *
163              
164             Karen Etheridge <ether@cpan.org>
165              
166             =item *
167              
168             Florian Ragwitz <rafl@debian.org>
169              
170             =item *
171              
172             Hans Dieter Pearcey <hdp@cpan.org>
173              
174             =item *
175              
176             Chris Prather <chris@prather.org>
177              
178             =item *
179              
180             Matt S Trout <mstrout@cpan.org>
181              
182             =back
183              
184             =head1 COPYRIGHT AND LICENSE
185              
186             This software is copyright (c) 2006 by Infinity Interactive, Inc.
187              
188             This is free software; you can redistribute it and/or modify it under
189             the same terms as the Perl 5 programming language system itself.
190              
191             =cut