Skip to content

Commit

Permalink
cleaning up demo code
Browse files Browse the repository at this point in the history
  • Loading branch information
bookshelfdave committed Dec 13, 2011
1 parent 92bd1e7 commit e0dab29
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 13 deletions.
8 changes: 8 additions & 0 deletions demo/demo1_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#disable_hacksaw
#show_matches_enable





modify :classes=>/.*\.DemoAccount/ do |c|
c.add_field 'public boolean active = false;'
c.modify :field=>"accountNumber" do |f|
Expand All @@ -18,6 +22,10 @@
end






account = com.quadcs.hacksaw.demo.DemoAccount.new("abcd")
puts "Valid account? #{account.isValidAccount()}"
account.accountNumber = "1234"
Expand Down
19 changes: 18 additions & 1 deletion demo/demo2_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

#disable_hacksaw
#show_matches_enable





modify :classes=>/com.quadcs.hacksaw.demo.DemoAccount/ do |c|
c.add_field 'public int secret = 99;'

Expand All @@ -27,10 +32,13 @@
c.add_method '
public String generateSwissAccountNumber(int salt) {
return accountNumber + "." + secret + "." + salt;
}'
}#'
#c.save_to("hacksaw")
end




account = com.quadcs.hacksaw.demo.DemoAccount.new("abcd")
puts account.secret

Expand All @@ -40,3 +48,12 @@
account.secret=200

puts "The Swiss bank account number is : #{account.generateSwissAccountNumber(123)}"


#modify :classes=>/.*DemoAccount/ do |c|
# c.modify :method=>"isValidAccount" do |m|
# m.replace_body "return true;"
# end
#end
#account = com.quadcs.hacksaw.demo.DemoAccount.new("abcd")
#puts account.isValidAccount()
8 changes: 6 additions & 2 deletions demo/demo3_method_bodies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
#disable_hacksaw
#show_matches_enable




modify :classes=>/com.quadcs.hacksaw.demo.[FB][a-z]+/ do |c|
c.add_field 'public String mynewfield = "Goodbye world";'

c.modify :method=>/getFoo/ do |m|
m.modify_method_calls :classname=>"java.lang.String",:methodname=>"toUpperCase" do
Expand All @@ -20,8 +22,10 @@
end
end



b = com.quadcs.hacksaw.demo.Bar.new()
#puts "MyNewField = #{b.mynewfield}"

# I really wish these could be lowercase...
puts b.getFoo()
puts b.getFoobar()
7 changes: 7 additions & 0 deletions demo/demo4_bytecode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,26 @@
#
#disable_hacksaw
#show_matches_enable




modify :classes=>/com.quadcs.hacksaw.demo.DemoAccount/ do |c|
c.modify :method=>"deposit" do |m|
# actual, this just shows the opcodes for now
#m.show_bytecode

# this replaces ALL exceptions thrown with a NOP *and* a POP
# this needs some work
m.map_bytecode do |bytes,op|
op == "athrow" ? 0 : bytes
end
end
#c.save_to("hacksaw")
end



account = com.quadcs.hacksaw.demo.DemoAccount.new("abcd")
puts "Account number = #{account.getAccountNumber()}"
puts "Balance = #{account.getBalance()}"
Expand Down
14 changes: 12 additions & 2 deletions demo/demo5_ruby_in_java.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,28 @@
#
#disable_hacksaw
#show_matches_enable





modify :classes=>/com.quadcs.hacksaw.demo.DemoAccount/ do |c|
c.modify :method=>"getBetterAccountNumber" do |m|
m.add_callback_before ["$1"] do |prefix,suffix|
# needs some more work
# that 1 right there tells hacksaw that we are changing
# parameter 1 via the return value of this block.
m.add_ruby_before 1 do |prefix,suffix|
puts "Ruby in your Java!!"
puts "Prefix in Ruby=#{prefix}"
puts "Suffix in Ruby=#{suffix}"
"<<<Suffix"
end
end
end





account = com.quadcs.hacksaw.demo.DemoAccount.new("1234")
puts account.getBetterAccountNumber(">>>","<<<")
puts "Finished"
56 changes: 49 additions & 7 deletions lib/hacksaw_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,17 @@ def add_line_after(line)
getMethodActions().add(a)
end

def add_callback_before(params,&blk)
def add_ruby_before(params,&blk)
a = AddCallbackBeforeMethod.new(params,&blk)
getMethodActions().add(a)
end


def replace_body_with_ruby(params)
cmb = ReplaceMethodBodyWithCallback.new(params,&blk)
getMethodActions().add(cmb)
end

def add_line_before(line)
a = AddLineBeforeMethod.new(line)
getMethodActions().add(a)
Expand All @@ -217,7 +223,7 @@ def modify_method_calls(params,&blk)
getMethodActions().add(m)
end

def change_body(params)
def replace_body(params)
cmb = ChangeMethodBody.new(params)
getMethodActions().add(cmb)
end
Expand Down Expand Up @@ -456,17 +462,53 @@ def exec(fm)
class AddCallbackBeforeMethod
include MethodAction
attr_accessor :blk
attr_accessor :paramtomodify

def initialize(params, &blk)
@params = params
def initialize(paramtomodify, &blk)
@paramtomodify = paramtomodify
@blk = blk
end

def exec(m)
p = RProcStub.new(@params,@blk)
id = HacksawMain.registerMethodCallback(p)
#m.insertBefore("System.out.println(\"FOO\");");
m.insertBefore("com.quadcs.hacksaw.HacksawMain.getMethodCallback(#{id}).call($args);")
id = HacksawMain.registerMethodCallback(p)
ptypes = m.getParameterTypes()
cast = ptypes[@paramtomodify].getName()
m.insertBefore("$#{@paramtomodify+1} = (#{cast})com.quadcs.hacksaw.HacksawMain.getMethodCallback(#{id}).call($args);")
end
end

# class ReplaceMethodBodyWithCallback
# include MethodAction
# attr_accessor :blk
# attr_accessor :paramtomodify
#
# def initialize(paramtomodify, &blk)
# @paramtomodify = paramtomodify
# @blk = blk
# end
#
# def exec(m)
# p = RProcStub.new(@params,@blk)
# id = HacksawMain.registerMethodCallback(p)
# #m.insertBefore("System.out.println(\"FOO\");");
#
# puts "$1 = ($sig[1])com.quadcs.hacksaw.HacksawMain.getMethodCallback(#{id}).call($args);"
# m.replaceBody("$_ = (String)com.quadcs.hacksaw.HacksawMain.getMethodCallback(#{id}).call($args);")
# end
# end


class ChangeMethodBody
include MethodAction
attr_accessor :newbody
def initialize(newbody)
super()
@newbody = newbody
end

def exec(m)
m.setBody(@newbody)
end
end

Expand Down
3 changes: 2 additions & 1 deletion src/com/quadcs/hacksaw/MethodAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ public interface MethodAction {
// }


public void exec(CtMethod c);
public void exec(CtMethod c);

}

0 comments on commit e0dab29

Please sign in to comment.