Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
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;
Expand All @@ -23,11 +22,10 @@
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;

/**
Expand Down Expand Up @@ -185,20 +183,10 @@ else if ( modifiers.contains( Modifier.PROTECTED ) ) {
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
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;
Expand Down Expand Up @@ -175,6 +176,9 @@ public AnnotationMetaEntity(
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,8 +30,6 @@
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;
Expand All @@ -39,6 +38,7 @@
import java.util.function.Function;

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;
Expand Down Expand Up @@ -671,6 +671,16 @@ public static boolean isMemberType(Element element) {
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;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.processor.test.classnamecollision;

import org.hibernate.processor.test.util.CompilationTest;
import org.hibernate.processor.test.util.WithClasses;
import org.junit.Test;

import static org.hibernate.processor.test.util.TestUtil.assertMetamodelClassGeneratedFor;
import static org.hibernate.processor.test.util.TestUtil.getMetaModelSourceAsString;
import static org.hibernate.processor.test.util.TestUtil.getMetamodelClassFor;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class ClassNameCollisionTest extends CompilationTest {

@Test
@WithClasses({
Something.class,
org.hibernate.processor.test.classnamecollision.somewhere.Something.class
})
public void testAmbiguousSimpleName() {
System.out.println( getMetaModelSourceAsString( Something.class ) );
assertMetamodelClassGeneratedFor( Something.class );
System.out.println( getMetaModelSourceAsString( org.hibernate.processor.test.classnamecollision.somewhere.Something.class ) );
assertMetamodelClassGeneratedFor( org.hibernate.processor.test.classnamecollision.somewhere.Something.class );
assertEquals(
getMetamodelClassFor( org.hibernate.processor.test.classnamecollision.somewhere.Something.class ).getName(),
getMetamodelClassFor( Something.class ).getSuperclass()
.getName() );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.processor.test.classnamecollision;

import jakarta.persistence.Entity;

@Entity
public class Something extends org.hibernate.processor.test.classnamecollision.somewhere.Something {
String alphaValue;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.processor.test.classnamecollision.somewhere;

import jakarta.persistence.MappedSuperclass;

@MappedSuperclass
abstract public class Something {
String name;
}
Loading