Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse: Is it possible to edit Java source known via source code "attachment"

Tags:

java

eclipse

jar

I am using Eclipse 3.4.2 to develop my code. As part of my project definition I reference a utility library to which I have attached the source code. So far, so good - I can see that source when I bring up classes from the library and while I am debugging.

Now however I would like to make a change to one of the classes while still retaining all the features of the Eclipse Java editor (specifically things like tool tips and quick fix). These features seem to work when I'm viewing the source (I can CTRL+LClick through method names for instance), but it is read-only. On the other hand I can explicitly open the source file which will allow me to edit it, but I lose all of the "smart" editing features.

I've recently switched to Eclipse from IntelliJ where this was possible so I'm hoping it is in Eclipse as well. Note that although I could simply include the code as a project in my workspace, I'd really rather not. The workspace is already quite large and I don't want to further slow Eclipse down by adding projects I rarely would ever touch.

like image 979
sfitts Avatar asked May 06 '09 16:05

sfitts


People also ask

How can I change source code in Eclipse?

In the model browser, right-click an item and select Edit Code to launch the corresponding source code in the Eclipse code editor. On the Eclipse code editor, highlight any items of interest and right-click to display the editing menu.

How do you change the source code in Java?

The IDE's built-in Source Editor enables you to view, create, and edit your Java source code. Open the Source Editor window by double-clicking a node in the Projects window, Files window, or Navigator window. Alternatively, you can open the Source Editor by choosing File > New to create a new file.

What is source attachment in Eclipse?

The Source Attachment dialog can be reached in several ways: Select a JAR in the Package Explorer and choose Properties > Source Attachment from the context menu or the File menu. Open the Java Build Path page of a project (File > Properties > Java Build Path). On the library page select a JAR and press Attach Source.


1 Answers

I am not sure I get your question right. When you add a precompiled library to your projects build path (the JAR) and attach source to this JAR, Eclipse will show you the source code when you click on a .class inside the JAR. The same goes for the debugger, which will also allow you to step through the code lines in the source, if the classes in the JAR were compiled with line number information.

Now what you seem to want to do is modify the classes inside the JAR (the source view is just an overlay which can even be off, if you attach a different version of the source), which is not possible, because they are wrapped up in binary form in the JAR archive - even though Eclipse is smart enough to display them individually.

I guess you would expect your changes to be hot-swapped into the running program by the debugger. This can only be done through a recompile once you finished your changes. Usually Eclipse does that automatically when you save a Java source file. As your source file is however not part of the workspace (or an external folder explicitly declared as Java source) - it will not do that recompile and swap.

I'd recommend to include the source of your external library as a project in Eclipse and not worry about performance too much - I work with 3.4.2 every day and my workspace has about 45 open projects with several 10.000 classes and millions of lines of code. I assign a Gigabyte of RAM to the Eclipse VM and have no problems with that on a Core2Duo 2.6GHz machine.

like image 114
Daniel Schneller Avatar answered Sep 23 '22 13:09

Daniel Schneller