From c882aeb2b8c04fd238b9b6c5354c3d6768fd7014 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=8Cedomir=20Igaly?= Date: Mon, 16 Dec 2024 23:23:25 +0100 Subject: [PATCH] HHH-18945 Import generated class full qualified name to prevent importing other classes with same simple name --- .../org/hibernate/processor/ClassWriter.java | 22 +++++-------------- .../annotation/AnnotationMetaEntity.java | 4 ++++ .../hibernate/processor/util/TypeUtils.java | 14 ++++++++++-- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/ClassWriter.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/ClassWriter.java index daeecb3e82..0c242c28f8 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/ClassWriter.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/ClassWriter.java @@ -7,7 +7,6 @@ package org.hibernate.processor; import org.hibernate.processor.annotation.InnerClassMetaAttribute; import org.hibernate.processor.model.MetaAttribute; import org.hibernate.processor.model.Metamodel; -import org.hibernate.processor.util.StringUtil; import javax.annotation.processing.FilerException; import javax.lang.model.element.Element; @@ -23,11 +22,10 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; -import java.util.Arrays; import java.util.List; import java.util.Set; -import java.util.stream.Collectors; +import static org.hibernate.processor.util.TypeUtils.getGeneratedClassFullyQualifiedName; import static org.hibernate.processor.util.TypeUtils.isMemberType; /** @@ -185,20 +183,10 @@ public final class ClassWriter { } private static String getFullyQualifiedClassName(Metamodel entity) { - final String metaModelPackage = entity.getPackageName(); - final String packageNamePrefix = !metaModelPackage.isEmpty() ? metaModelPackage + "." : ""; - final String className; - if ( entity.getElement().getKind() == ElementKind.PACKAGE ) { - className = getGeneratedClassName( entity ); - } - else { - className = Arrays.stream( - entity.getQualifiedName().substring( packageNamePrefix.length() ).split( "\\." ) ) - .map( StringUtil::removeDollar ) - .map( part -> entity.isJakartaDataStyle() ? '_' + part : part + '_' ) - .collect( Collectors.joining( "." ) ); - } - return packageNamePrefix + className; + return entity.getElement() instanceof PackageElement packageElement + ? packageElement.getQualifiedName().toString() + "." + getGeneratedClassName( entity ) + : getGeneratedClassFullyQualifiedName( + (TypeElement) entity.getElement(), entity.getPackageName(), entity.isJakartaDataStyle() ); } private static String getGeneratedClassName(Metamodel entity) { 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 0101e68089..162d6de043 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 @@ -88,6 +88,7 @@ import static org.hibernate.processor.util.TypeUtils.extendsClass; import static org.hibernate.processor.util.TypeUtils.findMappedSuperElement; import static org.hibernate.processor.util.TypeUtils.getAnnotationMirror; import static org.hibernate.processor.util.TypeUtils.getAnnotationValue; +import static org.hibernate.processor.util.TypeUtils.getGeneratedClassFullyQualifiedName; import static org.hibernate.processor.util.TypeUtils.hasAnnotation; import static org.hibernate.processor.util.TypeUtils.implementsInterface; import static org.hibernate.processor.util.TypeUtils.primitiveClassMatchesKind; @@ -175,6 +176,9 @@ public class AnnotationMetaEntity extends AnnotationMeta { this.quarkusInjection = context.isQuarkusInjection(); this.importContext = parent != null ? parent : new ImportContextImpl( getPackageName( context, element ) ); jakartaDataStaticModel = jakartaDataStaticMetamodel; + importContext.importType( + getGeneratedClassFullyQualifiedName( element, getPackageName( context, element ), + jakartaDataStaticModel ) ); } public static AnnotationMetaEntity create(TypeElement element, Context context) { 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 822c8d129f..183bf8fe2b 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 @@ -4,6 +4,7 @@ */ package org.hibernate.processor.util; +import jakarta.persistence.AccessType; import org.checkerframework.checker.nullness.qual.Nullable; import org.hibernate.processor.Context; import org.hibernate.processor.MetaModelGenerationException; @@ -29,8 +30,6 @@ import javax.lang.model.util.ElementFilter; import javax.lang.model.util.Elements; import javax.lang.model.util.SimpleTypeVisitor8; import javax.tools.Diagnostic; - -import jakarta.persistence.AccessType; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -38,6 +37,7 @@ import java.util.Map; import java.util.Set; import static java.beans.Introspector.decapitalize; +import static org.hibernate.internal.util.StringHelper.split; import static org.hibernate.processor.util.AccessTypeInformation.DEFAULT_ACCESS_TYPE; import static org.hibernate.processor.util.Constants.ACCESS; import static org.hibernate.processor.util.Constants.BASIC; @@ -665,6 +665,16 @@ public final class TypeUtils { return element.getEnclosingElement() instanceof TypeElement; } + public static String getGeneratedClassFullyQualifiedName(TypeElement element, String packageName, boolean jakartaDataStyle) { + final StringBuilder builder = new StringBuilder( !packageName.isEmpty() ? packageName + "." : "" ); + final int length = builder.length(); + for ( String s : split( ".", element.getQualifiedName().toString().substring( length ) ) ) { + String part = StringUtil.removeDollar( s ); + builder.append( jakartaDataStyle ? '_' + part : part + '_' ); + } + return builder.toString(); + } + static class EmbeddedAttributeVisitor extends SimpleTypeVisitor8<@Nullable TypeElement, Element> { private final Context context;