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 a c <he...@yahoo.co.uk> on 2003/08/10 15:00:44 UTC

Creating and returning a byte[]

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/

---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org


Re: Creating and returning a byte[]

Posted by adrian <he...@yahoo.co.uk>.
Problem partially solved at this end, it appears that
the code I was using, produced by the BCELifier and
copied and amended by me, to create a byte[] actually
creates an Object[] of byte[]s. The line:

il.append(oMyIF.createNewArray(newArrayType(Type.BYTE,
1), (short) 1));

should have been:

il.append(oMyIF.createNewArray(Type.BYTE, (short) 1));

Not sure where this error crept in, I'll investigate
and let the list know if it was due to my poor
copying!

Cheers.

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

Re: Creating and returning a byte[]

Posted by adrian <he...@yahoo.co.uk>.
Problem partially solved at this end, it appears that
the code I was using, produced by the BCELifier and
copied and amended by me, to create a byte[] actually
creates an Object[] of byte[]s. The line:

il.append(oMyIF.createNewArray(newArrayType(Type.BYTE,
1), (short) 1));

should have been:

il.append(oMyIF.createNewArray(Type.BYTE, (short) 1));

Not sure where this error crept in, I'll investigate
and let the list know if it was due to my poor
copying!

Cheers.

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

---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org


Re: Creating and returning a byte[]

Posted by adrian <he...@yahoo.co.uk>.
Rechecked the BCELifier code fragment and it appears
that this is where the problem may lie, unless there
is something I'm doing wrong to call BCELifier, but I
don't think so.

It would seem that an array of primitives (only
checked byte so far, need to check the other types) is
wrongly "decompiled" to be an Object[] of byte[]s.

Hope this helps.

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

Re: Creating and returning a byte[]

Posted by adrian <he...@yahoo.co.uk>.
Rechecked the BCELifier code fragment and it appears
that this is where the problem may lie, unless there
is something I'm doing wrong to call BCELifier, but I
don't think so.

It would seem that an array of primitives (only
checked byte so far, need to check the other types) is
wrongly "decompiled" to be an Object[] of byte[]s.

Hope this helps.

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

---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org