Skip to content

Commit

Permalink
False positive "class should override both the equals and hashCode me…
Browse files Browse the repository at this point in the history
…thods" for Enum values #295
  • Loading branch information
vladmihalcea committed Feb 25, 2021
1 parent c0defb2 commit abe66ce
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,23 @@ public static Method getMethod(Object target, String methodName, Class... parame
return getMethod(target.getClass(), methodName, parameterTypes);
}

/**
* Get the {@link Method} with the given signature (name and parameter types) belonging to
* the provided Java {@link Object} or {@code null} if no {@link Method} was found.
*
* @param targetClass target {@link Class}
* @param methodName method name
* @param parameterTypes method parameter types
* @return return {@link Method} matching the provided signature or {@code null}
*/
public static Method getMethodOrNull(Class targetClass, String methodName, Class... parameterTypes) {
try {
return getMethod(targetClass, methodName, parameterTypes);
} catch (RuntimeException e) {
return null;
}
}

/**
* Get the {@link Method} with the given signature (name and parameter types) belonging to
* the provided Java {@link Object} or {@code null} if no {@link Method} was found.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,13 @@ private void validatePropertyType() {
continue;
}
validatedTypes.add(genericType);
Method equalsMethod = ReflectionUtils.getDeclaredMethodOrNull(genericType, "equals", Object.class);
Method hashCodeMethod = ReflectionUtils.getDeclaredMethodOrNull(genericType, "hashCode");
Method equalsMethod = ReflectionUtils.getMethodOrNull(genericType, "equals", Object.class);
Method hashCodeMethod = ReflectionUtils.getMethodOrNull(genericType, "hashCode");

if(equalsMethod == null || hashCodeMethod == null) {
if(equalsMethod == null ||
hashCodeMethod == null ||
Object.class.equals(equalsMethod.getDeclaringClass()) ||
Object.class.equals(hashCodeMethod.getDeclaringClass())) {
LogUtils.LOGGER.warn("The {} class should override both the equals and hashCode methods based on the JSON object value it represents!", genericType);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,23 @@ public static Method getMethod(Object target, String methodName, Class... parame
return getMethod(target.getClass(), methodName, parameterTypes);
}

/**
* Get the {@link Method} with the given signature (name and parameter types) belonging to
* the provided Java {@link Object} or {@code null} if no {@link Method} was found.
*
* @param targetClass target {@link Class}
* @param methodName method name
* @param parameterTypes method parameter types
* @return return {@link Method} matching the provided signature or {@code null}
*/
public static Method getMethodOrNull(Class targetClass, String methodName, Class... parameterTypes) {
try {
return getMethod(targetClass, methodName, parameterTypes);
} catch (RuntimeException e) {
return null;
}
}

/**
* Get the {@link Method} with the given signature (name and parameter types) belonging to
* the provided Java {@link Object} or {@code null} if no {@link Method} was found.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,13 @@ private void validatePropertyType() {
continue;
}
validatedTypes.add(genericType);
Method equalsMethod = ReflectionUtils.getDeclaredMethodOrNull(genericType, "equals", Object.class);
Method hashCodeMethod = ReflectionUtils.getDeclaredMethodOrNull(genericType, "hashCode");
Method equalsMethod = ReflectionUtils.getMethodOrNull(genericType, "equals", Object.class);
Method hashCodeMethod = ReflectionUtils.getMethodOrNull(genericType, "hashCode");

if(equalsMethod == null || hashCodeMethod == null) {
if(equalsMethod == null ||
hashCodeMethod == null ||
Object.class.equals(equalsMethod.getDeclaringClass()) ||
Object.class.equals(hashCodeMethod.getDeclaringClass())) {
LogUtils.LOGGER.warn("The {} class should override both the equals and hashCode methods based on the JSON object value it represents!", genericType);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,23 @@ public static Method getMethod(Object target, String methodName, Class... parame
return getMethod(target.getClass(), methodName, parameterTypes);
}

/**
* Get the {@link Method} with the given signature (name and parameter types) belonging to
* the provided Java {@link Object} or {@code null} if no {@link Method} was found.
*
* @param targetClass target {@link Class}
* @param methodName method name
* @param parameterTypes method parameter types
* @return return {@link Method} matching the provided signature or {@code null}
*/
public static Method getMethodOrNull(Class targetClass, String methodName, Class... parameterTypes) {
try {
return getMethod(targetClass, methodName, parameterTypes);
} catch (RuntimeException e) {
return null;
}
}

