HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Javassist: Java Bytecode Engineering Made Simple"
Vol. 9, Issue 1, p. 30

	



Listing 1

1. ClassPool pool = ClassPool.getDefault();
2. CtClass cc = pool.get("Screen");
3. CtMethod cm = cc.getDeclaredMethod("draw", new CtClass[0]);
4. cm.instrument(new ExprEditor() {
5. public void edit(MethodCall m) throws CannotCompileException {
6. if (m.getClassName().equals("Point")
7. && m.getMethodName().equals("move"))
8. m.replace("{ System.out.println(\"move\"); $_ = $proceed($$); }");
9.   }
10. });
11. cc.writeFile();

Listing 2

1. ClassPool pool = ClassPool.getDefault();
2. CtClass ch = pool.makeClass("Hello");
3. CtClass ci = pool.get("IHello");
4. ch.addInterface(ci);
5. CtMethod m = CtNewMethod.make( "public void say() {System.out.println(\"Hello\"); }", ch);
6. ch.addMethod(m);
7. Class h = ch.toClass();
8. IHello obj = (IHello)h.newInstance();
9. obj.say();

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.