Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #244 from shifujun/redirectMethodCallToStatic
add a useful CodeConverter method:redirectMethodCallToStatic
- Loading branch information
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package javassist.convert; | ||
|
||
import javassist.CtMethod; | ||
import javassist.bytecode.BadBytecode; | ||
import javassist.bytecode.CodeIterator; | ||
import javassist.bytecode.ConstPool; | ||
import javassist.bytecode.Descriptor; | ||
import javassist.bytecode.Opcode; | ||
|
||
public class TransformCallToStatic extends TransformCall { | ||
public TransformCallToStatic(Transformer next, CtMethod origMethod, CtMethod substMethod) { | ||
super(next, origMethod, substMethod); | ||
methodDescriptor = origMethod.getMethodInfo2().getDescriptor(); | ||
} | ||
|
||
@Override | ||
protected int match(int c, int pos, CodeIterator iterator, int typedesc, ConstPool cp) { | ||
if (newIndex == 0) { | ||
String desc = Descriptor.insertParameter(classname, methodDescriptor); | ||
int nt = cp.addNameAndTypeInfo(newMethodname, desc); | ||
int ci = cp.addClassInfo(newClassname); | ||
newIndex = cp.addMethodrefInfo(ci, nt); | ||
constPool = cp; | ||
} | ||
iterator.writeByte(Opcode.INVOKESTATIC, pos); | ||
iterator.write16bit(newIndex, pos + 1); | ||
return pos; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package test3; | ||
|
||
public class MethodRedirectToStatic { | ||
|
||
public static void main(String[] args) { | ||
System.out.println(new MethodRedirectToStatic().test()); | ||
} | ||
|
||
int add(int a, int b) { | ||
return a + b; | ||
} | ||
|
||
public int test() { | ||
return add(1, 2); | ||
} | ||
} | ||
|
||
class MethodRedirectToStatic2 { | ||
public static int add2(MethodRedirectToStatic target, int a, int b) { | ||
return target.add(a * 10, b * 10); | ||
} | ||
} |