Sonar errors wehn using Lombok #Setter(value = AccessLevel.PRIVATE) - sonarqube

If I use the Lombok #Setting annotation on a field with a value of PRIVATE
#Data
#AllArgsConstructor
#NoArgsConstructor
public class Notification implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Setter(value = AccessLevel.PRIVATE)
private String id;
private long userId;
private Event event;
private long timestamp = System.currentTimeMillis();
public Notification(final String id) {
this.id = id;
}
}
The Sonar Maven plugin gives the following error:
ERROR] Failed to execute goal org.codehaus.mojo:sonar-maven-plugin:2.7.1:sonar (default-cli) on project mio-events: Unable to analyze .class file tv/nativ/mio/event/model/Notification: 0 is not a valid line for a file -> [Help 1]
Changing the #Setting value to public fixes the issue, as does removing #Setting altogether and adding a manual private setter for the field.
Any idea what the issue might be?
Thanks
Nick

Sonar doesn't really support Lombok: Feature request for lombok like tools
Prior to running sonar you should delombok the source and use the generated sources for analysis. Information about this is on the page: Delombok. If you are using maven there's an example of using this technique here: Example Pom

Related

Cannot invoke "org.hibernate.mapping.PersistentClass.getTable()" because "classMapping" is null

I have an entity "MasterSegment" with a composite key as the primary key. This key has a reference to another entity "BkrApplication". When I start the app without Liquibase, tables are generated perfectly and everything works fine.
public class MasterSegment extends Auditable {
#EmbeddedId
private MasterSegmentId id;
#OneToOne
#MapsId("appId")
private BkrApplication app;
// getters setters omitted
}
#Embeddable
public class MasterSegmentId implements Serializable {
#Column
private String name;
#Column(name = "app_id", nullable = false)
private Long appId;
// getters setters omitted
}
The problem is when I try to generate a Liquibase migration using mvn clean install liquibase:diff, I get the following error: Cannot invoke "org.hibernate.mapping.PersistentClass.getTable()" because "classMapping" is null.
Without any hint in the exception message, and after many hours of debugging, I noticed that #MapsId causes the issue. I try to remove it and I got mapping issues.
Any help would be much appreciated.
Thanks

JsonIgnore annotation is not working with Lombok

I am facing really weird issue with Lombok and Jackson. Following of piece of code on which I am working.
#Getter
#Setter
#NoArgsConstructor
#XmlRootElement
public class Order{
//#JsonIgnore
#Getter(onMethod = #__(#JsonIgnore))
private boolean userPresent;
}
So what I want , this dto supposed to serialized as Json then this userPresent attribute should not come as response attribute. I though #JsonIgnore will work for me. But I think it as some issue with Lombok as per https://stackoverflow.com/a/57119494/2111677 article.
Then I changed the approach to use OnMethod.
Now , on eclipse compiling perfectly fine but when I am trying to compile using mvn then it gives me following error.
Could someone help me fix when its not working with maven.
The #__ style is for javac7. For javac8+ you have to use this variant:
#Getter(onMethod_=#JsonIgnore)
However, it is sufficient to have the #JsonIgnore annotation on either the field, the getter, or the setter. If it is present on at least one of those, the whole "virtual property" is ignored completely during (de-)serialization. So if that is what you want, you don't need that onMethod_.
If you want it to be ignored only during serialization, but not on deserialization, you have to add a #JsonProperty on the setter:
#JsonIgnore
#Setter(onMethod_=#JsonProperty)
private boolean userPresent;
It does not work, a workaround is using #JsonIgnoreProperties at class level:
#JsonIgnoreProperties({"email"})
This worked for me.
#RequiredArgsConstructor
#Setter
#NoArgsConstructor
#Entity
public class ProductColor {
#Id
#GeneratedValue
#NonNull
#Getter
private Long id;
#NonNull
#Getter
private String color;
#ManyToMany(mappedBy="colors")
#Getter(onMethod_ = #JsonIgnore)
private Set<Product> products;
}

