Eclipse - make overriden methods and class final by default - java

When I tell to Eclipse to generate methods (Add unimplemented methods popup) from inherited interface/abstract class, can I configure it to make these methods final by default ?
I'm aware of the Save actions hook in Eclipse but there's no preconfigured action to specify something like: "Add final modifier to method signature" or am I missing something ?
Also, when I create a class, is there a way to check the checkbox final by default ? I know it's not hard to do it by hand but it could be useful for me in case I forgot to check it. I'm aware of this similar question but in 2011 there was no answer (maybe now there's one !).
PS: you can assume I'm using the latest version of Eclipse (Mars - 4.5.0).

Since I assume you don't like the answer given in the link you specified, which is to create your own custom 'new class wizard' for Eclipse, the answer is: No

Related

Not getting "Add unimplemented methods" error in eclipse

I am working on Selenium with Java and using Listeners.
I created the class, imported the TestNG, and then added method as;
Public class Listener implements ITestListener{
}
I got an error at "ITestListner" and then imported "ITestListner(org.testng). After this it was expected that I will get an error for "Listener" but I didn't get any error.
I tried creating with abstract class but still didn't get the error.
package com.testng;
import org.testng.ITestListener;
public class Listeners implements ITestListener{
}
The expectation is when I move the cursor over "Listeners" I should get "Add unimplemented methods" option.
Right click(on the Listeners class ) -> go to source-> click on overide/implement methods -> select the check boxes for the ITest listener (make sure all check box inside it should be checked )->click on oK. That's it !!!
As i got to know from the below link - TestNG 7.0.0 uses JDK8 and with it default methods within interfaces. What this means is that every listener would now have a default implementation (which doesn't do anything ) for all methods defined within the interface.
So now you won't see those indications from the IDE. The biggest benefit of default methods in interfaces is that let's say you are implementing an interface that has 10 methods defined in it, you can just implement whatever you need and the rest of the behavior comes via the default methods in the interface.
Reference-
https://github.com/cbeust/testng/issues/1964
Right click on the class --> select source --> choose override/implement methods --> then pick the methods you'd like to implement.
Try downgrading the TestNg version to 6.14.3.
It worked for me.
The Best solution is go to the source tab besides the File-Edit options and click the Overide/implement methods and you are set
All methods in ITestListener interface changed to default from 7 version onwards. You can call them with class object where you implemented interface.

this.localMethod() Inspection/Extraction in Intellij

My company's coding standards dictate that when invoking local methods, we have to prefix the call with this.
Is there an inspection I can enable in Intellij so that places where localMethod() are used instead of this.localMethod() are highlighted?
In addition to this, I frequently use the extract method function that Intellij offers, however the method call that gets left behind is localMethod() rather than this.localMethod(), is there a way I can alter the code that is inserted when using this extraction to include this automatically?
Yes, there is such an inspection. It's called "Instance method call not qualified with 'this".
I don't think there's an option for "Extract method" to always add 'this' qualifiers.

Eclipse: When displaying a list of methods (ctrl-space), is there a way to have methods in that class - bold?

Eclipse:
When displaying a list of methods (ctrl-space), is there a way to have methods found in that class have names in bold? Netbeans has or at least had a feature that would bold the names of methods found within that class while inherited methods remained normal text.
This made it very easy to know what methods are from within the class you are editing.
Is there a way to do this in the latest version of Eclipse?
Thanks.
It seems eclipse does not support it.
What I do in such cases (workaround):
Open the affected class (Navigate -> Open Type...)
Press CTRL-O, this will show the methods implemented in this class.
Press CTRL-O again, this will also show the inherited methods.

java code template

I'm trying to make a code template that will generate tostring, constructor from field, and a default constructor.
I already looked at Useful Eclipse Java Code Templates and in http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.jdt.doc.user/reference/ref-tostring-templates.htm but it was not what I was looking for.
I tried this plugin http://eclipse-jutils.sourceforge.net/ but I still need to manually select an option in the menu (and it doesn't have a "constructor from fields" option).
I need to generate these methods and constructors for more then 100 classes so this the best way i found coz eclipse dont give tool to do it for more then one class and for this one class that he give this tool i need to do it one by one the (generate tostring ,constructor from field and also default constructor)
i will love to some help or some advice on a way to create these methods for all my classes, automatically.
thanks in advance.
I don't know of a plugin that will do this for multiple classes.
I'd just do it manually, even though it'd take time.
You could also use reflection and a scripting language like Groovy/JRuby/etc. to create the constructors, and rely on something like Commons' ToStringBuilder to create a toString, or just use reflection again.
(One problem is if you don't want a property in the constructor or toString you need to have a mechanism to tell the generator as much.)
I have just used Practically Macros, within a few minutes of install from the market place, I could generate *constructors*, getters / setters, toString, hashcode and equals (basically chaining the standard eclipse commands) in a single command. Just what I was looking for and saved me loads of time. I can also see a lot more uses for it, well done to Earnst (the creator).

Can eclipse convert/refactor a method to a class?

This seems like it should be fairly straight-forward, but I can't see anything obvious. What I basically want to do it to point at a method and refactor->extract class. This would take the method in question to a new class with that method as top level public API. The refactoring would also drag any required methods and variables along with it to the new class, deleting them from the old class if nothing else in the old class is using it.
This is a repetitive task I often encounter when refactoring legacy code. Anyway, I'm currently using Eclipse 3.0.2, but would still be interested in the answer if its available in a more recent version of eclipse. Thanks!
I don't think this kind of refactoring exists yet.
Bug 225716 has been log for that kind of feature (since early 2008).
Bug 312347 would also be a good implementation of such a refactoring.
"Create a new class and move the relevant fields and methods from the old class into the new class."
I mention a workaround in this SO answer.
In Eclipse 3.7.1 there is an option to move methods and fields out of a class. To do so:
Make sure the destination class exists (empty class is fine, just as long as it exists in the project).
In the source class, select the methods that you want to remove (the outline view works great for this), right click on the selection, and choose Move
Select the destination class in the drop down/Browse
Your members are now extracted. Fix any visibility issues (Source > Generate Getters and Setters is very useful for this) and you are all set.
This seems like it should be fairly
straight-forward...
Actually, Extract Class is one of the more difficult refactorings. Even in your simple example of moving a single method and its dependencies, there are possible complications:
If the moved method might be used in code you don't know about, you need to have a proxy method in the original class that will delegate to (call) the moved method. (If your application is self-contained or if you know all the clients of the moved method, then the refactoring code could update the calling code.)
If the moved method is part of an interface or if the moved method is inherited, then you will also need to have a "proxy method".
Your method may call a private method/field that some other method calls. You need to choose a class for the called member (maybe in the class that uses it the most). You will need to change access from "private" to something more general.
Depending on how much the original class and the extracted class need to know about each other, one or both may need to have fields initialized that point to the other.
Etc.
This is why I encourage everybody to vote for bug 312347 to get fixed.
Have you tried the Move feature of the Refactor group ? You can create a helper class and move there anything you want.

Categories