Skip to content

Commit 91eee1c

Browse files
marko-bekhtayrodiere
authored andcommitted
HHH-19629 Adjust how Hibernate Processor constructs the type name as String
1 parent bc2ea33 commit 91eee1c

File tree

4 files changed

+73
-8
lines changed

4 files changed

+73
-8
lines changed
Lines changed: 24 additions & 0 deletions
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+
}
Lines changed: 16 additions & 0 deletions
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
@@ -62,6 +62,7 @@
6262
import java.util.Set;
6363
import java.util.StringTokenizer;
6464
import java.util.regex.Pattern;
65+
import java.util.stream.Collectors;
6566
import java.util.stream.Stream;
6667

6768
import jakarta.persistence.AccessType;
@@ -3426,15 +3427,21 @@ private List<String> parameterTypes(ExecutableElement method) {
34263427
* Workaround for a bug in Java 20/21. Should not be necessary!
34273428
*/
34283429
private String typeAsString(TypeMirror type) {
3429-
String result = type.toString();
3430-
for ( AnnotationMirror annotation : type.getAnnotationMirrors() ) {
3431-
final String annotationString = annotation.toString();
3432-
result = result
3433-
// if it has a space after it, we need to remove that too
3434-
.replace(annotationString + ' ', "")
3435-
// just in case it did not have a space after it
3436-
.replace(annotationString, "");
3430+
String result;
3431+
if ( type instanceof DeclaredType dt && dt.asElement() instanceof TypeElement te ) {
3432+
// get the "fqcn" without any type arguments
3433+
result = te.getQualifiedName().toString();
3434+
// add the < ? ,? ....> as necessary:
3435+
if ( !dt.getTypeArguments().isEmpty() ) {
3436+
result += dt.getTypeArguments().stream()
3437+
.map( this::typeAsString )
3438+
.collect( Collectors.joining( ",", "<", ">" ) );
3439+
}
34373440
}
3441+
else {
3442+
result = type.toString();
3443+
}
3444+
34383445
for ( AnnotationMirror annotation : type.getAnnotationMirrors() ) {
34393446
result = annotation.toString() + ' ' + result;
34403447
}

0 commit comments

Comments
 (0)