Spring Boot + Webflux + Reactive MongoDB - get document by property Id

I'd like to find all Offer documents by Offer.ProductProperties.brand:
#Document(collection = "offers")
public class Offer {
#Id
private String id;
#NotNull
#DBRef
private ProductProperties properties;
ProductProperties:
#Document(collection = "product_properties")
public class ProductProperties {
#Id
private String id;
#NotNull
#NotEmpty
private String brand;
Service:
Flux<ProductProperties> all = productPropertiesRepository.findAllByBrand(brand);
List<String> productPropIds = all.toStream()
.map(ProductProperties::getId)
.collect(Collectors.toList());
Flux<Offer> byProperties = offerRepository.findAllByProperties_Id(productPropIds);
But unfortunately byProperties is empty. Why?
My repository:
public interface OfferRepository extends ReactiveMongoRepository<Offer, String> {
Flux<Offer> findAllByProperties_Id(List<String> productPropertiesIds);
}
How to find all Offers by ProductProperties.brand?
Thanks!
After reading some documentation found out that You cannot query with #DBRef. Hence the message
Invalid path reference properties.brand! Associations can only be
pointed to directly or via their id property
If you remove DBRef from the field, you should be able to query by findAllByProperties_BrandAndProperties_Capacity.
So the only ways is how you are doing. i.e. Fetch id's and query by id.
As I said in the comment, the reason it is not working is because return type of findAllByProperties_Id is a Flux. So unless u execute a terminal operation, you wont have any result. Try
byProperties.collectList().block()

Serialization rule is not working properly in SonarQube

The SonarQube Rule "Fields in a "Serializable" class should either be transient or serializable" is not working properly. I have a very compliant example as mentioned in their rules definition like below, but still it is reported as an issue. I found that there was a bug alreeady raised for this (https://jira.codehaus.org/browse/SONARJAVA-917) and is resolved. And I have the latest version which has this fix as well, but still it is an issue. can some one help me how to resolve this?
`public class Address implements Serializable {
private static final long serialVersionUID = 2405172041950251807L;
}
public class Person implements Serializable {
private static final long serialVersionUID = 1905122041950251207L;
private String name;
private Address address;
}`

maven plugin - get artifactId of plugin in Mojo

Is it possible to get the artifactId of the current Mojo?
#Mojo(...)
public class MyMojo extends AbstractMojo {
#Parameter(property = "project")
private MavenProject project;
#Parameter(property = "inputDirectory", defaultValue = "${project.basedir}/src/main/${artifact id of the plugin}")
private File inputDirectory;
...
I could hardcode the artifact id of the plugin, but I would rather get it dynamically.
The accepted answer does not answer the question.
You can use a field to dynamically get the artifactId of the mojo (not the artifactId of the project which the plugin is attached on)
#Parameter(defaultValue = "${mojo.artifactId}")
private String mojoArtifactId;
Or in your case:
#Parameter(property = "project")
private MavenProject project;
#Parameter(property = "inputDirectory", defaultValue = "${project.basedir}/src/main/${mojo.artifactId}")
private File inputDirectory;
BTW what comes into my mind is that you are using old style injection
#Parameter(property = "project")
private MavenProject project;
#Parameter(property = "inputDirectory", defaultValue = "${project.basedir}/src/main/${artifact id of the plugin}")
private File inputDirectory;
They should look like this:
The expression values for defaultValue are documented here:
http://maven.apache.org/ref/3.1.1/maven-core/apidocs/org/apache/maven/plugin/PluginParameterExpressionEvaluator.html depending on your used Maven version.
#Parameter(defaultValue = "${project}, required = true, readonly = true)
private MavenProject project;
#Parameter(defaultValue = "${project.basedir}/src/main/${artifact id of the plugin}", property = "inputDirectory", required = true)
private File inputDirectory;

Resources