/**
* Get the {@link Method} with the given signature (name and parameter types) belonging to
* the provided Java {@link Object} or {@code null} if no {@link Method} was found.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,13 @@ private void validatePropertyType() {
continue;
}
validatedTypes.add(genericType);
Method equalsMethod = ReflectionUtils.getDeclaredMethodOrNull(genericType, "equals", Object.class);
Method hashCodeMethod = ReflectionUtils.getDeclaredMethodOrNull(genericType, "hashCode");
Method equalsMethod = ReflectionUtils.getMethodOrNull(genericType, "equals", Object.class);
Method hashCodeMethod = ReflectionUtils.getMethodOrNull(genericType, "hashCode");

if(equalsMethod == null || hashCodeMethod == null) {
if(equalsMethod == null ||
hashCodeMethod == null ||
Object.class.equals(equalsMethod.getDeclaringClass()) ||
Object.class.equals(hashCodeMethod.getDeclaringClass())) {
LogUtils.LOGGER.warn("The {} class should override both the equals and hashCode methods based on the JSON object value it represents!", genericType);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,23 @@ public static Method getMethod(Class targetClass, String methodName, Class... pa
}
}

/**
* Get the {@link Method} with the given signature (name and parameter types) belonging to
* the provided Java {@link Object} or {@code null} if no {@link Method} was found.
*
* @param targetClass target {@link Class}
* @param methodName method name
* @param parameterTypes method parameter types
* @return return {@link Method} matching the provided signature or {@code null}
*/
public static Method getMethodOrNull(Class targetClass, String methodName, Class... parameterTypes) {
try {
return getMethod(targetClass, methodName, parameterTypes);
} catch (RuntimeException e) {
return null;
}
}

/**
* Get the {@link Method} with the given signature (name and parameter types) belonging to
* the provided Java {@link Class}, excluding inherited ones, or {@code null} if no {@link Method} was found.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.vladmihalcea.hibernate.type.json;

import com.vladmihalcea.hibernate.type.util.AbstractPostgreSQLIntegrationTest;
import org.hibernate.Session;
import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.junit.Test;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;

/**
* @author Vlad Mihalcea
*/
public class PostgreSQLJsonListEnumTest extends AbstractPostgreSQLIntegrationTest {

@Override
protected Class<?>[] entities() {
return new Class<?>[]{
Book.class
};
}

@Test
public void test() {

doInJPA(entityManager -> {
entityManager.persist(
new Book()
.setIsbn("978-9730228236")
.addProperty(PropertyType.BEST_SELLER)
.addProperty(PropertyType.FREE_CHAPTER)
);
});

doInJPA(entityManager -> {
Book book = entityManager.unwrap(Session.class)
.bySimpleNaturalId(Book.class)
.load("978-9730228236");

List<PropertyType> bookProperties = book.getProperties();
assertEquals(2, bookProperties.size());
});
}

@Entity(name = "Book")
@Table(name = "book")
@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
public static class Book {

@Id
@GeneratedValue
private Long id;

@NaturalId
@Column(length = 15)
private String isbn;

@Type(type = "jsonb")
@Column(columnDefinition = "jsonb")
private List<PropertyType> propertyTypes = new ArrayList<>();

public String getIsbn() {
return isbn;
}

public Book setIsbn(String isbn) {
this.isbn = isbn;
return this;
}

public List<PropertyType> getProperties() {
return propertyTypes;
}

public Book setProperties(List<PropertyType> properties) {
this.propertyTypes = properties;
return this;
}

public Book addProperty(PropertyType propertyType) {
propertyTypes.add(propertyType);
return this;
}
}

public enum PropertyType {
BEST_SELLER,
FREE_CHAPTER
}
}

0 comments on commit abe66ce

Please sign in to comment.