jakarta-bcel-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a c <hexa...@yahoo.co.uk>
Subject Creating and returning a byte[]
Date Sun, 10 Aug 2003 13:00:44 GMT
Hope someone can spot my problem, being new to BCEL
I'm struggling a bit to see what I'm doing wrong.

The situation:
I'm trying to generate class files within an
application to allow users to store custom data so
that the application can be extended by the user.  It
may be that there is a better way to do this, but if I
can get this final method sorted I'll be happy as the
rest of the application seems to be working, at the
moment.

As seems to be the case with most new BCEL users I
created some test code to do roughly what I wanted,
compiled it and then ran it through BCELifier.  I then
took that code and tried to use it in the application,
all the methods bar this one worked fine, or appear to
at the moment.

Enough preamble, here's the original code, the BCEL
code, the application output when it tries to load my
new class, the output from Verifier and the results of
running JAD on the generated class file.

Original code:
=============   
    public byte[] getCustomData() {
        byte[] otest = new byte[]{(byte)12, (byte)14};
        return otest;
    }

=========================================================================================
BCELifier generated BCEL code:
=============================
    private void createComponentMethod_10(String
strClassName, byte[] oCustomData) {
        InstructionList il = new InstructionList();
        MethodGen method = new MethodGen(ACC_PUBLIC,
new ArrayType(Type.BYTE, 1), Type.NO_ARGS, new
String[] {  }, "getCustomData", strClassName, il,
oMyComponentInfoConstantPool);

        InstructionHandle ih_0 = il.append(new
PUSH(oMyComponentInfoConstantPool, 2));
       
il.append(oMyComponentInfoInstructionFactory.createNewArray(new
ArrayType(Type.BYTE, 1), (short) 1));
        il.append(InstructionConstants.DUP);
        il.append(new
PUSH(oMyComponentInfoConstantPool, 0));
        il.append(new
PUSH(oMyComponentInfoConstantPool, 12));
        il.append(InstructionConstants.BASTORE);
        il.append(InstructionConstants.DUP);
        il.append(new
PUSH(oMyComponentInfoConstantPool, 1));
        il.append(new
PUSH(oMyComponentInfoConstantPool, 14));
        il.append(InstructionConstants.BASTORE);
       
il.append(oMyComponentInfoInstructionFactory.createStore(Type.OBJECT,
1));
        InstructionHandle ih_19 =
il.append(oMyComponentInfoInstructionFactory.createLoad(Type.OBJECT,
1));
        InstructionHandle ih_20 =
il.append(oMyComponentInfoInstructionFactory.createReturn(Type.OBJECT));
        method.setMaxStack();
        method.setMaxLocals();
       
oMyComponentInfoClassGen.addMethod(method.getMethod());
        il.dispose();
    }

=========================================================================================
Trying to load into application:
===============================
Exception in thread "main" java.lang.VerifyError:
(class: Info/Components/aInfo, method: getCustomData
signature: ()[B) Expecting to find array of bytes on
stack

=========================================================================================
Verifier results:
================
Pass 3b, method number 0 ['public byte[]
getCustomData()']:
VERIFIED_REJECTED
Constraint violated in method 'public byte[]
getCustomData()':
Instruction BASTORE constraint violated: The
'arrayref' does not refer to an array with elements of
a Type.BYTE or Type.BOOLEAN but to an array of
'byte[]'.
InstructionHandle:    8: bastore[84](1)

Execution Frame:
Local Variables:
0: Info.Components.aInfo
1: <unknown object>
OperandStack:
Slots used: 4 MaxStack: 4.
int (Size: 1)
int (Size: 1)
byte[][] (Size: 1)
byte[][] (Size: 1)
Execution flow:
   0: iconst_2  [InstructionContext]
   1: anewarray 8       [InstructionContext]
   4: dup       [InstructionContext]
   5: iconst_0  [InstructionContext]
   6: bipush 12 [InstructionContext]
   8: bastore   [InstructionContext]
   
=========================================================================================
JAD results on class file:
=========================
    public byte[] getCustomData()
    {
        byte abyte0[] = {
            12, 14
        };
        return abyte0;
    }
=========================================================================================

I have another method returning a String[] which seems
to work, obviously the custom data will be slightly
more complex than {12, 14}, but I simplified it to try
and figure out the problem.

Sorry for the long message, but I wanted to try and
get everything I thought that would help into this
message.  Hope someone can spot the mistake and/or
tell me I'm not going mad or stupid, I suspect the
latter!

Adrian.

________________________________________________________________________
Want to chat instantly with your online friends?  Get the FREE Yahoo!
Messenger http://uk.messenger.yahoo.com/

Mime
View raw message