Skip to content

[6.6 Backport] HHH-19106 deconstruct inherited annotations before printing them #9710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@

import java.util.List;

import static jakarta.transaction.Transactional.TxType.REQUIRES_NEW;

@Repository
public interface Bookshop extends CrudRepository<Book,String> {
@Find
@Transactional
@Transactional(REQUIRES_NEW)
List<Book> byPublisher(String publisher_name);

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

@Query("select isbn where title like ?1 order by isbn")
String[] ssns(@NotBlank String title);
String[] ssns(@NotBlank String title);

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

@Query("select count(this)")
long countAll();
long countAll();

@Query("where isbn in :isbns and type = Book")
List<Book> books(List<String> isbns);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
import org.hibernate.processor.model.Metamodel;

import javax.annotation.processing.FilerException;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.tools.Diagnostic;
import javax.tools.FileObject;
import java.io.IOException;
Expand Down Expand Up @@ -94,7 +97,11 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
if ( context.addSuppressWarningsAnnotation() ) {
pw.println( writeSuppressWarnings(context) );
}
entity.inheritedAnnotations().forEach(pw::println);
entity.inheritedAnnotations()
.forEach( annotation -> {
printAnnotation( annotation, pw );
pw.print('\n');
} );

printClassDeclaration( entity, pw );

Expand All @@ -116,9 +123,10 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
pw.println('\t' + line);
if ( line.trim().startsWith("@Override") ) {
metaMember.inheritedAnnotations()
.forEach(x -> {
.forEach(annotation -> {
pw.print('\t');
pw.println(x);
printAnnotation( annotation, pw );
pw.print('\n');
});
}
});
Expand All @@ -131,6 +139,62 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
}
}

private static void printAnnotation(AnnotationMirror annotation, PrintWriter pw) {
pw.print('@');
final TypeElement type = (TypeElement) annotation.getAnnotationType().asElement();
pw.print( type.getQualifiedName().toString() );
var elementValues = annotation.getElementValues();
if (!elementValues.isEmpty()) {
pw.print('(');
boolean first = true;
for (var entry : elementValues.entrySet()) {
if (first) {
first = false;
}
else {
pw.print(',');
}
pw.print( entry.getKey().getSimpleName() );
pw.print( '=' );
printAnnotationValue( pw, entry.getValue() );
}
pw.print(')');
}
}

private static void printAnnotationValue(PrintWriter pw, AnnotationValue value) {
final Object argument = value.getValue();
if (argument instanceof VariableElement) {
VariableElement variable = (VariableElement) argument;
pw.print( variable.getEnclosingElement() );
pw.print('.');
pw.print( variable.getSimpleName().toString() );
}
else if (argument instanceof AnnotationMirror) {
AnnotationMirror childAnnotation = (AnnotationMirror) argument;
printAnnotation( childAnnotation, pw );
}
else if (argument instanceof List) {
final List<? extends AnnotationValue> list =
(List<? extends AnnotationValue>) argument;
pw.print('{');
boolean first = true;
for (AnnotationValue listedValue : list) {
if (first) {
first = false;
}
else {
pw.print(',');
}
printAnnotationValue( pw, listedValue );
}
pw.print('}');
}
else {
pw.print( argument );
}
}

private static void printClassDeclaration(Metamodel entity, PrintWriter pw) {
pw.print( "public " );
if ( !entity.isImplementation() && !entity.isJakartaDataStyle() ) {
Expand Down
Loading