私はactivejdbcのソースを読んでいますが、これらのメソッドは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.");
}
}
}
このクラスは、モデル クラスを拡張するために使用されます。最初のクラスは、最初instrument
にすべての非プライベート メソッドorg.javalite.activejdbc.Model
をその子モデル クラスに委譲します。つまり、そのようなメソッドを子に追加します。
public X f(...) {
return super.f(...);
}
デリゲートがなくてもこれらのメソッドを呼び出すことができるため、なぜこれを行うのかわかりません。