Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2003

Tip: Looking for answers? Try searching our database.

Bytecode Engineering Library (BCEL) query

Thread view: 
Derek Meehan - 01 Dec 2003 12:05 GMT
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());

  }
}
Jeffrey Palm - 01 Dec 2003 13:40 GMT
>  I am currently undertaking a project to animate java
> programs by  analysing bytecode method calls and attribute updates. I
[quoted text clipped - 116 lines]
>    }
> }

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
Signature

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

Derek Meehan - 01 Dec 2003 13:57 GMT
Thanks for your quick response. Do you think this problem can be avoided by
modifying the helloify program, or should a different approach be taken to
designing ComplexNumber.java?

Derek.

> >  I am currently undertaking a project to animate java
> > programs by  analysing bytecode method calls and attribute updates. I
[quoted text clipped - 91 lines]
> >    }
> >    }

//*************************************************************************
> >    //  Test the above methods
> >
[quoted text clipped - 57 lines]
> --
> Jeffrey Palm --> http://www.ccs.neu.edu/home/jpalm
Jeffrey Palm - 01 Dec 2003 14:07 GMT
> Thanks for your quick response. Do you think this problem can be
avoided by
> modifying the helloify program, or should a different approach be
taken to
> designing ComplexNumber.java?
>
[quoted text clipped - 3 lines]
> From: "Jeffrey Palm" <jpalm@ccs.neu.edu>
> Newsgroups:

comp.compilers.tools.javacc,comp.lang.java.help,comp.lang.java.softwaretools
> To: "Derek Meehan" <d.j.meehan@blueyonder.co.uk>
> Sent: Monday, December 01, 2003 1:40 PM
[quoted text clipped - 5 lines]
>>> am however having difficulty understanding results when running the
>>> program on the latest JDK virtual machine. I wonder if anyone could
help
>>> clarify this.
>>>
[quoted text clipped - 86 lines]
>>>   }
>>>   }

//*************************************************************************

>>>   //  Test the above methods
>>>
[quoted text clipped - 57 lines]
>> --
>> Jeffrey Palm --> http://www.ccs.neu.edu/home/jpalm

Yeah, it's not *really* a bug, more of a feature left out of the
helloify program.  You'd have to modify that for this to work, I guess,
I don't know much abou BCEL.

But, why don't you look at aspectj.  You could have simple 'before'
advice to captuer the helloify behavior:

  before(): execution(* *.(..)) {
    System.out.println("Hello from ...");
  }

That'll do it -- you should check it out, and get back if you have any
questions:

  http://aspectj.org

Jeff

Signature

Jeffrey Palm --> http://jeffpalm.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.