From 31b5af94c12cb61d43a3c19acd23ca0ef6d37996 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Fri, 29 Mar 2024 11:02:18 +0100 Subject: [PATCH] HHH-17882 test for list of warnings in addSuppressWarningsAnnotation Signed-off-by: Gavin King --- .../org/hibernate/processor/ClassWriter.java | 4 ++- ...plicitWarningsAnnotationGeneratedTest.java | 36 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/supresswarnings/SuppressExplicitWarningsAnnotationGeneratedTest.java diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/ClassWriter.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/ClassWriter.java index 6458bd1f32..5d98b67fa7 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/ClassWriter.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/ClassWriter.java @@ -204,7 +204,9 @@ public final class ClassWriter { final StringBuilder annotation = new StringBuilder("@SuppressWarnings({"); final String[] warnings = context.getSuppressedWarnings(); for (int i = 0; i < warnings.length; i++) { - if ( i>0 ) annotation.append(", "); + if ( i>0 ) { + annotation.append(", "); + } annotation.append('"').append(warnings[i]).append('"'); } return annotation.append("})").toString(); diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/supresswarnings/SuppressExplicitWarningsAnnotationGeneratedTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/supresswarnings/SuppressExplicitWarningsAnnotationGeneratedTest.java new file mode 100644 index 0000000000..aefd328423 --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/supresswarnings/SuppressExplicitWarningsAnnotationGeneratedTest.java @@ -0,0 +1,36 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.processor.test.supresswarnings; + +import org.hibernate.processor.HibernateProcessor; +import org.hibernate.processor.test.util.CompilationTest; +import org.hibernate.processor.test.util.WithClasses; +import org.hibernate.processor.test.util.WithProcessorOption; +import org.junit.Test; + +import static org.hibernate.processor.test.util.TestUtil.assertMetamodelClassGeneratedFor; +import static org.hibernate.processor.test.util.TestUtil.getMetaModelSourceAsString; +import static org.junit.Assert.assertTrue; + +/** + * @author Hardy Ferentschik + */ +public class SuppressExplicitWarningsAnnotationGeneratedTest extends CompilationTest { + @Test + @WithClasses(TestEntity.class) + @WithProcessorOption(key = HibernateProcessor.ADD_SUPPRESS_WARNINGS_ANNOTATION, value = "foo, bar") + public void testSuppressedWarningsAnnotationGenerated() { + assertMetamodelClassGeneratedFor( TestEntity.class ); + + // need to check the source because @SuppressWarnings is not a runtime annotation + String metaModelSource = getMetaModelSourceAsString( TestEntity.class ); + assertTrue( + "@SuppressWarnings should be added to the metamodel.", + metaModelSource.contains( "@SuppressWarnings({\"foo\", \"bar\"})" ) + ); + } +}