/** * Creates a public abstract method. The created method must be added to a class with <code> * CtClass.addMethod()</code>. * * @param declaring the class to which the created method is added. * @param returnType the type of the returned value * @param mname the method name * @param parameters a list of the parameter types * @see CtClass#addMethod(CtMethod) */ public CtMethod(CtClass returnType, String mname, CtClass[] parameters, CtClass declaring) { this(null, declaring); ConstPool cp = declaring.getClassFile2().getConstPool(); String desc = Descriptor.ofMethod(returnType, parameters); methodInfo = new MethodInfo(cp, mname, desc); setModifiers(Modifier.PUBLIC | Modifier.ABSTRACT); }
/** * Replace a method body with a new method body wrapping the given method. * * @param mbody the wrapped method * @param constParam the constant parameter given to the wrapped method (maybe <code>null</code>). * @see * CtNewMethod#wrapped(CtClass,String,CtClass[],CtClass[],CtMethod,CtMethod.ConstParameter,CtClass) */ public void setWrappedBody(CtMethod mbody, ConstParameter constParam) throws CannotCompileException { declaringClass.checkModify(); CtClass clazz = getDeclaringClass(); CtClass[] params; CtClass retType; try { params = getParameterTypes(); retType = getReturnType(); } catch (NotFoundException e) { throw new CannotCompileException(e); } Bytecode code = CtNewWrappedMethod.makeBody( clazz, clazz.getClassFile2(), mbody, params, retType, constParam); CodeAttribute cattr = code.toCodeAttribute(); methodInfo.setCodeAttribute(cattr); methodInfo.setAccessFlags(methodInfo.getAccessFlags() & ~AccessFlag.ABSTRACT); // rebuilding a stack map table is not needed. }
/** * Creates a method from a <code>MethodInfo</code> object. * * @param declaring the class declaring the method. * @throws CannotCompileException if the the <code>MethodInfo</code> object and the declaring * class have different <code>ConstPool</code> objects * @since 3.6 */ public static CtMethod make(MethodInfo minfo, CtClass declaring) throws CannotCompileException { if (declaring.getClassFile2().getConstPool() != minfo.getConstPool()) throw new CannotCompileException("bad declaring class"); return new CtMethod(minfo, declaring); }