diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/MockEntityPersister.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/MockEntityPersister.java index f36c2b5b06..db5319aa12 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/MockEntityPersister.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/MockEntityPersister.java @@ -149,7 +149,7 @@ public abstract class MockEntityPersister implements EntityPersister, Joinable, @Override public Set getSubclassEntityNames() { - Set names = new HashSet<>(); + final Set names = new HashSet<>(); names.add( entityName ); for (MockEntityPersister persister : factory.getMockEntityPersisters()) { if (persister.isSubclassPersister(this)) { diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/MockSessionFactory.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/MockSessionFactory.java index 9fc9a03f68..b1c471c6db 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/MockSessionFactory.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/MockSessionFactory.java @@ -257,21 +257,15 @@ public abstract class MockSessionFactory } static CollectionType createCollectionType(String role, String name) { - switch (name) { - case "Set": - case "SortedSet": + return switch ( name ) { + case "Set", "SortedSet" -> //might actually be a bag! //TODO: look for @OrderColumn on the property - return new SetType(role, null); - case "List": - case "SortedList": - return new ListType(role, null); - case "Map": - case "SortedMap": - return new MapType(role, null); - default: - return new BagType(role, null); - } + new SetType( role, null ); + case "List", "SortedList" -> new ListType( role, null ); + case "Map", "SortedMap" -> new MapType( role, null ); + default -> new BagType( role, null ); + }; } /** @@ -456,26 +450,17 @@ public abstract class MockSessionFactory } static Class toPrimitiveClass(Class type) { - switch (type.getName()) { - case "java.lang.Boolean": - return boolean.class; - case "java.lang.Character": - return char.class; - case "java.lang.Integer": - return int.class; - case "java.lang.Short": - return short.class; - case "java.lang.Byte": - 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; - } + return switch ( type.getName() ) { + case "java.lang.Boolean" -> boolean.class; + case "java.lang.Character" -> char.class; + case "java.lang.Integer" -> int.class; + case "java.lang.Short" -> short.class; + case "java.lang.Byte" -> byte.class; + case "java.lang.Long" -> long.class; + case "java.lang.Float" -> float.class; + case "java.lang.Double" -> double.class; + default -> Object.class; + }; } @Override @@ -539,7 +524,7 @@ public abstract class MockSessionFactory @Override public RuntimeMetamodelsImplementor getRuntimeMetamodels() { - RuntimeMetamodelsImpl runtimeMetamodels = new RuntimeMetamodelsImpl(); + final RuntimeMetamodelsImpl runtimeMetamodels = new RuntimeMetamodelsImpl(); runtimeMetamodels.setJpaMetamodel( metamodel.getJpaMetamodel() ); runtimeMetamodels.setMappingMetamodel( metamodel ); return runtimeMetamodels; @@ -789,12 +774,9 @@ public abstract class MockSessionFactory @Override public EntityDomainType entity(String entityName) { - if ( isEntityDefined(entityName) ) { - return new MockEntityDomainType<>(entityName); - } - else { - return null; - } + return isEntityDefined( entityName ) + ? new MockEntityDomainType<>( entityName ) + : null; } @Override @@ -805,7 +787,9 @@ public abstract class MockSessionFactory else if (isEntityDefined(queryName)) { return qualifyName(queryName); } - return null; + else { + return null; + } } @Override @@ -822,12 +806,9 @@ public abstract class MockSessionFactory @Override public EntityDomainType findEntityType(Class cls) { - if ( isEntityDefined( cls.getName() ) ) { - return new MockEntityDomainType<>( cls.getName() ); - } - else { - return null; - } + return isEntityDefined( cls.getName() ) + ? new MockEntityDomainType( cls.getName() ) + : null; } @Override @@ -843,7 +824,7 @@ public abstract class MockSessionFactory @Override public EnumJavaType getEnumType(String className) { if ( isEnum(className) ) { - return new EnumJavaType( Enum.class ) { + return new EnumJavaType<>( Enum.class ) { @Override public String getTypeName() { return className; @@ -857,10 +838,9 @@ public abstract class MockSessionFactory @Override public JavaType getJavaConstantType(String className, String fieldName) { - final Class fieldType = javaConstantType( className, fieldName ); return MockSessionFactory.this.getTypeConfiguration() .getJavaTypeRegistry() - .getDescriptor( fieldType ); + .getDescriptor( javaConstantType( className, fieldName ) ); } @Override @@ -898,7 +878,7 @@ public abstract class MockSessionFactory @Override public PersistentAttribute findDeclaredAttribute(String name) { - String typeName = getTypeName(); + final String typeName = getTypeName(); return isFieldDefined(typeName, name) ? createAttribute(name, typeName, propertyType(typeName, name), this) : null; @@ -946,21 +926,21 @@ public abstract class MockSessionFactory if (type == null) { return null; } - else if (type instanceof BasicDomainType) { + else if (type instanceof BasicDomainType basicDomainType) { return new BasicSqmPathSource<>( EntityIdentifierMapping.ID_ROLE_NAME, null, - (BasicDomainType) type, + basicDomainType, MockEntityDomainType.this.getExpressibleJavaType(), Bindable.BindableType.SINGULAR_ATTRIBUTE, false ); } - else if (type instanceof EmbeddableDomainType) { + else if (type instanceof EmbeddableDomainType embeddableDomainType) { return new EmbeddedSqmPathSource<>( EntityIdentifierMapping.ID_ROLE_NAME, null, - (EmbeddableDomainType) type, + embeddableDomainType, Bindable.BindableType.SINGULAR_ATTRIBUTE, false ); @@ -982,8 +962,8 @@ public abstract class MockSessionFactory if ( source != null ) { return source; } - String supertype = MockSessionFactory.this.getSupertype(getHibernateEntityName()); - PersistentAttribute superattribute + final String supertype = MockSessionFactory.this.getSupertype(getHibernateEntityName()); + final PersistentAttribute superattribute = new MockMappedDomainType<>(supertype).findAttribute(name); if (superattribute != null) { return (SqmPathSource) superattribute; @@ -991,7 +971,7 @@ public abstract class MockSessionFactory for (Map.Entry entry : entityPersistersByName.entrySet()) { if (!entry.getValue().getEntityName().equals(getHibernateEntityName()) && isSubtype(entry.getValue().getEntityName(), getHibernateEntityName())) { - PersistentAttribute subattribute + final PersistentAttribute subattribute = new MockEntityDomainType<>(entry.getValue().getEntityName()).findAttribute(name); if (subattribute != null) { return (SqmPathSource) subattribute; @@ -1003,12 +983,12 @@ public abstract class MockSessionFactory @Override public PersistentAttribute findAttribute(String name) { - PersistentAttribute attribute = super.findAttribute(name); + final PersistentAttribute attribute = super.findAttribute(name); if (attribute != null) { return attribute; } - String supertype = MockSessionFactory.this.getSupertype(getHibernateEntityName()); - PersistentAttribute superattribute + final String supertype = MockSessionFactory.this.getSupertype(getHibernateEntityName()); + final PersistentAttribute superattribute = new MockMappedDomainType<>(supertype).findAttribute(name); if (superattribute != null) { return superattribute; @@ -1018,7 +998,7 @@ public abstract class MockSessionFactory @Override public PersistentAttribute findDeclaredAttribute(String name) { - String entityName = getHibernateEntityName(); + final String entityName = getHibernateEntityName(); return isAttributeDefined(entityName, name) ? createAttribute(name, entityName, getReferencedPropertyType(entityName, name), this) : null; @@ -1030,7 +1010,7 @@ public abstract class MockSessionFactory throw new UnsupportedOperationException(entityName + "." + name); } else if ( type.isCollectionType() ) { - CollectionType collectionType = (CollectionType) type; + final CollectionType collectionType = (CollectionType) type; return createPluralAttribute(collectionType, entityName, name, owner); } else if ( type.isEntityType() ) { @@ -1049,7 +1029,7 @@ public abstract class MockSessionFactory ); } else if ( type.isComponentType() ) { - CompositeType compositeType = (CompositeType) type; + final CompositeType compositeType = (CompositeType) type; return new SingularAttributeImpl<>( owner, name, @@ -1070,8 +1050,8 @@ public abstract class MockSessionFactory name, AttributeClassification.BASIC, (DomainType) type, - type instanceof JdbcMapping - ? ((JdbcMapping) type).getJavaTypeDescriptor() + type instanceof JdbcMapping jdbcMapping + ? jdbcMapping.getJavaTypeDescriptor() : null, null, false, @@ -1084,26 +1064,26 @@ public abstract class MockSessionFactory } 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); } 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); } private DomainType getDomainType(String entityName, CollectionType collectionType, ManagedDomainType owner, Type elementType) { if ( elementType.isEntityType() ) { - String associatedEntityName = collectionType.getAssociatedEntityName(MockSessionFactory.this); + final String associatedEntityName = collectionType.getAssociatedEntityName(MockSessionFactory.this); return new MockEntityDomainType<>(associatedEntityName); } else if ( elementType.isComponentType() ) { - CompositeType compositeType = (CompositeType) elementType; + final CompositeType compositeType = (CompositeType) elementType; return createEmbeddableDomainType(entityName, compositeType, owner); } - else if ( elementType instanceof DomainType ) { - return (DomainType) elementType; + else if ( elementType instanceof DomainType domainType ) { + return domainType; } else { return OBJECT_BASIC_TYPE; @@ -1115,84 +1095,73 @@ public abstract class MockSessionFactory String entityName, String name, ManagedDomainType owner) { - Property property = new Property(); + final Property property = new Property(); property.setName(name); - JavaType collectionJavaType = + final JavaType collectionJavaType = typeConfiguration.getJavaTypeRegistry() .getDescriptor(collectionType.getReturnedClass()); - DomainType elementDomainType = getElementDomainType(entityName, collectionType, owner); - CollectionClassification classification = collectionType.getCollectionClassification(); - switch (classification) { - case LIST: - return new ListAttributeImpl( - new PluralAttributeBuilder<>( - collectionJavaType, - true, - AttributeClassification.MANY_TO_MANY, - classification, - elementDomainType, - typeConfiguration.getBasicTypeRegistry() - .getRegisteredType(Integer.class), - owner, - property, - null - ), - metadataContext - ); - case BAG: - case ID_BAG: - return new BagAttributeImpl( - new PluralAttributeBuilder<>( - collectionJavaType, - true, - AttributeClassification.MANY_TO_MANY, - classification, - elementDomainType, - null, - owner, - property, - null - ), - metadataContext - ); - case SET: - case SORTED_SET: - case ORDERED_SET: - return new SetAttributeImpl( - new PluralAttributeBuilder<>( - collectionJavaType, - true, - AttributeClassification.MANY_TO_MANY, - classification, - elementDomainType, - null, - owner, - property, - null - ), - metadataContext - ); - case MAP: - case SORTED_MAP: - case ORDERED_MAP: - DomainType keyDomainType = getMapKeyDomainType(entityName, collectionType, owner); - return new MapAttributeImpl( - new PluralAttributeBuilder<>( - collectionJavaType, - true, - AttributeClassification.MANY_TO_MANY, - classification, - elementDomainType, - keyDomainType, - owner, - property, - null - ), - metadataContext - ); - default: - return null; - } + final DomainType elementDomainType = getElementDomainType(entityName, collectionType, owner); + final CollectionClassification classification = collectionType.getCollectionClassification(); + return switch ( classification ) { + case LIST -> new ListAttributeImpl( + new PluralAttributeBuilder<>( + collectionJavaType, + true, + AttributeClassification.MANY_TO_MANY, + classification, + elementDomainType, + typeConfiguration.getBasicTypeRegistry() + .getRegisteredType( Integer.class ), + owner, + property, + null + ), + metadataContext + ); + case BAG, ID_BAG -> new BagAttributeImpl( + new PluralAttributeBuilder<>( + collectionJavaType, + true, + AttributeClassification.MANY_TO_MANY, + classification, + elementDomainType, + null, + owner, + property, + null + ), + metadataContext + ); + case SET, SORTED_SET, ORDERED_SET -> new SetAttributeImpl( + new PluralAttributeBuilder<>( + collectionJavaType, + true, + AttributeClassification.MANY_TO_MANY, + classification, + elementDomainType, + null, + owner, + property, + null + ), + metadataContext + ); + case MAP, SORTED_MAP, ORDERED_MAP -> new MapAttributeImpl( + new PluralAttributeBuilder<>( + collectionJavaType, + true, + AttributeClassification.MANY_TO_MANY, + classification, + elementDomainType, + getMapKeyDomainType( entityName, collectionType, owner ), + owner, + property, + null + ), + metadataContext + ); + default -> null; + }; } private EmbeddableTypeImpl createEmbeddableDomainType(String entityName, CompositeType compositeType, ManagedDomainType owner) { diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/ProcessorSessionFactory.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/ProcessorSessionFactory.java index f257e343ed..994d3550c3 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/ProcessorSessionFactory.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/ProcessorSessionFactory.java @@ -269,11 +269,11 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory { while (type!=null) { if (isMappedClass(type)) { //ignore unmapped intervening classes - AccessType accessType = getAccessType(type, defaultAccessType); + final AccessType accessType = getAccessType(type, defaultAccessType); for (Element member: type.getEnclosedElements()) { if (isPersistable(member, accessType)) { - String name = propertyName(member); - Type propertyType = + final String name = propertyName(member); + final Type propertyType = factory.propertyType(member, entityName, qualify(path, name), defaultAccessType); if (propertyType != null) { @@ -292,7 +292,7 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory { @Override public int getPropertyIndex(String name) { - String[] names = getPropertyNames(); + final String[] names = getPropertyNames(); for ( int i = 0, max = names.length; i < max; i++ ) { if ( names[i].equals( name ) ) { return i; @@ -504,14 +504,13 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory { } final String qualifiedName = entityNameMappings.get(entityName); if ( qualifiedName != null ) { - TypeElement result = elementUtil.getTypeElement(qualifiedName); + final TypeElement result = elementUtil.getTypeElement(qualifiedName); entityCache.put(entityName, result); return result; } - final StandardLocation location = StandardLocation.SOURCE_OUTPUT; - try (Reader reader = filer.getResource(location, ENTITY_INDEX, entityName) + try (Reader reader = filer.getResource( StandardLocation.SOURCE_OUTPUT, ENTITY_INDEX, entityName) .openReader(true); BufferedReader buffered = new BufferedReader(reader) ) { - TypeElement result = elementUtil.getTypeElement(buffered.readLine()); + final TypeElement result = elementUtil.getTypeElement(buffered.readLine()); entityCache.put(entityName, result); return result; } @@ -519,7 +518,7 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory { } try (Reader reader = filer.getResource(StandardLocation.CLASS_PATH, ENTITY_INDEX, entityName) .openReader(true); BufferedReader buffered = new BufferedReader(reader) ) { - TypeElement result = elementUtil.getTypeElement(buffered.readLine()); + final TypeElement result = elementUtil.getTypeElement(buffered.readLine()); entityCache.put(entityName, result); return result; } @@ -546,7 +545,7 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory { public static TypeElement findEntityByUnqualifiedName(String entityName, ModuleElement module) { for (Element element: module.getEnclosedElements()) { if (element.getKind() == ElementKind.PACKAGE) { - PackageElement pack = (PackageElement) element; + final PackageElement pack = (PackageElement) element; try { for (Element member : pack.getEnclosedElements()) { if (isMatchingEntity(member, entityName)) { @@ -1085,8 +1084,8 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory { } private static TypeMirror memberType(Element member) { - if (member instanceof ExecutableElement) { - return ((ExecutableElement) member).getReturnType(); + if (member instanceof ExecutableElement executableElement) { + return executableElement.getReturnType(); } else if (member instanceof VariableElement) { return member.asType();