0

I would like to know which is in your opinion the best way to parse a Java file and automatically change either a variable name, a method name or the class name. I would like to do so because I want to offuscate some code by just changing one small part of it (one of the three cited above).

For example I could have a class that has a global variable public static final int index = 0 and I would like to change it to public static final int xxx = 0. Of course i should be replaced to xxx in each occurrence in the file. Same goes for class name or method name.

I've been told ANTLR may work for me, but I've never used it and I don't want to waste time learning it if then I discover it's not suited for my purpose.

Thanks

EDIT: I do not need to obfuscate the code, I need to change a small part of it. Either one of those mentioned above.

2
  • have you considered using pro-guard..?? Commented Dec 12, 2011 at 17:40
  • In your question you claim you want to "offuscate some code", but in your response to Oliver, you say "I don't need to obfuscate the code". So which is it? Commented Dec 12, 2011 at 18:26

3 Answers 3

2

If you only need to change a few such identifiers, then refactoring (supported by most IDEs, including Eclipse) is simple, quick, and reliable.

If you need to change a lot (for example, if you're trying to replace uses of english with roughly equivalent identifiers in a foreign language - e.g., counter => compteur), then I'd personally find myself using something scriptable, like sed or Perl. And I'd be very careful to make sure I was hitting exact matches (e.g., when changing lie to truth, that I don't also change belief' to 'betruthf)

One caution if you go with an automated, do-a-lot-at-a-time solution: be very sure you can test functionality before and after to assure that you haven't broken anything.

Sign up to request clarification or add additional context in comments.

Comments

1

Maybe the easiest is to use an existing code obfuscator, like the free Proguard : http://proguard.sourceforge.net/

1 Comment

I don't need to obfuscate the code. I need to change a small part of it. Either the class name, a variable name or a method name.
1

Use a code obfuscator to do the work for you.

Or use an IDE like Eclipse, which has this kind of thing built in using the Refactor menu.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.