Commit | Line | Data |
38bf2a25 |
1 | |
2 | package Class::MOP::Method::Meta; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
7 | use Carp 'confess'; |
8 | use Scalar::Util 'blessed'; |
9 | |
38bf2a25 |
10 | use constant DEBUG_NO_META => $ENV{DEBUG_NO_META} ? 1 : 0; |
11 | |
12 | use base 'Class::MOP::Method'; |
13 | |
14 | sub _is_caller_mop_internal { |
15 | my $self = shift; |
16 | my ($caller) = @_; |
17 | return $caller =~ /^(?:Class::MOP|metaclass)(?:::|$)/; |
18 | } |
19 | |
20 | sub _generate_meta_method { |
21 | my $method_self = shift; |
22 | my $metaclass = shift; |
23 | sub { |
24 | # this will be compiled out if the env var wasn't set |
25 | if (DEBUG_NO_META) { |
26 | confess "'meta' method called by MOP internals" |
27 | # it's okay to call meta methods on metaclasses, since we |
28 | # explicitly ask for them |
29 | if !$_[0]->isa('Class::MOP::Object') |
30 | && !$_[0]->isa('Class::MOP::Mixin') |
31 | # it's okay if the test itself calls ->meta, we only care about |
32 | # if the mop internals call ->meta |
33 | && $method_self->_is_caller_mop_internal(scalar caller); |
34 | } |
35 | # we must re-initialize so that it |
36 | # works as expected in subclasses, |
37 | # since metaclass instances are |
38 | # singletons, this is not really a |
39 | # big deal anyway. |
40 | $metaclass->initialize(blessed($_[0]) || $_[0]) |
41 | }; |
42 | } |
43 | |
44 | sub wrap { |
45 | my ($class, @args) = @_; |
46 | |
47 | unshift @args, 'body' if @args % 2 == 1; |
48 | my %params = @args; |
49 | confess "Overriding the body of meta methods is not allowed" |
50 | if $params{body}; |
51 | |
52 | my $metaclass_class = $params{associated_metaclass}->meta; |
53 | $params{body} = $class->_generate_meta_method($metaclass_class); |
54 | return $class->SUPER::wrap(%params); |
55 | } |
56 | |
57 | sub _make_compatible_with { |
58 | my $self = shift; |
59 | my ($other) = @_; |
60 | |
61 | # XXX: this is pretty gross. the issue here is that CMOP::Method::Meta |
62 | # objects are subclasses of CMOP::Method, but when we get to moose, they'll |
63 | # need to be compatible with Moose::Meta::Method, which isn't possible. the |
64 | # right solution here is to make ::Meta into a role that gets applied to |
65 | # whatever the method_metaclass happens to be and get rid of |
66 | # _meta_method_metaclass entirely, but that's not going to happen until |
67 | # we ditch cmop and get roles into the bootstrapping, so. i'm not |
68 | # maintaining the previous behavior of turning them into instances of the |
69 | # new method_metaclass because that's equally broken, and at least this way |
70 | # any issues will at least be detectable and potentially fixable. -doy |
71 | return $self unless $other->_is_compatible_with($self->_real_ref_name); |
72 | |
73 | return $self->SUPER::_make_compatible_with(@_); |
74 | } |
75 | |
76 | 1; |
77 | |
78 | # ABSTRACT: Method Meta Object for C<meta> methods |
79 | |
80 | __END__ |
81 | |
82 | =pod |
83 | |
84 | =head1 DESCRIPTION |
85 | |
86 | This is a L<Class::MOP::Method> subclass which represents C<meta> |
87 | methods installed into classes by Class::MOP. |
88 | |
89 | =head1 METHODS |
90 | |
91 | =over 4 |
92 | |
93 | =item B<< Class::MOP::Method::Wrapped->wrap($metamethod, %options) >> |
94 | |
95 | This is the constructor. It accepts a L<Class::MOP::Method> object and |
96 | a hash of options. The options accepted are identical to the ones |
97 | accepted by L<Class::MOP::Method>, except that C<body> cannot be passed |
98 | (it will be generated automatically). |
99 | |
100 | =back |
101 | |
102 | =cut |
103 | |