Re: BCEL newbie question
Jacob Kjome <[email protected]> Sun, 30 Oct 2005 00:38:38 -0500
| Newsgroups | gmane.comp.jakarta.bcel.user |
|---|---|
| Message-ID | <[email protected]> |
At 10:15 PM 10/27/2005 -0700, you wrote:
>the problem is that the code you're generating is in fact really screwed
>up in many ways. class names are wrong, there aren't even enough
>methods, and maybe some other things.
It just occurred to me that you might be under the impression that I think
that the code below ought to generate a whole class. If so, no. The rest
of the class is being generated just fine. The *only* thing that is
generating badly is this tiny little static block. Again, I need this
(this was decompiled from a class that BCel generated from my existing
not-quite-working-properly code)...
static {
XMLC_GENERATED_CLASS = class$L$$XMLC_GENERATED$$$dyna$dyna01$html;
fDOMFactory =
XMLCDomFactoryCache.getFactory(class$Lorg$enhydra$xml$xmlc$dom$xerces$XercesHTMLDomFactory);
}
...to look like this (this was decompiled from a class that ASM2 generated
which works perfectly)...
static {
XMLC_GENERATED_CLASS = $$XMLC_GENERATED$$.dyna.dyna01.html.class;
fDOMFactory =
XMLCDomFactoryCache.getFactory(org.enhydra.xml.xmlc.dom.xerces.XercesHTMLDomFactory.class);
}
The original BCel code (which I believe was generated, but was commented
out because it wasn't working properly) was creating all kinds of extra
crap which I sifted out make it look at least close to code I want to
generate. The class names were always messed up, even in the code before I
manipulated it ("$" instead of "."). In fact, it bombed when attempting to
run the class generated by the original code. Although the code above is
malformed, the class actually successfully loads at runtime. It only craps
out when other code tries to access the fDOMFactory field which only
happens under rare circumstances. It is the rare circumstances that I want
to make work because there is no reason the class shouldn't work 100% properly.
> probably a good way to go would be
>for you to disassemble (using javap or some other tool) a class with the
>code you want and the class generated by bcel and compare them. then
>you can try to make them match by screwing around with the bcel code.
>if you can't figure out how to generate a particular segment, ask back here.
>
I am quite lost trying to figure out how to write BCel code to match the
disassembled code. Can you help out? Here's javap's output for the static
block in question...
static {};
Code:
0: getstatic #25; //Field class$0:Ljava/lang/Class;
3: dup
4: ifnonnull 32
7: pop
8: ldc #27; //String $$XMLC_GENERATED$$.dyna.dyna01.html
10: invokestatic #33; //Method
java/lang/Class.forName:(Ljava/lang/String;)Ljava/lang/Class;
13: dup
14: putstatic #25; //Field class$0:Ljava/lang/Class;
17: goto 32
20: new #35; //class java/lang/NoClassDefFoundError
23: dup_x1
24: swap
25: invokevirtual #41; //Method
java/lang/Throwable.getMessage:()Ljava/lang/String;
28: invokespecial #45; //Method
java/lang/NoClassDefFoundError."<init>":(Ljava/lang/String;)V
31: athrow
32: putstatic #47; //Field XMLC_GENERATED_CLASS:Ljava/lang/Class;
35: getstatic #49; //Field class$1:Ljava/lang/Class;
38: dup
39: ifnonnull 67
42: pop
43: ldc #51; //String
org.enhydra.xml.xmlc.dom.xerces.XercesHTMLDomFactory
45: invokestatic #33; //Method
java/lang/Class.forName:(Ljava/lang/String;)Ljava/lang/Class;
48: dup
49: putstatic #49; //Field class$1:Ljava/lang/Class;
52: goto 67
55: new #35; //class java/lang/NoClassDefFoundError
58: dup_x1
59: swap
60: invokevirtual #41; //Method
java/lang/Throwable.getMessage:()Ljava/lang/String;
63: invokespecial #45; //Method
java/lang/NoClassDefFoundError."<init>":(Ljava/lang/String;)V
66: athrow
67: invokestatic #57; //Method
org/enhydra/xml/xmlc/dom/XMLCDomFactoryCache.getFactory:(Ljava/lang/Class;)Lorg/enhydra/xml/xmlc/dom/XMLCDomFactory;
70: putstatic #59; //Field
fDOMFactory:Lorg/enhydra/xml/xmlc/dom/XMLCDomFactory;
73: return
Exception table:
from to target type
8 13 20 Class java/lang/ClassNotFoundException
43 48 55 Class java/lang/ClassNotFoundException
Here's the existing code block that needs to be modified to work properly...
private void createMethod_11() {
InstructionList il = new InstructionList();
MethodGen method = new MethodGen(ACC_STATIC, Type.VOID,
Type.NO_ARGS, new String[] {}, "<clinit>",
className, il, _cp);
il.append(_factory.createFieldAccess(className, "class$L" +
className.replace('.', '$'), new ObjectType(
"java.lang.Class"), Constants.GETSTATIC));
il.append(_factory.createFieldAccess(className,
"XMLC_GENERATED_CLASS",
new ObjectType("java.lang.Class"), Constants.PUTSTATIC));
il.append(_factory.createFieldAccess(className,
"class$Lorg$enhydra$xml$xmlc$dom$xerces$XercesHTMLDomFactory",
new ObjectType("java.lang.Class"),
Constants.GETSTATIC));
il.append(_factory.createInvoke("org.enhydra.xml.xmlc.dom.XMLCDomFactoryCache",
"getFactory", new
ObjectType("org.enhydra.xml.xmlc.dom.XMLCDomFactory"), new Type[] { new
ObjectType(
"java.lang.Class") }, Constants.INVOKESTATIC));
il.append(_factory.createFieldAccess(className, "fDOMFactory", new
ObjectType(
"org.enhydra.xml.xmlc.dom.XMLCDomFactory"),
Constants.PUTSTATIC));
il.append(InstructionFactory.createReturn(Type.VOID));
method.setMaxStack();
method.setMaxLocals();
_cg.addMethod(method.getMethod());
il.dispose();
}
thanks for the help!
Jake
>Jacob Kjome wrote:
>> Trying yet again. Is my question too newbie or something? I'm hoping
>> for a little direction here.
>>
>> thanks,
>>
>> Jake
>>
>> At 09:46 PM 9/19/2005 -0500, you wrote:
>> >
>> >Trying again. Any takers? Here's a link to the beginning of the thread
>> >for better readability....
>> >
>> >http://www.mail-archive.com/[email protected]/msg00780.html
>> >
>> >
>> >Jake
>> >
>> >At 12:24 AM 9/3/2005 -0500, you wrote:
>> > >
>> > >Is there someone that can comment on this? I'm sure I'm just missing
>> > >something small. But even though it is small, I'm still dead in the
>> water
>> > >until I get it figured out.
>> > >
>> > >thanks,
>> > >
>> > >Jake
>> > >
>> > >At 09:59 AM 8/27/2005 -0500, you wrote:
>> > > >
>> > > >How do I get this...
>> > > >
>> > > >private void createMethod_11() {
>> > > > InstructionList il = new InstructionList();
>> > > > MethodGen method = new MethodGen(ACC_STATIC | ACC_FINAL,
>> > > >Type.VOID, Type.NO_ARGS, new String[] {}, "<clinit>",
>> > > > className, il, _cp);
>> > > > il.append(_factory.createFieldAccess(className, "class$L" +
>> > > >className.replace('.', '$'), new ObjectType(
>> > > > "java.lang.Class"), Constants.GETSTATIC));
>> > > > il.append(_factory.createFieldAccess(className,
>> > > >"XMLC_GENERATED_CLASS",
>> > > > new ObjectType("java.lang.Class"),
>> Constants.PUTSTATIC));
>> > > > il.append(_factory.createFieldAccess(className,
>> > > >
>> > > >"class$Lorg$enhydra$xml$xmlc$dom$xerces$XercesHTMLDomFactory",
>> > > >
>> > > >new ObjectType("java.lang.Class"),
>> > > > Constants.GETSTATIC));
>> > > >
>> > >
>> >il.append(_factory.createInvoke("org.enhydra.xml.xmlc.dom.XMLCDomFacto
>> ryCa
>> > >che",
>> > > > "getFactory", new
>> > > >ObjectType("org.enhydra.xml.xmlc.dom.XMLCDomFactory"), new Type[]
>> { new
>> > > >ObjectType(
>> > > > "java.lang.Class") },
>> Constants.INVOKESTATIC));
>> > > > il.append(_factory.createFieldAccess(className,
>> "fDOMFactory", new
>> > > >ObjectType(
>> > > > "org.enhydra.xml.xmlc.dom.XMLCDomFactory"),
>> > > >Constants.PUTSTATIC));
>> > > > il.append(InstructionFactory.createReturn(Type.VOID));
>> > > > method.setMaxStack();
>> > > > method.setMaxLocals();
>> > > > _cg.addMethod(method.getMethod());
>> > > > il.dispose();
>> > > > }
>> > > >
>> > > >to generate this...
>> > > >
>> > > > static {
>> > > > XMLC_GENERATED_CLASS =
>> $$XMLC_GENERATED$$.dyna.dyna01.html.class;
>> > > > fDOMFactory =
>> > >
>> >XMLCDomFactoryCache.getFactory(org.enhydra.xml.xmlc.dom.xerces.XercesHTM
>> >LDom
>> > > >Factory.class);
>> > > > }
>> > > >
>> > > >currently, it is generating this...
>> > > >
>> > > > static final {
>> > > > XMLC_GENERATED_CLASS =
>> class$L$$XMLC_GENERATED$$$dyna$dyna01$html;
>> > > > fDOMFactory =
>> > >
>> >XMLCDomFactoryCache.getFactory(class$Lorg$enhydra$xml$xmlc$dom$xerces$Xe
>> >rces
>> > > >HTMLDomFactory);
>> > > > }
>> > > >
>> > > >
>> > > >The class was originally generated by the BCELifier (by someone
>> else on our
>> > > >team) based on an example generated XMLC (
>> http://xmlc.objectweb.org/ )
>> > > >class. We tweaked it for our purposes, but the code in the block
>> above
>> > > >never really worked so we had it commented out (I also removed
>> bits to
>> > > >avoid generating code that didn't need to be generated and got it
>> slimmed
>> > > >down to the code you see above). However, that means that some
>> > > >functionality isn't available where it works just fine for the
>> ASM2 version
>> > > >(built with the ASMifier). I would like the two to generate nearly
>> > > >identical code and this is the last little bit that I need to get
>> > > >working. Can someone please point out the syntax needed to
>> generate the
>> > > >proper code (like the former, which is spit out by the ASM2
>> version) rather
>> > > >than the improper code (like the latter, generated by the BCEL
>> code above,
>> > > >which is clearly incorrect, but I'm just not familiar enough with
>> these
>> > > >bytecode tools to know what the proper syntax is).
>> > > >
>> > > >thanks,
>> > > >
>> > > >
>> > > >Jake
>> > > >
>> > > >
>> > > >
>> > > >
>> > > >---------------------------------------------------------------------
>> > > >To unsubscribe, e-mail: [email protected]
>> > > >For additional commands, e-mail: [email protected]
>> > > >
>> > > >
>> > > >
>> > >
>> > >
>> > >---------------------------------------------------------------------
>> > >To unsubscribe, e-mail: [email protected]
>> > >For additional commands, e-mail: [email protected]
>> > >
>> > >
>> > >
>> >
>> >
>> >---------------------------------------------------------------------
>> >To unsubscribe, e-mail: [email protected]
>> >For additional commands, e-mail: [email protected]
>> >
>> >
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [email protected]
>> For additional commands, e-mail: [email protected]
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [email protected]
>For additional commands, e-mail: [email protected]
>
>
>