Test Your Promises

Debunk the myth that JavaScript is not easily testable. Whether you use Node.js, Express, MongoDB, jQuery, AngularJS, or directly manipulate the DOM, you can test-drive JavaScript. Learn the craft of writing meaningful, deterministic automated tests with Karma, Mocha, and Chai. Test asynchronous JavaScript, decouple and properly

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




12.1 Intercepting Methods Using GroovyInterceptable

Programming Groovy 2 — by Venkat Subramaniam (99 / 152)

👈 Chapter 12 Intercepting Methods Using MOP | TOC | 12.2 Intercepting Methods Using MetaClass 👉

If a Groovy object implements ​GroovyInterceptable​, then its ​invokeMethod​ is called when ​any​ of its methods are called—both existing methods and nonexistent methods. That is, ​GroovyInterceptable​’s ​invokeMethod​ hijacks all calls to the object.

If we want to perform an ​around advice​, we simply implement our logic in this method, and we’re done. However, if we want to implement the ​before​ or ​after advice​ (or both), we first implement our before/after logic, then route the call to the actual method at the appropriate time. To route the call, we’ll use the ​MetaMethod​ for the method we can obtain from the ​MetaClass​ (see Section 11.2, Querying Methods and Properties).

If a Groovy object implements the ​GroovyInterceptable​ interface, then its ​invokeMethod​ is called for ​all​ its method calls. For other Groovy objects, it is called only for methods that are nonexistent at call time. The exception to this is if we implement ​invokeMethod​ on an object’s ​MetaClass​. In that case, it is called always for both types of methods.

Suppose we want to run filters — such as validation, login verification, logging, and so on — before we run some methods of a class. We don’t want to manually edit each method to call the filters, because such effort is redundant, tedious, and error-prone. We don’t want to ask callers of our methods to invoke the filters, either, because there’s no guarantee they’ll call. Intercepting method calls to apply the filters is a good option. It’ll be seamless and automatic.

We use ​System.out.println()​ instead of ​println()​ in the examples in this chapter to avoid the interception of informational print messages. Whereas ​println()​ is a Groovy-injected method in ​Object​, calls to which the code we write will intercept, ​System.out.println()​ is a ​static​ method on the ​PrintStream​ class that’s not affected by our interceptions.

Let’s look at an example in which we want to run a filter method ​check​ on a ​Car​ before any other method is executed. Here’s the code that uses ​GroovyInterceptable​ to achieve this:

Add a comment

Related posts:

Chapter 9 Writing Your Own Methods

Printed in full color.For this new edition of the best-selling Learn to Program, Chris Pine has taken a good thing and made it even better. First, he used the feedback from hundreds of reader e-mails to update the content and make it even clearer. Second, he updated the examples in the book to use the latest stable version of R

Technology Should Be Invisible

As a Technologist at heart, this is a concept that I wrestle with every day. I love technology…wading into the the details of how, when, where, why, etc is exciting to me. But what I have come to…