1

In my team we are using java 1.4.2 Most of the machine for loop is getting compiled as for only.ie. if i decompile the class file I can get see for loop only but certain machines of certain developer's it becomes do while loop. ie when i decompile certain classes it becomes do while

How can it happen? Any possible reason, java version or configuration any body can think so i can reproduce this defect and fix it in all developers machines

user1228785
  • 512
  • 2
  • 6
  • 19
  • 4
    The compiler will perform optimisations. Why does it matter? – Oliver Charlesworth Feb 23 '12 at 16:01
  • What is your current for loop code? Exactly what Oli said, it does optimizations. – Halfwarr Feb 23 '12 at 16:02
  • 1) `1.4.2`? 2) why do you need decompile your own code? Maybe it depends on some compiler optimization, but I should worry first about the compiler version, then about why do I need to decompile, third about the for -> do-while thing... – helios Feb 23 '12 at 16:03
  • 1
    The decompiler does not always guess right what was that thing that got converted to a conditional jump forward / unconditional jump back. It does not matter a slightest bit. – Sergey Kalinichenko Feb 23 '12 at 16:03
  • We deliver our code to site. many times we need to compare clearcase code and site code to ensure they are same as at time site code undergoes some modification.So when we compare class files we see this difference. For loop code is simple for loop code.SO when i compile 2 class files in opne class file i can see it has for loop and other class file as do while – user1228785 Feb 23 '12 at 16:08
  • On similar way 1 class file as a!=null and in other as null!=a many such minor things do happen.That's why I want to know 2 same codes compiled in more or less similar java version on doing beyond compare and decompiling why does this difference occurs – user1228785 Feb 23 '12 at 16:10
  • 1
    @user1228785: If you deliver source code, then just compare the source code. If you want to know if the binary is different, then use an md5sum or something. – Oliver Charlesworth Feb 23 '12 at 16:15
  • No we deliver class files. I doubt md5sum would help – user1228785 Feb 23 '12 at 16:17
  • @user1228785: Are you clients manually modifying the class files? – Oliver Charlesworth Feb 23 '12 at 16:58
  • issue is i need to know any compiler optimization setting that may cause for loop to be compiled as do while – user1228785 Feb 23 '12 at 17:01

5 Answers5

7

I would not call this a defect. When you compile Java to bytecodes, some information is lost. When you subsequently decompile the bytecodes, there is no guarantee that the resulting Java source will closely match what you've started with.

In particular, the bytecode language has no specific instructions for different types of loop. Java loops get compiled into bytecodes that use comparison instructions and jumps. The decompiler has to make an educated guess when deciding which type of loop was used to produce the given bytecodes.

The difference in behaviour across machines probably has to do with differences in the exact versions of the compiler and the decompiler installed on those machines, or perhaps with how those tools are configured.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • decompiler version is same . So that's what I wanted to know If anybody has any idea what compiler difference or compiler configuration can result in such scenarios So I can fix it. As we have team of 40+ developers – user1228785 Feb 23 '12 at 16:15
  • 1
    @user1228785: If I were in your shoes, I would have a hard look at the version control practices at your shop. Having a development process that relies on looking at decompiled bytecodes sounds rather iffy. – NPE Feb 23 '12 at 16:22
  • this need to be done for older versions the product is running for last 10+yrs – user1228785 Feb 23 '12 at 17:27
6

The code for while and for are interchangable and there is no way to tell from the byte code which one was used (you could infer it) You can't reproduce comments from byte code and you cannot reliably tell the different between a for and while loop.

e.g.

while(condition)

and

for(;condition;)

and

while(true) {
   if(!condition) break;

}

are the same.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
3

In the bytecode, there are no loops, there are only conditional and unconditional jumps (aka gotos). So the decompiler does its best to reconstruct what loop that was based on jumps' structure.

Fixpoint
  • 9,619
  • 17
  • 59
  • 78
1

Compiler optimizations will never change what the code actually does.

Even if different compilers are making different optimizations to your code, there will never be any difference in the program's semantics: the user will always get the same results.

This is not a bug in need of a fix. There is no fix. There is no bug.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
-1

it's probably caused by the compiler's optimization settings. check optimization settings on different machines. try to disable all optimizations or equal the optimization level.

Emir Akaydın
  • 5,708
  • 1
  • 29
  • 57