From dea5156362fd172b97c0adb001402fdd6107f2a7 Mon Sep 17 00:00:00 2001 From: chiba Date: Wed, 14 May 2003 16:20:13 +0000 Subject: fixed several compiler bugs and updated the tutorial. git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@14 30ef5769-5b8d-40dd-aea6-55b5d6557bb3 --- tutorial/tutorial2.html | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) (limited to 'tutorial') diff --git a/tutorial/tutorial2.html b/tutorial/tutorial2.html index 6287f1ee..62c26a94 100644 --- a/tutorial/tutorial2.html +++ b/tutorial/tutorial2.html @@ -74,8 +74,8 @@ into the body of an existing method. The users can specify those code fragments with source text written in Java. Javassist includes a simple Java compiler for processing source text. It receives source text -written in Java and compiles it into Java bytecode, which will be inserted -into a method body. +written in Java and compiles it into Java bytecode, which will be +inlined into a method body.

The methods insertBefore(), insertAfter(), and addCatch() receives a String object representing @@ -989,6 +989,8 @@ exception.

5.3 Adding a new method or field

+

Adding a method

+

Javassist allows the users to create a new method and constructor from scratch. CtNewMethod and CtNewConstructor provide several factory methods, @@ -1032,6 +1034,34 @@ public int ymove(int dy) { this.move(0, dy); }

Note that $proceed has been replaced with this.move. +

Mutual recursive methods

+ +

Javassist cannot compile a method if it calls another method that +has not been added to a class. (Javassist can compile a method that +calls itself recursively.) To add mutual recursive methods to a class, +you need a trick shown below. Suppose that you want to add methods +m() and n() to a class represented +by cc: + +

+ +

You must first make two abstract methods and add them to the class. +Then you can give the method bodies to these methods even if the method +bodies include method calls to each other. Finally you must change the +class to a not-abstract class since addMethod() automatically +changes a class into an abstract one if an abstract method is added. + +

Adding a field

+

Javassist also allows the users to create a new field.