HHH-12990 Auto-detect the fqcn of the Generated annotation in jpamodelgen

It's "javax.annotation.Generated" in Java 8 and below, but
"javax.annotation.processing.Generated" in Java 9 and above.
This commit is contained in:
Yoann Rodière 2018-10-17 08:47:54 +02:00 committed by Guillaume Smet
parent 5fd97472b9
commit e436b316da
2 changed files with 16 additions and 1 deletions

View File

@ -198,7 +198,7 @@ public final class ClassWriter {
private static String writeGeneratedAnnotation(MetaEntity entity, Context context) {
StringBuilder generatedAnnotation = new StringBuilder();
generatedAnnotation.append( "@" )
.append( entity.importType( "javax.annotation.Generated" ) )
.append( entity.importType( context.getGeneratedAnnotation().getQualifiedName().toString() ) )
.append( "(value = \"" )
.append( JPAMetaModelEntityProcessor.class.getName() );
if ( context.addGeneratedDate() ) {

View File

@ -50,6 +50,7 @@ public final class Context {
private final boolean lazyXmlParsing;
private final String persistenceXmlLocation;
private final List<String> ormXmlFiles;
private final TypeElement generatedAnnotation;
/**
* Whether all mapping files are xml-mapping-metadata-complete. In this case no annotation processing will take
@ -94,6 +95,16 @@ public final class Context {
lazyXmlParsing = Boolean.parseBoolean( pe.getOptions().get( JPAMetaModelEntityProcessor.LAZY_XML_PARSING ) );
logDebug = Boolean.parseBoolean( pe.getOptions().get( JPAMetaModelEntityProcessor.DEBUG_OPTION ) );
TypeElement java8AndBelowGeneratedAnnotation =
pe.getElementUtils().getTypeElement( "javax.annotation.Generated" );
if ( java8AndBelowGeneratedAnnotation != null ) {
generatedAnnotation = java8AndBelowGeneratedAnnotation;
}
else {
// Using the new name for this annotation in Java 9 and above
generatedAnnotation = pe.getElementUtils().getTypeElement( "javax.annotation.processing.Generated" );
}
}
public ProcessingEnvironment getProcessingEnvironment() {
@ -104,6 +115,10 @@ public final class Context {
return addGeneratedAnnotation;
}
public TypeElement getGeneratedAnnotation() {
return generatedAnnotation;
}
public void setAddGeneratedAnnotation(boolean addGeneratedAnnotation) {
this.addGeneratedAnnotation = addGeneratedAnnotation;
}