@Test public void testFindMethod() { final CtClass ctclass = ClassPool.getDefault().makeClass("testFindMethodClass"); addNewMethod(ctclass, "method1"); addNewMethod(ctclass, "method2"); Method m = null; try { m = FindMethodTest.class.getMethod("method1"); } catch (NoSuchMethodException | SecurityException e1) { e1.printStackTrace(); return; } try { final CtMethod foundMethod = JavassistUtils.findMethod(ctclass, m); Assert.assertEquals("method1", foundMethod.getName()); } catch (final NotFoundException e) { e.printStackTrace(); fail("Could not find method in CtClass"); } }
public static CtMethod findMethod(final CtClass clz, final Method m) throws NotFoundException { final ClassPool pool = ClassPool.getDefault(); final Class<?>[] paramTypes = m.getParameterTypes(); final List<CtClass> paramTypesCtClass = new ArrayList<>(); for (final Class<?> claz : paramTypes) { paramTypesCtClass.add(pool.get(claz.getName())); } final String desc = Descriptor.ofMethod( pool.get(m.getReturnType().getName()), paramTypesCtClass.toArray(new CtClass[] {})); final CtMethod method = clz.getMethod(m.getName(), desc); return method; }
JavassistUtils { public static CtMethod findMethod(final CtClass clz, final Method m) throws NotFoundException { final ClassPool pool = ClassPool.getDefault(); final Class<?>[] paramTypes = m.getParameterTypes(); final List<CtClass> paramTypesCtClass = new ArrayList<>(); for (final Class<?> claz : paramTypes) { paramTypesCtClass.add(pool.get(claz.getName())); } final String desc = Descriptor.ofMethod( pool.get(m.getReturnType().getName()), paramTypesCtClass.toArray(new CtClass[] {})); final CtMethod method = clz.getMethod(m.getName(), desc); return method; } }
JavassistUtils { public static CtMethod findMethod(final CtClass clz, final Method m) throws NotFoundException { final ClassPool pool = ClassPool.getDefault(); final Class<?>[] paramTypes = m.getParameterTypes(); final List<CtClass> paramTypesCtClass = new ArrayList<>(); for (final Class<?> claz : paramTypes) { paramTypesCtClass.add(pool.get(claz.getName())); } final String desc = Descriptor.ofMethod( pool.get(m.getReturnType().getName()), paramTypesCtClass.toArray(new CtClass[] {})); final CtMethod method = clz.getMethod(m.getName(), desc); return method; } private  JavassistUtils(); }
JavassistUtils { public static CtMethod findMethod(final CtClass clz, final Method m) throws NotFoundException { final ClassPool pool = ClassPool.getDefault(); final Class<?>[] paramTypes = m.getParameterTypes(); final List<CtClass> paramTypesCtClass = new ArrayList<>(); for (final Class<?> claz : paramTypes) { paramTypesCtClass.add(pool.get(claz.getName())); } final String desc = Descriptor.ofMethod( pool.get(m.getReturnType().getName()), paramTypesCtClass.toArray(new CtClass[] {})); final CtMethod method = clz.getMethod(m.getName(), desc); return method; } private  JavassistUtils(); static AnnotationsAttribute cloneAnnotationsAttribute(
      final ConstPool constPool,
      final AnnotationsAttribute attr,
      final ElementType validElementType); static CtMethod findMethod(final CtClass clz, final Method m); static void copyClassAnnotations(final CtClass oldClass, final CtClass newClass); static void copyMethodAnnotationsToField(final CtMethod method, final CtField field); static String getNextUniqueClassName(); static String getNextUniqueFieldName(); static CtClass generateEmptyClass(); }
JavassistUtils { public static CtMethod findMethod(final CtClass clz, final Method m) throws NotFoundException { final ClassPool pool = ClassPool.getDefault(); final Class<?>[] paramTypes = m.getParameterTypes(); final List<CtClass> paramTypesCtClass = new ArrayList<>(); for (final Class<?> claz : paramTypes) { paramTypesCtClass.add(pool.get(claz.getName())); } final String desc = Descriptor.ofMethod( pool.get(m.getReturnType().getName()), paramTypesCtClass.toArray(new CtClass[] {})); final CtMethod method = clz.getMethod(m.getName(), desc); return method; } private  JavassistUtils(); static AnnotationsAttribute cloneAnnotationsAttribute(
      final ConstPool constPool,
      final AnnotationsAttribute attr,
      final ElementType validElementType); static CtMethod findMethod(final CtClass clz, final Method m); static void copyClassAnnotations(final CtClass oldClass, final CtClass newClass); static void copyMethodAnnotationsToField(final CtMethod method, final CtField field); static String getNextUniqueClassName(); static String getNextUniqueFieldName(); static CtClass generateEmptyClass(); static final String PREFIX_PACKAGE; }