Exemple #1
0
 public void onWrite(ClassPool pool, String className)
     throws NotFoundException, CannotCompileException {
   CtClass cc = pool.get(className);
   try {
     if (isPersistent(className)) {
       CtClass base = cc.getSuperclass();
       CtConstructor cons = new CtConstructor(constructorParams, cc);
       if (base.subclassOf(persistent) || base == object) {
         cons.setBody(null);
         cc.addConstructor(cons);
         if (base == object) {
           cc.setSuperclass(persistent);
         }
       } else {
         if (!isPersistent(base.getName())) {
           throw new NotFoundException(
               "Base class " + base.getName() + " was not declared as persistent");
         }
         cons.setBody("super($0);");
         cc.addConstructor(cons);
       }
       preprocessMethods(cc, true, true);
       if (base == persistent || base == object) {
         CtMethod m = new CtMethod(isRecursive, cc, null);
         m.setBody("return false;");
         cc.addMethod(m);
         addSerializeMethods(cc, false);
       } else if (base.subtypeOf(serializable)) {
         addSerializeMethods(cc, true);
       }
       if ((cc.getModifiers() & Modifier.PRIVATE) == 0) {
         CtClass f = pool.makeClass(className + "LoadFactory");
         f.addInterface(factory);
         CtMethod c = new CtMethod(create, f, null);
         c.setBody("return new " + className + "($1);");
         f.addMethod(c);
         CtNewConstructor.defaultConstructor(f);
       }
     } else {
       preprocessMethods(
           cc, cc.subtypeOf(persistent) && cc != persistent, !className.startsWith("org.nachodb"));
     }
   } catch (Exception x) {
     x.printStackTrace();
   }
 }
  private void changeMethod(CtClass ctClass, CtMethod ctMethod) throws CannotCompileException {
    // basically your before-advice...
    ctMethod.insertBefore("System.out.println(\"started method at \" + new java.util.Date());");
    // basically your after-advice...
    ctMethod.insertAfter("System.out.println(\"ended method at \" + new java.util.Date());");

    // basically your around-advice...
    String methodName = ctMethod.getName();
    String proxyName = methodName + "_$proxy";
    CtMethod proxy = CtNewMethod.copy(ctMethod, proxyName, ctClass, null);
    ctMethod.setName(ctMethod.getName() + "_orig");
    proxy.setBody(
        "{ System.out.println(\"hoot!\"); return $proceed($$);}", "this", ctMethod.getName());
    proxy.setName(methodName);
    ctClass.addMethod(proxy);
  }
Exemple #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);
   }
 }
Exemple #4
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;
  }