How can I define custom code generation like Getters/Setters in IntelliJ. I took a look on their docs but they don't specify where I can do this. The code I would like IntelliJ to generate for me is like below:
public class Person {
private String name;
private String username;
//I want IntelliJ to propose me to generate this after Alt+Insert
public Person withName(String name){
setName(name);
return this;
}
//and this
public Person withUsername(String username){
setUsername(username);
return this;
}
}
Thanks a lot
When you press alt+insert you can click Getter and Setter. There are Getter template and Setter template drop-downs that you can select. Click the ... and you can create new templates.
It appears you're trying to follow the builder pattern. IntelliJ already has a setter template for this called "Builder". You can select it from the setter drop-down and you should be good.
Related
I am wondering if there is a shortcut to create full builder without manually adding fields one by one from class annotated with Builder?
Let's say we have a class:
#Builder
public class Classroom {
private Long id;
private String name;
private String location;
private String fullName;
}
It creates Classroom.builder().build(), but instead of that I want to instantly unfold a code with every field added like:
Classroom.builder()
.id()
.name()
.location()
.fullName()
.build()
Any Intellij shortcut to make that happend?
suppose i have a interface like this:
public interface Dumb{
String getDumbName();
}
Is there any shortcut or menu in intellij-idea to create new classes implementing the interface with dummy implement methods like this:
public class Dumber{
public String getDumbName(){
return null;
}
}
There are multiple ways to go about this.
On the interface name itself, you can hit Alt+Enter (Option+Enter on Mac), then pick 'Implement interface'. IDEA will prompt for a class name and a package to put the new class in, then generate an implementation class.
Alternatively, create the class, then add implements Dumb after the name (im<tab> Dumb). IDEA will complain that your class doesn't implement the correct methods, and offer (Alt+Enter Enter Enter) to generate them for you. Hitting Ctrl+I or clicking 'Implement methods' in the Code menu also works.
I am writing a new app and I have chosen to use Java for flexibility. It is a GUI app so I will use JavaFX. This is my first time using Java but I have experience with C#.
I am getting familiar with JavaFX Properties, they look like a great way of bi-directional binding between front-end and back-end.
My code uses classes from an open-source API, and I would like to convert the members of these classes to JavaFX Properties (String => StringProperty, etc). I believe this would be transparent to any objects that refer to these members.
Is it ok to do this?
Is it the suggested way of dealing with existing classes?
What do I do about Enum types? E.g. an enum member has it's value changed, how should I connect the enum member to the front-end?
Thank you :)
In general, as long as you don't change the public API of the class - in other words you don't remove any public methods, modify their parameter types or return types, or change their functionality - you should not break any code that uses them.
So, e.g. a change from
public class Foo {
private String bar ;
public String getBar() {
return bar ;
}
public void setBar(String bar) {
this.bar = bar ;
}
}
to
public class Foo {
private final StringProperty bar = new SimpleStringProperty();
public StringProperty barProperty() {
return bar ;
}
public String getBar() {
return barProperty().get();
}
public void setBar(String bar) {
barProperty().set(bar);
}
}
should not break any clients of the class Foo. The only possible problem is that classes that have subclassed Foo and overridden getBar() and/or setBar(...) might get unexpected behavior if their superclass is replaced with the new implementation (specifically, if getBar() and setBar(...) are not final, you have no way to enforce that getBar()==barProperty().get(), which is desirable).
For enums (and other objects) you can use an ObjectProperty<>:
Given
public enum Option { FIRST_CHOICE, SECOND_CHOICE, THIRD_CHOICE }
Then you can do
public class Foo {
private final ObjectProperty<Option> option = new SimpleObjectProperty<>();
public ObjectProperty<Option> optionProperty() {
return option ;
}
public Option getOption() {
return optionProperty().get();
}
public void setOption(Option choice) {
optionProperty().set(choice);
}
}
One caveat to all this is that you do introduce a dependency on the JavaFX API that wasn't previously present in these classes. JavaFX ships with the Oracle JDK, but it is not a full part of the JSE (e.g. it is not included in OpenJDK by default, and not included in some other JSE implementations). So in practice, you're highly unlikely to be able to persuade the developers of the open source library to accept your changes to the classes in the library. Since it's open source, you can of course maintain your own fork of the library with JavaFX properties, but then it will get tricky if you want to incorporate new versions of that library (you will need to merge two different sets of changes, essentially).
Another option is to use bound properties in the classes, and wrap them using a Java Bean Property Adapter. This is described in this question.
Im using IntelliJ IDEA 12. Can I customise code that is generated using "Refactor" functional?
For example I want to change the template of setting generation(Encapsulate fields) from:
public void setField(String field) {
this.field = field;
}
to
public MyClass setField(String field) {
this.field = field;
return this;
}
As far as I know, you can't do what you're suggesting in Intellij. There are plugins, however, that can help with the generation of fluent interfaces.
Some things you can change the template for. The setters and getters is not one of them. You can vote for the feature request IDEA-28206 Allow customization of generated getter/setter. Engineer Dollery is correct that there are some plugins for inserting customer setters & getters. But I do not believe there are any that will override the template used by the refactoring.
I was wondering if it is possible to change the auto-formatting in Android Studio in such a way that braces for anonymous classes are placed on the same line while still putting braces for regular classes, methods and blocks on a new line.
Currently, IntelliJ is giving me this result:
class TestClass
{
public void test()
{
FooBar foo = new FooBar(new Runnable() // I want the brace here...
{ // ...not here.
#Override
public void run()
{
//
}
});
}
}
However, I would like the code to be formatted like this:
class TestClass
{
public void test()
{
FooBar foo = new FooBar(new Runnable() { // <- Same Line
#Override
public void run()
{
//
}
});
}
}
Everything is fine, except the one detail that I cannot get the brace to be formatted like in the second example. Is this not possible or did I just overlook the setting?
I've wanted this for a long time now as well, but unfortunately it is not possible in Intellij.
The closest you can come is setting Wrapping and Braces/Braces Placement/In class declaration to "Next line if wrapped" or "End of line" (what I use). This of course modifies the way the brace is wrapped for not only anonymous inner classes (the desired result), but also for top level and inner classes; however, methods/if/else/for/while/do-while/try/catch/finally/switch etc are unaffected.
I really wish IntelliJ would add a Wrapping and Braces/Braces Placement/In anonymous class declaration option like Eclipse has.