public void testSuperInterface() throws Exception { CtClass cc = sloader.makeClass("test2.SuperInterface3"); CtClass cc2 = sloader.get("test2.SuperInterface2"); cc.addInterface(cc2); cc.addField(new CtField(cc2, "inner", cc)); CtMethod m = CtNewMethod.make("public int getAge() { return inner.getAge(); }", cc); cc.addMethod(m); cc.writeFile(); }
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 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; }