WWII Allied Bombing Data Analysis

This is an overview of some descriptive analytics of the THOR WWII dataset. Theater History of Operations (THOR), is a cultivated database of historic aerial bombings from World War II. It’s goal is…

Smartphone

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




15.3 Review of MOP Techniques

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

👈 15.2 Method Delegation: Putting It All Together | TOC | Chapter 16 Applying Compile-Time Metaprogramming 👉

We’ve seen a number of options to intercept, inject, and synthesize methods. In this section, we’ll figure out which option is right for us.

We discussed method interception in Chapter 12, Intercepting Methods Using MOP, and Section 13.1, Injecting Methods Using Categories. We can use ​GroovyInterceptable​, ​ExpandoMetaClass​, or categories.

If we have the privileges to modify the class source, we can implement ​GroovyInterceptable​ on the class we want to intercept method calls. The effort is as simple as implementing ​invokeMethod​ .

If we can’t modify the class or if the class is a Java class, then we can use ​ExpandoMetaClass​ or categories. ​ExpandoMetaClass​ clearly stands out in this case because a single ​invokeMethod​ can take care of intercepting any methods of our class. Categories, on the other hand, would require separate methods, one per intercepted method. Also, if we use categories we’re restricted by the ​use​ block.

We discussed method injection in Section 13.1, Injecting Methods Using Categories. We can use categories or ​ExpandoMetaClass​.

Categories compete well with ​ExpandoMetaClasses​ for method injection. If we use categories, we can control the location where methods are injected. We can easily implement different versions of method injection by using different categories. We can easily nest and mix multiple categories, as well. The control that categories offer—that method injection takes effect only within the ​use​ blocks and is limited to the executing thread—may also be considered a restriction. If we want to use the injected methods at any location and also want to inject static methods and constructors, ​ExpandoMetaClass​ is a better choice. Beware, though, that ​ExpandoMetaClass​ is not the default ​MetaClass​ in Groovy.

Using the ​ExpandoMetaClass​, we can inject methods into specific instances of a class instead of affecting the entire class.

Add a comment

Related posts:

Your Brand Voice sounds different?

Customer Relationship is often misinterpreted by managers, leading towards misconduct of relationship, and eventually losing them. The traditional form of Customer Relationship management often fails…

Leveraging the Laziness of Streams

Lambda expressions are lightweight, highly concise anonymous methods backed by functional interfaces in Java 8. You can use them to leap forward into a whole new world of programming in Java. With functional programming capabilities, which have been around for decades in other languages, you can now write elegant, concise, less

BADSHAHI MOSQUE

Badshahi mosque is one of the few significant architectural monuments built during Emperor Aurangzeb’s long rule from 1658 to 1707. It is presently the fifth largest mosque in the world and was…