03
Sep
11

jruby monkeypatching java methods



require "java"

a = java.util.ArrayList
a.instance_eval do
  alias :size_original :size

  def size
     puts "calling size method"
     size_original
  end
end

As simple as that, you could redefine methods in your Java code. It’s not always good practice to monkeypatch, as inheritance is as effective, but in case of one-off scripts, it could be the way to go.

Advertisements

0 Responses to “jruby monkeypatching java methods”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Blog Stats

  • 223,848 hits

%d bloggers like this: