Skip to content

HHH-19627 improve determination of @Basic-ness of field type #10592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Name;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.ArrayType;
import javax.lang.model.type.DeclaredType;
Expand All @@ -18,9 +19,7 @@
import java.io.Serializable;

import static org.hibernate.processor.util.Constants.BASIC_ARRAY_TYPES;
import static org.hibernate.processor.util.Constants.BASIC_TYPES;
import static org.hibernate.processor.util.TypeUtils.hasAnnotation;
import static org.hibernate.processor.util.TypeUtils.isClassOrRecordType;

/**
* Checks whether the visited type is a basic attribute according to the JPA 2 spec
Expand Down Expand Up @@ -50,17 +49,27 @@ public Boolean visitArray(ArrayType arrayType, Element element) {
@Override
public Boolean visitDeclared(DeclaredType declaredType, Element element) {
final ElementKind kind = element.getKind();
if ( kind == ElementKind.ENUM ) {
return true;
}
else if ( isClassOrRecordType(element) || kind == ElementKind.INTERFACE ) {
final TypeElement typeElement = (TypeElement) element;
return BASIC_TYPES.contains( typeElement.getQualifiedName().toString() )
|| hasAnnotation( element, Constants.EMBEDDABLE )
|| isSerializable( typeElement );
}
else {
return false;
switch ( kind ) {
case ENUM -> {
return true;
}
case INTERFACE -> {
// TODO: this isn't really correct
final TypeElement typeElement = (TypeElement) element;
final Name qualifiedName = typeElement.getQualifiedName();
return qualifiedName.contentEquals( "java.sql.Blob" )
|| qualifiedName.contentEquals( "java.sql.Clob" )
|| qualifiedName.contentEquals( "java.sql.NClob" );
}
case CLASS, RECORD -> {
// TODO: this is simply wrong
final TypeElement typeElement = (TypeElement) element;
return hasAnnotation( element, Constants.EMBEDDABLE )
|| isSerializable( typeElement );
}
default -> {
return false;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
*/
package org.hibernate.processor.util;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -190,28 +188,6 @@ public final class Constants {
SPRING_STATELESS_SESSION_PROVIDER
);

//TODO: this is not even an exhaustive list of built-in basic types
// so any logic that relies on incomplete this list is broken!
public static final Set<String> BASIC_TYPES = Set.of(
String.class.getName(),
Boolean.class.getName(),
Byte.class.getName(),
Character.class.getName(),
Short.class.getName(),
Integer.class.getName(),
Long.class.getName(),
Float.class.getName(),
Double.class.getName(),
BigInteger.class.getName(),
BigDecimal.class.getName(),
java.util.Date.class.getName(),
java.util.Calendar.class.getName(),
java.sql.Date.class.getName(),
java.sql.Time.class.getName(),
java.sql.Timestamp.class.getName(),
java.sql.Blob.class.getName()
);

public static final List<String> BASIC_ARRAY_TYPES = List.of(
Character.class.getName(),
Byte.class.getName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ && isProperty( element.getSimpleName().toString(),
}

public static boolean isBasicAttribute(Element element, Element returnedElement, Context context) {
//TODO: This is extremely incomplete
return hasAnnotation( element, BASIC, ONE_TO_ONE, MANY_TO_ONE, EMBEDDED, EMBEDDED_ID, ID )
|| hasAnnotation( element, "org.hibernate.annotations.Type") // METAGEN-28
|| returnedElement.asType().accept( new BasicAttributeVisitor( context ), returnedElement );
Expand Down