Eclipse auto-complete with place-holders? - java

When I invoke a method or constructor and use Ctrl-Space to select the signature I want, the output I get is shown below. Note that place-holders have been used for the arguments.
Now, if started with a non-argument constructor and use Ctrl-Space to pick the same signature:
I get this, note the lack of place-holders:
Is there any way to trigger Eclipse to put in place-holders in this latter example?

Uhm ... yes ... sort of. You can also delete the () and then Ctrl+Space which will get you back to the start.
But I'm not aware of any way to trigger the place holders again (which would often be useful when you already have 2 arguments and want to replace the following ones).

Related

How to precisely detect when a variable changes value using Eclipse?

There is a possibility to add a watchpoint in the Eclipse, like mentioned e.g. here How to detect when a variable changes value
After invoking the watchpoint, the class which contains watched field is displayed and I am able to see that a setter was called. What I would like to know, is where exactly, in which place in the code, the setter(or constructor) was called.
This existing answer suggests that there is no such feature.
But beyond that, there is a simply workaround: use eclipse to find all usages of the method/ctor that sets the thing you are interested in, and then put break points on each of those.
Alternatively, you could put a test in your code under test, to throw exceptions in certain cases, delivering you a nice stack trace containing the call chain.

IntelliJ- how to find method calls?

