/** * Creates a copy of a method with a new name. * This method is provided for creating * a new method based on an existing method. * This is a convenience method for calling * {@link CtMethod#CtMethod(CtMethod, CtClass, ClassMap) this constructor}. * See the description of the constructor for particular behavior of the copying. * * @param src the source method. * @param name the name of the created method. * @param declaring the class to which the created method is added. * @param map the hash table associating original class names * with substituted names. * It can be <code>null</code>. * * @see CtMethod#CtMethod(CtMethod,CtClass,ClassMap) */ public static CtMethod copy(CtMethod src, String name, CtClass declaring, ClassMap map) throws CannotCompileException { CtMethod cm = new CtMethod(src, declaring, map); cm.setName(name); return cm; }
/** * Creates a copy of a method with a new name. * This method is provided for creating * a new method based on an existing method. * This is a convenience method for calling * {@link CtMethod#CtMethod(CtMethod, CtClass, ClassMap) this constructor}. * See the description of the constructor for particular behavior of the copying. * * @param src the source method. * @param name the name of the created method. * @param declaring the class to which the created method is added. * @param map the hash table associating original class names * with substituted names. * It can be <code>null</code>. * * @see CtMethod#CtMethod(CtMethod,CtClass,ClassMap) */ public static CtMethod copy(CtMethod src, String name, CtClass declaring, ClassMap map) throws CannotCompileException { CtMethod cm = new CtMethod(src, declaring, map); cm.setName(name); return cm; }
originalMethod.setName(renameMethodPrefix + method.getName()); } else { clazz.removeMethod(originalMethod);
m2.setName(ClassMetaobject.methodPrefix + identifier + "_" + name);
method.setName(name); return method;
m2.setName(ClassMetaobject.methodPrefix + identifier + "_" + name);
method.setName(name); return method;
public void setName(String name) { ((CtMethod)ctBehavior).setName(name); typeInfo.clearMethodCache(); }
/** * Creates a copy of a method with a new name. * This method is provided for creating * a new method based on an existing method. * * @param src the source method. * @param name the name of the created method. * @param declaring the class to which the created method is added. * @param map the hashtable associating original class names * with substituted names. * It can be <code>null</code>. * * @see CtMethod#CtMethod(CtMethod,CtClass,ClassMap) */ public static CtMethod copy(CtMethod src, String name, CtClass declaring, ClassMap map) throws CannotCompileException { CtMethod cm = new CtMethod(src, declaring, map); cm.setName(name); return cm; }
/** * Creates a copy of a method with a new name. * This method is provided for creating * a new method based on an existing method. * This is a convenience method for calling * {@link CtMethod#CtMethod(CtMethod, CtClass, ClassMap) this constructor}. * See the description of the constructor for particular behavior of the copying. * * @param src the source method. * @param name the name of the created method. * @param declaring the class to which the created method is added. * @param map the hash table associating original class names * with substituted names. * It can be <code>null</code>. * * @see CtMethod#CtMethod(CtMethod,CtClass,ClassMap) */ public static CtMethod copy(CtMethod src, String name, CtClass declaring, ClassMap map) throws CannotCompileException { CtMethod cm = new CtMethod(src, declaring, map); cm.setName(name); return cm; }
/** * Creates a copy of a method with a new name. * This method is provided for creating * a new method based on an existing method. * This is a convenience method for calling * {@link CtMethod#CtMethod(CtMethod, CtClass, ClassMap) this constructor}. * See the description of the constructor for particular behavior of the copying. * * @param src the source method. * @param name the name of the created method. * @param declaring the class to which the created method is added. * @param map the hash table associating original class names * with substituted names. * It can be <code>null</code>. * * @see CtMethod#CtMethod(CtMethod,CtClass,ClassMap) */ public static CtMethod copy(CtMethod src, String name, CtClass declaring, ClassMap map) throws CannotCompileException { CtMethod cm = new CtMethod(src, declaring, map); cm.setName(name); return cm; }
/** * Creates a copy of a method with a new name. * This method is provided for creating * a new method based on an existing method. * This is a convenience method for calling * {@link CtMethod#CtMethod(CtMethod, CtClass, ClassMap) this constructor}. * See the description of the constructor for particular behavior of the copying. * * @param src the source method. * @param name the name of the created method. * @param declaring the class to which the created method is added. * @param map the hash table associating original class names * with substituted names. * It can be <code>null</code>. * * @see CtMethod#CtMethod(CtMethod,CtClass,ClassMap) */ public static CtMethod copy(CtMethod src, String name, CtClass declaring, ClassMap map) throws CannotCompileException { CtMethod cm = new CtMethod(src, declaring, map); cm.setName(name); return cm; }
/** * Creates a copy of a method with a new name. * This method is provided for creating * a new method based on an existing method. * This is a convenience method for calling * {@link CtMethod#CtMethod(CtMethod, CtClass, ClassMap) this constructor}. * See the description of the constructor for particular behavior of the copying. * * @param src the source method. * @param name the name of the created method. * @param declaring the class to which the created method is added. * @param map the hash table associating original class names * with substituted names. * It can be <code>null</code>. * * @see CtMethod#CtMethod(CtMethod,CtClass,ClassMap) */ public static CtMethod copy(CtMethod src, String name, CtClass declaring, ClassMap map) throws CannotCompileException { CtMethod cm = new CtMethod(src, declaring, map); cm.setName(name); return cm; }
import java.util.Arrays; import javassist.ClassPool; import javassist.CtClass; import javassist.CtMethod; public class Main { public static void main(String[] args) throws Exception { ClassPool pool = ClassPool.getDefault(); CtClass ctClass = pool.get("ExtendedArrayList"); CtClass[] sortParams = new CtClass[]{ pool.get("java.util.Comparator")}; CtMethod sortMethod = ctClass.getDeclaredMethod("sort", sortParams); sortMethod.setName("sortV7"); // rename ctClass.toClass(); ExtendedArrayList<String> arrayList = new ExtendedArrayList<String>(); arrayList.addAll(Arrays.asList("z", "y", "x")); System.err.println(arrayList); // print [z, y, x] arrayList.sort(String::compareTo); System.err.println(arrayList); // print [x, y, z] } }
CtClass clas = ClassPool.getDefault().get("classFullName"); CtMethod mold = clas.getMethod("MethodName", "MethodParameters"); //Rename the original method name String nname = mname+"$impl"; mold.setName(nname); CtMethod mnew = CtNewMethod.copy(mold, mname, clas, null); String bodyText = "New Method body"; StringBuffer body = new StringBuffer(); body.append(bodyText); //Replace the body of the intercepter method with generated code block and add it to class. mnew.setBody(body.toString()); clas.addMethod(mnew); clas.writeFile(); clas.toClass();
private static void declare5(CtClass clazz, CtMethod method) throws NotFoundException, CannotCompileException { method.setName("wrappedDefineClass"); CtMethod wrapper = CtNewMethod.make(Modifier.PROTECTED, method.getReturnType(), "defineClass", method.getParameterTypes(), method.getExceptionTypes(), null, clazz); String code = "{" + " byte[] newBytes = org.jboss.aop.hook.JDK14TransformerManager.transform($0, $1, $2) ;" + " if (newBytes != (byte[])null) {" + " return wrappedDefineClass($1, newBytes, 0, newBytes.length, $5); " + " } else {" + " return wrappedDefineClass($1, $2, $3, $4, $5); " + " }" + "}"; wrapper.setBody(code); clazz.addMethod(wrapper); }
public boolean instrumentMethod(CtClass clazz, CtMethod method) throws InterceptorException { try { String mname = method.getName(); // rename old method to synthetic name, then duplicate the // method with original name for use as interceptor String nname = mname + oldMethodSufix; method.setName(nname); CtMethod mnew = CtNewMethod.copy(method, mname, clazz, null); // replace the body of the interceptor method with generated // code block and add it to class mnew.setBody(StringUtil.replaceAll(code, "$origMethodName", nname)); clazz.addMethod(mnew); } catch (Exception e) { throw new InterceptorException("Failed to replace method body " + method.getName(), e); } return true; }
private void letSuperclassMethodsOverride(final CtClass clazz) throws CannotCompileException, NotFoundException { for (final CtMethod method : clazz.getSuperclass().getDeclaredMethods()) { final CtMethod method2 = clazz.getMethod(method.getName(), method.getSignature()); if (method2.getDeclaringClass().equals(clazz)) { // make sure no calls/accesses to GUI components are remaining method2.setBody(method, null); method2.setName("narf" + method.getName()); } } }
private void letSuperclassMethodsOverride(final CtClass clazz) throws CannotCompileException, NotFoundException { for (final CtMethod method : clazz.getSuperclass().getDeclaredMethods()) { final CtMethod method2 = clazz.getMethod(method.getName(), method.getSignature()); if (method2.getDeclaringClass().equals(clazz)) { // make sure no calls/accesses to GUI components are remaining method2.setBody(method, null); method2.setName("narf" + method.getName()); } } }
protected void transformMethod(MethodTransformation trans, boolean wrap) throws NotFoundException, CannotCompileException { String methodInfoField = addMethodInfoField(Modifier.PRIVATE | Modifier.STATIC, trans.getClazz(), trans); // generate Wrapper String wrappedName = ClassAdvisor.notAdvisedMethodName(trans.getClazzName(), trans.getMethod().getName()); CtMethod wmethod = CtNewMethod.copy(trans.getMethod(), trans.getClazz(), null); String originalName = trans.getOriginalName(); wmethod.setName(wrappedName); trans.getClazz().addMethod(wmethod); moveAnnotationsAndCopySignature(trans.getMethod(), wmethod); trans.getMethod().setName(wrappedName); Instrumentor.addSyntheticAttribute(trans.getMethod()); wmethod.setName(originalName); trans.setWMethod(wmethod, wrappedName); // prepareForWrapping getWrapper().prepareForWrapping(wmethod, WrapperTransformer.SINGLE_TRANSFORMATION_INDEX); if (wrap) { // wrap getWrapper().wrap(wmethod, WrapperTransformer.SINGLE_TRANSFORMATION_INDEX); // executeWrapping setWrapperBody(trans, methodInfoField); } }