I am writing a lot of unit tests these days. And I want to minimize the amount of typing I have to do.
So I have created several Eclipse coding templates.
Most of them work fine. But, recently I want to do a bit more advanced stuff.
I use EasyMock and when writing expectations , I find myself writing stuff over and over again. I want a code template that is context-aware of the objects, and once selected, i want it to present me with the methods of this object.
So far, I have this template, and it works fine (it shows me all objects that are in my test):
EasyMock.expect(${a:var}.${cursor});
It will write me the EasyMock.expect part, and then present me with a list of objects I can select. Once selected, I get a dot and the cursor at the right, so all I need to do is press CTRL-SPACE in order to see the methods of this object.
What I want is to be presented with this list of methods.
Is this possible? And if yes, how? I can't find any solution. Perhaps there is no such template variable yet?
EasyMock.expect(${a:var}${cursor});
And then type a dot after selecting it (instead of pressing ctrl-space), which will automatically trigger content-assist
Related
Hard to explain, but I remember in Eclipse there was very handy to use Ctrl+Space with builders or DTOs.
But in Intellij IDEA if I want to fill some DTO I'm writing:
Dto dto = new Dto;
dto.
/\
||
Pressing Ctrl+Space here and see some setter, for example .setId.
OK, I'm pressing enter end entering value. Then I want to set another value, not Id, because Id was already set, I'm writing:
Dto dto = new Dto;
dto.setId(id);
dto.
/\
||
Pressing Ctrl+Space here and what do I see?
Maybe some other setter? No, the same setId!!
Eclipse understands that this method was already used and in 99% of cases there no need to call it again in this scope or by near the same call.
So my question: is there some setting or plugin to make my life easier?
UPD:
I want IDEA to understand that I've already used some method 1 line above and don't offer it any more. I suppose that situation when you need to call some method 2 times in row is very rare. And also it should understand that if I'm writing block of code like
Dto dto = new Dto();
dto.method1();
dto.method2();
dto.method3();
dto.method4();
dto.
/\
||
Here I want IDEA to offer me method5() or method6() but not methods 1-4.
1-4 should be in the very bottom of the list of offers.
Honestly, I find IDEA scarily good at reading my mind of what method I want to call next. And it usually just takes a couple letters for it to narrow down what I'm looking for pretty precisely.
But, I think what you may be looking for is the "Sort lookup items lexicographically" option, in the Settings window under Editor / General / Code Completion. This always sorts the list by the name, rather than by predicting a few things to put at the top.
You can read more about it and the other code completion options in the IntelliJ IDEA documentation.
I am using IntelliJ IDEA 15, and I noticed that (by default) Eclipse offers a much more convenient auto-completion, when it comes to writing method calls, which require multiple parameters.
Eclipse automatically fills in default parameters and allows you to quickly navigate through them by pressing Tab:
However, IntelliJ IDEA does not handle it as conveniently and makes you write them manually:
Is there a way to make IntelliJ IDEA, handle method call auto-completion in a similar way to Eclipse's, and pre-write all the parameters for you, having you just press Tab (or another key) to navigate through them? If it is not possible in original IntelliJ, is there an add-on/plugin/external tool that will improve the intelligent code completion in such cases?
Note: I am not being lazy, it just gets quite annoying, having to manually complete each parameter and put a comma after each one, when code completion should do it for you.
IntelliJ doesn't do it and AFAIK there isn't a plugin for it.
There is no solution for you (unless you built a plugin yourself, but then you'd just have another job to maintain that plugin)
The alternative is to break the habit/need for it and use IntelliJ's Code Completion shortcuts:
Ctrl+P to view possible parameters for function.
Ctrl+Shift+Space to list the possible variables that could be entered as a parameter in the respective parameter position (based on type).
This also enters a comma if another parameter is required for the function.
Hardcoding numbers/strings as parameters to a custom function wouldn't be advisable as that negates the point of the parameters. It's better practice and more common to pass in a pre-defined variable; At which point Ctrl+Shift+Space is the easiest way for Code Completion.
This would also prevent you from closing quotations and adding commas.
Also note: IntelliSense is Microsoft's Intelligent Code Completion implementation which neither IntelliJ nor Eclipse uses.
As frant.hartm states in their answer:
Unfortunately the view of Intellij developers is that this feature would be too error prone. See this issue on youtrack.
They even state that people are welcome to make a plugin that does this.
The closest thing to this AFAIK is "method parameter completion", which allows you to auto complete parameters of current method as parameters of the inner method call at once (works for methods and constructors when calling super()).
Unfortunately the view of Intellij developers is that this feature would be too error prone. See this issue on youtrack.
IDEA doesn't fill the arguments automatically. You can use Ctrl+Shift+Space for auto-completion (completion is based on type, not name) or Ctrl+Alt+Space for suggestion. Or Ctrl+P to see what arguments are accepted.
Try
Ctrl + Space
for
Basic Code Completion
And like previously was written
Ctrl + Shift + Space
for
Type Code Completion
or try the second variant TWICE. More about Auto-Completing Code is in here
I know with javax.tools.* it is possible, but since this is not included in the Android API, I'm desperately wondering, is this possible?
Right now, my goal is to create a drag-and-drop tool to allow users to create their own layouts (as not everyone wants to learn Mobile Development, as it requires a lot of time, dedication and practice) similar to how Android Studio does it's own. However, of course the most important thing is to implement functionality via onClickListener and onTouchListeners. I've begun remedying this by creating my own DSL (Domain-Specific-Language) with a GUI front-end allowing users to choose what they want via PopupMenu and SubMenus. For example...
Statements
{ if, for, while }
Statements must be followed immediately by a reference and then a conditional (obtained from that reference), like a "if(Object.conditional())" statement.
References
{ Object1, Object2, Object3 }
The objects are references to other Views (I.E, Buttons, Layouts, WebView, etc.).
Conditionals|Actions|Getters|Setters
{ isSomething(), doSomething(), getSomething(), setSomething() }
Each Reference's methods, wrapped so that each wrapper keeps track of it's method's attributes and description (hence documentation).
It would go something like such...
IF ImageView1.isVisible()
ImageView1.setVisible(false)
ELSE
ImageView1.setVisible(true)
Of course, the method setVisible(boolean) is a wrapped version of setVisiblity(int).None of this is typed, it is obtained from a simple PopupMenu which shows them the applicable selections based on current context.
How I plan on transcribing this to compiling code was to convert the statement into Java code, inserting references on the fly as they are needed (I.E, ImageView1 would be defined in java as private ImageView ImageView1;), generate methods somewhat similar to how ButterKnife generates it's extra classes for it's onClick and onTouch annotations, etc.
Then, after planning all of this (been working on it for 2 weeks now), I find out that Android does not have support for compiling code like this. Please tell me something like this is possible. It's something I 100% wanted to do. Is this possible with any third party libraries?
If not, is there some possible way to mimic doing so? I could do it the long and slow way, of preparing every such possible way, keeping track of the references myself through a map, and when it is about to be called, directly call the implemented method for the View associated with that key, which theoretically COULD work. In fact, that'd be my second go-to if I can't. It'd be messy though.
Sorry if this is too long, I just want to get this to work.
TL;DR: Is there a way to compile a generated Java file created at Runtime in Android (since javax.tools.* does not exist), and if not what would be the best way to do so?
I've been rewriting a code base and I've been trying to workout where in Eclipse I can remove all occurrence of a function and any argument/param it may have.
I'm not after clever regex solutions or anything like that, I'm sure the functionality would be in Eclipse, just not sure where ?:)
Many thanks
Just to clarify:
Here's an example...
The function's class myFoo no longer exists. Because it has been refactored incorrectly before arriving with me. Thus I want to search the code base (via an eclipse tool) and remove any line that uses the foo(String string) method from the myFoo class.
Hilight the object (method or variable), then select menu "Search/Refrences" or "Search/Declarations". Do the changes manually, one at a time, so you can review each change. I would never make widespread code changes with a script due to the high probability of unintended consequences.
I'd use replace from the edit menu with
replace='function name' to '// TODO:'
This would highlight them on the gutter. Select that line and press Ctrl + D to delete that line.
It may be an option to remove the body of the function itself and then inline it via Refactor->Inline?
If needed you can of course save the function's code and insert it again afterwards.
I don't know of such a functionality within the JDT. But here is an automated solution you could use:
With MoDisco you can create a model of your java projects. Then you can use an ATL refining transformation to remove all invocations of that method in that model. You can even replace the invocation with some other expressions, if you want to replace the functionality.
If your codebase is large enough than its probably a good idea to have a closer look at these eclipse projects.
Just type a 'MUST_BE_DELETED' or something similar at the beginning/middle/end of the method name (don't use the rename function) then eclipse will highlight all the places that call it in the problems window. When you've fixed all the problems you can go and delete the method.
If you have code in jsp pages then you'll have to manually search for those.
That's what I used to do:
put your cursor at the method name, and press Control+Shift+g (in Windows), or you can do Search -> Reference -> Workspace.
This will search all reference to this method, in the workspace, and remove the reference manually
(Copied from my comment)
If you insist to have the "Remove" done automatically, there is another workaround (may not always works, depends on situation):
make your method body empty,
and then use Refactor -> Inline (you can even choose to delete method declaration)
But I believe there are some cases that this approach is not working. Worth a try to see if it works for your case anyway
Use ctrl+F short cut to open find/replace dialog.Than write your method name in space front of "Find" textbox .Do not write anything in "Replace With" textbox.
Then press "Replace All" button.
This is the most shortcut method.
enjoy :)
I want to create an object of a class in and then set all the properties in it using setter methods. There are more than 150 setter methods and I do want to type each one of them or even type in the object instance name Object instance type in dot . and then hit the spacebar for Eclipse to give me suggestions and then go and select the setter method. I do not want to do that 150 times.
Thus, I was looking for some sort of shortcut in Eclipse that allows you to call all the setters on the method. So like you type in the instance name and Eclipse calls all the setter methods e.g.
instanceName.setterOne("valOne");
instanceName.setterTwo("valOne");
instanceName.setterThree("valOne");
I cannot create another constructor in the the class, I am not allowed to do so
From my experience last time , I cannot find eclipse has such feature .The most that I can do is open the Type Hierarchy View (by pressing F4 when viewing that class ), and then sort by the method name of that class and copy all the setters for further edit.
Or , you can use reflection to find out all the methods of this class , and print out the setter calls . Suppose this class is called Foo , you can have something like this:
for (Method m : Foo.class.getMethods()) {
if (m.getName().startsWith("set")) {
System.out.println(String.format("instanceName.%s(\"valOne\");", m.getName()));
}
}
See this question: Generate all setXXX calls of a POJO in Eclipse?
It has a great and simple way of doing what you want to do.
Oh and try to ignore the haters!
If you're using IntelliJ, check out this plugin:
https://plugins.jetbrains.com/plugin/9360-generateallsetter
I do not want to do that 150 times
I'm not aware of any Eclipse feature that will help you here.
You could fall back to using something clunky like emacs keyboard macros or perl scripting to massage the source code outside of Eclipse.
Your other option (though it probably won't be productive ...) is to request that the cumbersome API be revised; e.g. to support the "builder" pattern.
I would seriously consider redesigning your class, given that you have reached this situation. Without knowing much about this class or your goals, I would venture that there is almost no reason to have 150 individual fields, especially if they are of the same type. Think about it - if you are already in this predicament, how easy will it be to maintain this code in the future?
If the fields are all of the same type, consider using an array, a List or a Map (property->value). If they are many diverse types, think about how you might break them up into component classes that could be more easily managed.
I have Eclipse 4.6.0 - which has an option to auto-generate getters and setters (not sure about earlier versions of Eclipse).
(a) Select your class (and right click)
(b) Select Source (Shift+Alt+S)
(c) Select Getters and Setters
The resultant pop-up window (Generate getters & setters) allows you to select ALL or Individual fields.
Hope this helps (if still relevant).
Here are some tips I used -
Ctrl+Shift+A - > eclipse block selection (this is the magic tool 01)
Ctrl + F -> eclipse find/ find and replace ( magic tool 02)
Ctrl +Shift + X -> to upper case (tool 03)
Here how I did
block selection -> select all field names (tool 01)
Ex : user
select all first letters and make the first letter uppercase (tool 01/03)
Ex : User
select all and put the word set (tool 01)
Ex: setUser
Align all fields nicely in line. So you can use the block selector straight
select all and set "();"
Ex: setUser();
now you have all the setters ready
When you want to insert a value inside parentheses you can use magic tool 02. Find and replace ( think a little and you will see an option )
Finally, save your time of boredom from getters/setters settings.
cheers !
I created my own more general solution to this problem based on #Ken Chan anwser, which set not only basic types. It is far from perfect, still to some it might be some starting point.
Code is here