Skip to content

Commit 29f74d4

Browse files
committed
HHH-18693 Fixing to work with Jakarta Data annotations; properly naming metadata classes
1 parent 9f2798c commit 29f74d4

File tree

11 files changed

+84
-97
lines changed

11 files changed

+84
-97
lines changed

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

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
*/
55
package org.hibernate.processor;
66

7-
import jakarta.annotation.Nullable;
8-
import org.hibernate.processor.annotation.AnnotationMetaEntity;
97
import org.hibernate.processor.annotation.InnerClassMetaAttribute;
108
import org.hibernate.processor.model.MetaAttribute;
119
import org.hibernate.processor.model.Metamodel;
10+
import org.hibernate.processor.util.StringUtil;
1211

1312
import javax.annotation.processing.FilerException;
13+
import javax.lang.model.element.Element;
1414
import javax.lang.model.element.ElementKind;
1515
import javax.lang.model.element.Modifier;
16+
import javax.lang.model.element.PackageElement;
1617
import javax.lang.model.element.TypeElement;
1718
import javax.tools.Diagnostic;
1819
import javax.tools.FileObject;
@@ -27,7 +28,6 @@
2728
import java.util.Set;
2829
import java.util.stream.Collectors;
2930

30-
3131
/**
3232
* Helper class to write the actual metamodel class using the {@link javax.annotation.processing.Filer} API.
3333
*
@@ -40,7 +40,6 @@ private ClassWriter() {
4040
}
4141

4242
public static void writeFile(Metamodel entity, Context context) {
43-
if (entity.hasParent()) throw new IllegalStateException();
4443
try {
4544
String metaModelPackage = entity.getPackageName();
4645
// need to generate the body first, since this will also update
@@ -105,7 +104,7 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
105104
}
106105
entity.inheritedAnnotations().forEach(pw::println);
107106

108-
printClassDeclaration( entity, pw, context );
107+
printClassDeclaration( entity, pw );
109108

110109
pw.println();
111110

@@ -115,6 +114,7 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
115114
generateBody( innerClass.getMetaEntity(), context )
116115
.toString().lines()
117116
.forEach(line -> pw.println('\t' + line));
117+
context.markGenerated( innerClass.getMetaEntity() );
118118
}
119119
}
120120

@@ -149,12 +149,13 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
149149
}
150150
}
151151

152-
private static void printClassDeclaration(Metamodel entity, PrintWriter pw, Context context) {
152+
private static void printClassDeclaration(Metamodel entity, PrintWriter pw) {
153153
if ( entity.hasParent() ) {
154154
final Set<Modifier> modifiers = entity.getElement().getModifiers();
155-
if (modifiers.contains( Modifier.PUBLIC )){
155+
if ( modifiers.contains( Modifier.PUBLIC ) ) {
156156
pw.print( "public " );
157-
}else if (modifiers.contains( Modifier.PROTECTED )) {
157+
}
158+
else if ( modifiers.contains( Modifier.PROTECTED ) ) {
158159
pw.print( "protected " );
159160
}
160161
pw.print( "static " );
@@ -168,70 +169,49 @@ private static void printClassDeclaration(Metamodel entity, PrintWriter pw, Cont
168169
pw.print( entity.isJakartaDataStyle() ? "interface " : "class " );
169170
pw.print( getGeneratedClassName(entity) );
170171

171-
String superClassName = entity.getSupertypeName();
172-
if ( superClassName != null ) {
173-
Metamodel superClassEntity = context.getMetaEntity( superClassName );
174-
if (superClassEntity == null) {
175-
superClassEntity = context.getMetaEmbeddable( superClassName );
176-
}
177-
final String superclassName = getGeneratedSuperclassName(
178-
superClassName, superClassEntity, entity.isJakartaDataStyle() );
179-
if (entity.getSimpleName().equals( "OffsetDateTimeTest" )) {
180-
System.err.printf( "Extends (?) %s - superClassName is not null%n", superClassName );
181-
}
182-
pw.print( " extends " + entity.importType(superclassName) );
172+
final Element superTypeElement = entity.getSuperTypeElement();
173+
if ( superTypeElement != null ) {
174+
pw.print( " extends " +
175+
entity.importType(getGeneratedSuperclassName( superTypeElement, entity.isJakartaDataStyle() )) );
183176
}
184177
if ( entity.isImplementation() ) {
185178
pw.print( entity.getElement().getKind() == ElementKind.CLASS ? " extends " : " implements " );
186179
pw.print( entity.getSimpleName() );
187-
if (entity.getSimpleName().equals( "OffsetDateTimeTest" )) {
188-
System.err.printf( "Extends (?) %s -- implementation (?!?)%n", entity.getSimpleName() );
189-
}
190180
}
191181

192182
pw.println( " {" );
193183
}
194184

195185
private static String getFullyQualifiedClassName(Metamodel entity) {
196186
final String metaModelPackage = entity.getPackageName();
197-
String fullyQualifiedClassName = "";
198-
if ( !metaModelPackage.isEmpty() ) {
199-
fullyQualifiedClassName = fullyQualifiedClassName + metaModelPackage + ".";
200-
}
187+
final String packageNamePrefix = !metaModelPackage.isEmpty() ? metaModelPackage + "." : "";
201188
final String className;
202189
if ( entity.getElement().getKind() == ElementKind.PACKAGE ) {
203190
className = getGeneratedClassName( entity );
204191
}
205192
else {
206193
className = Arrays.stream(
207-
entity.getQualifiedName().substring( fullyQualifiedClassName.length() ).split( "\\." ) )
208-
.map( AnnotationMetaEntity::removeDollar )
194+
entity.getQualifiedName().substring( packageNamePrefix.length() ).split( "\\." ) )
195+
.map( StringUtil::removeDollar )
209196
.map( part -> entity.isJakartaDataStyle() ? '_' + part : part + '_' )
210197
.collect( Collectors.joining( "." ) );
211198
}
212-
return fullyQualifiedClassName + className;
199+
return packageNamePrefix + className;
213200
}
214201

215202
private static String getGeneratedClassName(Metamodel entity) {
216203
final String className = entity.getSimpleName();
217-
return entity.isJakartaDataStyle() ? '_' + className : className + '_'; // TODO : check inner class!!!
204+
return entity.isJakartaDataStyle() ? '_' + className : className + '_';
218205
}
219206

220-
private static String getGeneratedSuperclassName(String superClassName, @Nullable Metamodel superClassEntity, boolean jakartaDataStyle) {
221-
if ( jakartaDataStyle ) {
222-
int lastDot = superClassName.lastIndexOf('.');
223-
if ( lastDot<0 ) {
224-
return '_' + superClassName;
225-
}
226-
else {
227-
return superClassName.substring(0,lastDot+1)
228-
+ '_' + superClassName.substring(lastDot+1);
229-
}
230-
}
231-
else {
232-
return superClassEntity == null ? superClassName + '_'
233-
: getFullyQualifiedClassName( superClassEntity );
234-
}
207+
private static String getGeneratedSuperclassName(Element superClassElement, boolean jakartaDataStyle) {
208+
final TypeElement typeElement = (TypeElement) superClassElement;
209+
final String simpleName = typeElement.getSimpleName().toString();
210+
final Element enclosingElement = typeElement.getEnclosingElement();
211+
return (enclosingElement instanceof TypeElement
212+
? getGeneratedSuperclassName( enclosingElement, jakartaDataStyle )
213+
: ((PackageElement) enclosingElement).getQualifiedName().toString())
214+
+ "." + (jakartaDataStyle ? '_' + simpleName : simpleName + '_');
235215
}
236216

237217
private static String writeGeneratedAnnotation(Metamodel entity, Context context) {

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

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -414,14 +414,26 @@ else if ( element instanceof TypeElement typeElement ) {
414414
parentMeta = parentMetaEntity;
415415
}
416416
}
417-
if (element.getSimpleName().toString().equals( "OffsetDateTimeTest" )) {
418-
System.err.println("Create OffsetDateTimeTest as non-entity class#1");
419-
}
420417
final NonManagedMetamodel metaEntity =
421418
NonManagedMetamodel .create(
422419
typeElement, context,
423-
parentMeta );
420+
false, parentMeta );
424421
context.addMetaEntity( metaEntity.getQualifiedName(), metaEntity );
422+
if ( context.generateJakartaDataStaticMetamodel()) {
423+
AnnotationMetaEntity parentDataMeta = null;
424+
if ( parent instanceof TypeElement parentElement ) {
425+
final String key = parentElement.getQualifiedName().toString();
426+
if ( context.getDataMetaEntity( key ) instanceof AnnotationMetaEntity parentMetaEntity ) {
427+
parentDataMeta = parentMetaEntity;
428+
}
429+
}
430+
final NonManagedMetamodel dataMetaEntity =
431+
NonManagedMetamodel .create(
432+
typeElement, context,
433+
true, parentDataMeta );
434+
context.addDataMetaEntity( dataMetaEntity.getQualifiedName(), dataMetaEntity );
435+
}
436+
425437
}
426438
}
427439
}
@@ -469,17 +481,8 @@ private void createMetaModelClasses() {
469481
}
470482
}
471483

472-
for ( Metamodel entity : context.getMetaEntities() ) {
473-
if ( !context.isAlreadyGenerated(entity) && !entity.hasParent()) {
474-
context.logMessage( Diagnostic.Kind.OTHER,
475-
"Writing Jakarta Persistence metamodel for entity '" + entity + "'" );
476-
ClassWriter.writeFile( entity, context );
477-
context.markGenerated(entity);
478-
}
479-
}
480-
481484
for ( Metamodel entity : context.getDataMetaEntities() ) {
482-
if ( !context.isAlreadyGenerated(entity) ) {
485+
if ( !context.isAlreadyGenerated(entity) && !entity.hasParent()) {
483486
context.logMessage( Diagnostic.Kind.OTHER,
484487
"Writing Jakarta Data metamodel for entity '" + entity + "'" );
485488
ClassWriter.writeFile( entity, context );
@@ -502,7 +505,7 @@ private void processEmbeddables(Collection<Metamodel> models) {
502505
final int toProcessCountBeforeLoop = models.size();
503506
for ( Metamodel metamodel : models ) {
504507
// see METAGEN-36
505-
if ( context.isAlreadyGenerated(metamodel) || metamodel.hasParent()) {
508+
if ( context.isAlreadyGenerated(metamodel) ) {
506509
processed.add( metamodel );
507510
}
508511
else if ( !modelGenerationNeedsToBeDeferred(models, metamodel ) ) {
@@ -626,9 +629,6 @@ private void handleRootElementAnnotationMirrors(final Element element, @Nullable
626629
}
627630
final boolean requiresLazyMemberInitialization
628631
= hasAnnotation( element, EMBEDDABLE, MAPPED_SUPERCLASS );
629-
if (element.getSimpleName().toString().equals( "OffsetDateTimeTest" )) {
630-
System.err.println("Create OffsetDateTimeTest in handleRootElementAnnotationMirrors#2");
631-
}
632632
final AnnotationMetaEntity metaEntity =
633633
AnnotationMetaEntity.create( typeElement, context,
634634
requiresLazyMemberInitialization,
@@ -646,13 +646,17 @@ && hasAnnotation( element, ENTITY, MAPPED_SUPERCLASS )
646646
// let a handwritten metamodel "override" the generated one
647647
// (this is used in the Jakarta Data TCK)
648648
&& !hasHandwrittenMetamodel(element) ) {
649-
if (element.getSimpleName().toString().equals( "OffsetDateTimeTest" )) {
650-
System.err.println("Create OffsetDateTimeTest in handleRootElementAnnotationMirrors#3");
649+
AnnotationMetaEntity parentDataEntity = null;
650+
if ( parent instanceof TypeElement parentTypeElement ) {
651+
if ( context.getDataMetaEntity( parentTypeElement.getQualifiedName().toString() )
652+
instanceof AnnotationMetaEntity pme ) {
653+
parentDataEntity = pme;
654+
}
651655
}
652656
final AnnotationMetaEntity dataMetaEntity =
653657
AnnotationMetaEntity.create( typeElement, context,
654658
requiresLazyMemberInitialization,
655-
true, true, parentMetaEntity );
659+
true, true, parentDataEntity );
656660
// final Metamodel alreadyExistingDataMetaEntity =
657661
// tryGettingExistingDataEntityFromContext( mirror, '_' + qualifiedName );
658662
// if ( alreadyExistingDataMetaEntity != null ) {

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

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,12 @@
8080
import static org.hibernate.processor.annotation.QueryMethod.isPageParam;
8181
import static org.hibernate.processor.util.Constants.*;
8282
import static org.hibernate.processor.util.NullnessUtil.castNonNull;
83+
import static org.hibernate.processor.util.StringUtil.removeDollar;
8384
import static org.hibernate.processor.util.TypeUtils.containsAnnotation;
8485
import static org.hibernate.processor.util.TypeUtils.determineAccessTypeForHierarchy;
8586
import static org.hibernate.processor.util.TypeUtils.determineAnnotationSpecifiedAccessType;
8687
import static org.hibernate.processor.util.TypeUtils.extendsClass;
87-
import static org.hibernate.processor.util.TypeUtils.findMappedSuperClass;
88+
import static org.hibernate.processor.util.TypeUtils.findMappedSuperElement;
8889
import static org.hibernate.processor.util.TypeUtils.getAnnotationMirror;
8990
import static org.hibernate.processor.util.TypeUtils.getAnnotationValue;
9091
import static org.hibernate.processor.util.TypeUtils.hasAnnotation;
@@ -232,18 +233,6 @@ private String getConstructorName() {
232233
return getSimpleName() + '_';
233234
}
234235

235-
/**
236-
* If this is an "intermediate" class providing {@code @Query}
237-
* annotations for the query by magical method name crap, then
238-
* by convention it will be named with a trailing $ sign. Strip
239-
* that off, so we get the standard constructor.
240-
*/
241-
public static String removeDollar(String simpleName) {
242-
return simpleName.endsWith("$")
243-
? simpleName.substring(0, simpleName.length()-1)
244-
: simpleName;
245-
}
246-
247236
@Override
248237
public final String getQualifiedName() {
249238
if ( qualifiedName == null ) {
@@ -253,8 +242,8 @@ public final String getQualifiedName() {
253242
}
254243

255244
@Override
256-
public @Nullable String getSupertypeName() {
257-
return repository ? null : findMappedSuperClass( this, context );
245+
public @Nullable Element getSuperTypeElement() {
246+
return repository ? null : findMappedSuperElement( this, context );
258247
}
259248

260249
@Override
@@ -278,7 +267,7 @@ public List<MetaAttribute> getMembers() {
278267
}
279268

280269
public void addInnerClass(AnnotationMetaEntity metaEntity) {
281-
putMember( "INNER_"+ metaEntity.getQualifiedName(), new InnerClassMetaAttribute( metaEntity ) );
270+
putMember( "INNER_" + metaEntity.getQualifiedName(), new InnerClassMetaAttribute( metaEntity ) );
282271
}
283272

284273
@Override

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.hibernate.processor.model.MetaAttribute;
1212

1313
import javax.lang.model.element.AnnotationMirror;
14+
import javax.lang.model.element.Element;
1415
import javax.lang.model.element.PackageElement;
1516
import javax.tools.Diagnostic;
1617
import java.util.ArrayList;
@@ -69,7 +70,7 @@ public final String getQualifiedName() {
6970
}
7071

7172
@Override
72-
public @Nullable String getSupertypeName() {
73+
public @Nullable Element getSuperTypeElement() {
7374
return null;
7475
}
7576

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public String getAttributeDeclarationString() {
6666
final StringBuilder declaration = new StringBuilder();
6767
declaration
6868
.append('\n');
69-
if ( annotationMetaEntity.getSupertypeName() == null ) {
69+
if ( annotationMetaEntity.getSuperTypeElement() == null ) {
7070
declaration
7171
.append("@")
7272
.append(annotationMetaEntity.importType("jakarta.persistence.PersistenceUnit"));

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@
1111

1212
public class NonManagedMetamodel extends AnnotationMetaEntity {
1313

14-
public NonManagedMetamodel(TypeElement element, Context context, @Nullable AnnotationMeta parent) {
15-
super( element, context, false, false, parent );
14+
public NonManagedMetamodel(TypeElement element, Context context, boolean jakartaDataStaticMetamodel, @Nullable AnnotationMeta parent) {
15+
super( element, context, false, jakartaDataStaticMetamodel, parent );
1616
}
1717

1818
public static NonManagedMetamodel create(
1919
TypeElement element, Context context,
20+
boolean jakartaDataStaticMetamodel,
2021
@Nullable AnnotationMetaEntity parent) {
2122
final NonManagedMetamodel metamodel =
22-
new NonManagedMetamodel( element, context, parent );
23+
new NonManagedMetamodel( element, context, jakartaDataStaticMetamodel, parent );
2324
if ( parent != null ) {
2425
metamodel.setParentElement( parent.getElement() );
2526
parent.addInnerClass( metamodel );

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public String getAttributeDeclarationString() {
6767
final StringBuilder declaration = new StringBuilder();
6868
declaration
6969
.append('\n');
70-
if ( annotationMetaEntity.getSupertypeName() == null ) {
70+
if ( annotationMetaEntity.getSuperTypeElement() == null ) {
7171
declaration
7272
.append("protected ");
7373
if ( !dataRepository ) {
@@ -96,7 +96,7 @@ public String getAttributeDeclarationString() {
9696
.append(" ")
9797
.append(sessionVariableName)
9898
.append(") {\n");
99-
if ( annotationMetaEntity.getSupertypeName() != null ) {
99+
if ( annotationMetaEntity.getSuperTypeElement() != null ) {
100100
declaration
101101
.append("\tsuper(")
102102
.append(sessionVariableName)
@@ -112,7 +112,7 @@ public String getAttributeDeclarationString() {
112112
}
113113
declaration
114114
.append("}");
115-
if ( annotationMetaEntity.getSupertypeName() == null ) {
115+
if ( annotationMetaEntity.getSuperTypeElement() == null ) {
116116
declaration
117117
.append("\n\n");
118118
if (addOverrideAnnotation) {

tooling/metamodel-generator/src/main/java/org/hibernate/processor/model/Metamodel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public interface Metamodel extends ImportContext {
1919

2020
String getQualifiedName();
2121

22-
@Nullable String getSupertypeName();
22+
@Nullable Element getSuperTypeElement();
2323

2424
String getPackageName();
2525

tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/StringUtil.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,16 @@ private static boolean startsWithSeveralUpperCaseLetters(String string) {
119119
&& isUpperCase( string.charAt( 0 ) )
120120
&& isUpperCase( string.charAt( 1 ) );
121121
}
122+
123+
/**
124+
* If this is an "intermediate" class providing {@code @Query}
125+
* annotations for the query by magical method name crap, then
126+
* by convention it will be named with a trailing $ sign. Strip
127+
* that off, so we get the standard constructor.
128+
*/
129+
public static String removeDollar(String simpleName) {
130+
return simpleName.endsWith("$")
131+
? simpleName.substring(0, simpleName.length()-1)
132+
: simpleName;
133+
}
122134
}

0 commit comments

Comments
 (0)