Exemplo n.º 1
0
  /**
   * Return javaassist source snippet which lists all the parameters and their values. If available
   * the source names are extracted from the debug information and used, otherwise just a number is
   * shown.
   *
   * @param method
   * @return
   * @throws NotFoundException
   */
  public static String getSignature(CtBehavior method) throws NotFoundException {

    CtClass[] parameterTypes = method.getParameterTypes();

    CodeAttribute codeAttribute = method.getMethodInfo().getCodeAttribute();

    LocalVariableAttribute locals = null;

    if (codeAttribute != null) {
      AttributeInfo attribute;
      attribute = codeAttribute.getAttribute("LocalVariableTable");
      locals = (LocalVariableAttribute) attribute;
    }

    String methodName = method.getName();

    StringBuilder sb = new StringBuilder(methodName).append("(\" ");
    for (int i = 0; i < parameterTypes.length; i++) {
      if (i > 0) {
        // add a comma and a space between printed values
        sb.append(" + \", \" ");
      }

      CtClass parameterType = parameterTypes[i];
      boolean isArray = parameterType.isArray();
      CtClass arrayType = parameterType.getComponentType();
      if (isArray) {
        while (arrayType.isArray()) {
          arrayType = arrayType.getComponentType();
        }
      }

      sb.append(" + \"");
      try {
        sb.append(parameterNameFor(method, locals, i));
      } catch (Exception e) {
        sb.append(i + 1);
      }
      sb.append("\" + \"=");

      if (parameterType.isPrimitive()) {
        // let the compiler handle primitive -> string
        sb.append("\"+ $").append(i + 1);
      } else {
        String s = "org.slf4j.instrumentation.ToStringHelper.render";
        sb.append("\"+ ").append(s).append("($").append(i + 1).append(')');
      }
    }
    sb.append("+\")");

    String signature = sb.toString();
    return signature;
  }
Exemplo n.º 2
0
  private boolean addSerializeMethods(CtClass cc, boolean callSuper)
      throws NotFoundException, CannotCompileException {
    CtField[] fields = cc.getDeclaredFields();
    int size = 0;
    StringBuffer sb = new StringBuffer();
    sb.append(callSuper ? "{$2=super.pack($1, $2);$1.extend($2" : "{$1.extend($2");
    for (int i = 0; i < fields.length; i++) {
      CtField f = fields[i];
      if ((f.getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) == 0) {
        CtClass type = f.getType();
        if (type.isPrimitive()) {
          if (type == CtClass.booleanType || type == CtClass.byteType) {
            size += 1;
          } else if (type == CtClass.charType || type == CtClass.shortType) {
            size += 2;
          } else if (type == CtClass.longType || type == CtClass.doubleType) {
            size += 8;
          } else {
            size += 4;
          }
        } else if (type.getName().equals("java.lang.String")) {
          sb.append("+org.nachodb.impl.Bytes#sizeof(");
          sb.append(f.getName());
          sb.append(",$3)");
        } else {
          return false;
        }
      }
    }
    cc.addInterface(serializable);

    CtMethod m = new CtMethod(pack, cc, null);
    sb.append('+');
    sb.append(size);
    sb.append(");");
    for (int i = 0; i < fields.length; i++) {
      CtField f = fields[i];
      if ((f.getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) == 0) {
        CtClass type = f.getType();
        String name = f.getName();
        if (type == CtClass.booleanType) {
          sb.append("$1.arr[$2++]=(byte)(");
          sb.append(name);
          sb.append("?1:0);");
        } else if (type == CtClass.charType) {
          sb.append("org.nachodb.impl.Bytes#pack2($1.arr,$2,(short)");
          sb.append(name);
          sb.append(");$2+=2;");
        } else if (type == CtClass.byteType) {
          sb.append("$1.arr[$2++]=");
          sb.append(name);
          sb.append(";");
        } else if (type == CtClass.shortType) {
          sb.append("org.nachodb.impl.Bytes#pack2($1.arr,$2,");
          sb.append(name);
          sb.append(");$2+=2;");
        } else if (type == CtClass.intType) {
          sb.append("org.nachodb.impl.Bytes#pack4($1.arr,$2,");
          sb.append(name);
          sb.append(");$2+=4;");
        } else if (type == CtClass.longType) {
          sb.append("org.nachodb.impl.Bytes#pack8($1.arr,$2,");
          sb.append(name);
          sb.append(");$2+=8;");
        } else if (type == CtClass.doubleType) {
          sb.append("org.nachodb.impl.Bytes#packF8($1.arr,$2,");
          sb.append(name);
          sb.append(");$2+=8;");
        } else if (type == CtClass.floatType) {
          sb.append("org.nachodb.impl.Bytes#packF4($1.arr,$2,");
          sb.append(name);
          sb.append(");$2+=4;");
        } else {
          sb.append("$2=org.nachodb.impl.Bytes#packStr($1.arr,$2,");
          sb.append(name);
          sb.append(",$3);");
        }
      }
    }
    sb.append("return $2;}");
    m.setBody(sb.toString());
    cc.addMethod(m);

    m = new CtMethod(unpack, cc, null);
    sb = new StringBuffer();
    sb.append(callSuper ? "{$2=super.unpack($1, $2);" : "{");
    for (int i = 0; i < fields.length; i++) {
      CtField f = fields[i];
      if ((f.getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) == 0) {
        CtClass type = f.getType();
        String name = f.getName();
        sb.append(name);
        sb.append('=');
        if (type == CtClass.booleanType) {
          sb.append("$1[$2++]!=0;");
        } else if (type == CtClass.charType) {
          sb.append("(char)org.nachodb.impl.Bytes#unpack2($1,$2);$2+=2;");
        } else if (type == CtClass.byteType) {
          sb.append("$1[$2++];");
        } else if (type == CtClass.shortType) {
          sb.append("org.nachodb.impl.Bytes#unpack2($1,$2);$2+=2;");
        } else if (type == CtClass.intType) {
          sb.append("org.nachodb.impl.Bytes#unpack4($1,$2);$2+=4;");
        } else if (type == CtClass.longType) {
          sb.append("org.nachodb.impl.Bytes#unpack8($1,$2);$2+=8;");
        } else if (type == CtClass.doubleType) {
          sb.append("org.nachodb.impl.Bytes#unpackF8($1,$2);$2+=8;");
        } else if (type == CtClass.floatType) {
          sb.append("org.nachodb.impl.Bytes#unpackF4($1,$2);$2+=4;");
        } else {
          sb.append(
              "org.nachodb.impl.Bytes#unpackStr($1,$2,$3);$2+=org.nachodb.impl.Bytes#sizeof($1,$2);");
        }
      }
    }
    sb.append("return $2;}");
    m.setBody(sb.toString());
    cc.addMethod(m);
    return true;
  }