I have just started using IntelliJ for the first time on a project that I've just started working on, and am still getting familiar witht the setup and how it works. Previously when working on Java projects, I have predominantly used Eclipse as my IDE.
In Eclipse, when working on a particular method, there was the functionality to right-click the method, and select the option 'Find All References', to list everywhere in the project that the method was called.
I have made a few changes to the definitions of a couple of methods (the changes have mainly been in their parameters) in the project in IntelliJ, and now want to 'Find All References' on the methods, so that I can ensure that they are called with the correct parameters. However, when I right-click on the method definitions, and select 'Find Usages' from the menu, I get a popup message displaying the following warning:
Method 'abc() of class def' implements method of interface ghi. Do you want to find the usages of the base method?
Regardless of whether I select 'Yes' or 'No', the search results only return one result- the source file & line that I clicked on in order to do the search.
My guess is that there's something I need to change in the settings somewhere to ensure that doing this returns all of the places where that particular method is used in the code? I checked with a colleague, and when they do exactly the same thing, they get a list of all of the places within the project where that method is called...
How can I resolve this, so that I can find all of the method calls for the one I have highlighted?
Have you tried CTRL+click on method declaration?
I usually use Ctrl-Alt-H for Hierarchy and get a list of all callers even in tests.
By selecting the method name (double click on it) and pressing alt+ctrl+shift+f7 keys, a window named Fined Usages will popup. There is a checkbox in this window, you can check: Usages, Overriding methods, Search for text occurrences or Skip result tab with one usage. Then you can set the scope on All Places and click on Find. So, a search window appears and all classes and methods that called this method are listed there.
Alternatively, you can use "Find in path" option which lets you search for a raw string in any files in the project (although it's of course configurable). It is under Ctrl-Shift-F.

IntelliJ IDEA code completion show all constructor signatures?

I'm considering switching from Eclipse to IntelliJ IDEA, but there's one thing holding me back: the autocomplete. Eclipse shows me all the information I want from the start whereas IntelliJ holds back some information. The only thing I'm still missing is separate entries in code completion for each constructor signature. And I believe it's pretty weird that's missing since method signatures are done separately.
My question being, is it possible (am I overlooking something) to get IntelliJ to show separate entries for each signature for constructors WITH documentation, instead of just showing the class name and making you figure out afterwards if you're even in the right place or not.
Eclipse way of showing (preferred)
IntelliJ IDEA way of showing (not preferred)
This answer is base on IDEA version 2021.2.2
In the Settings -> Editor -> General -> Code Completion, check Show parameter name hints on completion under Parameter Info session.
Combine with Ctrl + Q, you can view the javadoc for each constructor.
Type the class name and the parenthesis. Inside parenthesis, type Ctrl+P. Eg: new BufferedWriter( <ctrl+P> )
In fact you can type, ctrl+P to get details of any function, not just constructor.
It is also possible to show overloaded constructors by enabling a hidden option. Invoke the Help | Find Action menu item and type Registry to go to the Registry. Here enable the java.completion.show.constructors option.
Copied From: https://stackoverflow.com/a/43639241/2920861

Eclipse - how to debug input parameter change

I have the following problem, we might even call it a classic one:
public void myMethod(Map<Object, Object> parameter){
someOtherObject.method(parameter);
.
.
.
someOtherThirdPartyObject.method(parameter);
}
And suddenly, in the end some method touched the input parameter Map, and I don't know where and how. Now, I know it would be desirable to make the parameter immutable, but it is not and that is the root of the problem. For instance, the methods inside myMethod are intended to perform some validations, but they do some more as well, which is wrong by design.
So, the question is how to create a breakpoint in this method where the execution pauses if an attribute of this parameter Map changes? It might be a good idea to put a conditional breakpoint after each method call, but if you have 20-odd methods, it's rather painful.
How can I debug when this input parameter is changing?
What you want appears to be called a "watchpoint". I actually didn't know this functionality existed and I used to work on the Eclipse Project!
http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.cdt.doc.user%2Ftasks%2Fcdt_t_add_watch.htm
It looks like you'll have to figure out what fields are being editted and then set a "Write" watchpoint using the help document above.
Additionally, Eclipse highlights variables which are modified, so if you step over your method calls one by one you will be able to see which one is modifying the value (and which field is being modified) because it will be highlighted (bright yellow, by default) in the "variables" tab in the "debug" perspective. Once you know which method if modifying the data you can run debug again, but this time debug the method that changes the value and just keep repeating until you find the problem.
This is a classic problem solving scenario where you start with a very large search space and systematically and methodologically narrow it down until the search space is small enough for you to locate the problem.
If you're trying to locate a spot where your map is being modified incorrectly, you might want to first start at the higher levels of the myMethod. Put breakpoints around the methods called inside the myMethod method. At each breakpoint, look at the contents of the Map. Eclipse has a variable watch panel where you can see the contents of every variable at a specific moment in time.
When you hit the breakpoint where you notice something is wrong. Stop. You now know to dig into someOtherObject.method(parameter); assuming the data was changed at it's breakpoint.
Now, someotherObject.method will likely have other methods inside it. Put your breakpoints inside this method around all of it's function calls and repeat the process. Continue repeating until there are no more methods left. Eventually, you will narrow down the problem and have the answer.
Unfortunately, there is no magic "fix my code" button for these types of problems. It just takes good, old fashioned Sherlock Holmes style investigative skills and reasoning to eliminate areas of the code that you know aren't the problem until you're left with a smaller section that allows you to get at the root cause.
If no code modification is allowed, you can
use the watchpoints method described by acattle to watch changes at this specific map instance or
have breakpoints in the Map methods modifying its state (if you want to do that for multiple instances). It does not matter that the Map code is binary only, you can still open it using Ctrl-Shift-T (Open Type), select the methods like put(...) or remove(...) in the outline view and add breakpoints using the context menu in the outline view.

Refactoring tool in Eclipse

My need is pretty simple: I want to change a method call objClass1.method1() by a call objClass2.method2() in my whole Eclipse project. Unfortunately, I can't find a plugin able to do this. Can you help?
Edit:
To be more accurate, objClass1 is part of a third party library, so I need to change the method calls. I can't start at the method definition. When I right-click on a method1 call, I have no "rename" option in my "Refactor" menu.
I don't want to change or rename my methods. I want to exchange one call by another in my whole project.
An example of what needs to be done:
Before refactoring:
Injector injector=Guice.createInjector(new IContactModule());
After refactoring:
Injector injector=IContactInjectorSingleton.getInjector();
And this needs to be done a several points in my project.
What you ask for is no refactoring. A refactoring is defined as "a change that alters the code while not changing the behavior of the code". In this sense renaming a class or renaming a method is a refactoring (you change the code but the program does the same as before). But what you suggest does NOT preserve the behavior of the code so there will never be a "refactoring" for this.
Of course one might be able to write a plugin that is able to perform the text changes you want in a more or less safe way. But this will only work in very specific circumstances (what if your new method needs an argument the old one dons't need? What if there are more than one method with the same name but different parameters? ...). So I don't believe such a plugin exists, nor it makes much sense to develop such a plugin.
Just right click on the class/method name and choose Refactor > Rename.
EDIT:
To be more accurate, objClass1 is part of a third party library, so I need to change the method calls. I can't start at the method definition. When I right-click on a method1 call, I have no "rename" option in my "Refactor" menu.
Hence I would suggest you to simply make a replacement:
Search menu > File, type the old name, choose the context of the search ("Enclosing project"), click on Replace and type the new name.
EDIT2:
From the example you added to the question I think that a manual replacement, using the tool I just suggested, it's the best way. It's a complex issue, as #Arne pointed out, so it's better to make it in a controlled way. Moreover I doubt it is such a frequent operation to require a plugin to be built.
You could use the eclipse refactoring by selecting the methods name. Right click for context menu or Alt-Shift-R, in the Rename-Dialog a preview dialog is available which shows all suggested changes in one place.
First, move the body of objClass1.method1() into objClass2.method2(), and have method1 simply call method2. It may not be quite as "simple" as that, if for instance method1 uses fields of Class1 for instance, in which case you should probably include this as a parameter to the new method and perhaps use getters for the fields. If you can make the method static before doing this, it will be easier to avoid those kinds of problems. Anyway, make that transformation, so method1 is just calling method2. Now use the Inline Method refactoring to make method1 go away. You're done.

Categories