String interfaceName = "org.apache.s4.processor.OverloadDispatcher";
if (forSlot) {
interfaceName = "org.apache.s4.processor.OverloadDispatcherSlot";
}
ClassGen cg = new ClassGen(dispatcherClassName,
"java.lang.Object",
dispatcherClassName + ".java",
Constants.ACC_PUBLIC | Constants.ACC_SUPER,
new String[] { interfaceName });
ConstantPoolGen cp = cg.getConstantPool();
InstructionFactory instFactory = new InstructionFactory(cg, cp);
InstructionList il = new InstructionList();
// build constructor method for new class
MethodGen constructor = new MethodGen(Constants.ACC_PUBLIC,
Type.VOID,
Type.NO_ARGS,
new String[] {},
"<init>",
dispatcherClassName,
il,
cp);
il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
il.append(instFactory.createInvoke("java.lang.Object",
"<init>",
Type.VOID,
Type.NO_ARGS,
Constants.INVOKESPECIAL));
il.append(InstructionFactory.createReturn(Type.VOID));
constructor.setMaxStack();
constructor.setMaxLocals();
cg.addMethod(constructor.getMethod());
il.dispose();
// build dispatch method
il = new InstructionList();
Type[] dispatchArgumentTypes = null;
String[] dispatchArgumentNames = null;
int postArgumentVariableSlot = 3;
if (forSlot) {
dispatchArgumentTypes = new Type[] { ObjectType.OBJECT,
ObjectType.OBJECT, ObjectType.LONG, abstractWindowingPEType };
dispatchArgumentNames = new String[] { "slot", "event", "slotTime",
"pe" };
postArgumentVariableSlot = 6;
} else {
dispatchArgumentTypes = new Type[] { ObjectType.OBJECT,
ObjectType.OBJECT };
dispatchArgumentNames = new String[] { "pe", "event" };
}
MethodGen method = new MethodGen(Constants.ACC_PUBLIC,
Type.VOID,
dispatchArgumentTypes,
dispatchArgumentNames,
"dispatch",
dispatcherClassName,
il,
cp);
List<InstructionHandle> targetInstructions = new ArrayList<InstructionHandle>();
List<BranchInstruction> branchInstructions = new ArrayList<BranchInstruction>();
List<BranchInstruction> gotoInstructions = new ArrayList<BranchInstruction>();
ObjectType peType = new ObjectType(targetClass.getName());
il.append(InstructionFactory.createLoad(Type.OBJECT, 1));
il.append(instFactory.createCheckCast(peType));
il.append(InstructionFactory.createStore(peType,
postArgumentVariableSlot));
for (int i = 0; i < hierarchies.size(); i++) {
Hierarchy hierarchy = hierarchies.get(i);
ObjectType hierarchyTop = new ObjectType(hierarchy.getTop()
.getName());
InstructionHandle ih = il.append(InstructionFactory.createLoad(Type.OBJECT,
2));
if (i > 0) {
targetInstructions.add(ih);
}
il.append(new INSTANCEOF(cp.addClass(hierarchyTop)));
BranchInstruction bi = InstructionFactory.createBranchInstruction(Constants.IFEQ,
null);
il.append(bi);
branchInstructions.add(bi);
il.append(InstructionFactory.createLoad(peType,
postArgumentVariableSlot));
il.append(InstructionFactory.createLoad(hierarchyTop, 2));
il.append(instFactory.createCheckCast(hierarchyTop));
if (forSlot) {
il.append(InstructionFactory.createLoad(ObjectType.LONG, 3));
il.append(InstructionFactory.createLoad(abstractWindowingPEType,
5));
}
Type[] argumentTypes = null;
if (forSlot) {
argumentTypes = new Type[] { hierarchyTop, ObjectType.LONG,
abstractWindowingPEType };
} else {
argumentTypes = new Type[] { hierarchyTop };
}
il.append(instFactory.createInvoke(targetClass.getName(),
"processEvent",
Type.VOID,
argumentTypes,
Constants.INVOKEVIRTUAL));
// no branch needed for last check
if (i < (hierarchies.size() - 1)) {
bi = InstructionFactory.createBranchInstruction(Constants.GOTO,
null);
il.append(bi);
gotoInstructions.add(bi);
}
}
InstructionHandle returnInstruction = il.append(InstructionFactory.createReturn(Type.VOID));
for (int i = 0; i < targetInstructions.size(); i++) {
branchInstructions.get(i).setTarget(targetInstructions.get(i));
}
branchInstructions.get(branchInstructions.size() - 1)
.setTarget(returnInstruction);
for (BranchInstruction gotoInstruction : gotoInstructions) {
gotoInstruction.setTarget(returnInstruction);
}
method.setMaxStack();
method.setMaxLocals();
cg.addMethod(method.getMethod());
il.dispose();
JavaClass jc = cg.getJavaClass();
OverloadDispatcherClassLoader cl = new OverloadDispatcherClassLoader();
// debug
if (classDumpFile != null) {
FileOutputStream fos = null;