Skip to content

Commit 1eea2ea

Browse files
committed
HHH-19629 Adjust how Hibernate Processor constructs the type name as String
1 parent f3c6c19 commit 1eea2ea

File tree

4 files changed

+73
-8
lines changed

4 files changed

+73
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.processor.test.data.constraint;
6+
7+
import org.hibernate.processor.test.util.CompilationTest;
8+
import org.hibernate.processor.test.util.WithClasses;
9+
import org.junit.jupiter.api.Test;
10+
11+
import static org.hibernate.processor.test.util.TestUtil.assertMetamodelClassGeneratedFor;
12+
import static org.hibernate.processor.test.util.TestUtil.getMetaModelSourceAsString;
13+
14+
@CompilationTest
15+
class DataTest {
16+
@Test
17+
@WithClasses({MyEntity.class, MyConstrainedRepository.class})
18+
void test() {
19+
System.out.println( getMetaModelSourceAsString( MyEntity.class ) );
20+
System.out.println( getMetaModelSourceAsString( MyConstrainedRepository.class ) );
21+
assertMetamodelClassGeneratedFor( MyEntity.class );
22+
// assertMetamodelClassGeneratedFor( MyConstrainedRepository.class );
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.processor.test.data.constraint;
6+
7+
import jakarta.data.repository.CrudRepository;
8+
import jakarta.data.repository.Find;
9+
import jakarta.data.repository.Repository;
10+
import jakarta.validation.constraints.NotNull;
11+
import jakarta.validation.constraints.Size;
12+
13+
@Repository
14+
public interface MyConstrainedRepository extends CrudRepository<MyEntity, Long> {
15+
16+
@Find
17+
MyEntity findByName(@NotNull @Size(min = 5) String name);
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.processor.test.data.constraint;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.Id;
9+
10+
@Entity
11+
public class MyEntity {
12+
13+
@Id
14+
private Long id;
15+
private String name;
16+
}

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import java.util.Set;
6161
import java.util.StringTokenizer;
6262
import java.util.regex.Pattern;
63+
import java.util.stream.Collectors;
6364
import java.util.stream.Stream;
6465

6566
import jakarta.persistence.AccessType;
@@ -3388,15 +3389,21 @@ private List<String> parameterTypes(ExecutableElement method) {
33883389
* Workaround for a bug in Java 20/21. Should not be necessary!
33893390
*/
33903391
private String typeAsString(TypeMirror type) {
3391-
String result = type.toString();
3392-
for ( AnnotationMirror annotation : type.getAnnotationMirrors() ) {
3393-
final String annotationString = annotation.toString();
3394-
result = result
3395-
// if it has a space after it, we need to remove that too
3396-
.replace(annotationString + ' ', "")
3397-
// just in case it did not have a space after it
3398-
.replace(annotationString, "");
3392+
String result;
3393+
if ( type instanceof DeclaredType dt && dt.asElement() instanceof TypeElement te ) {
3394+
// get the "fqcn" without any type arguments
3395+
result = te.getQualifiedName().toString();
3396+
// add the < ? ,? ....> as necessary:
3397+
if ( !dt.getTypeArguments().isEmpty() ) {
3398+
result += dt.getTypeArguments().stream()
3399+
.map( this::typeAsString )
3400+
.collect( Collectors.joining( ",", "<", ">" ) );
3401+
}
33993402
}
3403+
else {
3404+
result = type.toString();
3405+
}
3406+
34003407
for ( AnnotationMirror annotation : type.getAnnotationMirrors() ) {
34013408
result = annotation.toString() + ' ' + result;
34023409
}

0 commit comments

Comments
 (0)