Comment / annotate old code while debugging without changing - java

I often have to debug old legacy code in Eclipse which I cannot or should not change. The code is often very long spaghetti code with confusing variable names.
While I debug I normally find out how things work.
I would now like to make comments on variables, methods and lines which are only disabled while I debug the code but will not change the code file itself. I could even think of changing the name of a variable from XI to countRowsProcessed just in the debugger.
Is there any plugin or maybe IDE which can do such a thing?

When Eclipse is debugging, it takes the current class file and "attaches" a source file to it, which is the source file you interact with.
I'm not sure how to do this, but you can probably make a copy of your source file, say sourceFile-wDebugComments.java and tell Eclipse to use that file for debugging purposes, rather than the original one. This way, it's not a problem to add comments, as long as you don't insert new lines in the source file.
As for renaming variables, I'm afraid vanilla Eclipse doesn't allow that.
Anyway, you might just make a copy of your whole source tree, and then modify the source file you normally can't modify. In general "can't modify" is referred to the copy on the source trunk. You can always do what you want on your local copies. If you then want to keep the thing, you can store the diff from the original file as a .diff or .patch file, and re-apply it every time you need it.

Related

Eclipse - renaming a package got rid of a bunch of stuff, is there a way to recover it?

So I made a package, lets call it dev.example.project for example purposes. all my other packages extended off of that one, with names like dev.example.project.handler or dev.example.project.assets. But I decided to go back and change the dev.example.project package's name to something else, like dev.betterexample.project.
I thought it would be a simple matter of renaming all the other packages and import statements so no errors would happen, but right after i changed the name, all the packages in my project turned white and became inaccessible. I changed the name back to what it originally was, but when i opened Eclipse again, some packages and files were simply not there.
I spent a lot of time on this project, someone help?
Depending how exactly you did that renaming, various things could have gone wrong.
Remember: you can always go in and work outside of eclipse. Close it, open a file explorer and create directories that use the names you want to use. Then move files manually to their target directories. If you want to, open them in another text editor and fix the package lines. When you now open eclipse, do a refresh and full rebuild. Things should be fine then.
That is probably not the most elegant solution, but it always works. And more importantly, you are in full control of everything that happens!
And for the record: the real answer to avoid catastrophic loss of code is to use a source code management system such as git. And to then push your changes constantly out to that "backup" facility.

copy source and form from one pc to another and jframe form no longer works

I tried to copy a source package from one pc to another. I did this by copying and pasting the source code. I see this was the wrong approach because the copy did not copy something. I have resolved these issues in the programs that did not have forms. However in the one program which uses a form I get the following message:
"The form seems to be corrupted. The GUI builder is not able to find the sections with the generated code. The special comments that denote the start and the end of these sections were removed or modified. The form will only be opened in read mode"
The result is that I cannot make modifications to the forms. I cannot go back to the original pc to do the whole process correctly, since it has crashed. How can I regenerate the lost special comments that denote the start and the end of the generated code? I compared the generated code from the original source to the generated code which I copied. I cannot see any difference.
I also researched the link http://wiki.netbeans.org/FormGuardedBlockError but did not obtain any useful information.

Change compiled code while debugging in Eclipse

It may sound stupid but: is it possible to tell Eclipse to change some already compiled code (in a .class file) while debugging?
I would like to check a couple of things for some values. I know that if it is compiled I cannot change it, so it makes no sense to try this. Well, in this concrete case it does make sense.
I am also aware of the hot code replace functionality of Eclipse but it doesn't work for me because it is for source files.
EDIT: In my case I don't want to change the value of a variable. I would like to put a 1 instead of a 0 in this call within a .class file
getTabFolder().setSelection(0);
If it is just a variable value you want to change you can do this.
When you are debugging in the debug view of eclipse there is a variables window. If you right click on the variable you wish to change and press 'Change Value' a window will pop up and you can change the value of that variable in there.
I think that you can change body of existing methods, but you cannot add new methods.
You can also change variable values.
It depends, if you are running the code locally Eclipse supports hot swapping.
You can find some info here.
If you change the code While debugging, eclipse will automatically transfer it to the target VM

Debugging using Java source code

Is there a way to include whole Java source code into an eclipse project so the program is easier to debbug (e.g. by inserting println in methods you otherwise couldn't insert anything)?
I have a bug in my code. But to better understand why the bug in my code appears, I'd like to see what intermediate results in some system method (on which use the bug occurs) are.
For example, I'd like to know what JViewPort.scrollRectToVisible() exactly does and how my input behaves in it by printing out some intermediate results that occur in the method itself.
EDIT:
Instead of using JRE System Library X, I want to add the source code from JDK as if I had written the code myself. I want to be able to edit any System class just as I am able to edit any class I created myself. I want editable .java files, not packed .jars...
You would need to add the 3rd-party library to your Eclipse workspace as an project. (How you would do that depends on the code you are dealing with.) Once you have done that, you can hack your copy of the library to add trace prints etcetera.
A better alternative is to simply attach the source code for the 3rd-party library so that the debugger can show you source as you step through the code, set breakpoints,. Then use "advanced breakpoint" techniques instead of trace prints; e.g. http://www.vogella.com/articles/EclipseDebugging/article.html#advanced
You cannot change the library code, but you can view it by using de-compiler. The max you can do is this. Now if you change any code in the libraries which you reached via the de-compiler, you would find an error stating "the integrity of the .jar package has changed which is not allowed"
Eclipse have built-in support for what you wanna do.
All you have to do is set breakpoint and execute application in debugging mode.
You can use these icons in Eclipse debugging perspective.
Follow along Eclipse Debugging Tutorial for details.

Can jar files be decompiled to get original source?

I have a major problem that happened to me. I am writing a program that has taken me 3 months+ to make and today I accidentally deleted the source code.
I am working in NetBeans and I had just cleaned and built it, so I still have the working .jar file. The program was saved on a flash drive, not my computer, so the classes are not in the recycling bin.
Can anyone tell me about how I can go about getting my source code back?
Unless you explicitly packaged your source code in the JAR file, there's no way to get back the original source. You could try using a decompiler (like this) but that will hardly give you nice, readable code.
The best solution would be to stop using your computer right now. Then use a recovery tool to recover your deleted files. The more recently it was deleted, the higher the chance you'll get it back. There are lots of tools that can be used to do this (just Google it).
You can use a decompiler ( see How to decompile a whole Jar file? ) but you won't be getting "original" source back.

Categories