HHH-16813 don't add @Generated annotations if it's going to cause a compilation failure
This commit is contained in:
parent
b1998782e9
commit
56cf0c414c
|
@ -98,6 +98,10 @@ public final class ClassWriter {
|
||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
try ( PrintWriter pw = new PrintWriter(sw) ) {
|
try ( PrintWriter pw = new PrintWriter(sw) ) {
|
||||||
|
|
||||||
|
if ( entity.getElement() instanceof TypeElement ) {
|
||||||
|
pw.println(writeStaticMetaModelAnnotation(entity));
|
||||||
|
}
|
||||||
|
|
||||||
if (context.addGeneratedAnnotation()) {
|
if (context.addGeneratedAnnotation()) {
|
||||||
pw.println(writeGeneratedAnnotation(entity, context));
|
pw.println(writeGeneratedAnnotation(entity, context));
|
||||||
}
|
}
|
||||||
|
@ -105,10 +109,6 @@ public final class ClassWriter {
|
||||||
pw.println(writeSuppressWarnings());
|
pw.println(writeSuppressWarnings());
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( entity.getElement() instanceof TypeElement ) {
|
|
||||||
pw.println(writeStaticMetaModelAnnotation(entity));
|
|
||||||
}
|
|
||||||
|
|
||||||
printClassDeclaration(entity, pw, context);
|
printClassDeclaration(entity, pw, context);
|
||||||
|
|
||||||
pw.println();
|
pw.println();
|
||||||
|
@ -203,18 +203,26 @@ public final class ClassWriter {
|
||||||
|
|
||||||
private static String writeGeneratedAnnotation(Metamodel entity, Context context) {
|
private static String writeGeneratedAnnotation(Metamodel entity, Context context) {
|
||||||
StringBuilder generatedAnnotation = new StringBuilder();
|
StringBuilder generatedAnnotation = new StringBuilder();
|
||||||
generatedAnnotation.append( "@" )
|
generatedAnnotation
|
||||||
|
.append( "@" )
|
||||||
.append( entity.importType( "jakarta.annotation.Generated" ) )
|
.append( entity.importType( "jakarta.annotation.Generated" ) )
|
||||||
.append( "(value = \"" )
|
.append( "(" );
|
||||||
.append( JPAMetaModelEntityProcessor.class.getName() );
|
|
||||||
if ( context.addGeneratedDate() ) {
|
if ( context.addGeneratedDate() ) {
|
||||||
generatedAnnotation.append( "\", date = \"" )
|
generatedAnnotation
|
||||||
|
.append( "value = " );
|
||||||
|
}
|
||||||
|
generatedAnnotation
|
||||||
|
.append( "\"" )
|
||||||
|
.append( JPAMetaModelEntityProcessor.class.getName() )
|
||||||
|
.append( "\"" );
|
||||||
|
if ( context.addGeneratedDate() ) {
|
||||||
|
generatedAnnotation
|
||||||
|
.append( ", date = " )
|
||||||
|
.append( "\"" )
|
||||||
.append( SIMPLE_DATE_FORMAT.get().format( new Date() ) )
|
.append( SIMPLE_DATE_FORMAT.get().format( new Date() ) )
|
||||||
.append( "\")" );
|
.append( "\"" );
|
||||||
}
|
|
||||||
else {
|
|
||||||
generatedAnnotation.append( "\")" );
|
|
||||||
}
|
}
|
||||||
|
generatedAnnotation.append( ")" );
|
||||||
return generatedAnnotation.toString();
|
return generatedAnnotation.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -90,6 +90,12 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
|
||||||
boolean addGeneratedAnnotation = Boolean.parseBoolean( tmp );
|
boolean addGeneratedAnnotation = Boolean.parseBoolean( tmp );
|
||||||
context.setAddGeneratedAnnotation( addGeneratedAnnotation );
|
context.setAddGeneratedAnnotation( addGeneratedAnnotation );
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
PackageElement jakartaAnnotationPackage =
|
||||||
|
context.getProcessingEnvironment().getElementUtils()
|
||||||
|
.getPackageElement( "jakarta.annotation" );
|
||||||
|
context.setAddGeneratedAnnotation( jakartaAnnotationPackage != null );
|
||||||
|
}
|
||||||
|
|
||||||
tmp = env.getOptions().get( JPAMetaModelEntityProcessor.ADD_GENERATION_DATE );
|
tmp = env.getOptions().get( JPAMetaModelEntityProcessor.ADD_GENERATION_DATE );
|
||||||
boolean addGenerationDate = Boolean.parseBoolean( tmp );
|
boolean addGenerationDate = Boolean.parseBoolean( tmp );
|
||||||
|
|
|
@ -28,7 +28,7 @@ public class GeneratedAnnotationTest extends CompilationTest {
|
||||||
|
|
||||||
// need to check the source because @Generated is not a runtime annotation
|
// need to check the source because @Generated is not a runtime annotation
|
||||||
String metaModelSource = getMetaModelSourceAsString( TestEntity.class );
|
String metaModelSource = getMetaModelSourceAsString( TestEntity.class );
|
||||||
String generatedString = "@Generated(value = \"org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor\")";
|
String generatedString = "@Generated(\"org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor\")";
|
||||||
assertTrue( "@Generated should be added to the metamodel.", metaModelSource.contains( generatedString ) );
|
assertTrue( "@Generated should be added to the metamodel.", metaModelSource.contains( generatedString ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue