This content has been marked as final.
Show 1 reply
-
1. Re: Question on CtClass FieldAccess, and ExprEditor
chiba Oct 21, 2004 11:54 AM (in response to liam)1. Methods such as getMethods, getMethod, getClassInitializer are not implemented functionable in CtClass. Are we expecting them to be implemented soon?
CtClass is an abstract class! Those methods have been implemented
in CtClassType. The implementation in CtClass is mainly for primitive
types.2. In FieldAccess class, why isn't there a getOpcode() function?
Do you want to know which the opcode is, getstatic, getfield, putstatic,
or putfield? If yes, you can instead use FieldAccess#isStatic(), #isReader(),
#isWriter().However, this does not work anymore as the static method ClassPool.GetDefault() does not take Translator as parameter anymore in javassist 3.
Yes, the article http://www-106.ibm.com/developerworks/java/library/j-dyn0302.html depends on 2.6.
I showed a modified version of Listing 3 in the article above:public class Dissect { public static void main(String[] args) { if (args.length >= 1) { try { // set up class loader with translator Translator xlat = new DissectionTranslator(); ClassPool pool = ClassPool.getDefault(); Loader loader = new Loader(); loader.addTranslator(pool, xlat); // invoke the "main" method of the application class String[] pargs = new String[args.length-1]; System.arraycopy(args, 1, pargs, 0, pargs.length); loader.run(args[0], pargs); } catch (Throwable ex) { ex.printStackTrace(); } } else { System.out.println ("Usage: Dissect main-class args..."); } } public static class DissectionTranslator implements Translator { public void start(ClassPool pool) {} public void onLoad(ClassPool pool, String cname) throws NotFoundException, CannotCompileException { System.out.println("Dissecting class " + cname); CtClass clas = pool.get(cname); clas.instrument(new VerboseEditor()); } }
In 3.0, Translator must be passed to Loader instead of ClassPool.
Translator#onWrite() was renamed into #onLoad because of this
move. This is for clarifying the role of ClassPool (and also to make
Javassist work better with JBoss AOP. :p)