I have a lot of code with method invocations like:
speak(name)
foo(bar, "string", var2)
I want to surround the parameter like:
speak(check(name))
foo(check(bar), "string", check(var2))
I need to do this for many methods and parameters!
How can I create a script/macro on eclipse so that I just click the parameters and press some key and it will surround it with check() method?
A non-answer: don't do this. Instead of changing
speak(name)
to
speak(check(name))
change it to
speakWithCheck(name)
Meaning: either simply use the refactoring capabilities of eclipse to change the method name; and all invocations to speakWithCheck(); or at least add that new method that clearly describes what it is doing instead of polluting many many places in your source code like this.
You can then declare speak() to be #deprecated; and over time get rid of that method altogether.
The fact that you can somehow make such "mass manipulation" of code doesn't mean that it is a good idea.
There are several plugins that support macro function (recording of keyboard clicks, play with a special key combo) check the eclipse marketplace https://marketplace.eclipse.org/category/free-tagging/keyboard-macros
Related
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'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 would like to know which is in your opinion the best way to parse a Java file and automatically change either a variable name, a method name or the class name. I would like to do so because I want to offuscate some code by just changing one small part of it (one of the three cited above).
For example I could have a class that has a global variable public static final int index = 0 and I would like to change it to public static final int xxx = 0. Of course i should be replaced to xxx in each occurrence in the file. Same goes for class name or method name.
I've been told ANTLR may work for me, but I've never used it and I don't want to waste time learning it if then I discover it's not suited for my purpose.
Thanks
EDIT:
I do not need to obfuscate the code, I need to change a small part of it. Either one of those mentioned above.
If you only need to change a few such identifiers, then refactoring (supported by most IDEs, including Eclipse) is simple, quick, and reliable.
If you need to change a lot (for example, if you're trying to replace uses of english with roughly equivalent identifiers in a foreign language - e.g., counter => compteur), then I'd personally find myself using something scriptable, like sed or Perl. And I'd be very careful to make sure I was hitting exact matches (e.g., when changing lie to truth, that I don't also change belief' to 'betruthf)
One caution if you go with an automated, do-a-lot-at-a-time solution: be very sure you can test functionality before and after to assure that you haven't broken anything.
Maybe the easiest is to use an existing code obfuscator, like the free Proguard :
http://proguard.sourceforge.net/
Use a code obfuscator to do the work for you.
Or use an IDE like Eclipse, which has this kind of thing built in using the Refactor menu.
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
I'd like to know how to - if even possible - reflect what method calls are executed inside the method during execution. I'm especially interested in either external method calls (that is, methods in other classes) or calling some specific method like getDatabaseConnection().
My intention would be to monitor predefined objects' actions inside methods and execute additional code if some specific conditions are met like some method is called with specific values. The monitor would be completely external class or a set of classes with no direct access to the object to be monitored by no other way than reflection.
Aspect J will solve your problem.
Try to define a pointcut like this:
pointcut profilling(): execution(public * *(..)) && (
within(com.myPackage..*) ||
In this way you will catch all the call to any public method within the package com.myPackage. Add as many within clauses you need.
Then add the following code:
Object around(): profilling() {
//Do wherever you need before method call
proceed();
//Do wherever you need after method call
}
IF you want to learn something more about aspectJ follow this guide.
I'd expect BCEL to be able to do this. From the web site:
The Byte Code Engineering Library is
intended to give users a convenient
possibility to analyze, create, and
manipulate (binary) Java class files
(those ending with .class).
The "analyze" part being the important bit here. The JavaDoc isn't visible on the web site (as far as I can see) so I can't easily be sure whether or not it'll help you, but it's a reasonable starting point.
BCEL should offer this capability, but ...
... your requirements sound a lot like Aspect-Oriented Programming (AOP), so you should probably also look at AspectJ (with Eclipse tooling).
The main advantage of AspectJ is that it offers a well-designed way to express your specific conditions.