diff options
author | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2006-08-25 18:41:32 +0000 |
---|---|---|
committer | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2006-08-25 18:41:32 +0000 |
commit | e496b412604f8b19a84a1b34e01effccf4992bdd (patch) | |
tree | eb2ba7f76e22b74492413b6543af26a58d746ad4 | |
parent | 816bdec6e44d43111d7cd686980f417954adb2b2 (diff) | |
download | javassist-e496b412604f8b19a84a1b34e01effccf4992bdd.tar.gz javassist-e496b412604f8b19a84a1b34e01effccf4992bdd.zip |
extended CodeConverter.
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@318 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
-rw-r--r-- | src/main/javassist/CodeConverter.java | 35 | ||||
-rw-r--r-- | src/main/javassist/convert/TransformCall.java | 18 |
2 files changed, 46 insertions, 7 deletions
diff --git a/src/main/javassist/CodeConverter.java b/src/main/javassist/CodeConverter.java index b4c47676..d8a645ff 100644 --- a/src/main/javassist/CodeConverter.java +++ b/src/main/javassist/CodeConverter.java @@ -204,7 +204,7 @@ public class CodeConverter { /** * Modify method invocations in a method body so that a different - * method is invoked. + * method will be invoked. * * <p>Note that the target object, the parameters, or * the type of invocation @@ -226,11 +226,44 @@ public class CodeConverter { if (!d1.equals(d2)) throw new CannotCompileException("signature mismatch"); + int mod1 = origMethod.getModifiers(); + int mod2 = substMethod.getModifiers(); + if (Modifier.isPrivate(mod1) != Modifier.isPrivate(mod2) + || Modifier.isStatic(mod1) != Modifier.isStatic(mod2) + || origMethod.getDeclaringClass().isInterface() + != substMethod.getDeclaringClass().isInterface()) + throw new CannotCompileException("invoke-type mismatch"); + transformers = new TransformCall(transformers, origMethod, substMethod); } /** + * Correct invocations to a method that has been renamed. + * If a method is renamed, calls to that method must be also + * modified so that the method with the new name will be called. + * + * <p>The method must be declared in the same class before and + * after it is renamed. + * + * <p>Note that the target object, the parameters, or + * the type of invocation + * (static method call, interface call, or private method call) + * are not modified. Only the method name is changed. + * + * @param oldMethodName the old name of the method. + * @param newMethod the method with the new name. + * @see javassist.CtMethod#setName(String) + */ + public void redirectMethodCall(String oldMethodName, + CtMethod newMethod) + throws CannotCompileException + { + transformers + = new TransformCall(transformers, oldMethodName, newMethod); + } + + /** * Insert a call to another method before an existing method call. * That "before" method must be static. The return type must be * <code>void</code>. As parameters, the before method receives diff --git a/src/main/javassist/convert/TransformCall.java b/src/main/javassist/convert/TransformCall.java index c9f62488..43508c2f 100644 --- a/src/main/javassist/convert/TransformCall.java +++ b/src/main/javassist/convert/TransformCall.java @@ -30,13 +30,19 @@ public class TransformCall extends Transformer { public TransformCall(Transformer next, CtMethod origMethod, CtMethod substMethod) { + this(next, origMethod.getName(), substMethod); + classname = origMethod.getDeclaringClass().getName(); + } + + public TransformCall(Transformer next, String oldMethodName, + CtMethod substMethod) + { super(next); - this.classname = origMethod.getDeclaringClass().getName(); - this.methodname = origMethod.getName(); - this.methodDescriptor = origMethod.getMethodInfo2().getDescriptor(); - this.newClassname = substMethod.getDeclaringClass().getName(); - this.newMethodname = substMethod.getName(); - this.constPool = null; + methodname = oldMethodName; + methodDescriptor = substMethod.getMethodInfo2().getDescriptor(); + classname = newClassname = substMethod.getDeclaringClass().getName(); + newMethodname = substMethod.getName(); + constPool = null; } public void initialize(ConstPool cp, CodeAttribute attr) { |