joedobo

Searching for byte code insertion points or method/field reference edit points.

4 posts in this topic

 

One of the more common issues I run into with byte code and WU updates is that line indexes (they are listed in the, "LineNumberTable" for method) change. Also, sometimes the  byte code indexes change (these are used with the expression editor and the, ".indexOfBytecode()"). What I often see is that the code doesn't change, it just get moved around. I think if I had a tool to search for a pattern and return a position my mods would be more stable.


How would you do this? Or maybe you got another approach. 


Right now I'm leaning toward using the code iterator to find the position of a bytecode pattern in the raw byte code. 
For line number something like this should work assuming I've got a bytecode index:

LineNumberAttribute table = (LineNumberAttribute) jAssistMethodData.getCodeAttribute().getAttribute(LineNumberAttribute.tag);

// jAssistMethodData is my custom JA object, just ignore it and assume you're working with a CodeAttribute.
int lineNumber = table.toLineNumber(byteCodeIndex);

Share this post


Link to post
Share on other sites

I see what you did there bdew. Find and replace works well for me also. I have been using the CodeReplacer bundled with modloader.

 

Here are the two things that often break:

1. JA CtMethod.insertAt(int lineNumber, String source)

2. JA ExprEditor, using either methodCall or fieldAccess,  and I have to use the .getLineNumber() to identify a method/field that occurs multiple times. The same thing with .indexOfBytecode() although I only use this when the editing target is buried in a block associated with a single line number (usualy some big and complex if statement).

 

I suppose I could write out the bytecode and do find-replace instead of using those two above. But it's so much easier to use them.

Share this post


Link to post
Share on other sites

I don't use insertAt/getLineNumber for exactly that reason.

 

Though i guess you could use the same technique i do to locate the place for modification and then use that with the above, but that feels... brittle :P

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now