Re: Trying fo add several fields.

Scott Vachalek <[email protected]>
Newsgroups gmane.comp.java.objectweb.asm
Message-ID <[email protected]>
Hi Adolfo,

I just noticed no one ever responded to this.  I think the problem is that
you are calling visitEnd after every field, when you should only call it at
the end of the class.  I wouldn't mix ClassNodes with direct calls to visit
for this purpose either; it is better to stick with a pure visitor pattern
or create FieldNodes and add them to the "fields" of the ClassNode.

Scott


On Fri, Oct 2, 2009 at 7:05 AM, <[email protected]> wrote:

> Hi all, currently I'm working in something using asm, I'm trying to
> genereta a
> .class from a certain file. My file describe an abstract class which could
> contain attributes, and methods.
>
> Something as.
>
> Class Test
>  attribute public foo:Integer
>  attribute public bar:Integer
> end
>
> My problem comes when I try to put the attributes inside the new class, I
> first
> put the class information in a class node, and then I call accept(cw),
> where cw
> is an instance of ClassWriter. I managed to generate the .class which
> contains
> the abstract class I declared without any problem, but from the n
> attributes
> which could be declared I just get the first one. I'm putting the
> attributes
> like this :
>
>  for (Attribute a : clazz.getAttributes ())
>            {
>
>                System.out.println ("the type is: "+ a.getType ());
>
>                if (a.getType ().equals ("Integer"))
>                    type = "I";
>                else if (a.getType ().equals ("Number"))
>                    type = "D";
>                else if (a.getType ().equals("String"))
>                    type = "Ljava/lang/String;";
>                else
>                    type = "L"+pkgName+"/"+a.getType ()+";";
>
>
>                int visibility =  Opcodes.ACC_PRIVATE ;
>                if ( a.getVisibility ().equals ("public"))
>                    visibility = Opcodes.ACC_PUBLIC;
>
>
> cw.visitField(visibility,a.getName(),type,null,null).visitEnd();
>
>        }
>
> From the examples I have seen, I understand that  that should work, but It
> doesn't .
> I tried using FieldNode and then using the accept (cw), but It didn't work
> neither.
>
> For the example above, The result is  something like
>
> public abstract class Test.Test extends java.lang.Object{
>    public int foo;
> }
> There is one attribute missing.
>
> Thanks all.
>
> Adolfo
>
>
> --
> You receive this message as a subscriber of the [email protected] mailing list.
> To unsubscribe: mailto:[email protected]
> For general help: mailto:[email protected]?subject=help
> OW2 mailing lists service home page: http://www.ow2.org/wws
>
>
message-footer.txt (text/plain, 238 B)
-- 
You receive this message as a subscriber of the [email protected] mailing list.
To unsubscribe: mailto:[email protected]
For general help: mailto:[email protected]?subject=help
OW2 mailing lists service home page: http://www.ow2.org/wws
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.