Redirection of call to static Method

Peter Rader <[email protected]> Wed, 31 Oct 2007 12:38:56 +0100
Newsgroups gmane.comp.jakarta.bcel.user
Message-ID <[email protected]>
Hi,

this time i devolope an leightweight ajax server where the 
java-serverside and the java-applet shares classes.

(i.e.)
incomming request to class: org.enexus.Tools -> rewrite class (invoke 
agent)-> send class to applet -> applet call static Tools-method - > 
agent serialize to XML -> agent XML to Server -> deserialize to 
reflection call -> call method on server -> serialize result object to 
XML -> send to applet -> agent deserialize xml to result object -> 
Tools-method returns result object.


Now, when the applet requests for an server-side-class apperars my 
server send a rewrited class.
The rewrite have to:
 call an invoker-method on applet which becomes all the method 
parameters, the full-qualified method-name and the method's result-object.

Everything works fine, but i down know how to plug the parameters into 
the agents-method-parameters.

This code i have written:

      for (int i = 0; i < classMethods.length; i++)
            if (!classMethods[i].getName().equals("<init>") && 
classMethods[i].isPublic()) {

                com.sun.org.apache.bcel.internal.classfile.Method m = 
classMethods[i];
                Type[] argumentTypes = classMethods[i].getArgumentTypes();
                String[] namen = new String[argumentTypes.length];
                for (int j = 0; j < namen.length; j++) {
                    namen[j] = "arg" + j;
                }

                InstructionList instructionList = new InstructionList();
                Type returnType = classMethods[i].getReturnType();
                MethodGen mg = new MethodGen(classMethods[i]
                        .getAccessFlags(), returnType,
                        argumentTypes, namen,
                        classMethods[i].getName(), c.getName(),
                        instructionList, cg.getConstantPool());
                mg.setMaxStack(50);
                InstructionFactory inf = new InstructionFactory(cg);
               
                instructionList.append(inf.createNew(Type.OBJECT));
               
                instructionList.append(inf.createInvoke(
                        "de.enexus.BCELConnector", "callFromApplet", 
returnType,
                        argumentTypes, Constants.INVOKESTATIC));
               
               
                // If there is a return value, return it
                instructionList.append(inf.createReturn(returnType));
               
                mg.setMaxStack();
                mg.setMaxLocals();
               

                cg.addMethod(mg.getMethod());
                System.out.println(mg.getMethod());

            }
        byte[] bytes = cg.getJavaClass().getBytes();
        String string = new String(bytes);
        return string;