From 0ed1ede707440a9a981c2e0cfd5421b0ba0e364d Mon Sep 17 00:00:00 2001 From: chiba Date: Sun, 18 May 2003 15:34:30 +0000 Subject: [PATCH] fixed a bug in CtBehavior#setBody(). git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@19 30ef5769-5b8d-40dd-aea6-55b5d6557bb3 --- src/main/javassist/compiler/CodeGen.java | 24 +++---- src/main/javassist/compiler/Javac.java | 2 + tutorial/tutorial2.html | 91 +++++++++++++++++++++++- 3 files changed, 102 insertions(+), 15 deletions(-) diff --git a/src/main/javassist/compiler/CodeGen.java b/src/main/javassist/compiler/CodeGen.java index e0b2dfa4..35ca2605 100644 --- a/src/main/javassist/compiler/CodeGen.java +++ b/src/main/javassist/compiler/CodeGen.java @@ -267,18 +267,18 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { } } - private boolean needsSuperCall(Stmnt body) { - if (body.getOperator() == BLOCK) { - Stmnt first = (Stmnt)body.head(); - if (first != null && first.getOperator() == EXPR) { - ASTree expr = first.head(); - if (expr != null && expr instanceof Expr - && ((Expr)expr).getOperator() == CALL) { - ASTree target = ((Expr)expr).head(); - if (target instanceof Keyword) { - int token = ((Keyword)target).get(); - return token != THIS && token != SUPER; - } + private boolean needsSuperCall(Stmnt body) throws CompileError { + if (body.getOperator() == BLOCK) + body = (Stmnt)body.head(); + + if (body != null && body.getOperator() == EXPR) { + ASTree expr = body.head(); + if (expr != null && expr instanceof Expr + && ((Expr)expr).getOperator() == CALL) { + ASTree target = ((Expr)expr).head(); + if (target instanceof Keyword) { + int token = ((Keyword)target).get(); + return token != THIS && token != SUPER; } } } diff --git a/src/main/javassist/compiler/Javac.java b/src/main/javassist/compiler/Javac.java index 9ff801ef..de322cdf 100644 --- a/src/main/javassist/compiler/Javac.java +++ b/src/main/javassist/compiler/Javac.java @@ -170,6 +170,8 @@ public class Javac { /** * Compiles a method (or constructor) body. + * + * @src a single statement or a block. */ public Bytecode compileBody(CtBehavior method, String src) throws CompileError diff --git a/tutorial/tutorial2.html b/tutorial/tutorial2.html index b9ca5bed..5016474a 100644 --- a/tutorial/tutorial2.html +++ b/tutorial/tutorial2.html @@ -363,11 +363,12 @@ For example, if the result type is int, then ($r) does not convert a type; it does nothing. However, if the operand is a call to a void method, then ($r) results in null. For example, +if the result type is void and +foo() is a void method, then -

is a valid statement if the result type is void. -Here, foo() is a void method. +

is a valid statement.

The cast operator ($r) is also useful in a return statement. Even if the result type is @@ -485,7 +486,74 @@ catch (java.io.IOException e) {

5.2 Modifying a method body

-

javassist.expr.ExprEditor is a class +

CtMethod and CtConstructor provide +setBody() for substituting a whole +method body. They compile the given source text into Java bytecode +and substitutes it for the original method body. + +

In the source text given to setBody(), the identifiers +starting with $ have special meaning + +

+ +Note that $_ is not available. + + +

Javassist allows modifying only an expression included in a method body. +javassist.expr.ExprEditor is a class for replacing an expression in a method body. The users can define a subclass of ExprEditor to specify how an expression is modified. @@ -1047,6 +1115,23 @@ public int ymove(int dy) { this.move(0, dy); }

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

Javassist provides another way to add a new method. +You can first create an abstract method and later give it a method body: + +

+ +

Since Javassist makes a class abstract if an abstract method is +added to the class, you have to explicitly change the class back to a +non-abstract one after calling setBody(). + +

Mutual recursive methods

Javassist cannot compile a method if it calls another method that -- 2.39.5