This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMain.hx
61 lines (41 loc) · 1.51 KB
/
Main.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package;
class Cls {
var i:Int;
public function new (i:Int)
this.i = i;
public function objMethod (arg:Int) : Int {
trace("called original objMethod");
return arg + this.i;
}
public static function clsMethod (arg:Int) : Int {
trace("called original clsMethod");
return arg * 2;
}
}
class Main {
public static function main () {
trace("--------------------------------------------------------------");
trace("--- REWRITE CLASS METHODS AT RUNTIME ---");
var obj:Cls = new Cls(10);
trace("--------------------------------------------------------------");
trace("--- ORIGINAL METHODS ---");
trace('orig objMethod res: ${obj.objMethod(5)}');
trace('orig clsMethod res: ${Cls.clsMethod(10)}');
trace("--------------------------------------------------------------");
trace("--- MODIFIED METHOD OF OBJECT ---");
var origObjMethod:Int->Int = Reflect.field(obj, "objMethod");
Reflect.setField(obj, "objMethod", function (arg:Int) : Int {
trace("called modified objMethod");
return origObjMethod(arg) - 100;
});
trace('mod objMethod res: ${obj.objMethod(5)}');
trace("--------------------------------------------------------------");
trace("--- MODIFIED STATIC METHOD OF CLASS ---");
var origClsMethod:Int->Int = Reflect.field(Cls, "clsMethod");
Reflect.setField(Cls, "clsMethod", function (arg:Int) : Int {
trace("called modified clsMethod");
return origClsMethod(arg) - 100;
});
trace('mod clsMethod res: ${Cls.clsMethod(10)}');
}
}