minor code style issues in the query validator

This commit is contained in:
Gavin King 2024-11-22 13:56:44 +01:00
parent b9274f5b75
commit e7c3f52499
3 changed files with 128 additions and 160 deletions

View File

@ -149,7 +149,7 @@ public abstract class MockEntityPersister implements EntityPersister, Joinable,
@Override @Override
public Set<String> getSubclassEntityNames() { public Set<String> getSubclassEntityNames() {
Set<String> names = new HashSet<>(); final Set<String> names = new HashSet<>();
names.add( entityName ); names.add( entityName );
for (MockEntityPersister persister : factory.getMockEntityPersisters()) { for (MockEntityPersister persister : factory.getMockEntityPersisters()) {
if (persister.isSubclassPersister(this)) { if (persister.isSubclassPersister(this)) {

View File

@ -257,21 +257,15 @@ public abstract class MockSessionFactory
} }
static CollectionType createCollectionType(String role, String name) { static CollectionType createCollectionType(String role, String name) {
switch (name) { return switch ( name ) {
case "Set": case "Set", "SortedSet" ->
case "SortedSet":
//might actually be a bag! //might actually be a bag!
//TODO: look for @OrderColumn on the property //TODO: look for @OrderColumn on the property
return new SetType(role, null); new SetType( role, null );
case "List": case "List", "SortedList" -> new ListType( role, null );
case "SortedList": case "Map", "SortedMap" -> new MapType( role, null );
return new ListType(role, null); default -> new BagType( role, null );
case "Map": };
case "SortedMap":
return new MapType(role, null);
default:
return new BagType(role, null);
}
} }
/** /**
@ -456,26 +450,17 @@ public abstract class MockSessionFactory
} }
static Class<?> toPrimitiveClass(Class<?> type) { static Class<?> toPrimitiveClass(Class<?> type) {
switch (type.getName()) { return switch ( type.getName() ) {
case "java.lang.Boolean": case "java.lang.Boolean" -> boolean.class;
return boolean.class; case "java.lang.Character" -> char.class;
case "java.lang.Character": case "java.lang.Integer" -> int.class;
return char.class; case "java.lang.Short" -> short.class;
case "java.lang.Integer": case "java.lang.Byte" -> byte.class;
return int.class; case "java.lang.Long" -> long.class;
case "java.lang.Short": case "java.lang.Float" -> float.class;
return short.class; case "java.lang.Double" -> double.class;
case "java.lang.Byte": default -> Object.class;
return byte.class; };
case "java.lang.Long":
return long.class;
case "java.lang.Float":
return float.class;
case "java.lang.Double":
return double.class;
default:
return Object.class;
}
} }
@Override @Override
@ -539,7 +524,7 @@ public abstract class MockSessionFactory
@Override @Override
public RuntimeMetamodelsImplementor getRuntimeMetamodels() { public RuntimeMetamodelsImplementor getRuntimeMetamodels() {
RuntimeMetamodelsImpl runtimeMetamodels = new RuntimeMetamodelsImpl(); final RuntimeMetamodelsImpl runtimeMetamodels = new RuntimeMetamodelsImpl();
runtimeMetamodels.setJpaMetamodel( metamodel.getJpaMetamodel() ); runtimeMetamodels.setJpaMetamodel( metamodel.getJpaMetamodel() );
runtimeMetamodels.setMappingMetamodel( metamodel ); runtimeMetamodels.setMappingMetamodel( metamodel );
return runtimeMetamodels; return runtimeMetamodels;
@ -789,12 +774,9 @@ public abstract class MockSessionFactory
@Override @Override
public EntityDomainType<?> entity(String entityName) { public EntityDomainType<?> entity(String entityName) {
if ( isEntityDefined(entityName) ) { return isEntityDefined( entityName )
return new MockEntityDomainType<>(entityName); ? new MockEntityDomainType<>( entityName )
} : null;
else {
return null;
}
} }
@Override @Override
@ -805,8 +787,10 @@ public abstract class MockSessionFactory
else if (isEntityDefined(queryName)) { else if (isEntityDefined(queryName)) {
return qualifyName(queryName); return qualifyName(queryName);
} }
else {
return null; return null;
} }
}
@Override @Override
public <X> ManagedDomainType<X> managedType(String typeName) { public <X> ManagedDomainType<X> managedType(String typeName) {
@ -822,12 +806,9 @@ public abstract class MockSessionFactory
@Override @Override
public <X> EntityDomainType<X> findEntityType(Class<X> cls) { public <X> EntityDomainType<X> findEntityType(Class<X> cls) {
if ( isEntityDefined( cls.getName() ) ) { return isEntityDefined( cls.getName() )
return new MockEntityDomainType<>( cls.getName() ); ? new MockEntityDomainType<X>( cls.getName() )
} : null;
else {
return null;
}
} }
@Override @Override
@ -843,7 +824,7 @@ public abstract class MockSessionFactory
@Override @Override
public EnumJavaType<?> getEnumType(String className) { public EnumJavaType<?> getEnumType(String className) {
if ( isEnum(className) ) { if ( isEnum(className) ) {
return new EnumJavaType( Enum.class ) { return new EnumJavaType<>( Enum.class ) {
@Override @Override
public String getTypeName() { public String getTypeName() {
return className; return className;
@ -857,10 +838,9 @@ public abstract class MockSessionFactory
@Override @Override
public JavaType<?> getJavaConstantType(String className, String fieldName) { public JavaType<?> getJavaConstantType(String className, String fieldName) {
final Class<?> fieldType = javaConstantType( className, fieldName );
return MockSessionFactory.this.getTypeConfiguration() return MockSessionFactory.this.getTypeConfiguration()
.getJavaTypeRegistry() .getJavaTypeRegistry()
.getDescriptor( fieldType ); .getDescriptor( javaConstantType( className, fieldName ) );
} }
@Override @Override
@ -898,7 +878,7 @@ public abstract class MockSessionFactory
@Override @Override
public PersistentAttribute<X,?> findDeclaredAttribute(String name) { public PersistentAttribute<X,?> findDeclaredAttribute(String name) {
String typeName = getTypeName(); final String typeName = getTypeName();
return isFieldDefined(typeName, name) return isFieldDefined(typeName, name)
? createAttribute(name, typeName, propertyType(typeName, name), this) ? createAttribute(name, typeName, propertyType(typeName, name), this)
: null; : null;
@ -946,21 +926,21 @@ public abstract class MockSessionFactory
if (type == null) { if (type == null) {
return null; return null;
} }
else if (type instanceof BasicDomainType) { else if (type instanceof BasicDomainType<?> basicDomainType) {
return new BasicSqmPathSource<>( return new BasicSqmPathSource<>(
EntityIdentifierMapping.ID_ROLE_NAME, EntityIdentifierMapping.ID_ROLE_NAME,
null, null,
(BasicDomainType<?>) type, basicDomainType,
MockEntityDomainType.this.getExpressibleJavaType(), MockEntityDomainType.this.getExpressibleJavaType(),
Bindable.BindableType.SINGULAR_ATTRIBUTE, Bindable.BindableType.SINGULAR_ATTRIBUTE,
false false
); );
} }
else if (type instanceof EmbeddableDomainType) { else if (type instanceof EmbeddableDomainType<?> embeddableDomainType) {
return new EmbeddedSqmPathSource<>( return new EmbeddedSqmPathSource<>(
EntityIdentifierMapping.ID_ROLE_NAME, EntityIdentifierMapping.ID_ROLE_NAME,
null, null,
(EmbeddableDomainType<?>) type, embeddableDomainType,
Bindable.BindableType.SINGULAR_ATTRIBUTE, Bindable.BindableType.SINGULAR_ATTRIBUTE,
false false
); );
@ -982,8 +962,8 @@ public abstract class MockSessionFactory
if ( source != null ) { if ( source != null ) {
return source; return source;
} }
String supertype = MockSessionFactory.this.getSupertype(getHibernateEntityName()); final String supertype = MockSessionFactory.this.getSupertype(getHibernateEntityName());
PersistentAttribute<? super Object, ?> superattribute final PersistentAttribute<? super Object, ?> superattribute
= new MockMappedDomainType<>(supertype).findAttribute(name); = new MockMappedDomainType<>(supertype).findAttribute(name);
if (superattribute != null) { if (superattribute != null) {
return (SqmPathSource<?>) superattribute; return (SqmPathSource<?>) superattribute;
@ -991,7 +971,7 @@ public abstract class MockSessionFactory
for (Map.Entry<String, MockEntityPersister> entry : entityPersistersByName.entrySet()) { for (Map.Entry<String, MockEntityPersister> entry : entityPersistersByName.entrySet()) {
if (!entry.getValue().getEntityName().equals(getHibernateEntityName()) if (!entry.getValue().getEntityName().equals(getHibernateEntityName())
&& isSubtype(entry.getValue().getEntityName(), getHibernateEntityName())) { && isSubtype(entry.getValue().getEntityName(), getHibernateEntityName())) {
PersistentAttribute<? super Object, ?> subattribute final PersistentAttribute<? super Object, ?> subattribute
= new MockEntityDomainType<>(entry.getValue().getEntityName()).findAttribute(name); = new MockEntityDomainType<>(entry.getValue().getEntityName()).findAttribute(name);
if (subattribute != null) { if (subattribute != null) {
return (SqmPathSource<?>) subattribute; return (SqmPathSource<?>) subattribute;
@ -1003,12 +983,12 @@ public abstract class MockSessionFactory
@Override @Override
public PersistentAttribute<? super X, ?> findAttribute(String name) { public PersistentAttribute<? super X, ?> findAttribute(String name) {
PersistentAttribute<? super X, ?> attribute = super.findAttribute(name); final PersistentAttribute<? super X, ?> attribute = super.findAttribute(name);
if (attribute != null) { if (attribute != null) {
return attribute; return attribute;
} }
String supertype = MockSessionFactory.this.getSupertype(getHibernateEntityName()); final String supertype = MockSessionFactory.this.getSupertype(getHibernateEntityName());
PersistentAttribute<? super Object, ?> superattribute final PersistentAttribute<? super Object, ?> superattribute
= new MockMappedDomainType<>(supertype).findAttribute(name); = new MockMappedDomainType<>(supertype).findAttribute(name);
if (superattribute != null) { if (superattribute != null) {
return superattribute; return superattribute;
@ -1018,7 +998,7 @@ public abstract class MockSessionFactory
@Override @Override
public PersistentAttribute<X,?> findDeclaredAttribute(String name) { public PersistentAttribute<X,?> findDeclaredAttribute(String name) {
String entityName = getHibernateEntityName(); final String entityName = getHibernateEntityName();
return isAttributeDefined(entityName, name) return isAttributeDefined(entityName, name)
? createAttribute(name, entityName, getReferencedPropertyType(entityName, name), this) ? createAttribute(name, entityName, getReferencedPropertyType(entityName, name), this)
: null; : null;
@ -1030,7 +1010,7 @@ public abstract class MockSessionFactory
throw new UnsupportedOperationException(entityName + "." + name); throw new UnsupportedOperationException(entityName + "." + name);
} }
else if ( type.isCollectionType() ) { else if ( type.isCollectionType() ) {
CollectionType collectionType = (CollectionType) type; final CollectionType collectionType = (CollectionType) type;
return createPluralAttribute(collectionType, entityName, name, owner); return createPluralAttribute(collectionType, entityName, name, owner);
} }
else if ( type.isEntityType() ) { else if ( type.isEntityType() ) {
@ -1049,7 +1029,7 @@ public abstract class MockSessionFactory
); );
} }
else if ( type.isComponentType() ) { else if ( type.isComponentType() ) {
CompositeType compositeType = (CompositeType) type; final CompositeType compositeType = (CompositeType) type;
return new SingularAttributeImpl<>( return new SingularAttributeImpl<>(
owner, owner,
name, name,
@ -1070,8 +1050,8 @@ public abstract class MockSessionFactory
name, name,
AttributeClassification.BASIC, AttributeClassification.BASIC,
(DomainType<?>) type, (DomainType<?>) type,
type instanceof JdbcMapping type instanceof JdbcMapping jdbcMapping
? ((JdbcMapping) type).getJavaTypeDescriptor() ? jdbcMapping.getJavaTypeDescriptor()
: null, : null,
null, null,
false, false,
@ -1084,26 +1064,26 @@ public abstract class MockSessionFactory
} }
private DomainType<?> getElementDomainType(String entityName, CollectionType collectionType, ManagedDomainType<?> owner) { private DomainType<?> getElementDomainType(String entityName, CollectionType collectionType, ManagedDomainType<?> owner) {
Type elementType = collectionType.getElementType(MockSessionFactory.this); final Type elementType = collectionType.getElementType(MockSessionFactory.this);
return getDomainType(entityName, collectionType, owner, elementType); return getDomainType(entityName, collectionType, owner, elementType);
} }
private DomainType<?> getMapKeyDomainType(String entityName, CollectionType collectionType, ManagedDomainType<?> owner) { private DomainType<?> getMapKeyDomainType(String entityName, CollectionType collectionType, ManagedDomainType<?> owner) {
Type keyType = getMappingMetamodel().getCollectionDescriptor( collectionType.getRole() ).getIndexType(); final Type keyType = getMappingMetamodel().getCollectionDescriptor( collectionType.getRole() ).getIndexType();
return getDomainType(entityName, collectionType, owner, keyType); return getDomainType(entityName, collectionType, owner, keyType);
} }
private DomainType<?> getDomainType(String entityName, CollectionType collectionType, ManagedDomainType<?> owner, Type elementType) { private DomainType<?> getDomainType(String entityName, CollectionType collectionType, ManagedDomainType<?> owner, Type elementType) {
if ( elementType.isEntityType() ) { if ( elementType.isEntityType() ) {
String associatedEntityName = collectionType.getAssociatedEntityName(MockSessionFactory.this); final String associatedEntityName = collectionType.getAssociatedEntityName(MockSessionFactory.this);
return new MockEntityDomainType<>(associatedEntityName); return new MockEntityDomainType<>(associatedEntityName);
} }
else if ( elementType.isComponentType() ) { else if ( elementType.isComponentType() ) {
CompositeType compositeType = (CompositeType) elementType; final CompositeType compositeType = (CompositeType) elementType;
return createEmbeddableDomainType(entityName, compositeType, owner); return createEmbeddableDomainType(entityName, compositeType, owner);
} }
else if ( elementType instanceof DomainType ) { else if ( elementType instanceof DomainType<?> domainType ) {
return (DomainType<?>) elementType; return domainType;
} }
else { else {
return OBJECT_BASIC_TYPE; return OBJECT_BASIC_TYPE;
@ -1115,16 +1095,15 @@ public abstract class MockSessionFactory
String entityName, String entityName,
String name, String name,
ManagedDomainType<?> owner) { ManagedDomainType<?> owner) {
Property property = new Property(); final Property property = new Property();
property.setName(name); property.setName(name);
JavaType<Object> collectionJavaType = final JavaType<?> collectionJavaType =
typeConfiguration.getJavaTypeRegistry() typeConfiguration.getJavaTypeRegistry()
.getDescriptor(collectionType.getReturnedClass()); .getDescriptor(collectionType.getReturnedClass());
DomainType<?> elementDomainType = getElementDomainType(entityName, collectionType, owner); final DomainType<?> elementDomainType = getElementDomainType(entityName, collectionType, owner);
CollectionClassification classification = collectionType.getCollectionClassification(); final CollectionClassification classification = collectionType.getCollectionClassification();
switch (classification) { return switch ( classification ) {
case LIST: case LIST -> new ListAttributeImpl(
return new ListAttributeImpl(
new PluralAttributeBuilder<>( new PluralAttributeBuilder<>(
collectionJavaType, collectionJavaType,
true, true,
@ -1132,16 +1111,14 @@ public abstract class MockSessionFactory
classification, classification,
elementDomainType, elementDomainType,
typeConfiguration.getBasicTypeRegistry() typeConfiguration.getBasicTypeRegistry()
.getRegisteredType(Integer.class), .getRegisteredType( Integer.class ),
owner, owner,
property, property,
null null
), ),
metadataContext metadataContext
); );
case BAG: case BAG, ID_BAG -> new BagAttributeImpl(
case ID_BAG:
return new BagAttributeImpl(
new PluralAttributeBuilder<>( new PluralAttributeBuilder<>(
collectionJavaType, collectionJavaType,
true, true,
@ -1155,10 +1132,7 @@ public abstract class MockSessionFactory
), ),
metadataContext metadataContext
); );
case SET: case SET, SORTED_SET, ORDERED_SET -> new SetAttributeImpl(
case SORTED_SET:
case ORDERED_SET:
return new SetAttributeImpl(
new PluralAttributeBuilder<>( new PluralAttributeBuilder<>(
collectionJavaType, collectionJavaType,
true, true,
@ -1172,27 +1146,22 @@ public abstract class MockSessionFactory
), ),
metadataContext metadataContext
); );
case MAP: case MAP, SORTED_MAP, ORDERED_MAP -> new MapAttributeImpl(
case SORTED_MAP:
case ORDERED_MAP:
DomainType<?> keyDomainType = getMapKeyDomainType(entityName, collectionType, owner);
return new MapAttributeImpl(
new PluralAttributeBuilder<>( new PluralAttributeBuilder<>(
collectionJavaType, collectionJavaType,
true, true,
AttributeClassification.MANY_TO_MANY, AttributeClassification.MANY_TO_MANY,
classification, classification,
elementDomainType, elementDomainType,
keyDomainType, getMapKeyDomainType( entityName, collectionType, owner ),
owner, owner,
property, property,
null null
), ),
metadataContext metadataContext
); );
default: default -> null;
return null; };
}
} }
private EmbeddableTypeImpl<?> createEmbeddableDomainType(String entityName, CompositeType compositeType, ManagedDomainType<?> owner) { private EmbeddableTypeImpl<?> createEmbeddableDomainType(String entityName, CompositeType compositeType, ManagedDomainType<?> owner) {

View File

@ -269,11 +269,11 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
while (type!=null) { while (type!=null) {
if (isMappedClass(type)) { //ignore unmapped intervening classes if (isMappedClass(type)) { //ignore unmapped intervening classes
AccessType accessType = getAccessType(type, defaultAccessType); final AccessType accessType = getAccessType(type, defaultAccessType);
for (Element member: type.getEnclosedElements()) { for (Element member: type.getEnclosedElements()) {
if (isPersistable(member, accessType)) { if (isPersistable(member, accessType)) {
String name = propertyName(member); final String name = propertyName(member);
Type propertyType = final Type propertyType =
factory.propertyType(member, entityName, factory.propertyType(member, entityName,
qualify(path, name), defaultAccessType); qualify(path, name), defaultAccessType);
if (propertyType != null) { if (propertyType != null) {
@ -292,7 +292,7 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
@Override @Override
public int getPropertyIndex(String name) { public int getPropertyIndex(String name) {
String[] names = getPropertyNames(); final String[] names = getPropertyNames();
for ( int i = 0, max = names.length; i < max; i++ ) { for ( int i = 0, max = names.length; i < max; i++ ) {
if ( names[i].equals( name ) ) { if ( names[i].equals( name ) ) {
return i; return i;
@ -504,14 +504,13 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
} }
final String qualifiedName = entityNameMappings.get(entityName); final String qualifiedName = entityNameMappings.get(entityName);
if ( qualifiedName != null ) { if ( qualifiedName != null ) {
TypeElement result = elementUtil.getTypeElement(qualifiedName); final TypeElement result = elementUtil.getTypeElement(qualifiedName);
entityCache.put(entityName, result); entityCache.put(entityName, result);
return result; return result;
} }
final StandardLocation location = StandardLocation.SOURCE_OUTPUT; try (Reader reader = filer.getResource( StandardLocation.SOURCE_OUTPUT, ENTITY_INDEX, entityName)
try (Reader reader = filer.getResource(location, ENTITY_INDEX, entityName)
.openReader(true); BufferedReader buffered = new BufferedReader(reader) ) { .openReader(true); BufferedReader buffered = new BufferedReader(reader) ) {
TypeElement result = elementUtil.getTypeElement(buffered.readLine()); final TypeElement result = elementUtil.getTypeElement(buffered.readLine());
entityCache.put(entityName, result); entityCache.put(entityName, result);
return result; return result;
} }
@ -519,7 +518,7 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
} }
try (Reader reader = filer.getResource(StandardLocation.CLASS_PATH, ENTITY_INDEX, entityName) try (Reader reader = filer.getResource(StandardLocation.CLASS_PATH, ENTITY_INDEX, entityName)
.openReader(true); BufferedReader buffered = new BufferedReader(reader) ) { .openReader(true); BufferedReader buffered = new BufferedReader(reader) ) {
TypeElement result = elementUtil.getTypeElement(buffered.readLine()); final TypeElement result = elementUtil.getTypeElement(buffered.readLine());
entityCache.put(entityName, result); entityCache.put(entityName, result);
return result; return result;
} }
@ -546,7 +545,7 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
public static TypeElement findEntityByUnqualifiedName(String entityName, ModuleElement module) { public static TypeElement findEntityByUnqualifiedName(String entityName, ModuleElement module) {
for (Element element: module.getEnclosedElements()) { for (Element element: module.getEnclosedElements()) {
if (element.getKind() == ElementKind.PACKAGE) { if (element.getKind() == ElementKind.PACKAGE) {
PackageElement pack = (PackageElement) element; final PackageElement pack = (PackageElement) element;
try { try {
for (Element member : pack.getEnclosedElements()) { for (Element member : pack.getEnclosedElements()) {
if (isMatchingEntity(member, entityName)) { if (isMatchingEntity(member, entityName)) {
@ -1085,8 +1084,8 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
} }
private static TypeMirror memberType(Element member) { private static TypeMirror memberType(Element member) {
if (member instanceof ExecutableElement) { if (member instanceof ExecutableElement executableElement) {
return ((ExecutableElement) member).getReturnType(); return executableElement.getReturnType();
} }
else if (member instanceof VariableElement) { else if (member instanceof VariableElement) {
return member.asType(); return member.asType();