Testing Sanity

The core task of software development is sanity testing.

  • warning: Creating default object from empty value in /home2/thepalls/public_html/cgpsoftware/modules/taxonomy/taxonomy.pages.inc on line 34.
  • warning: Parameter 2 to gmap_gmap() expected to be a reference, value given in /home2/thepalls/public_html/cgpsoftware/includes/module.inc on line 483.
  • strict warning: Non-static method view::load() should not be called statically in /home2/thepalls/public_html/cgpsoftware/sites/all/modules/views/views.module on line 879.

dynamic proxies

Java Dynamic Proxies

20 Jan 2012
Posted by cgp

Dynamic proxies are one of those topics along with Reflection (to a lesser extent) that seem like magic in Java. They exist outside of the safe, procedural oriented nature of the language and add useful complexity and color to an otherwise dull language.

What are Dynamic Proxies exactly? Dynamic Proxies allow you to run custom code before, after, (or before and after) other pieces of code. For example:

class Blah {
  public void myMethod() { System.out.println("I ran!"); }
}

Say I wanted the above method to run before every call to Math.cos(). So, I would extend a new class from the Proxy class and tell it to intercept calls to Math.cos(). The I could tell it that instead of calling Math.cos() call my code instead. In my Proxy class, I would have the option of calling the original function or not.

Buzzword Bingo

Essentially, we have created a "logger" for Math.cos. Logging is frequently referred to as a "cross cutting concern". I point this out now to perhaps trigger recognition that one of the chief reasons to use Dynamic Proxies is to insert code around methods in an independent fashion without affecting the original code. Dynamic Proxies are basically what AOP frameworks like SpringAOP and Guice use under the hood to provide their functionality. (psst!

Typical use cases for Dynamic Proxies:

Rules and Limitations, more to follow

There are rules and limitations to doing the above, but for today, we'll leave it at that.