HHH-18945 Import generated class full qualified name to prevent importing other classes with same simple name

This commit is contained in:
Čedomir Igaly 2024-12-16 23:23:25 +01:00 committed by Gavin King
parent 92e29b63db
commit c882aeb2b8
3 changed files with 21 additions and 19 deletions

View File

@ -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) {

View File

@ -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) {

View File

@ -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;