Esempio n. 1
0
  private static CtMethod delegator0(CtMethod delegate, CtClass declaring)
      throws CannotCompileException, NotFoundException {
    MethodInfo deleInfo = delegate.getMethodInfo2();
    String methodName = deleInfo.getName();
    String desc = deleInfo.getDescriptor();
    ConstPool cp = declaring.getClassFile2().getConstPool();
    MethodInfo minfo = new MethodInfo(cp, methodName, desc);
    minfo.setAccessFlags(deleInfo.getAccessFlags());

    ExceptionsAttribute eattr = deleInfo.getExceptionsAttribute();
    if (eattr != null) minfo.setExceptionsAttribute((ExceptionsAttribute) eattr.copy(cp, null));

    Bytecode code = new Bytecode(cp, 0, 0);
    boolean isStatic = Modifier.isStatic(delegate.getModifiers());
    CtClass deleClass = delegate.getDeclaringClass();
    CtClass[] params = delegate.getParameterTypes();
    int s;
    if (isStatic) {
      s = code.addLoadParameters(params, 0);
      code.addInvokestatic(deleClass, methodName, desc);
    } else {
      code.addLoad(0, deleClass);
      s = code.addLoadParameters(params, 1);
      code.addInvokespecial(deleClass, methodName, desc);
    }

    code.addReturn(delegate.getReturnType());
    code.setMaxLocals(++s);
    code.setMaxStack(s < 2 ? 2 : s); // for a 2-word return value
    minfo.setCodeAttribute(code.toCodeAttribute());
    return new CtMethod(minfo, declaring);
  }
Esempio n. 2
0
 /**
  * Creates a public abstract method.
  *
  * @param returnType the type of the returned value
  * @param mname the method name
  * @param parameters a list of the parameter types
  * @param exceptions a list of the exception types
  * @param declaring the class to which the created method is added.
  * @see CtMethod#CtMethod(CtClass,String,CtClass[],CtClass)
  */
 public static CtMethod abstractMethod(
     CtClass returnType,
     String mname,
     CtClass[] parameters,
     CtClass[] exceptions,
     CtClass declaring)
     throws NotFoundException {
   CtMethod cm = new CtMethod(returnType, mname, parameters, declaring);
   cm.setExceptionTypes(exceptions);
   return cm;
 }
Esempio n. 3
0
 /**
  * Creates a method.
  *
  * @param modifiers access modifiers.
  * @param returnType the type of the returned value.
  * @param mname the method name.
  * @param parameters a list of the parameter types.
  * @param exceptions a list of the exception types.
  * @param body the source text of the method body. It must be a block surrounded by <code>{}
  *     </code>. If it is <code>null</code>, the created method does nothing except returning zero
  *     or null.
  * @param declaring the class to which the created method is added.
  * @see Modifier
  */
 public static CtMethod make(
     int modifiers,
     CtClass returnType,
     String mname,
     CtClass[] parameters,
     CtClass[] exceptions,
     String body,
     CtClass declaring)
     throws CannotCompileException {
   try {
     CtMethod cm = new CtMethod(returnType, mname, parameters, declaring);
     cm.setModifiers(modifiers);
     cm.setExceptionTypes(exceptions);
     cm.setBody(body);
     return cm;
   } catch (NotFoundException e) {
     throw new CannotCompileException(e);
   }
 }
Esempio n. 4
0
 /**
  * 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;
 }