You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bcel-user@jakarta.apache.org by theUser BL <th...@hotmail.com> on 2009/11/15 14:29:20 UTC

Bug in BCEL or BCELifier

Hi!

Have a look at this one:
------------------------
C:\>dir
 Volume in Laufwerk C: hat keine Bezeichnung.
 Volumeseriennummer: 5481-B7B1

 Verzeichnis von C:\

15.11.2009  00:02    <DIR>          .
15.11.2009  00:02    <DIR>          ..
15.11.2009  00:04               372 Test.java
               1 Datei(en),            372 Bytes
               2 Verzeichnis(se), 158.248.124.416 Bytes frei

C:\>type Test.java
public class Test {

  public static void TestMethod (int first, double second, float third) {

  third = 15;
  first = 12;
  second = 17.2;
  third = 19;
  System.out.println(first);
  System.out.println(second);
  System.out.println(third);
  System.out.println(third);

  }

  public static void main(String[] args) {
    TestMethod (0,0,0);
  }

}

C:\>javac Test.java

C:\>java Test
12
17.2
19.0
19.0

C:\>java org.apache.bcel.util.BCELifier Test > TestCreator.java

C:\>type TestCreator.java
import org.apache.bcel.generic.*;
import org.apache.bcel.classfile.*;
import org.apache.bcel.*;
import java.io.*;

public class TestCreator implements Constants {
  private InstructionFactory _factory;
  private ConstantPoolGen    _cp;
  private ClassGen           _cg;

  public TestCreator() {
    _cg = new ClassGen("Test", "java.lang.Object", "Test.java", ACC_PUBLIC | ACC_SUPER, new String[] {  });

    _cp = _cg.getConstantPool();
    _factory = new InstructionFactory(_cg, _cp);
  }

  public void create(OutputStream out) throws IOException {
    createMethod_0();
    createMethod_1();
    createMethod_2();
    _cg.getJavaClass().dump(out);
  }

  private void createMethod_0() {
    InstructionList il = new InstructionList();
    MethodGen method = new MethodGen(ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] {  }, "<init>", "Test", il, _cp);

    InstructionHandle ih_0 = il.append(_factory.createLoad(Type.OBJECT, 0));
    il.append(_factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));
    InstructionHandle ih_4 = il.append(_factory.createReturn(Type.VOID));
    method.setMaxStack();
    method.setMaxLocals();
    _cg.addMethod(method.getMethod());
    il.dispose();
  }

  private void createMethod_1() {
    InstructionList il = new InstructionList();
    MethodGen method = new MethodGen(ACC_PUBLIC | ACC_STATIC, Type.VOID, new Type[] { Type.INT, Type.DOUBLE, Type.FLOAT }, new String[] { "arg0", "arg1", "arg2" }, "TestMethod", "Test", il, _cp);

    InstructionHandle ih_0 = il.append(new PUSH(_cp, 15.0));
    il.append(_factory.createStore(Type.FLOAT, 3));
    InstructionHandle ih_3 = il.append(new PUSH(_cp, 12));
    il.append(_factory.createStore(Type.INT, 0));
    InstructionHandle ih_6 = il.append(new PUSH(_cp, 17.2));
    il.append(_factory.createStore(Type.DOUBLE, 1));
    InstructionHandle ih_10 = il.append(new PUSH(_cp, 19.0));
    il.append(_factory.createStore(Type.FLOAT, 3));
    InstructionHandle ih_13 = il.append(_factory.createFieldAccess("java.lang.System", "out", new ObjectType("java.io.PrintStream"), Constants.GETSTATIC));
    il.append(_factory.createLoad(Type.INT, 0));
    il.append(_factory.createInvoke("java.io.PrintStream", "println", Type.VOID, new Type[] { Type.INT }, Constants.INVOKEVIRTUAL));
    InstructionHandle ih_20 = il.append(_factory.createFieldAccess("java.lang.System", "out", new ObjectType("java.io.PrintStream"), Constants.GETSTATIC));
    il.append(_factory.createLoad(Type.DOUBLE, 1));
    il.append(_factory.createInvoke("java.io.PrintStream", "println", Type.VOID, new Type[] { Type.DOUBLE }, Constants.INVOKEVIRTUAL));
    InstructionHandle ih_27 = il.append(_factory.createFieldAccess("java.lang.System", "out", new ObjectType("java.io.PrintStream"), Constants.GETSTATIC));
    il.append(_factory.createLoad(Type.FLOAT, 3));
    il.append(_factory.createInvoke("java.io.PrintStream", "println", Type.VOID, new Type[] { Type.FLOAT }, Constants.INVOKEVIRTUAL));
    InstructionHandle ih_34 = il.append(_factory.createFieldAccess("java.lang.System", "out", new ObjectType("java.io.PrintStream"), Constants.GETSTATIC));
    il.append(_factory.createLoad(Type.FLOAT, 3));
    il.append(_factory.createInvoke("java.io.PrintStream", "println", Type.VOID, new Type[] { Type.FLOAT }, Constants.INVOKEVIRTUAL));
    InstructionHandle ih_41 = il.append(_factory.createReturn(Type.VOID));
    method.setMaxStack();
    method.setMaxLocals();
    _cg.addMethod(method.getMethod());
    il.dispose();
  }

  private void createMethod_2() {
    InstructionList il = new InstructionList();
    MethodGen method = new MethodGen(ACC_PUBLIC | ACC_STATIC, Type.VOID, new Type[] { new ArrayType(Type.STRING, 1) }, new String[] { "arg0" }, "main", "Test", il, _cp);

    InstructionHandle ih_0 = il.append(new PUSH(_cp, 0));
    il.append(new PUSH(_cp, 0.0));
    il.append(new PUSH(_cp, 0.0));
    il.append(_factory.createInvoke("Test", "TestMethod", Type.VOID, new Type[] { Type.INT, Type.DOUBLE, Type.FLOAT }, Constants.INVOKESTATIC));
    InstructionHandle ih_6 = il.append(_factory.createReturn(Type.VOID));
    method.setMaxStack();
    method.setMaxLocals();
    _cg.addMethod(method.getMethod());
    il.dispose();
  }

  public static void main(String[] args) throws Exception {
    TestCreator creator = new TestCreator();
    creator.create(new FileOutputStream("Test.class"));
  }
}

C:\>ren Test.class Test.class.old

C:\>javac TestCreator.java

C:\>java TestCreator

C:\>java Test
Exception in thread "main" java.lang.VerifyError: (class: Test, method: main signature: ([Ljava/lang/String;)V) Expecting to find float on stack
Could not find the main class: Test.  Program will exit.

C:\>java -version
java version "1.6.0_16"
Java(TM) SE Runtime Environment (build 1.6.0_16-b01)
Java HotSpot(TM) Client VM (build 14.2-b01, mixed mode, sharing)

C:\>
-----------------------------------------------

With all other programs, which I have tested, it runs ok. But Test.java can not be BCELified and then again with it correct created.

Btw: BCEL-Version: 5.2 (the latest one).

Greatings
theuserbl

 		 	   		  
_________________________________________________________________
http://redirect.gimas.net/?n=M0911xDateiensenden3
Große Daten übertragen? Ganz einfach mit dem Messenger!