Eclipse plugin for Fluent API methods - java

I'm looking for an eclipse plugin that can generate fluent API methods in my beans.
For instance, given this bean:
public class MyBean {
private String name;
private int age;
//Setters and getters
}
Is there any eclipse plugin that generates these methods for me?
public class MyBean {
private String name;
private int age;
public MyBean withName(String name) {
setName(name);
return this;
}
public MyBean withAge(int age) {
setAge(age);
return this;
}
//Setters and getters
}
I've found a google plugin that generates Builder objects, but I prefer fluent API inside each Bean class.

While can't find anything, you can do like me.
Generate the setters, then "Find" (checking "regular expressions") for:
\tpublic void set(.+)\((.+)\) \{\R\t\tthis\.(.+) = (.+);\R\t\}
 and replace with:
\tpublic [PUT_TYPE_HERE] with$1\($2\) \{\R\t\tthis\.$3 = $4;\R\t\treturn this;\R\t\}
Probably there's a simpler expression, but this works ;)
[UPDATE] # 07-MAR-2018
I'm now using lombok which generates getters, setters and builders throught simple annotations. (#Getter, #Setter and #Builder respectively)
It can generate with methods using the #Wither annotation too, but unfortunately its an experimental feature so it should be avoided.

Related

Jackson conflicting setters, even with #JsonIgnore and #JsonProperty

I'm at a complete loss here. I have a class with overloaded setters for a property, and for the life of me cannot get Jackson to pick a correct setter. Stripping out the things not needed from the class, here's the base of what I've got:
class TestDTO {
#Setter(onMethod_={#JsonProperty})
#JsonProperty
protected CustomWrapper wrappedValues = new CustomWrapper();
#JsonIgnore
#XmlTransient
public RecordAudit setWrappedValues(List<WrappedObject> wrappedValues) {
this.customWrapper = new CustomWrapper(wrappedValues);
return this;
}
#JsonIgnore
#XmlTransient
public RecordAudit setWrappedValues(CustomWrapper customWrapper) {
this.customWrapper = customWrapper;
return this;
}
}
I have tried every combination I can think of of #JsonIgnore and #JsonProperty. I've tried just adding #JsonProperty to the #Setter annotation, I've tried only adding #JsonIgnore to the two custom setters, I've tried only #JsonProperty on the field itself, but no matter what I try, I get the following error:
Conflicting setter definitions for property "wrappedValues": ...#setWrappedValues(1 params) vs ...#setWrappedValues(1 params)
Does anyone have any ideas what's going on here? Using Jackson 2.12.4, so I think just #JsonProperty should be all that's needed, but as I mentioned above, that still results in the same error.
This is on JDK 11 if that makes a difference, I'm still new to 11, so am not sure how much that affects this.
You need to mark setter you want to use as com.fasterxml.jackson.annotation.JsonSetter.
class TestDTO {
protected CustomWrapper wrappedValues = new CustomWrapper();
public RecordAudit setWrappedValues(List<WrappedObject> wrappedValues) {
this.customWrapper = new CustomWrapper(wrappedValues);
return this;
}
#JsonSetter
public RecordAudit setWrappedValues(CustomWrapper customWrapper) {
this.customWrapper = customWrapper;
return this;
}
}
P.S. Your #Setter aren't generating anything since there are methods with name setWrappedValues

Intellij move/refactor private fields and their accessor methods to different class

In my Java project, i have a Java response object which has several private fields and their getters and setters.
public class Response {
String resp1Id;
String resp1Message;
String resp2Id;
String resp2Message;
//getters & setters
}
I want to group the members to their own classes and have those objects in my response object like below using Intellij refactoring. The response object is being used in several places, and I cannot refactor it manually. I tried to make it happen using intellj refactor/extract to class but could not do it the way i want to to be. If I use extract-delegate, it comes out differently which I don't want. Any help is appreciated.
public class Response {
Resp1 resp1;
Resp2 resp2Id;
//getters & setters
}
public class Resp1 {
String resp1Id;
String resp1Message;
//getters & setters
}
public class Resp2 {
String resp2Id;
String resp2Message;
//getters & setters
}
I achieved it by following the below steps. There is no single step refactoring tool available in Intellij. [Took clue from Can IntelliJ refactor properties (get/setters) to fields?
1) Make field public in Response.java class.
public String resp1Id;
2) Refactor accessor methods (both getters & setters) using 'inline...' refactoring in intellij. (sample code after this step)
response.resp1Id = "abcdef";
String id = response.resp1Id;
3) Extract field to delegate class
New class will be created with fields. (sample code after this step)
public String resp1Id;
4) Refactor - encapsulate fields in new class to make them private and create accessor methods. (sample code after this step)
response.getResp1().setResp1Id("ram");
String kumar = response.getResp1().getResp1Id();

