HHH-17882 accept a list of warnings to suppress in addSuppressWarningsAnnotation

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-03-29 10:57:43 +01:00 committed by Christian Beikov
parent 26beb3db64
commit 16ebcc427b
4 changed files with 31 additions and 10 deletions

View File

@ -120,5 +120,5 @@ part of the `javac` execution using standard link:{ann-proc-options}[-A] options
`-AlazyXmlParsing=[true|false]`:: Controls whether the processor should attempt to determine whether any `orm.xml` files have changed.
`-AaddGeneratedAnnotation=[true|false]`:: Controls whether the processor should add `@jakarta.annotation.Generated` to the generated classes.
`-addGenerationDate=[true|false]`:: Controls whether the processor should add `@jakarta.annotation.Generated#date`.
`-addSuppressWarningsAnnotation=[true|false]`:: Controls whether the processor should add `@SuppressWarnings({"deprecation","rawtypes"})` to the generated classes.
`-addSuppressWarningsAnnotation=[warning[,warning]*|true]`:: A comma-separated list of warnings to suppress, or simply `true` if `@SuppressWarnings({"deprecation","rawtypes"})` should be added to the generated classes.

View File

@ -92,7 +92,7 @@ public final class ClassWriter {
pw.println( writeGeneratedAnnotation( entity, context ) );
}
if ( context.addSuppressWarningsAnnotation() ) {
pw.println( writeSuppressWarnings() );
pw.println( writeSuppressWarnings(context) );
}
entity.inheritedAnnotations().forEach(pw::println);
@ -200,8 +200,14 @@ public final class ClassWriter {
return generatedAnnotation.toString();
}
private static String writeSuppressWarnings() {
return "@SuppressWarnings({\"deprecation\", \"rawtypes\"})";
private static String writeSuppressWarnings(Context context) {
final StringBuilder annotation = new StringBuilder("@SuppressWarnings({");
final String[] warnings = context.getSuppressedWarnings();
for (int i = 0; i < warnings.length; i++) {
if ( i>0 ) annotation.append(", ");
annotation.append('"').append(warnings[i]).append('"');
}
return annotation.append("})").toString();
}
private static String writeScopeAnnotation(Metamodel entity) {

View File

@ -83,7 +83,7 @@ public final class Context {
private boolean addNonnullAnnotation = false;
private boolean addGeneratedAnnotation = true;
private boolean addGenerationDate;
private boolean addSuppressWarningsAnnotation;
private String[] suppressedWarnings;
private boolean addTransactionScopedAnnotation;
private AccessType persistenceUnitDefaultAccessType;
private boolean generateJakartaDataStaticMetamodel;
@ -185,11 +185,15 @@ public final class Context {
}
public boolean addSuppressWarningsAnnotation() {
return addSuppressWarningsAnnotation;
return suppressedWarnings != null;
}
public void setAddSuppressWarningsAnnotation(boolean addSuppressWarningsAnnotation) {
this.addSuppressWarningsAnnotation = addSuppressWarningsAnnotation;
public String[] getSuppressedWarnings() {
return suppressedWarnings;
}
public void setSuppressedWarnings(String[] suppressedWarnings) {
this.suppressedWarnings = suppressedWarnings;
}
public boolean addTransactionScopedAnnotation() {

View File

@ -124,7 +124,9 @@ public class HibernateProcessor extends AbstractProcessor {
public static final String ADD_GENERATION_DATE = "addGenerationDate";
/**
* Controls whether {@code @SuppressWarnings({"deprecation","rawtypes"})} should be added to the generated classes
* A comma-separated list of warnings to suppress, or simply {@code true}
* if {@code @SuppressWarnings({"deprecation","rawtypes"})} should be
* added to the generated classes.
*/
public static final String ADD_SUPPRESS_WARNINGS_ANNOTATION = "addSuppressWarningsAnnotation";
@ -213,7 +215,16 @@ public class HibernateProcessor extends AbstractProcessor {
context.setAddGenerationDate( parseBoolean( options.get( ADD_GENERATION_DATE ) ) );
context.setAddSuppressWarningsAnnotation( parseBoolean( options.get( ADD_SUPPRESS_WARNINGS_ANNOTATION ) ) );
String suppressedWarnings = options.get( ADD_SUPPRESS_WARNINGS_ANNOTATION );
if ( suppressedWarnings != null ) {
if ( parseBoolean(suppressedWarnings) ) {
// legacy behavior from HHH-12068
context.setSuppressedWarnings(new String[] {"deprecation", "rawtypes"});
}
else {
context.setSuppressedWarnings( suppressedWarnings.replace(" ","").split(",") );
}
}
return parseBoolean( options.get( FULLY_ANNOTATION_CONFIGURED_OPTION ) );
}