Skip to content

Commit 0140bad

Browse files
committed
HHH-19627 fix determination of singularity in static metamodel generation
1 parent ede640a commit 0140bad

File tree

6 files changed

+30
-127
lines changed

6 files changed

+30
-127
lines changed

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
import static org.hibernate.processor.util.TypeUtils.getInheritedAnnotationMirror;
102102
import static org.hibernate.processor.util.TypeUtils.hasAnnotation;
103103
import static org.hibernate.processor.util.TypeUtils.implementsInterface;
104+
import static org.hibernate.processor.util.TypeUtils.isPluralAttribute;
104105
import static org.hibernate.processor.util.TypeUtils.primitiveClassMatchesKind;
105106
import static org.hibernate.processor.util.TypeUtils.propertyName;
106107
import static org.hibernate.processor.util.TypeUtils.resolveTypeMirror;
@@ -2548,7 +2549,7 @@ enum FieldType {
25482549
final boolean idClassRef = isIdRef( path ) && hasAnnotation( entityType, ID_CLASS );
25492550
final Element member = idClassRef ? null : memberMatchingPath( entityType, path );
25502551
if ( member != null ) {
2551-
if ( containsAnnotation( member, MANY_TO_MANY, ONE_TO_MANY, ELEMENT_COLLECTION ) ) {
2552+
if ( isPluralAttribute( member ) ) {
25522553
message( param,
25532554
"matching field is a collection",
25542555
Diagnostic.Kind.ERROR );

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/DataMetaAttributeGenerationVisitor.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import org.checkerframework.checker.nullness.qual.Nullable;
88
import org.hibernate.processor.Context;
9-
import org.hibernate.processor.util.Constants;
109

1110
import javax.lang.model.element.Element;
1211
import javax.lang.model.element.TypeElement;
@@ -18,9 +17,8 @@
1817
import javax.lang.model.util.SimpleTypeVisitor8;
1918
import javax.lang.model.util.Types;
2019

21-
import static org.hibernate.processor.util.NullnessUtil.castNonNull;
2220
import static org.hibernate.processor.util.TypeUtils.getTargetEntity;
23-
import static org.hibernate.processor.util.TypeUtils.isBasicAttribute;
21+
import static org.hibernate.processor.util.TypeUtils.isPluralAttribute;
2422
import static org.hibernate.processor.util.TypeUtils.isPropertyGetter;
2523
import static org.hibernate.processor.util.TypeUtils.toArrayTypeString;
2624
import static org.hibernate.processor.util.TypeUtils.toTypeString;
@@ -70,20 +68,20 @@ private Types typeUtils() {
7068
@Override
7169
public @Nullable DataAnnotationMetaAttribute visitDeclared(DeclaredType declaredType, Element element) {
7270
final TypeElement returnedElement = (TypeElement) typeUtils().asElement( declaredType );
71+
if ( returnedElement == null ) {
72+
return null;
73+
}
7374
// WARNING: .toString() is necessary here since Name equals does not compare to String
74-
final String returnTypeName = castNonNull( returnedElement ).getQualifiedName().toString();
75-
final String collection = Constants.COLLECTIONS.get( returnTypeName );
7675
final String targetEntity = getTargetEntity( element.getAnnotationMirrors() );
77-
if ( collection != null ) {
76+
if ( isPluralAttribute( element ) ) {
77+
// final String returnTypeName = returnedElement.getQualifiedName().toString();
78+
// final String collection = Constants.COLLECTIONS.get( returnTypeName );
7879
return null;
7980
}
80-
else if ( isBasicAttribute( element, returnedElement, context ) ) {
81+
else {
8182
final String type = targetEntity != null ? targetEntity : returnedElement.getQualifiedName().toString();
8283
return new DataAnnotationMetaAttribute( entity, element, type, path );
8384
}
84-
else {
85-
return null;
86-
}
8785
}
8886

8987
@Override

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/MetaAttributeGenerationVisitor.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import static org.hibernate.processor.util.TypeUtils.getKeyType;
4242
import static org.hibernate.processor.util.TypeUtils.getTargetEntity;
4343
import static org.hibernate.processor.util.TypeUtils.hasAnnotation;
44-
import static org.hibernate.processor.util.TypeUtils.isBasicAttribute;
44+
import static org.hibernate.processor.util.TypeUtils.isPluralAttribute;
4545
import static org.hibernate.processor.util.TypeUtils.isPropertyGetter;
4646
import static org.hibernate.processor.util.TypeUtils.toArrayTypeString;
4747
import static org.hibernate.processor.util.TypeUtils.toTypeString;
@@ -70,7 +70,7 @@ private Types typeUtils() {
7070

7171
@Override
7272
public @Nullable AnnotationMetaAttribute visitArray(ArrayType arrayType, Element element) {
73-
if ( hasAnnotation( element, MANY_TO_MANY, ONE_TO_MANY, ELEMENT_COLLECTION ) ) {
73+
if ( isPluralAttribute( element ) ) {
7474
return new AnnotationMetaCollection( entity, element, LIST_ATTRIBUTE,
7575
toTypeString(arrayType.getComponentType()) );
7676
}
@@ -89,21 +89,25 @@ private Types typeUtils() {
8989
@Override
9090
public @Nullable AnnotationMetaAttribute visitDeclared(DeclaredType declaredType, Element element) {
9191
final TypeElement returnedElement = (TypeElement) typeUtils().asElement( declaredType );
92-
assert returnedElement != null;
92+
if ( returnedElement == null ) {
93+
return null;
94+
}
9395
// WARNING: .toString() is necessary here since Name equals does not compare to String
94-
final String returnTypeName = castNonNull( returnedElement ).getQualifiedName().toString();
95-
final String collection = Constants.COLLECTIONS.get( returnTypeName );
9696
final String targetEntity = getTargetEntity( element.getAnnotationMirrors() );
97-
if ( collection != null ) {
98-
return createMetaCollectionAttribute( declaredType, element, returnTypeName, collection, targetEntity );
97+
if ( isPluralAttribute( element ) ) {
98+
final String returnTypeName = returnedElement.getQualifiedName().toString();
99+
final String collection = Constants.COLLECTIONS.get( returnTypeName );
100+
if ( collection != null ) {
101+
return createMetaCollectionAttribute( declaredType, element, returnTypeName, collection, targetEntity );
102+
}
103+
else {
104+
return null;
105+
}
99106
}
100-
else if ( isBasicAttribute( element, returnedElement, context ) ) {
107+
else {
101108
final String type = targetEntity != null ? targetEntity : returnedElement.getQualifiedName().toString();
102109
return new AnnotationMetaSingleAttribute( entity, element, type );
103110
}
104-
else {
105-
return null;
106-
}
107111
}
108112

109113
private AnnotationMetaAttribute createMetaCollectionAttribute(

tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/BasicAttributeVisitor.java

Lines changed: 0 additions & 75 deletions
This file was deleted.

tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/Constants.java

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
*/
55
package org.hibernate.processor.util;
66

7-
import java.math.BigDecimal;
8-
import java.math.BigInteger;
97
import java.util.List;
108
import java.util.Map;
119
import java.util.Set;
@@ -21,6 +19,8 @@ public final class Constants {
2119
public static final String MAPPED_SUPERCLASS = "jakarta.persistence.MappedSuperclass";
2220
public static final String EMBEDDABLE = "jakarta.persistence.Embeddable";
2321
public static final String EMBEDDED = "jakarta.persistence.Embedded";
22+
public static final String ENUMERATED = "jakarta.persistence.Enumerated";
23+
public static final String LOB = "jakarta.persistence.Lob";
2424
public static final String ID = "jakarta.persistence.Id";
2525
public static final String ID_CLASS = "jakarta.persistence.IdClass";
2626
public static final String EMBEDDED_ID = "jakarta.persistence.EmbeddedId";
@@ -191,28 +191,6 @@ public final class Constants {
191191
SPRING_STATELESS_SESSION_PROVIDER
192192
);
193193

194-
//TODO: this is not even an exhaustive list of built-in basic types
195-
// so any logic that relies on incomplete this list is broken!
196-
public static final Set<String> BASIC_TYPES = Set.of(
197-
String.class.getName(),
198-
Boolean.class.getName(),
199-
Byte.class.getName(),
200-
Character.class.getName(),
201-
Short.class.getName(),
202-
Integer.class.getName(),
203-
Long.class.getName(),
204-
Float.class.getName(),
205-
Double.class.getName(),
206-
BigInteger.class.getName(),
207-
BigDecimal.class.getName(),
208-
java.util.Date.class.getName(),
209-
java.util.Calendar.class.getName(),
210-
java.sql.Date.class.getName(),
211-
java.sql.Time.class.getName(),
212-
java.sql.Timestamp.class.getName(),
213-
java.sql.Blob.class.getName()
214-
);
215-
216194
public static final List<String> BASIC_ARRAY_TYPES = List.of(
217195
Character.class.getName(),
218196
Byte.class.getName()

tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/TypeUtils.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,8 @@
4444
import static org.hibernate.internal.util.StringHelper.split;
4545
import static org.hibernate.processor.util.AccessTypeInformation.DEFAULT_ACCESS_TYPE;
4646
import static org.hibernate.processor.util.Constants.ACCESS;
47-
import static org.hibernate.processor.util.Constants.BASIC;
4847
import static org.hibernate.processor.util.Constants.ELEMENT_COLLECTION;
4948
import static org.hibernate.processor.util.Constants.EMBEDDABLE;
50-
import static org.hibernate.processor.util.Constants.EMBEDDED;
5149
import static org.hibernate.processor.util.Constants.EMBEDDED_ID;
5250
import static org.hibernate.processor.util.Constants.ENTITY;
5351
import static org.hibernate.processor.util.Constants.ID;
@@ -573,10 +571,9 @@ && isProperty( element.getSimpleName().toString(),
573571
toTypeString( executable.getReturnType() ) );
574572
}
575573

576-
public static boolean isBasicAttribute(Element element, Element returnedElement, Context context) {
577-
return hasAnnotation( element, BASIC, ONE_TO_ONE, MANY_TO_ONE, EMBEDDED, EMBEDDED_ID, ID )
578-
|| hasAnnotation( element, "org.hibernate.annotations.Type") // METAGEN-28
579-
|| returnedElement.asType().accept( new BasicAttributeVisitor( context ), returnedElement );
574+
public static boolean isPluralAttribute(Element element) {
575+
// TODO: should MANY_TO_ANY be on this list?
576+
return hasAnnotation( element, MANY_TO_MANY, ONE_TO_MANY, ELEMENT_COLLECTION );
580577
}
581578

582579
public static @Nullable String getFullyQualifiedClassNameOfTargetEntity(

0 commit comments

Comments
 (0)