Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why my compiled class has it's methods local variables renamed?

Tags:

java

javac

I have a Kitchen.jar file. I need to modify a class inside it. I decompile it with JD. Then I modify the Toster.java file and compile it with:

javac -classpath . Toster.java

And then I take it back into the Kitchen.jar with:

jar -uf Kitchen.jar Toster.class

All works except for one problem. When I open updated Kitchen.jar in JD I see that local variables inside all methods are renamed to something like localLongVar. Why?

The reason I ask is because Kitchen.jar refuses to work after the modification. And I suspect it has to be the compilation problem. Maybe I've misused some flags or anything. Not sure. I have no knowledge of Java whatsoever, except for the basic syntax.

My guess is that I compile it with latest 1.7 version and original jar is compiled with older JDK. That may explain failure of operation, but that doesn't explain the renaming of locals.

EXAMPLE

The random line from the original jar:

BigInteger[] result = new BigInteger[bis.length / 2];

And the very same line of my class:

BigInteger[] arrayOfBigInteger1 = new BigInteger[paramArrayOfBigInteger.length * 2];

So its result vs arrayOfBigInteger1.

like image 500
Aleksandr Makov Avatar asked Mar 12 '13 09:03

Aleksandr Makov


People also ask

Can local variables in different methods of the same class have the same name?

Answer: Yes — the scopes will not overlap so there will be two local variables, one per method, each with the same name.

Are method variables local?

Variables that are declared inside a method are called local variables because they can only be utilized and referenced in the method itself. Take a look at the code below showing the add() method with a local variable inside.

What is the variables declared in a class for the use of all methods of the class called?

Variables in method declarations—these are called parameters.

Can class name be same as variable name?

No variable name should not has same name with **any** class name.


2 Answers

By default javac removes debugging information other than source file and line number. Compile with javac -g or javac -g:vars.

From the documentation of javac

-g Generate all debugging information, including local variables. By default, only line number and source file information is generated.

-g:none Do not generate any debugging information.

-g:{keyword list} Generate only some kinds of debugging information, specified by a comma separated list of keywords. Valid keywords are:

source Source file debugging information

lines Line number debugging information

vars Local variable debugging information

like image 165
Javier Avatar answered Oct 29 '22 02:10

Javier


The names of the variables are not preserved in compiled code. Most obvious to reduce the size of the compiled class. The compiler will replace them by shorter names. Doing this is also good for obfuscating the code so that someone who decompiles the code has problems to understand the logic. The localLongVar you see in JD is what the compiler makes of the replaced variable names.

like image 37
Kai Avatar answered Oct 29 '22 03:10

Kai