Skip to content

Commit 5d322c0

Browse files
gavinkingbeikov
authored andcommitted
HHH-19106 deconstruct inherited annotations before printing them
(cherry-picked from commit 225d9b3) Signed-off-by: Damiano Renfer <[email protected]>
1 parent 605f389 commit 5d322c0

File tree

2 files changed

+72
-6
lines changed
  • tooling/metamodel-generator/src

2 files changed

+72
-6
lines changed

tooling/metamodel-generator/src/jakartaData/java/org/hibernate/processor/test/data/eg/Bookshop.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@
1111

1212
import java.util.List;
1313

14+
import static jakarta.transaction.Transactional.TxType.REQUIRES_NEW;
15+
1416
@Repository
1517
public interface Bookshop extends CrudRepository<Book,String> {
1618
@Find
17-
@Transactional
19+
@Transactional(REQUIRES_NEW)
1820
List<Book> byPublisher(String publisher_name);
1921

2022
@Find
2123
List<Book> byTitle(@Nonnull String title);
2224

2325
@Query("select isbn where title like ?1 order by isbn")
24-
String[] ssns(@NotBlank String title);
26+
String[] ssns(@NotBlank String title);
2527

2628
@Query("select count(this) where title like ?1 order by isbn")
2729
long count1(@NotNull String title);
@@ -33,7 +35,7 @@ public interface Bookshop extends CrudRepository<Book,String> {
3335
int length(@Nonnull String title);
3436

3537
@Query("select count(this)")
36-
long countAll();
38+
long countAll();
3739

3840
@Query("where isbn in :isbns and type = Book")
3941
List<Book> books(List<String> isbns);

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

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
import org.hibernate.processor.model.Metamodel;
1111

1212
import javax.annotation.processing.FilerException;
13+
import javax.lang.model.element.AnnotationMirror;
14+
import javax.lang.model.element.AnnotationValue;
1315
import javax.lang.model.element.ElementKind;
1416
import javax.lang.model.element.TypeElement;
17+
import javax.lang.model.element.VariableElement;
1518
import javax.tools.Diagnostic;
1619
import javax.tools.FileObject;
1720
import java.io.IOException;
@@ -94,7 +97,11 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
9497
if ( context.addSuppressWarningsAnnotation() ) {
9598
pw.println( writeSuppressWarnings(context) );
9699
}
97-
entity.inheritedAnnotations().forEach(pw::println);
100+
entity.inheritedAnnotations()
101+
.forEach( annotation -> {
102+
printAnnotation( annotation, pw );
103+
pw.print('\n');
104+
} );
98105

99106
printClassDeclaration( entity, pw );
100107

@@ -116,9 +123,10 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
116123
pw.println('\t' + line);
117124
if ( line.trim().startsWith("@Override") ) {
118125
metaMember.inheritedAnnotations()
119-
.forEach(x -> {
126+
.forEach(annotation -> {
120127
pw.print('\t');
121-
pw.println(x);
128+
printAnnotation( annotation, pw );
129+
pw.print('\n');
122130
});
123131
}
124132
});
@@ -131,6 +139,62 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
131139
}
132140
}
133141

142+
private static void printAnnotation(AnnotationMirror annotation, PrintWriter pw) {
143+
pw.print('@');
144+
final TypeElement type = (TypeElement) annotation.getAnnotationType().asElement();
145+
pw.print( type.getQualifiedName().toString() );
146+
var elementValues = annotation.getElementValues();
147+
if (!elementValues.isEmpty()) {
148+
pw.print('(');
149+
boolean first = true;
150+
for (var entry : elementValues.entrySet()) {
151+
if (first) {
152+
first = false;
153+
}
154+
else {
155+
pw.print(',');
156+
}
157+
pw.print( entry.getKey().getSimpleName() );
158+
pw.print( '=' );
159+
printAnnotationValue( pw, entry.getValue() );
160+
}
161+
pw.print(')');
162+
}
163+
}
164+
165+
private static void printAnnotationValue(PrintWriter pw, AnnotationValue value) {
166+
final Object argument = value.getValue();
167+
if (argument instanceof VariableElement) {
168+
VariableElement variable = (VariableElement) argument;
169+
pw.print( variable.getEnclosingElement() );
170+
pw.print('.');
171+
pw.print( variable.getSimpleName().toString() );
172+
}
173+
else if (argument instanceof AnnotationMirror) {
174+
AnnotationMirror childAnnotation = (AnnotationMirror) argument;
175+
printAnnotation( childAnnotation, pw );
176+
}
177+
else if (argument instanceof List) {
178+
final List<? extends AnnotationValue> list =
179+
(List<? extends AnnotationValue>) argument;
180+
pw.print('{');
181+
boolean first = true;
182+
for (AnnotationValue listedValue : list) {
183+
if (first) {
184+
first = false;
185+
}
186+
else {
187+
pw.print(',');
188+
}
189+
printAnnotationValue( pw, listedValue );
190+
}
191+
pw.print('}');
192+
}
193+
else {
194+
pw.print( argument );
195+
}
196+
}
197+
134198
private static void printClassDeclaration(Metamodel entity, PrintWriter pw) {
135199
pw.print( "public " );
136200
if ( !entity.isImplementation() && !entity.isJakartaDataStyle() ) {

0 commit comments

Comments
 (0)