Re: Re: Re: Two MethodVisitor with the same instances of Labels

John Bergin <[email protected]> Thu, 20 Mar 2014 12:09:49 +0000
Newsgroups gmane.comp.java.objectweb.asm
Message-ID <[email protected]>
Grzesiek - if you want to stick with the Core API
then for each pass labels will be visited in the
same order and you could use your own mapping
for label names that is consistent across each
pass. You could add the following to your
MethodVisitor.

// LabelData is whatever you want to store about the labels.
static Map<Integer, LabelData> labels;

Integer currentLabel;

// This is visited before visitLabel.
visitTryCatchBlock(Label start, Label end, Label handler, String type) {
     addLabel(start);
     // And do the same for the remaining Label parameters.
}

visitLabel(Label label) {
     addLabel(label);
     currentLabel = getLabel(label);
}

// Took this from somewhere in the ASM code base I
// think I might have taken it from the Textifier class.
void addLabel(Label label) {
     LabelData ld = labels.get(label);
     if( ld == null ) {
         labels.put(Integer.valueOf(labels.size()), new LabelData());
     }
}

LabelData getLabel(Label label) {
     return labels.get( label );
}

Hope this helps.

On 19/03/14 16:06, Eugene Kuleshov wrote:
>     You can just extend MethodNode class, overwrite corresponding
> methods (just make sure to call super) to collect your information on
> the first pass and then you can call accept() on it only once.
>
>    regards,
>    Eugene
>
>
> On Wed, Mar 19, 2014 at 12:04 PM, Roberto Andrioli
> <[email protected]> wrote:
>> In TreeAPI you can visit the instructions list only. Some thing like:
>> methodNode. instructions.accept(visitor).
>>
>> The methodNode.accept(...) will reset the label if the methodNode was
>> already visited.
>>
>> So, my approach is: first, visiting only the instructions list, collect the
>> information that you need. then use the secondVisitor exactly how you doing.
>> :)
>>
>> Roberto
>>
>> On Mar 19, 2014 12:48 PM, <[email protected]> wrote:
>>>
>>> Hi !
>>>
>>> I need to visit method twice,
>>> during first time I gather information about labels (I add them to map
>>> with
>>> "state description")
>>> durign second time, when I encounter label I want to perform some actions
>>> depending  on "state" collected in previous visiting. Unfourtnatley it
>>> seem
>>> that "Label" object I get are different beetwen these two passes.
>>>
>>>
>>> I also try to use Tree Api, when in visitEnd Method I do
>>>
>>>
>>>      @Override
>>>      public void visitEnd()
>>>      {
>>>          this.accept(_firstVisitor);
>>>          _secodnVisitor.setData(_firstVisitor.getData());
>>>          this.accept(_secondVisitor);
>>>          super.visitEnd();
>>>      }
>>>
>>> but this also generates different Label objects.
>>>
>>>
>>> Is there any way to do it or I should try to Tree API editing of methods
>>> ??
>>>
>>>
>>> Thanks in advance,
>>>
>>> Grzesiek
>>>
>>>
>>> --
>>> 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
>>>
>>
>> --
>> 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