Question

I'm reading the source of activejdbc, found these methods in ModelInstrumentation.

public void instrument(CtClass modelClass) throws Exception {
    addDelegates(modelClass);
    CtMethod m = CtNewMethod.make("public static String getClassName() { return \"" + modelClass.getName()
            + "\"; }", modelClass);
    CtMethod getClassNameMethod = modelClass.getDeclaredMethod("getClassName");
    modelClass.removeMethod(getClassNameMethod);
    modelClass.addMethod(m);
}

CtClass modelClass = ClassPool.getDefault().get("org.javalite.activejdbc.Model");

private void addDelegates(CtClass target) throws NotFoundException, CannotCompileException {
    CtMethod[] modelMethods = modelClass.getDeclaredMethods();
    CtMethod[] targetMethods = target.getDeclaredMethods();
    for (CtMethod method : modelMethods) {

        if (Modifier.PRIVATE == method.getModifiers()) {
            continue;
        }

        CtMethod newMethod = CtNewMethod.delegator(method, target);

        if (!targetHasMethod(targetMethods, newMethod)) {
            target.addMethod(newMethod);
        } else {
            System.out.println("Detected method: " + newMethod.getName() + ", skipping delegate.");
        }
    }

}

This class is used to enhance a model class, the first one instrument will firstly delegate all non-private methods from org.javalite.activejdbc.Model to its child model class, which means it will add such methods to the child:

public X f(...) {
    return super.f(...);
}

I don't understand why it does this, since we can invoke these methods even if there is no delegates.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top