tailieunhanh - Programming Groovy dynamic productivity for the java developer phần 7

Chặn cuộc gọi hoặc phương pháp ban đầu có có một tài sản với tên phương pháp? không có Làm thế nào Groovy xử lý các cuộc gọi phương thức trên POGO gọi invokeMethod nó () có phương thức tồn tại trong MetaClass hoặc lớp? tài sản đó là đóng cửa loại? | 187 Figure How Groovy handles method calls on a POGO For a POJO Groovy fetches its MetaClass from the applicationwide MetaClassRegistry and delegates method invocation to it. So any interceptors or methods you ve defined on its MetaClass take precedence over the original method of the POJO. For a POGO Groovy takes a few extra steps as illustrated in Figure . If the object implements Groovylnterceptable then all calls are routed to its invokeMethod . Within this interceptor you can route calls to the actual method if you want allowing you to do AOP-like operations. If the POGO does not implement Groovylnterceptable then Groovy looks for the method first in the POGO s MetaClass and then if not found on Groovy Object 188 the pogo itself. If the pogo has no such method Groovy looks for a property or a field with the method name. If that property or field is of type closure Groovy invokes that in place of the method call. If Groovy finds no such property or field it makes two last attempts. If the pogo has a method named methodMissing it calls it. otherwise it calls the pogo s invokeMethod . If you ve implemented this method on your pogo it is used. The default implementation of invokeMethod throws a MissingMethodException indicating the failure of the call. Let s see in code the mechanism discussed earlier. I ve created classes with different options to illustrate Groovy s method handling. Study the code and see whether you can figure out which methods Groovy executes in each of the cases while walking through the following code refer to Figure on the preceding page Download ExploringMOP class TestMethodInvocation extends GroovyTestCase void testMethodCallonPOJO def val new Integer 3 assertEquals 3 void testInterceptedMethodCallonPOJO def val new Integer 3 - intercepted assertEquals intercepted void testInterceptableCalled def obj new AnInterceptable assertEquals intercepted

TỪ KHÓA LIÊN QUAN