diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/Context.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/Context.java index c2076f02ff2a..8350400e6f7b 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/Context.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/Context.java @@ -504,12 +504,25 @@ public Map> getEnumTypesByValue() { return enumTypesByValue; } - public void addEnumValue(String type, String value) { - enumTypesByValue.computeIfAbsent( value, s -> new TreeSet<>() ).add( type ); + public void addEnumValue( + String qualifiedTypeName, String shortTypeName, + @Nullable String outerTypeQualifiedName, @Nullable String outerShortTypeName, + String value) { + addEnumValue( qualifiedTypeName, value ); + addEnumValue( qualifiedTypeName, qualifiedTypeName + '.' + value ); + addEnumValue( qualifiedTypeName, shortTypeName + '.' + value ); + if ( outerShortTypeName != null ) { + addEnumValue( qualifiedTypeName, outerShortTypeName + '.' + shortTypeName + '.' + value ); + addEnumValue( qualifiedTypeName, outerShortTypeName + '$' + shortTypeName + '.' + value ); + addEnumValue( qualifiedTypeName, outerTypeQualifiedName + '$' + shortTypeName + '.' + value ); + } + } + + private void addEnumValue(String qualifiedTypeName, String value) { + enumTypesByValue.computeIfAbsent( value, s -> new TreeSet<>() ).add( qualifiedTypeName ); } - @Nullable - public TypeElement entityType(String entityName) { + public @Nullable TypeElement entityType(String entityName) { final Elements elementUtils = getElementUtils(); final String qualifiedName = qualifiedNameForEntityName(entityName); if ( qualifiedName != null ) { diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/HibernateProcessor.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/HibernateProcessor.java index a1a84c5c6eef..88e5696910bc 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/HibernateProcessor.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/HibernateProcessor.java @@ -269,13 +269,9 @@ && packagePresent(quarkusOrmPanachePackage) ) { final String suppressedWarnings = options.get( ADD_SUPPRESS_WARNINGS_ANNOTATION ); if ( suppressedWarnings != null ) { - if ( parseBoolean(suppressedWarnings) ) { - // legacy behavior from HHH-12068 - context.setSuppressedWarnings(new String[] {"deprecation", "rawtypes"}); - } - else { - context.setSuppressedWarnings( suppressedWarnings.replace(" ","").split(",\\s*") ); - } + context.setSuppressedWarnings( parseBoolean( suppressedWarnings ) + ? new String[] {"deprecation", "rawtypes"} // legacy behavior from HHH-12068 + : suppressedWarnings.replace( " ", "" ).split( ",\\s*" ) ); } context.setInclude( options.getOrDefault( INCLUDE, "*" ) ); @@ -647,9 +643,15 @@ private void indexEnumValues(TypeMirror type) { final DeclaredType declaredType = (DeclaredType) type; final TypeElement fieldType = (TypeElement) declaredType.asElement(); if ( fieldType.getKind() == ElementKind.ENUM ) { - for (Element enumMember : fieldType.getEnclosedElements() ) { + for ( Element enumMember : fieldType.getEnclosedElements() ) { if ( enumMember.getKind() == ElementKind.ENUM_CONSTANT ) { + final Element enclosingElement = fieldType.getEnclosingElement(); + final boolean hasOuterType = + enclosingElement.getKind().isClass() || enclosingElement.getKind().isInterface(); context.addEnumValue( fieldType.getQualifiedName().toString(), + fieldType.getSimpleName().toString(), + hasOuterType ? ((TypeElement) enclosingElement).getQualifiedName().toString() : null, + hasOuterType ? enclosingElement.getSimpleName().toString() : null, enumMember.getSimpleName().toString() ); } } diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java index a4b8a8f649a1..65b547399c93 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java @@ -30,7 +30,6 @@ import org.hibernate.query.sqm.tree.expression.SqmParameter; import org.hibernate.query.sqm.tree.select.SqmSelectStatement; -import javax.annotation.processing.ProcessingEnvironment; import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.Element; @@ -48,7 +47,6 @@ import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeVariable; import javax.lang.model.type.WildcardType; -import javax.lang.model.util.Elements; import javax.lang.model.util.Types; import javax.tools.Diagnostic; @@ -85,10 +83,12 @@ import static org.hibernate.processor.util.TypeUtils.containsAnnotation; import static org.hibernate.processor.util.TypeUtils.determineAccessTypeForHierarchy; import static org.hibernate.processor.util.TypeUtils.determineAnnotationSpecifiedAccessType; +import static org.hibernate.processor.util.TypeUtils.extendsClass; import static org.hibernate.processor.util.TypeUtils.findMappedSuperClass; import static org.hibernate.processor.util.TypeUtils.getAnnotationMirror; import static org.hibernate.processor.util.TypeUtils.getAnnotationValue; import static org.hibernate.processor.util.TypeUtils.hasAnnotation; +import static org.hibernate.processor.util.TypeUtils.implementsInterface; import static org.hibernate.processor.util.TypeUtils.primitiveClassMatchesKind; import static org.hibernate.processor.util.TypeUtils.propertyName; @@ -652,41 +652,18 @@ boolean needsDefaultConstructor() { } private boolean isPanacheType(TypeElement type) { - return isOrmPanacheType( type ) - || isReactivePanacheType( type ); + return context.usesQuarkusOrm() && isOrmPanacheType( type ) + || context.usesQuarkusReactive() && isReactivePanacheType( type ); } private boolean isOrmPanacheType(TypeElement type) { - final ProcessingEnvironment processingEnvironment = context.getProcessingEnvironment(); - final Elements elements = processingEnvironment.getElementUtils(); - final TypeElement panacheRepositorySuperType = elements.getTypeElement( PANACHE_ORM_REPOSITORY_BASE ); - final TypeElement panacheEntitySuperType = elements.getTypeElement( PANACHE_ORM_ENTITY_BASE ); - if ( panacheRepositorySuperType == null || panacheEntitySuperType == null ) { - return false; - } - else { - final Types types = processingEnvironment.getTypeUtils(); - // check against a raw supertype of PanacheRepositoryBase, which .asType() is not - return types.isSubtype( type.asType(), types.getDeclaredType( panacheRepositorySuperType ) ) - || types.isSubtype( type.asType(), panacheEntitySuperType.asType() ); - } + return implementsInterface( type, PANACHE_ORM_REPOSITORY_BASE ) + || extendsClass( type, PANACHE_ORM_ENTITY_BASE ); } private boolean isReactivePanacheType(TypeElement type) { - final ProcessingEnvironment processingEnvironment = context.getProcessingEnvironment(); - final Elements elements = processingEnvironment.getElementUtils(); - final TypeElement panacheRepositorySuperType = elements.getTypeElement( PANACHE_REACTIVE_REPOSITORY_BASE ); - final TypeElement panacheEntitySuperType = elements.getTypeElement( PANACHE_REACTIVE_ENTITY_BASE ); - - if ( panacheRepositorySuperType == null || panacheEntitySuperType == null ) { - return false; - } - else { - final Types types = processingEnvironment.getTypeUtils(); - // check against a raw supertype of PanacheRepositoryBase, which .asType() is not - return types.isSubtype( type.asType(), types.getDeclaredType( panacheRepositorySuperType ) ) - || types.isSubtype( type.asType(), panacheEntitySuperType.asType() ); - } + return implementsInterface( type, PANACHE_REACTIVE_REPOSITORY_BASE ) + || extendsClass( type, PANACHE_REACTIVE_ENTITY_BASE ); } /** diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/TypeUtils.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/TypeUtils.java index 598813de934d..556e6bec4b3b 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/TypeUtils.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/TypeUtils.java @@ -129,12 +129,11 @@ protected TypeMirror defaultAction(TypeMirror e, Void aVoid) { } public static @Nullable TypeElement getSuperclassTypeElement(TypeElement element) { - final TypeMirror superClass = element.getSuperclass(); + final TypeMirror superclass = element.getSuperclass(); //superclass of Object is of NoType which returns some other kind - if ( superClass.getKind() == TypeKind.DECLARED ) { - //F..king Ch...t Have those people used their horrible APIs even once? - final Element superClassElement = ( (DeclaredType) superClass ).asElement(); - return (TypeElement) superClassElement; + if ( superclass.getKind() == TypeKind.DECLARED ) { + final DeclaredType declaredType = (DeclaredType) superclass; + return (TypeElement) declaredType.asElement(); } else { return null; @@ -466,14 +465,11 @@ private static void setDefaultAccessTypeForMappedSuperclassesInHierarchy(TypeEle } private static @Nullable AccessType getAccessTypeOfIdAnnotation(Element element) { - switch ( element.getKind() ) { - case FIELD: - return AccessType.FIELD; - case METHOD: - return AccessType.PROPERTY; - default: - return null; - } + return switch ( element.getKind() ) { + case FIELD -> AccessType.FIELD; + case METHOD -> AccessType.PROPERTY; + default -> null; + }; } private static boolean isIdAnnotation(AnnotationMirror annotationMirror) { @@ -522,26 +518,17 @@ public static boolean isClassOrRecordType(Element element) { } public static boolean primitiveClassMatchesKind(Class itemType, TypeKind kind) { - switch (kind) { - case SHORT: - return itemType.equals(Short.class); - case INT: - return itemType.equals(Integer.class); - case LONG: - return itemType.equals(Long.class); - case BOOLEAN: - return itemType.equals(Boolean.class); - case FLOAT: - return itemType.equals(Float.class); - case DOUBLE: - return itemType.equals(Double.class); - case CHAR: - return itemType.equals(Character.class); - case BYTE: - return itemType.equals(Byte.class); - default: - return false; - } + return switch ( kind ) { + case SHORT -> itemType.equals( Short.class ); + case INT -> itemType.equals( Integer.class ); + case LONG -> itemType.equals( Long.class ); + case BOOLEAN -> itemType.equals( Boolean.class ); + case FLOAT -> itemType.equals( Float.class ); + case DOUBLE -> itemType.equals( Double.class ); + case CHAR -> itemType.equals( Character.class ); + case BYTE -> itemType.equals( Byte.class ); + default -> false; + }; } public static boolean isPropertyGetter(ExecutableType executable, Element element) { @@ -602,7 +589,7 @@ else if ( element.getKind() == ElementKind.METHOD ) { return elementsUtil.getName(decapitalize(name.substring(3))).toString(); } else if ( name.startsWith( "is" ) ) { - return (elementsUtil.getName(decapitalize(name.substring(2)))).toString(); + return elementsUtil.getName(decapitalize(name.substring(2))).toString(); } return elementsUtil.getName(decapitalize(name)).toString(); } @@ -613,8 +600,7 @@ else if ( name.startsWith( "is" ) ) { public static @Nullable String findMappedSuperClass(Metamodel entity, Context context) { final Element element = entity.getElement(); - if ( element instanceof TypeElement ) { - final TypeElement typeElement = (TypeElement) element; + if ( element instanceof TypeElement typeElement ) { TypeMirror superClass = typeElement.getSuperclass(); //superclass of Object is of NoType which returns some other kind while ( superClass.getKind() == TypeKind.DECLARED ) { @@ -654,6 +640,33 @@ private static boolean extendsSuperMetaModel(Element superClassElement, boolean || !entityMetaComplete && containsAnnotation( superClassElement, ENTITY, MAPPED_SUPERCLASS ); } + public static boolean implementsInterface(TypeElement type, String interfaceName) { + for ( TypeMirror iface : type.getInterfaces() ) { + if ( iface.getKind() == TypeKind.DECLARED ) { + final DeclaredType declaredType = (DeclaredType) iface; + final TypeElement typeElement = (TypeElement) declaredType.asElement(); + if ( typeElement.getQualifiedName().contentEquals( interfaceName ) + || implementsInterface( typeElement, interfaceName ) ) { + return true; + } + } + } + return false; + } + + public static boolean extendsClass(TypeElement type, String className) { + TypeMirror superclass = type.getSuperclass(); + while ( superclass != null && superclass.getKind() == TypeKind.DECLARED ) { + final DeclaredType declaredType = (DeclaredType) superclass; + final TypeElement typeElement = (TypeElement) declaredType.asElement(); + if ( typeElement.getQualifiedName().contentEquals( className ) ) { + return true; + } + superclass = typeElement.getSuperclass(); + } + return false; + } + static class EmbeddedAttributeVisitor extends SimpleTypeVisitor8<@Nullable TypeElement, Element> { private final Context context; @@ -665,7 +678,7 @@ static class EmbeddedAttributeVisitor extends SimpleTypeVisitor8<@Nullable TypeE public @Nullable TypeElement visitDeclared(DeclaredType declaredType, Element element) { final TypeElement returnedElement = (TypeElement) context.getTypeUtils().asElement( declaredType ); - return containsAnnotation( NullnessUtil.castNonNull( returnedElement ), EMBEDDABLE ) ? returnedElement : null; + return containsAnnotation( castNonNull( returnedElement ), EMBEDDABLE ) ? returnedElement : null; } @Override 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 9ddfed33d231..f257e343edf3 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 @@ -57,7 +57,6 @@ import static org.hibernate.internal.util.StringHelper.root; import static org.hibernate.internal.util.StringHelper.split; import static org.hibernate.internal.util.StringHelper.unroot; -import static org.hibernate.metamodel.model.domain.internal.JpaMetamodelImpl.addAllowedEnumLiteralsToEnumTypesMap; import static org.hibernate.processor.util.Constants.JAVA_OBJECT; /** @@ -90,7 +89,7 @@ public static MockSessionFactory create( private final Types typeUtil; private final Filer filer; private final Map entityNameMappings; - private final Map> allowedEnumLiteralsToEnumTypeNames; + private final Map> enumTypesByValue; public ProcessorSessionFactory( ProcessingEnvironment processingEnvironment, @@ -100,45 +99,29 @@ public ProcessorSessionFactory( typeUtil = processingEnvironment.getTypeUtils(); filer = processingEnvironment.getFiler(); this.entityNameMappings = entityNameMappings; - final Map> allowedEnumLiteralsToEnumTypeNames = new HashMap<>( enumTypesByValue.size() << 2 ); - for ( Map.Entry> entry : enumTypesByValue.entrySet() ) { - final String enumConstantName = entry.getKey(); - for ( String enumClassName : entry.getValue() ) { - final TypeElement enumTypeElement = elementUtil.getTypeElement( enumClassName ); - if ( enumTypeElement != null ) { - addAllowedEnumLiteralsToEnumTypesMap( - allowedEnumLiteralsToEnumTypeNames, - enumConstantName, - enumTypeElement.getSimpleName().toString(), - elementUtil.getBinaryName( enumTypeElement ).toString(), - enumClassName - ); - } - } - } - this.allowedEnumLiteralsToEnumTypeNames = allowedEnumLiteralsToEnumTypeNames; + this.enumTypesByValue = enumTypesByValue; } @Override MockEntityPersister createMockEntityPersister(String entityName) { - TypeElement type = findEntityClass(entityName); + final TypeElement type = findEntityClass(entityName); return type == null ? null : entityPersister.make(entityName, type, this); } @Override MockCollectionPersister createMockCollectionPersister(String role) { - String entityName = root(role); //only works because entity names don't contain dots - String propertyPath = unroot(role); - TypeElement entityClass = findEntityClass(entityName); - AccessType defaultAccessType = getDefaultAccessType(entityClass); - Element property = findPropertyByPath(entityClass, propertyPath, defaultAccessType); - CollectionType collectionType = collectionType(memberType(property), role); + final String entityName = root(role); //only works because entity names don't contain dots + final String propertyPath = unroot(role); + final TypeElement entityClass = findEntityClass(entityName); + final AccessType defaultAccessType = getDefaultAccessType(entityClass); + final Element property = findPropertyByPath(entityClass, propertyPath, defaultAccessType); + final CollectionType collectionType = collectionType(memberType(property), role); if (isToManyAssociation(property)) { return toManyPersister.make(role, collectionType, getToManyTargetEntityName(property), this); } else if (isElementCollectionProperty(property)) { - Element elementType = asElement(getElementCollectionElementType(property)); + final Element elementType = asElement(getElementCollectionElementType(property)); return collectionPersister.make(role, collectionType, elementType, propertyPath, defaultAccessType, this); } @@ -150,9 +133,9 @@ else if (isElementCollectionProperty(property)) { @Override Type propertyType(String typeName, String propertyPath) { - TypeElement type = findClassByQualifiedName(typeName); - AccessType accessType = getAccessType(type, AccessType.FIELD); - Element propertyByPath = findPropertyByPath(type, propertyPath, accessType); + final TypeElement type = findClassByQualifiedName(typeName); + final AccessType accessType = getAccessType(type, AccessType.FIELD); + final Element propertyByPath = findPropertyByPath(type, propertyPath, accessType); return propertyByPath == null ? null : propertyType(propertyByPath, typeName, propertyPath, accessType); } @@ -171,9 +154,8 @@ private static Element dereference(AccessType defaultAccessType, Element symbol, return null; } else { - Element element = asElement(symbol.asType()); - return element instanceof TypeElement - ? findProperty((TypeElement) element, segment, defaultAccessType) + return asElement(symbol.asType()) instanceof TypeElement element + ? findProperty(element, segment, defaultAccessType) : null; } } @@ -223,7 +205,7 @@ public String getTypeName() { } private static JdbcType enumJdbcType(Element member) { - VariableElement mapping = (VariableElement) + final VariableElement mapping = (VariableElement) getAnnotationMember(getAnnotation(member,"Enumerated"), "value"); return mapping != null && mapping.getSimpleName().contentEquals("STRING") ? VarcharJdbcType.INSTANCE @@ -234,7 +216,7 @@ private static JdbcType enumJdbcType(Element member) { @Override @Nullable Set getEnumTypesForValue(String value) { - Set result = allowedEnumLiteralsToEnumTypeNames.get( value); + final Set result = enumTypesByValue.get(value); if ( result != null ) { return result; } @@ -242,13 +224,13 @@ Set getEnumTypesForValue(String value) { .openReader(true); BufferedReader buffered = new BufferedReader(reader) ) { return Set.of(split(" ", buffered.readLine())); } - catch (IOException e) { + catch (IOException ignore) { } try (Reader reader = filer.getResource(StandardLocation.CLASS_PATH, ENTITY_INDEX, '.' + value) .openReader(true); BufferedReader buffered = new BufferedReader(reader) ) { return Set.of(split(" ", buffered.readLine())); } - catch (IOException e) { + catch (IOException ignore) { } return null; } @@ -282,8 +264,8 @@ public Component(TypeElement type, ProcessorSessionFactory factory) { this.type = type; - List names = new ArrayList<>(); - List types = new ArrayList<>(); + final List names = new ArrayList<>(); + final List types = new ArrayList<>(); while (type!=null) { if (isMappedClass(type)) { //ignore unmapped intervening classes @@ -387,19 +369,19 @@ public String getRootEntityName() { @Override boolean isSamePersister(MockEntityPersister entityPersister) { - EntityPersister persister = (EntityPersister) entityPersister; + final EntityPersister persister = (EntityPersister) entityPersister; return typeUtil.isSameType( persister.type.asType(), type.asType() ); } @Override boolean isSubclassPersister(MockEntityPersister entityPersister) { - EntityPersister persister = (EntityPersister) entityPersister; + final EntityPersister persister = (EntityPersister) entityPersister; return typeUtil.isSubtype( persister.type.asType(), type.asType() ); } @Override Type createPropertyType(String propertyPath) { - Element symbol = findPropertyByPath(type, propertyPath, defaultAccessType); + final Element symbol = findPropertyByPath(type, propertyPath, defaultAccessType); return symbol == null ? null : factory.propertyType(symbol, getEntityName(), propertyPath, defaultAccessType); } @@ -471,7 +453,7 @@ public ElementCollectionPersister(String role, @Override Type getElementPropertyType(String propertyPath) { - Element symbol = findPropertyByPath(elementType, propertyPath, defaultAccessType); + final Element symbol = findPropertyByPath(elementType, propertyPath, defaultAccessType); return symbol == null ? null : factory.propertyType(symbol, getOwnerEntityName(), propertyPath, defaultAccessType); } @@ -484,13 +466,13 @@ boolean isEntityDefined(String entityName) { @Override String qualifyName(String entityName) { - TypeElement entityClass = findEntityClass(entityName); + final TypeElement entityClass = findEntityClass(entityName); return entityClass == null ? null : entityClass.getQualifiedName().toString(); } @Override boolean isAttributeDefined(String entityName, String fieldName) { - TypeElement entityClass = findEntityClass(entityName); + final TypeElement entityClass = findEntityClass(entityName); return entityClass != null && findPropertyByPath(entityClass, fieldName, getDefaultAccessType(entityClass)) != null; } @@ -508,7 +490,7 @@ else if (entityName.indexOf('.')>0) { } private TypeElement findEntityByQualifiedName(String entityName) { - TypeElement type = findClassByQualifiedName(entityName); + final TypeElement type = findClassByQualifiedName(entityName); return type != null && isEntity(type) ? type : null; } @@ -516,24 +498,24 @@ private TypeElement findEntityByQualifiedName(String entityName) { private final Map entityCache = new HashMap<>(); private TypeElement findEntityByUnqualifiedName(String entityName) { - TypeElement cached = entityCache.get(entityName); + final TypeElement cached = entityCache.get(entityName); if ( cached != null ) { return cached; } - String qualifiedName = entityNameMappings.get(entityName); + final String qualifiedName = entityNameMappings.get(entityName); if ( qualifiedName != null ) { TypeElement result = elementUtil.getTypeElement(qualifiedName); entityCache.put(entityName, result); return result; } - StandardLocation location = StandardLocation.SOURCE_OUTPUT; + final StandardLocation location = StandardLocation.SOURCE_OUTPUT; try (Reader reader = filer.getResource(location, ENTITY_INDEX, entityName) .openReader(true); BufferedReader buffered = new BufferedReader(reader) ) { TypeElement result = elementUtil.getTypeElement(buffered.readLine()); entityCache.put(entityName, result); return result; } - catch (IOException e) { + catch (IOException ignore) { } try (Reader reader = filer.getResource(StandardLocation.CLASS_PATH, ENTITY_INDEX, entityName) .openReader(true); BufferedReader buffered = new BufferedReader(reader) ) { @@ -541,7 +523,7 @@ private TypeElement findEntityByUnqualifiedName(String entityName) { entityCache.put(entityName, result); return result; } - catch (IOException e) { + catch (IOException ignore) { } TypeElement symbol = @@ -572,7 +554,7 @@ public static TypeElement findEntityByUnqualifiedName(String entityName, ModuleE } } } - catch (Exception e) { + catch (Exception ignore) { } } } @@ -581,7 +563,7 @@ public static TypeElement findEntityByUnqualifiedName(String entityName, ModuleE private static boolean isMatchingEntity(Element symbol, String entityName) { if (symbol.getKind() == ElementKind.CLASS) { - TypeElement type = (TypeElement) symbol; + final TypeElement type = (TypeElement) symbol; return isEntity(type) && getEntityName(type).equals(entityName); } @@ -594,7 +576,7 @@ private static Element findProperty(TypeElement type, String propertyName, Acces //iterate up the superclass hierarchy 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 (isMatchingProperty(member, propertyName, accessType)) { return member; @@ -634,11 +616,11 @@ private static boolean hasAnnotation(Element member, String annotationName) { private static AnnotationMirror getAnnotation(Element member, String annotationName) { for (AnnotationMirror mirror : member.getAnnotationMirrors()) { - TypeElement annotationType = (TypeElement) mirror.getAnnotationType().asElement(); + final TypeElement annotationType = (TypeElement) mirror.getAnnotationType().asElement(); if ( annotationType.getSimpleName().contentEquals(annotationName) && annotationType.getNestingKind() == NestingKind.TOP_LEVEL ) { - PackageElement pack = (PackageElement) annotationType.getEnclosingElement(); - Name packageName = pack.getQualifiedName(); + final PackageElement pack = (PackageElement) annotationType.getEnclosingElement(); + final Name packageName = pack.getQualifiedName(); if (packageName.contentEquals(jakartaPersistence) || packageName.contentEquals(javaxPersistence)) { return mirror; @@ -705,7 +687,7 @@ private static boolean isEnumProperty(Element member) { @Override boolean isEnum(String className) { - TypeElement typeElement = elementUtil.getTypeElement( className ); + final TypeElement typeElement = elementUtil.getTypeElement( className ); return typeElement != null && typeElement.getKind() == ElementKind.ENUM; } @@ -727,36 +709,28 @@ Class javaConstantType(String className, String fieldName) { if ( typeElement == null ) { return null; } - final TypeMirror typeMirror = typeElement.getEnclosedElements() - .stream() - .filter( e -> fieldName.equals( e.getSimpleName().toString() ) ) - .filter( ProcessorSessionFactory::isStaticFinalField ) - .findFirst().map( Element::asType ) - .orElse( null ); + final TypeMirror typeMirror = + typeElement.getEnclosedElements() + .stream() + .filter( e -> fieldName.equals( e.getSimpleName().toString() ) ) + .filter( ProcessorSessionFactory::isStaticFinalField ) + .findFirst().map( Element::asType ) + .orElse( null ); if ( typeMirror == null ) { return null; } try { - switch ( typeMirror.getKind() ) { - case BYTE: - return byte.class; - case SHORT: - return short.class; - case INT: - return int.class; - case LONG: - return long.class; - case FLOAT: - return float.class; - case DOUBLE: - return double.class; - case BOOLEAN: - return boolean.class; - case CHAR: - return char.class; - default: - return Class.forName( typeMirror.toString() ); - } + return switch ( typeMirror.getKind() ) { + case BYTE -> byte.class; + case SHORT -> short.class; + case INT -> int.class; + case LONG -> long.class; + case FLOAT -> float.class; + case DOUBLE -> double.class; + case BOOLEAN -> boolean.class; + case CHAR -> char.class; + default -> Class.forName( typeMirror.toString() ); + }; } catch (ClassNotFoundException ignored) { return null; @@ -765,8 +739,8 @@ Class javaConstantType(String className, String fieldName) { private static boolean isStaticFinalField(Element e) { return e.getKind() == ElementKind.FIELD - && e.getModifiers().contains( Modifier.STATIC ) - && e.getModifiers().contains( Modifier.FINAL ); + && e.getModifiers().contains( Modifier.STATIC ) + && e.getModifiers().contains( Modifier.FINAL ); } private static boolean isEmbeddableType(TypeElement type) { @@ -799,12 +773,12 @@ private static boolean isToManyAssociation(Element member) { } private static AnnotationMirror toOneAnnotation(Element member) { - AnnotationMirror manyToOne = + final AnnotationMirror manyToOne = getAnnotation(member, "ManyToOne"); if (manyToOne!=null) { return manyToOne; } - AnnotationMirror oneToOne = + final AnnotationMirror oneToOne = getAnnotation(member, "OneToOne"); if (oneToOne!=null) { return oneToOne; @@ -813,12 +787,12 @@ private static AnnotationMirror toOneAnnotation(Element member) { } private static AnnotationMirror toManyAnnotation(Element member) { - AnnotationMirror manyToMany = + final AnnotationMirror manyToMany = getAnnotation(member, "ManyToMany"); if (manyToMany!=null) { return manyToMany; } - AnnotationMirror oneToMany = + final AnnotationMirror oneToMany = getAnnotation(member, "OneToMany"); if (oneToMany!=null) { return oneToMany; @@ -843,14 +817,14 @@ private static String simpleName(Element type) { } private static String qualifiedName(Element type) { - if ( type instanceof PackageElement ) { - return ((PackageElement) type).getQualifiedName().toString(); + if ( type instanceof PackageElement packageElement ) { + return packageElement.getQualifiedName().toString(); } - else if ( type instanceof TypeElement ) { - return ((TypeElement) type).getQualifiedName().toString(); + else if ( type instanceof TypeElement typeElement ) { + return typeElement.getQualifiedName().toString(); } else { - Element enclosingElement = type.getEnclosingElement(); + final Element enclosingElement = type.getEnclosingElement(); return enclosingElement != null ? qualifiedName(enclosingElement) + '.' + simpleName(type) : simpleName(type); @@ -858,25 +832,22 @@ else if ( type instanceof TypeElement ) { } private static AccessType getAccessType(TypeElement type, AccessType defaultAccessType) { - AnnotationMirror annotation = + final AnnotationMirror annotation = getAnnotation(type, "Access"); if (annotation==null) { return defaultAccessType; } else { - VariableElement member = (VariableElement) + final VariableElement member = (VariableElement) getAnnotationMember(annotation, "value"); if (member==null) { return defaultAccessType; //does not occur } - switch (member.getSimpleName().toString()) { - case "PROPERTY": - return AccessType.PROPERTY; - case "FIELD": - return AccessType.FIELD; - default: - throw new IllegalStateException(); - } + return switch (member.getSimpleName().toString()) { + case "PROPERTY" -> AccessType.PROPERTY; + case "FIELD" -> AccessType.FIELD; + default -> throw new IllegalStateException(); + }; } } @@ -884,14 +855,14 @@ static String getEntityName(TypeElement type) { if ( type == null ) { return null; } - AnnotationMirror entityAnnotation = + final AnnotationMirror entityAnnotation = getAnnotation(type, "Entity"); if (entityAnnotation==null) { //not an entity! return null; } else { - String name = (String) + final String name = (String) getAnnotationMember(entityAnnotation, "name"); //entity names are unqualified class names return name==null ? simpleName(type) : name; @@ -899,23 +870,23 @@ static String getEntityName(TypeElement type) { } private TypeMirror getCollectionElementType(Element property) { - DeclaredType declaredType = (DeclaredType) memberType(property); - List typeArguments = declaredType.getTypeArguments(); - TypeMirror elementType = typeArguments.get(typeArguments.size()-1); + final DeclaredType declaredType = (DeclaredType) memberType(property); + final List typeArguments = declaredType.getTypeArguments(); + final TypeMirror elementType = typeArguments.get(typeArguments.size()-1); return elementType==null ? elementUtil.getTypeElement(JAVA_OBJECT).asType() : elementType; } private static String getToOneTargetEntity(Element property) { - AnnotationMirror annotation = toOneAnnotation(property); - TypeMirror classType = (TypeMirror) + final AnnotationMirror annotation = toOneAnnotation(property); + final TypeMirror classType = (TypeMirror) getAnnotationMember(annotation, "targetEntity"); - TypeMirror targetType = + final TypeMirror targetType = classType == null || classType.getKind() == TypeKind.VOID ? memberType(property) : classType; - Element element = asElement(targetType); + final Element element = asElement(targetType); return element != null && element.getKind() == ElementKind.CLASS //entity names are unqualified class names ? getEntityName((TypeElement) element) @@ -923,14 +894,14 @@ private static String getToOneTargetEntity(Element property) { } private String getToManyTargetEntityName(Element property) { - AnnotationMirror annotation = toManyAnnotation(property); - TypeMirror classType = (TypeMirror) + final AnnotationMirror annotation = toManyAnnotation(property); + final TypeMirror classType = (TypeMirror) getAnnotationMember(annotation, "targetEntity"); - TypeMirror targetType = + final TypeMirror targetType = classType == null || classType.getKind() == TypeKind.VOID ? getCollectionElementType(property) : classType; - Element element = asElement(targetType); + final Element element = asElement(targetType); return element != null && element.getKind() == ElementKind.CLASS //entity names are unqualified class names ? getEntityName((TypeElement) element) @@ -938,9 +909,8 @@ private String getToManyTargetEntityName(Element property) { } private TypeMirror getElementCollectionElementType(Element property) { - AnnotationMirror annotation = getAnnotation(property, - "ElementCollection"); - TypeMirror classType = (TypeMirror) + final AnnotationMirror annotation = getAnnotation(property, "ElementCollection"); + final TypeMirror classType = (TypeMirror) getAnnotationMember(annotation, "getElementCollectionClass"); return classType == null || classType.getKind() == TypeKind.VOID @@ -967,7 +937,7 @@ boolean isClassDefined(String qualifiedName) { @Override boolean isFieldDefined(String qualifiedClassName, String fieldName) { - TypeElement type = findClassByQualifiedName(qualifiedClassName); + final TypeElement type = findClassByQualifiedName(qualifiedClassName); return type != null && type.getEnclosedElements().stream() .anyMatch(element -> element.getKind() == ElementKind.FIELD @@ -976,21 +946,21 @@ boolean isFieldDefined(String qualifiedClassName, String fieldName) { @Override boolean isConstructorDefined(String qualifiedClassName, List argumentTypes) { - TypeElement symbol = findClassByQualifiedName(qualifiedClassName); + final TypeElement symbol = findClassByQualifiedName(qualifiedClassName); if (symbol==null) { return false; } for (Element cons: symbol.getEnclosedElements()) { if ( cons.getKind() == ElementKind.CONSTRUCTOR ) { - ExecutableElement constructor = (ExecutableElement) cons; - List parameters = constructor.getParameters(); + final ExecutableElement constructor = (ExecutableElement) cons; + final List parameters = constructor.getParameters(); if (parameters.size()==argumentTypes.size()) { boolean argumentsCheckOut = true; for (int i=0; i primitive; + final Class primitive; try { primitive = toPrimitiveClass( type.getReturnedClass() ); } @@ -1003,10 +973,9 @@ boolean isConstructorDefined(String qualifiedClassName, List argumentTypes } } else { - TypeElement typeClass; - if (type instanceof EntityType) { - EntityType entityType = (EntityType) type; - String entityName = entityType.getAssociatedEntityName(); + final TypeElement typeClass; + if ( type instanceof EntityType entityType ) { + final String entityName = entityType.getAssociatedEntityName(); typeClass = findEntityClass(entityName); } //TODO: @@ -1014,7 +983,7 @@ boolean isConstructorDefined(String qualifiedClassName, List argumentTypes // typeClass = ((Component) ((CompositeCustomType) type).getUserType()).type; // } else if (type instanceof BasicType) { - String className; + final String className; //TODO: custom impl of getReturnedClassName() // for many more Hibernate types! try { @@ -1047,26 +1016,17 @@ else if (type instanceof BasicType) { } private static Class toPrimitiveClass(VariableElement param) { - switch (param.asType().getKind()) { - case BOOLEAN: - return boolean.class; - case CHAR: - return char.class; - case INT: - return int.class; - case SHORT: - return short.class; - case BYTE: - return byte.class; - case LONG: - return long.class; - case FLOAT: - return float.class; - case DOUBLE: - return double.class; - default: - return Object.class; - } + return switch ( param.asType().getKind() ) { + case BOOLEAN -> boolean.class; + case CHAR -> char.class; + case INT -> int.class; + case SHORT -> short.class; + case BYTE -> byte.class; + case LONG -> long.class; + case FLOAT -> float.class; + case DOUBLE -> double.class; + default -> Object.class; + }; } private TypeElement findClassByQualifiedName(String path) { @@ -1141,14 +1101,11 @@ public static Element asElement(TypeMirror type) { return null; } else { - switch (type.getKind()) { - case DECLARED: - return ((DeclaredType)type).asElement(); - case TYPEVAR: - return ((TypeVariable)type).asElement(); - default: - return null; - } + return switch ( type.getKind() ) { + case DECLARED -> ((DeclaredType) type).asElement(); + case TYPEVAR -> ((TypeVariable) type).asElement(); + default -> null; + }; } } }