From 4c968d7f7ac8618c5df92815570b9dbacb3f41ad Mon Sep 17 00:00:00 2001 From: Hardy Ferentschik Date: Fri, 20 Jan 2012 12:16:36 +0100 Subject: [PATCH] METAGEN-79 Adding skipGeneratedAnnotation flag. Deprecating addGeneratedAnnotation. --- .../src/main/docbook/en-US/master.xml | 20 ++++++++++++++----- .../hibernate/jpamodelgen/ClassWriter.java | 2 +- .../org/hibernate/jpamodelgen/Context.java | 10 +++++----- .../JPAMetaModelEntityProcessor.java | 9 +++++++-- .../GeneratedAnnotationTest.java | 6 ++++-- ....java => SkipGeneratedAnnotationTest.java} | 15 +++++++------- 6 files changed, 39 insertions(+), 23 deletions(-) rename tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/{GeneratedAnnotationTest2.java => SkipGeneratedAnnotationTest.java} (79%) diff --git a/tooling/metamodel-generator/src/main/docbook/en-US/master.xml b/tooling/metamodel-generator/src/main/docbook/en-US/master.xml index 80d4570cf1..75ee04df5f 100644 --- a/tooling/metamodel-generator/src/main/docbook/en-US/master.xml +++ b/tooling/metamodel-generator/src/main/docbook/en-US/master.xml @@ -550,12 +550,22 @@ cq.where( cb.equal(itemNode.get(Item_.id), 5 ) ).distinct(true); addGeneratedAnnotation + this option is deprecated and has no effect. The + @Generation annotation is added per + default. + + + + skipGeneratedAnnotation + if set to true the processor will - add the @Generated to the generated - Java source file. Per default this annotation is not - generated, because the code would not compile under JDK 5. If - you are using a JDK 6 you can force the generation of this - annotation using this flag. + not the @Generated to the generated + Java source file. The default for this option is + false and the + @Generated annotation is added to the + source code. Adding this annotation using JDK 5will cause a + compilation error. In this case set the flag to + true. diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/ClassWriter.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/ClassWriter.java index 09e49ae7c3..95de3384fd 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/ClassWriter.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/ClassWriter.java @@ -95,7 +95,7 @@ public final class ClassWriter { PrintWriter pw = null; try { pw = new PrintWriter( sw ); - if ( context.isAddGeneratedAnnotation() ) { + if ( !context.skipGeneratedAnnotation() ) { pw.println( writeGeneratedAnnotation( entity ) ); } if ( context.isAddSuppressWarningsAnnotation() ) { diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/Context.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/Context.java index 25565717e7..a674956a7d 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/Context.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/Context.java @@ -61,7 +61,7 @@ public final class Context { private final List ormXmlFiles; private boolean isPersistenceUnitCompletelyXmlConfigured; - private boolean addGeneratedAnnotation; + private boolean skipGeneratedAnnotation; private boolean addSuppressWarningsAnnotation; private AccessType persistenceUnitDefaultAccessType; @@ -101,12 +101,12 @@ public final class Context { return pe; } - public boolean isAddGeneratedAnnotation() { - return addGeneratedAnnotation; + public boolean skipGeneratedAnnotation() { + return skipGeneratedAnnotation; } - public void setAddGeneratedAnnotation(boolean addGeneratedAnnotation) { - this.addGeneratedAnnotation = addGeneratedAnnotation; + public void setSkipGeneratedAnnotation(boolean skipGeneratedAnnotation) { + this.skipGeneratedAnnotation = skipGeneratedAnnotation; } public boolean isAddSuppressWarningsAnnotation() { diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java index cb1210d563..395c70a318 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java @@ -63,6 +63,7 @@ import org.hibernate.jpamodelgen.xml.XmlParser; JPAMetaModelEntityProcessor.ORM_XML_OPTION, JPAMetaModelEntityProcessor.FULLY_ANNOTATION_CONFIGURED_OPTION, JPAMetaModelEntityProcessor.LAZY_XML_PARSING, + JPAMetaModelEntityProcessor.SKIP_GENERATED_ANNOTATION, JPAMetaModelEntityProcessor.ADD_GENERATED_ANNOTATION, JPAMetaModelEntityProcessor.ADD_SUPPRESS_WARNINGS_ANNOTATION }) @@ -72,6 +73,10 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor { public static final String ORM_XML_OPTION = "ormXml"; public static final String FULLY_ANNOTATION_CONFIGURED_OPTION = "fullyAnnotationConfigured"; public static final String LAZY_XML_PARSING = "lazyXmlParsing"; + public static final String SKIP_GENERATED_ANNOTATION = "skipGeneratedAnnotation"; + /** + * @deprecated since 1.2 + */ public static final String ADD_GENERATED_ANNOTATION = "addGeneratedAnnotation"; public static final String ADD_SUPPRESS_WARNINGS_ANNOTATION = "addSuppressWarningsAnnotation"; @@ -87,9 +92,9 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor { Diagnostic.Kind.NOTE, "Hibernate JPA 2 Static-Metamodel Generator " + Version.getVersionString() ); - String tmp = env.getOptions().get( JPAMetaModelEntityProcessor.ADD_GENERATED_ANNOTATION ); + String tmp = env.getOptions().get( JPAMetaModelEntityProcessor.SKIP_GENERATED_ANNOTATION ); boolean addGeneratedAnnotation = Boolean.parseBoolean( tmp ); - context.setAddGeneratedAnnotation( addGeneratedAnnotation ); + context.setSkipGeneratedAnnotation( addGeneratedAnnotation ); tmp = env.getOptions().get( JPAMetaModelEntityProcessor.ADD_SUPPRESS_WARNINGS_ANNOTATION ); boolean addSuppressWarningsAnnotation = Boolean.parseBoolean( tmp ); diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest.java index 008be36a27..4262c50185 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest.java @@ -19,10 +19,11 @@ package org.hibernate.jpamodelgen.test.generatedannotation; import org.testng.annotations.Test; import org.hibernate.jpamodelgen.test.util.CompilationTest; +import org.hibernate.jpamodelgen.test.util.TestForIssue; import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor; import static org.hibernate.jpamodelgen.test.util.TestUtil.getMetaModelSourceAsString; -import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; /** * @author Hardy Ferentschik @@ -30,12 +31,13 @@ import static org.testng.Assert.assertFalse; public class GeneratedAnnotationTest extends CompilationTest { @Test + @TestForIssue(jiraKey = "METAGEN-79") public void testGeneratedAnnotationNotGenerated() { assertMetamodelClassGeneratedFor( TestEntity.class ); // need to check the source because @Generated is not a runtime annotation String metaModelSource = getMetaModelSourceAsString( TestEntity.class ); - assertFalse( metaModelSource.contains( "@Generated" ), "@Generated should not be added to the metamodel." ); + assertTrue( metaModelSource.contains( "@Generated" ), "@Generated should be added to the metamodel." ); } @Override diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest2.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/SkipGeneratedAnnotationTest.java similarity index 79% rename from tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest2.java rename to tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/SkipGeneratedAnnotationTest.java index ab9efb978d..60709952f1 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest2.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/SkipGeneratedAnnotationTest.java @@ -25,36 +25,35 @@ import org.testng.annotations.Test; import org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor; import org.hibernate.jpamodelgen.test.util.CompilationTest; +import org.hibernate.jpamodelgen.test.util.TestForIssue; import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor; import static org.hibernate.jpamodelgen.test.util.TestUtil.getMetaModelSourceAsString; -import static org.testng.Assert.assertTrue; +import static org.testng.Assert.assertFalse; /** * @author Hardy Ferentschik */ -public class GeneratedAnnotationTest2 extends CompilationTest { +public class SkipGeneratedAnnotationTest extends CompilationTest { @Test + @TestForIssue(jiraKey = "METAGEN-79") public void testGeneratedAnnotationGenerated() { assertMetamodelClassGeneratedFor( TestEntity.class ); // need to check the source because @Generated is not a runtime annotation String metaModelSource = getMetaModelSourceAsString( TestEntity.class ); - assertTrue( metaModelSource.contains( "@Generated" ), "@Generated should be added to the metamodel." ); + assertFalse( metaModelSource.contains( "@Generated" ), "@Generated should not be added to the metamodel." ); } @Override protected Map getProcessorOptions() { Map properties = new HashMap(); - properties.put( - JPAMetaModelEntityProcessor.ADD_GENERATED_ANNOTATION, - "true" - ); + properties.put( JPAMetaModelEntityProcessor.SKIP_GENERATED_ANNOTATION, "true" ); return properties; } @Override protected String getPackageNameOfCurrentTest() { - return GeneratedAnnotationTest2.class.getPackage().getName(); + return SkipGeneratedAnnotationTest.class.getPackage().getName(); } } \ No newline at end of file