Skip to content

Commit 8e64575

Browse files
marko-bekhtayrodiere
authored andcommitted
HHH-19629 Adjust how Hibernate Processor constructs the type name as String
1 parent a485efa commit 8e64575

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+
}
Lines changed: 18 additions & 0 deletions
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;
@@ -3425,15 +3426,21 @@ private List<String> parameterTypes(ExecutableElement method) {
34253426
* Workaround for a bug in Java 20/21. Should not be necessary!
34263427
*/
34273428
private String typeAsString(TypeMirror type) {
3428-
String result = type.toString();
3429-
for ( AnnotationMirror annotation : type.getAnnotationMirrors() ) {
3430-
final String annotationString = annotation.toString();
3431-
result = result
3432-
// if it has a space after it, we need to remove that too
3433-
.replace(annotationString + ' ', "")
3434-
// just in case it did not have a space after it
3435-
.replace(annotationString, "");
3429+
String result;
3430+
if ( type instanceof DeclaredType dt && dt.asElement() instanceof TypeElement te ) {
3431+
// get the "fqcn" without any type arguments
3432+
result = te.getQualifiedName().toString();
3433+
// add the < ? ,? ....> as necessary:
3434+
if ( !dt.getTypeArguments().isEmpty() ) {
3435+
result += dt.getTypeArguments().stream()
3436+
.map( this::typeAsString )
3437+
.collect( Collectors.joining( ",", "<", ">" ) );
3438+
}
34363439
}
3440+
else {
3441+
result = type.toString();
3442+
}
3443+
34373444
for ( AnnotationMirror annotation : type.getAnnotationMirrors() ) {
34383445
result = annotation.toString() + ' ' + result;
34393446
}

0 commit comments

Comments
 (0)