Ejemplo 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);
  }
Ejemplo n.º 2
0
  /**
   * Creates a public setter method. The setter method assigns the value of the first parameter to
   * the specified field in the class to which this method is added. The created method is not
   * static even if the field is static. You may not change it to be static by <code>setModifiers()
   * </code> in <code>CtBehavior</code>.
   *
   * @param methodName the name of the setter
   * @param field the field accessed.
   */
  public static CtMethod setter(String methodName, CtField field) throws CannotCompileException {
    FieldInfo finfo = field.getFieldInfo2();
    String fieldType = finfo.getDescriptor();
    String desc = "(" + fieldType + ")V";
    ConstPool cp = finfo.getConstPool();
    MethodInfo minfo = new MethodInfo(cp, methodName, desc);
    minfo.setAccessFlags(AccessFlag.PUBLIC);

    Bytecode code = new Bytecode(cp, 3, 3);
    try {
      String fieldName = finfo.getName();
      if ((finfo.getAccessFlags() & AccessFlag.STATIC) == 0) {
        code.addAload(0);
        code.addLoad(1, field.getType());
        code.addPutfield(Bytecode.THIS, fieldName, fieldType);
      } else {
        code.addLoad(1, field.getType());
        code.addPutstatic(Bytecode.THIS, fieldName, fieldType);
      }

      code.addReturn(null);
    } catch (NotFoundException e) {
      throw new CannotCompileException(e);
    }

    minfo.setCodeAttribute(code.toCodeAttribute());
    return new CtMethod(minfo, field.getDeclaringClass());
  }
 /**
  * Client proxies are equal to other client proxies for the same bean.
  *
  * <p>The corresponding java code: <code>
  * return other instanceof MyProxyClassType.class
  * </code>
  */
 @Override
 protected MethodInfo generateEqualsMethod(ClassFile proxyClassType) {
   MethodInfo method =
       new MethodInfo(proxyClassType.getConstPool(), "equals", "(Ljava/lang/Object;)Z");
   method.setAccessFlags(AccessFlag.PUBLIC);
   Bytecode b = new Bytecode(proxyClassType.getConstPool());
   b.addAload(1);
   b.addInstanceof(proxyClassType.getName());
   b.add(Opcode.IRETURN);
   b.setMaxLocals(2);
   b.setMaxStack(1);
   method.setCodeAttribute(b.toCodeAttribute());
   return method;
 }
 /** Client proxies use the following hashCode: <code>MyProxyName.class.hashCode()</code> */
 @Override
 protected MethodInfo generateHashCodeMethod(ClassFile proxyClassType) {
   MethodInfo method = new MethodInfo(proxyClassType.getConstPool(), "hashCode", "()I");
   method.setAccessFlags(AccessFlag.PUBLIC);
   Bytecode b = new Bytecode(proxyClassType.getConstPool());
   // MyProxyName.class.hashCode()
   int classLocation = proxyClassType.getConstPool().addClassInfo(proxyClassType.getName());
   b.addLdc(classLocation);
   // now we have the class object on top of the stack
   b.addInvokevirtual("java.lang.Object", "hashCode", "()I");
   // now we have the hashCode
   b.add(Opcode.IRETURN);
   b.setMaxLocals(1);
   b.setMaxStack(1);
   method.setCodeAttribute(b.toCodeAttribute());
   return method;
 }
  /** Add a method to a class that simply delegates to the parent implementation of the method */
  public static void addDelegatingMethod(ClassFile file, MethodData mData)
      throws BadBytecode, DuplicateMemberException {
    MethodInfo m =
        new MethodInfo(file.getConstPool(), mData.getMethodName(), mData.getDescriptor());
    m.setAccessFlags(mData.getAccessFlags());
    Bytecode code = new Bytecode(file.getConstPool());

    String[] params = DescriptorUtils.descriptorStringToParameterArray(mData.getDescriptor());
    code.add(Opcode.ALOAD_0); // push this
    int count = 1; // zero is the this pointer
    int maxLocals = 1;
    for (String p : params) {
      // int char short boolean byte
      if (p.equals("I") || p.equals("C") || p.equals("S") || p.equals("Z") || p.equals("B")) {
        // push integer 0
        code.addIload(count);
        maxLocals++;
      }
      // long
      else if (p.equals("J")) {
        code.addLload(count);
        maxLocals += 2;
        count++;
      }
      // double
      else if (p.equals("D")) {
        code.addDload(count);
        maxLocals += 2;
        count++;
      }
      // float
      else if (p.equals("F")) {
        code.addFload(count);
        maxLocals++;
      }
      // arrays and reference types
      else {
        code.addAload(count);
        maxLocals++;
      }
      count++;
    }
    code.addInvokespecial(file.getSuperclass(), mData.getMethodName(), mData.getDescriptor());
    String p = DescriptorUtils.getReturnTypeInJvmFormat(mData.getDescriptor());
    // int char short boolean byte
    if (p.equals("I") || p.equals("C") || p.equals("S") || p.equals("Z") || p.equals("B")) {
      code.add(Opcode.IRETURN);
    }
    // long
    else if (p.equals("J")) {
      code.add(Opcode.LRETURN);
    }
    // double
    else if (p.equals("D")) {
      code.add(Opcode.DRETURN);
    }
    // float
    else if (p.equals("F")) {
      code.add(Opcode.FRETURN);
    }
    // void
    else if (p.equals("V")) {
      code.add(Opcode.RETURN);
    }
    // arrays and reference types
    else {
      code.add(Opcode.ARETURN);
    }
    CodeAttribute ca = code.toCodeAttribute();
    ca.computeMaxStack();
    ca.setMaxLocals(maxLocals);
    m.setCodeAttribute(ca);
    file.addMethod(m);
  }