Re: Bytecode Engineering Library (BCEL) query

From: Jeffrey Palm (jpalm_at_ccs.neu.edu)
Date: 12/01/03


Date: Mon, 01 Dec 2003 08:40:17 -0500
To: Derek Meehan <d.j.meehan@blueyonder.co.uk>

Derek Meehan wrote:

> I am currently undertaking a project to animate java
> programs by analysing bytecode method calls and attribute updates. I
> have been studying the BCEL API as a means of implementing my design. I
> am however having difficulty understanding results when running the
> program on the latest JDK virtual machine. I wonder if anyone could help
> clarify this.
>
> I have been experimenting with the helloify.java program which I can
> get working in most cases but I get the following error when executing
> it on the java program which i have included below.
>
> "Exception in thread "main" java.lang.VerifyError: (class:
> ComplexNumber_hello, method: add signature:
> (LComplexNumber;)LComplexNumber;) Incompatible type for getting or
> setting field"
>
> My java program simply creates two complexNumber objects and adds them
> together. This compiles and runs successfully on "Java(TM) 2 Runtime
> Environment, Standard Edition (build 1.4.2_02-b03), Java HotSpot(TM)
> Client VM (build 1.4.2_02-b03, mixed mode)". The
> ComplexNumber_hello.class file produces the error above and I cannot
> understand why. It seems to happen when trying to return the sum of the
> two complex numbers. I have tried to rectify this problem with no
> success. When using the Kaffe JVM supplied with RedHat Linux 7.2 I get
> no errors when running ComplexNumber_hello or ComplexNumber classes. Is
> this an issue with the latest JVM or am I missing something important?
>
> Thanks.
>
>
>
> /*
> * Class ComplexNumber specify Complex Number object and
> * associated functions
> *
> */
>
>
> class ComplexNumber {
>
> // Private data fields for the complex number:
> // realPart + imagPart * i
> private double realPart;
> private double imagPart;
>
>
> /**
> * initializes the
> * the complex number to be zero.
> */
> ComplexNumber() {
> realPart = 0;
> imagPart = 0;
> }
>
> /**
> * Constructor that allows the real and imaginary
> * parts of the complex number to be initialized
> * to arbitrary values.
> */
> ComplexNumber(double realPart, double imagPart) {
> this.realPart = realPart;
> this.imagPart = imagPart;
> }
>
>
> /**
> * Set the value of a complex number by specifying its
> * real and imaginary parts.
> */
> public void setValue(double realPart, double imagPart) {
> this.realPart = realPart;
> this.imagPart = imagPart;
> }
>
>
> /**
> * Return a new complex number with its value equal
> * to the sum of the invoking ComplexNumber and
> * the Complex Number argument.
> */
> public ComplexNumber add(ComplexNumber theNum) {
> return new ComplexNumber(realPart + theNum.realPart,
> imagPart + theNum.imagPart);
> }
>
>
>
>
> /**
> * Convert the complex number into a string representation.
> * The string representation is realPart + imagPart i.
> */
> public String toString() {
>
> // If the imagPart is >= 0 print out a + b i
> if (imagPart >= 0) {
> return realPart + " + " + imagPart + " i";
> }
> // If the imagPart is < 0 print out a - b i
> else {
> return realPart + " - " + (-imagPart) + " i";
> }
> }
>
>
>
> //*************************************************************************
> // Test the above methods
>
>
> public static void main(String args[]) {
>
> // Create 3 complex number objects and initialize
> ComplexNumber A = new ComplexNumber();
> ComplexNumber B = new ComplexNumber();
> ComplexNumber result = new ComplexNumber();
>
> System.out.println("Testing ComplexNumber.java...");
>
> A.setValue(5,1);
> B.setValue(2,3);
>
> // Test the add method
> result = A.add(B);
>
> // output the result
> System.out.println(A.toString() + " + " + B.toString() + "
> = " + result.toString());
>
> }
> }
>
>

Looks like a bug in BCEL... the modified program declares add to return
a ComplexNumber (this *should* be ComplexNumber_hello) but tries
returning a ComplexNumber_hello. Compare the disassembled code from
ComplexNumber_hello:

     // This should be ComplexNumber_hello add(ComplexNumber_hello arg0)
     public ComplexNumber add(ComplexNumber arg0)
     {
     // 0 0:getstatic #49 <Field PrintStream System.out>
     // 1 3:ldc1 #78 <String "Hello from public
ComplexNumber add(ComplexNumber arg1)">
     // 2 5:invokevirtual #57 <Method void
PrintStream.println(String)>
     // 3 8:new #1 <Class ComplexNumber_hello>
     // 4 11:dup
     ...
     // 15 30:invokespecial #20 <Method void
ComplexNumber_hello(double, double)>
     // 16 33:areturn
     }

with that from ComplexNumber:

     public ComplexNumber add(ComplexNumber complexnumber)
     {
     ...
     // 3 8:new #1 <Class ComplexNumber>
     // 4 11:dup
     ...
     // 15 30:invokespecial #34 <Method void
ComplexNumber(double, double)>
     // 16 33:areturn
     }

Jeff

-- 
Jeffrey Palm --> http://www.ccs.neu.edu/home/jpalm


Relevant Pages

  • Re: Bytecode Engineering Library (BCEL) query
    ... > have been studying the BCEL API as a means of implementing my design. ... > no errors when running ComplexNumber_hello or ComplexNumber classes. ... > ComplexNumber(double realPart, double imagPart) { ... > public ComplexNumber add{ ...
    (comp.lang.java.help)
  • Re: Bytecode Engineering Library (BCEL) query
    ... designing ComplexNumber.java? ... >> it on the java program which i have included below. ... >> My java program simply creates two complexNumber objects and adds them ... > public ComplexNumber add ...
    (comp.lang.java.help)
  • Re: Bytecode Engineering Library (BCEL) query
    ... designing ComplexNumber.java? ... >> it on the java program which i have included below. ... >> My java program simply creates two complexNumber objects and adds them ... > public ComplexNumber add ...
    (comp.lang.java.softwaretools)
  • Bytecode Engineering Library (BCEL) query
    ... I am currently undertaking a project to animate java ... My java program simply creates two complexNumber objects and adds them ... ComplexNumber(double realPart, double imagPart) { ...
    (comp.lang.java.help)
  • Bytecode Engineering Library (BCEL) query
    ... I am currently undertaking a project to animate java ... My java program simply creates two complexNumber objects and adds them ... ComplexNumber(double realPart, double imagPart) { ...
    (comp.lang.java.softwaretools)