Add test for non-alphanumeric attr names (with inlining)
[gitmo/Class-MOP.git] / lib / Class / MOP / Method / Inlined.pm
CommitLineData
29d4e92a 1package Class::MOP::Method::Inlined;
2
3use strict;
4use warnings;
5
6use Carp 'confess';
7use Scalar::Util 'blessed', 'weaken', 'looks_like_number', 'refaddr';
8
3e2c8600 9our $VERSION = '0.97_01';
29d4e92a 10$VERSION = eval $VERSION;
11our $AUTHORITY = 'cpan:STEVAN';
12
13use base 'Class::MOP::Method::Generated';
14
15sub _expected_method_class { $_[0]{_expected_method_class} }
16
17sub _uninlined_body {
18 my $self = shift;
19
c298d62e 20 my $super_method
f5d98441 21 = $self->associated_metaclass->find_next_method_by_name( $self->name )
c298d62e 22 or return;
23
24 if ( $super_method->isa(__PACKAGE__) ) {
25 return $super_method->_uninlined_body;
f5d98441 26 }
27 else {
c298d62e 28 return $super_method->body;
29d4e92a 29 }
30}
31
32sub can_be_inlined {
33 my $self = shift;
34 my $metaclass = $self->associated_metaclass;
f5d98441 35 my $class = $metaclass->name;
29d4e92a 36
46c48e08 37 # If we don't find an inherited method, this is a rather weird
38 # case where we have no method in the inheritance chain even
39 # though we're expecting one to be there
40 my $inherited_method
41 = $metaclass->find_next_method_by_name( $self->name );
42
43 if ( $inherited_method
44 && $inherited_method->isa('Class::MOP::Method::Wrapped') ) {
45 warn "Not inlining '"
46 . $self->name
47 . "' for $class since it "
48 . "has method modifiers which would be lost if it were inlined\n";
49
50 return 0;
51 }
52
c298d62e 53 my $expected_class = $self->_expected_method_class
54 or return 1;
29d4e92a 55
c298d62e 56 # if we are shadowing a method we first verify that it is
57 # compatible with the definition we are replacing it with
71e9cdeb 58 my $expected_method = $expected_class->can( $self->name );
a828455d 59
71e9cdeb 60 if ( ! $expected_method ) {
61 warn "Not inlining '"
62 . $self->name
63 . "' for $class since ${expected_class}::"
64 . $self->name
65 . " is not defined\n";
66
67 return 0;
68 }
69
70 my $actual_method = $class->can( $self->name )
71 or return 1;
72
73 # the method is what we wanted (probably Moose::Object::new)
74 return 1
75 if refaddr($expected_method) == refaddr($actual_method);
76
71e9cdeb 77 # otherwise we have to check that the actual method is an inlined
78 # version of what we're expecting
79 if ( $inherited_method->isa(__PACKAGE__) ) {
9d61c6b1 80 if ( $inherited_method->_uninlined_body
81 && refaddr( $inherited_method->_uninlined_body )
71e9cdeb 82 == refaddr($expected_method) ) {
29d4e92a 83 return 1;
84 }
71e9cdeb 85 }
86 elsif ( refaddr( $inherited_method->body )
87 == refaddr($expected_method) ) {
88 return 1;
89 }
c298d62e 90
71e9cdeb 91 my $warning
92 = "Not inlining '"
93 . $self->name
94 . "' for $class since it is not"
95 . " inheriting the default ${expected_class}::"
96 . $self->name . "\n";
a828455d 97
71e9cdeb 98 if ( $self->isa("Class::MOP::Method::Constructor") ) {
b64fd605 99
71e9cdeb 100 # FIXME kludge, refactor warning generation to a method
b64fd605 101 $warning
71e9cdeb 102 .= "If you are certain you don't need to inline your"
103 . " constructor, specify inline_constructor => 0 in your"
104 . " call to $class->meta->make_immutable\n";
105 }
b64fd605 106
71e9cdeb 107 warn $warning;
c298d62e 108
71e9cdeb 109 return 0;
29d4e92a 110}
111
c298d62e 1121;
84e329d6 113
114__END__
115
116=pod
117
118=head1 NAME
119
120Class::MOP::Method::Inlined - Method base class for methods which have been inlined
121
122=head1 DESCRIPTION
123
124This is a L<Class::MOP::Method::Generated> subclass for methods which
125can be inlined.
126
127=head1 METHODS
128
129=over 4
130
131=item B<< $metamethod->can_be_inlined >>
132
133This method returns true if the method in question can be inlined in
134the associated metaclass.
135
136If it cannot be inlined, it spits out a warning and returns false.
137
138=back
139
140=head1 AUTHORS
141
142Stevan Little E<lt>stevan@iinteractive.comE<gt>
143
144=head1 COPYRIGHT AND LICENSE
145
3e2c8600 146Copyright 2006-2010 by Infinity Interactive, Inc.
84e329d6 147
148L<http://www.iinteractive.com>
149
150This library is free software; you can redistribute it and/or modify
151it under the same terms as Perl itself.
152
153=cut
154