I am currently undertaking a project to animate java
programs by analysing bytecode method calls and
attribute updates. I
have been studying your 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
you could help
clarify this.
I have been experimenting with your 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?
Any help you could offer would be greatly appreciated.
Thank you,
Derek.
/*
* 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());
}
}