Skip to content

Commit 3cfec2f

Browse files
committed
HHH-17882 accept a list of warnings to suppress in addSuppressWarningsAnnotation
Signed-off-by: Gavin King <[email protected]>
1 parent b88d496 commit 3cfec2f

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

documentation/src/main/asciidoc/userguide/chapters/tooling/modelgen.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,5 @@ part of the `javac` execution using standard link:{ann-proc-options}[-A] options
120120
`-AlazyXmlParsing=[true|false]`:: Controls whether the processor should attempt to determine whether any `orm.xml` files have changed.
121121
`-AaddGeneratedAnnotation=[true|false]`:: Controls whether the processor should add `@jakarta.annotation.Generated` to the generated classes.
122122
`-addGenerationDate=[true|false]`:: Controls whether the processor should add `@jakarta.annotation.Generated#date`.
123-
`-addSuppressWarningsAnnotation=[true|false]`:: Controls whether the processor should add `@SuppressWarnings({"deprecation","rawtypes"})` to the generated classes.
123+
`-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.
124124

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
9292
pw.println( writeGeneratedAnnotation( entity, context ) );
9393
}
9494
if ( context.addSuppressWarningsAnnotation() ) {
95-
pw.println( writeSuppressWarnings() );
95+
pw.println( writeSuppressWarnings(context) );
9696
}
9797
entity.inheritedAnnotations().forEach(pw::println);
9898

@@ -200,8 +200,14 @@ private static String writeGeneratedAnnotation(Metamodel entity, Context context
200200
return generatedAnnotation.toString();
201201
}
202202

203-
private static String writeSuppressWarnings() {
204-
return "@SuppressWarnings({\"deprecation\", \"rawtypes\"})";
203+
private static String writeSuppressWarnings(Context context) {
204+
final StringBuilder annotation = new StringBuilder("@SuppressWarnings({");
205+
final String[] warnings = context.getSuppressedWarnings();
206+
for (int i = 0; i < warnings.length; i++) {
207+
if ( i>0 ) annotation.append(", ");
208+
annotation.append('"').append(warnings[i]).append('"');
209+
}
210+
return annotation.append("})").toString();
205211
}
206212

207213
private static String writeScopeAnnotation(Metamodel entity) {

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public final class Context {
8383
private boolean addNonnullAnnotation = false;
8484
private boolean addGeneratedAnnotation = true;
8585
private boolean addGenerationDate;
86-
private boolean addSuppressWarningsAnnotation;
86+
private String[] suppressedWarnings;
8787
private boolean addTransactionScopedAnnotation;
8888
private AccessType persistenceUnitDefaultAccessType;
8989
private boolean generateJakartaDataStaticMetamodel;
@@ -185,11 +185,15 @@ public void setAddGenerationDate(boolean addGenerationDate) {
185185
}
186186

187187
public boolean addSuppressWarningsAnnotation() {
188-
return addSuppressWarningsAnnotation;
188+
return suppressedWarnings != null;
189189
}
190190

191-
public void setAddSuppressWarningsAnnotation(boolean addSuppressWarningsAnnotation) {
192-
this.addSuppressWarningsAnnotation = addSuppressWarningsAnnotation;
191+
public String[] getSuppressedWarnings() {
192+
return suppressedWarnings;
193+
}
194+
195+
public void setSuppressedWarnings(String[] suppressedWarnings) {
196+
this.suppressedWarnings = suppressedWarnings;
193197
}
194198

195199
public boolean addTransactionScopedAnnotation() {

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ public class HibernateProcessor extends AbstractProcessor {
124124
public static final String ADD_GENERATION_DATE = "addGenerationDate";
125125

126126
/**
127-
* Controls whether {@code @SuppressWarnings({"deprecation","rawtypes"})} should be added to the generated classes
127+
* A comma-separated list of warnings to suppress, or simply {@code true}
128+
* if {@code @SuppressWarnings({"deprecation","rawtypes"})} should be
129+
* added to the generated classes.
128130
*/
129131
public static final String ADD_SUPPRESS_WARNINGS_ANNOTATION = "addSuppressWarningsAnnotation";
130132

@@ -213,7 +215,16 @@ private boolean handleSettings(ProcessingEnvironment environment) {
213215

214216
context.setAddGenerationDate( parseBoolean( options.get( ADD_GENERATION_DATE ) ) );
215217

216-
context.setAddSuppressWarningsAnnotation( parseBoolean( options.get( ADD_SUPPRESS_WARNINGS_ANNOTATION ) ) );
218+
String suppressedWarnings = options.get( ADD_SUPPRESS_WARNINGS_ANNOTATION );
219+
if ( suppressedWarnings != null ) {
220+
if ( parseBoolean(suppressedWarnings) ) {
221+
// legacy behavior from HHH-12068
222+
context.setSuppressedWarnings(new String[] {"deprecation", "rawtypes"});
223+
}
224+
else {
225+
context.setSuppressedWarnings( suppressedWarnings.replace(" ","").split(",") );
226+
}
227+
}
217228

218229
return parseBoolean( options.get( FULLY_ANNOTATION_CONFIGURED_OPTION ) );
219230
}

0 commit comments

Comments
 (0)