How to override Lombok Setter methods

I'm using lombok in my project and generation Setters and Getters using #Setters and #Getters annotations on top of POJO class. I'm trying to override setters method of a property but it's not working
I want to check if JSON property is Empty or Null i want to set default value in Setter method
#Setter
#Getter
#NoArgsConstructor
#AllArgsConstructor
#Accessors(chain = true)
#ToString
public class DefaultModel {
private String name;
#Setter(AccessLevel.NONE)private String age;
public void setAge(String age) {
if(age==null||age.trim().isEmpty()||age.equals("null")) {
this.age="10";
}else {
this.age=age;
}
}
}
Working scenarios:
{
"name":"some",
"age":null
}
{
"name":"some",
"age":"null"
}
{
"name":"some",
"age":" "
}
Failed Scenario :
{
"name":"some"
}
Output:
DefaultModel(name=some, age=null)
And i'm following this as reference also here, but no luck so far
Either you just hit a bug I've never seen or you're testing it wrong.
An annotation like
#Setter(AccessLevel.NONE) private String age;
on the field level indeed stops the setter from being generated. But given that you're defining a setter, you don't even need it. An explicit #Setter stops the generation, too.
I've just tried your example using Eclipse 4.7.3a and Lombok 1.18.0 and your (buggy) setter gets called. I've been using Lombok a lot over a few years and never encountered such a bug.
Most probably the problem is that your JSON deserializer does not use setters at all. I guess, you're testing something like
DefaultModel defaultModel = deserialize("{\"name\":\"some\"}", DefaultModel.class);
instead of testing the setter directly. And that's the problem.
It possible that JSON deserializer uses constructor generated by Lombok (not setters).
Have a look here:
Jackson deserialize default values missing

Use of Spring ToStringCreator

I am working on a Spring project wherein I have a POJO class
public class Owner extends Person {
private String address;
private String city;
private String telephone;
Getters and Setters
#Override
public String toString() {
return new ToStringCreator(this)
.append("id", this.getId())
.append("new", this.isNew())
.append("lastName", this.getLastName())
.append("firstName", this.getFirstName())
.append("address", this.address)
.append("city", this.city)
.append("telephone", this.telephone)
.toString();
}
}
I have two doubts why a toString() method is being used here? What is its use?
What is ToStringCreator method doing?
As per official docs
ToStringCreator is a Utility class that builds pretty-printing toString() methods with pluggable styling conventions. By default, ToStringCreator adheres to Spring's toString() styling conventions.
It is a helper class for customizing toString() presentation.

Simulate `property` in Java: how to use public field as property?

In playframework, it uses javassist library to let the public fields of a class can be used as property.
See the example:
public class User {
public String name;
}
User user = new User();
user.name = "Freewind"
System.out.println(user.name);
In compilation time, play enhanced the bytecode with javassist, the final code is similar to:
public class User {
private String name;
public String getName() { return this.name; };
public void setName() { this.name = name; };
}
User user = new User();
user.setName("Freewind");
System.out.println(user.getName());
You can see not only the field name has getter and setter, but also the invocations of it changed to getters and setters.
I wonder if there is any other way to do the same (use other things than javassist)?
I found Annotation Processing Tool, but I'm not sure it can do it.
Or aspectj? Or something else?
You can look at Project Lombok, which does something similar, but with annotations. With project lombok you do need to use the getters and setters in your own code.
Not without other tools.
Unlike C#, Java does not support properties.

Categories

Resources