Author: hardy.ferentschik
Date: 2010-10-01 11:38:46 -0400 (Fri, 01 Oct 2010)
New Revision: 20764
Added:
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest2.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/TestEntity.java
Modified:
jpamodelgen/trunk/src/main/docbook/en-US/master.xml
jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/ClassWriter.java
jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/Context.java
jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/util/TestUtil.java
Log:
METAGEN-33 Added 'addGeneratedAnnotation' option
Modified: jpamodelgen/trunk/src/main/docbook/en-US/master.xml
===================================================================
--- jpamodelgen/trunk/src/main/docbook/en-US/master.xml 2010-10-01 14:18:53 UTC (rev
20763)
+++ jpamodelgen/trunk/src/main/docbook/en-US/master.xml 2010-10-01 15:38:46 UTC (rev
20764)
@@ -551,6 +551,17 @@
ignore <filename>orm.xml</filename> and
<filename>persistence.xml.</filename></entry>
</row>
+
+ <row>
+ <entry>addGeneratedAnnotation</entry>
+
+ <entry>if set to <literal>true</literal> the processor
will
+ add the <classname>@Generated</classname> 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.</entry>
+ </row>
</tbody>
</tgroup>
</table></para>
Modified: jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/ClassWriter.java
===================================================================
--- jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/ClassWriter.java 2010-10-01
14:18:53 UTC (rev 20763)
+++ jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/ClassWriter.java 2010-10-01
15:38:46 UTC (rev 20764)
@@ -24,6 +24,7 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
+import javax.annotation.Generated;
import javax.annotation.processing.FilerException;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
@@ -39,9 +40,9 @@
/**
* @author Emmanuel Bernard
*/
-public class ClassWriter {
+public final class ClassWriter {
- private ClassWriter(){
+ private ClassWriter() {
}
public static void writeFile(MetaEntity entity, Context context) {
@@ -91,8 +92,9 @@
PrintWriter pw = null;
try {
pw = new PrintWriter( sw );
- // we cannot use add @Generated into the metamodel class since this would not work in
a JDK 5 environment
- //pw.println( "@" + entity.importType( Generated.class.getName() ) +
"(\"JPA MetaModel for " + entity.getQualifiedName() + "\")"
);
+ if ( context.isAddGeneratedAnnotation() ) {
+ pw.println( "@" + entity.importType( Generated.class.getName() ) +
"(\"JPA MetaModel for " + entity.getQualifiedName() + "\")"
);
+ }
pw.println( "@" + entity.importType(
"javax.persistence.metamodel.StaticMetamodel" ) + "(" +
entity.getSimpleName() + ".class)" );
printClassDeclaration( entity, pw, context );
pw.println();
Modified: jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/Context.java
===================================================================
--- jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/Context.java 2010-10-01
14:18:53 UTC (rev 20763)
+++ jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/Context.java 2010-10-01
15:38:46 UTC (rev 20764)
@@ -60,9 +60,10 @@
private final boolean logDebug;
private final boolean lazyXmlParsing;
private final String persistenceXmlLocation;
+ private final List<String> ormXmlFiles;
- private final List<String> ormXmlFiles;
private boolean isPersistenceUnitCompletelyXmlConfigured;
+ private boolean addGeneratedAnnotation;
private AccessType persistenceUnitDefaultAccessType;
public Context(ProcessingEnvironment pe) {
@@ -101,6 +102,14 @@
return pe;
}
+ public boolean isAddGeneratedAnnotation() {
+ return addGeneratedAnnotation;
+ }
+
+ public void setAddGeneratedAnnotation(boolean addGeneratedAnnotation) {
+ this.addGeneratedAnnotation = addGeneratedAnnotation;
+ }
+
public Elements getElementUtils() {
return pe.getElementUtils();
}
Modified:
jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java
===================================================================
---
jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java 2010-10-01
14:18:53 UTC (rev 20763)
+++
jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java 2010-10-01
15:38:46 UTC (rev 20764)
@@ -70,7 +70,8 @@
JPAMetaModelEntityProcessor.PERSISTENCE_XML_OPTION,
JPAMetaModelEntityProcessor.ORM_XML_OPTION,
JPAMetaModelEntityProcessor.FULLY_ANNOTATION_CONFIGURED_OPTION,
- JPAMetaModelEntityProcessor.LAZY_XML_PARSING
+ JPAMetaModelEntityProcessor.LAZY_XML_PARSING,
+ JPAMetaModelEntityProcessor.ADD_GENERATED_ANNOTATION
})
public class JPAMetaModelEntityProcessor extends AbstractProcessor {
public static final String DEBUG_OPTION = "debug";
@@ -78,6 +79,7 @@
public static final String ORM_XML_OPTION = "ormXmlList";
public static final String FULLY_ANNOTATION_CONFIGURED_OPTION =
"fullyAnnotationConfigured";
public static final String LAZY_XML_PARSING = "lazyXmlParsing";
+ public static final String ADD_GENERATED_ANNOTATION =
"addGeneratedAnnotation";
private static final Boolean ALLOW_OTHER_PROCESSORS_TO_CLAIM_ANNOTATIONS =
Boolean.FALSE;
@@ -90,7 +92,11 @@
Diagnostic.Kind.NOTE, "Hibernate JPA 2 Static-Metamodel Generator " +
Version.getVersionString()
);
- String tmp = env.getOptions().get(
JPAMetaModelEntityProcessor.FULLY_ANNOTATION_CONFIGURED_OPTION );
+ String tmp = env.getOptions().get( JPAMetaModelEntityProcessor.ADD_GENERATED_ANNOTATION
);
+ boolean addGeneratedAnnotation = Boolean.parseBoolean( tmp );
+ context.setAddGeneratedAnnotation( addGeneratedAnnotation );
+
+ tmp = env.getOptions().get(
JPAMetaModelEntityProcessor.FULLY_ANNOTATION_CONFIGURED_OPTION );
boolean fullyAnnotationConfigured = Boolean.parseBoolean( tmp );
if ( !fullyAnnotationConfigured ) {
Copied:
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest.java
(from rev 20721,
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generics/GenericsTest.java)
===================================================================
---
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest.java
(rev 0)
+++
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest.java 2010-10-01
15:38:46 UTC (rev 20764)
@@ -0,0 +1,48 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// $Id$
+
+package org.hibernate.jpamodelgen.test.generatedannotation;
+
+import org.testng.annotations.Test;
+
+import org.hibernate.jpamodelgen.test.util.CompilationTest;
+
+import static
org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor;
+import static org.hibernate.jpamodelgen.test.util.TestUtil.getMetaModelSourceAsString;
+import static org.testng.Assert.assertFalse;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class GeneratedAnnotationTest extends CompilationTest {
+
+ @Test
+ 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." );
+ }
+
+ @Override
+ protected String getPackageNameOfTestSources() {
+ return GeneratedAnnotationTest.class.getPackage().getName();
+ }
+}
\ No newline at end of file
Property changes on:
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added:
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest2.java
===================================================================
---
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest2.java
(rev 0)
+++
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest2.java 2010-10-01
15:38:46 UTC (rev 20764)
@@ -0,0 +1,62 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// $Id: GenericsTest.java 20721 2010-09-27 12:40:10Z hardy.ferentschik $
+
+package org.hibernate.jpamodelgen.test.generatedannotation;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.testng.annotations.Test;
+
+import org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor;
+import org.hibernate.jpamodelgen.test.util.CompilationTest;
+
+import static
org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor;
+import static org.hibernate.jpamodelgen.test.util.TestUtil.getMetaModelSourceAsString;
+import static org.testng.Assert.assertTrue;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class GeneratedAnnotationTest2 extends CompilationTest {
+
+ @Test
+ 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." );
+ }
+
+ @Override
+ protected Map<String, String> getProcessorOptions() {
+ Map<String, String> properties = new HashMap<String, String>();
+ properties.put(
+ JPAMetaModelEntityProcessor.ADD_GENERATED_ANNOTATION,
+ "true"
+ );
+ return properties;
+ }
+
+ @Override
+ protected String getPackageNameOfTestSources() {
+ return GeneratedAnnotationTest2.class.getPackage().getName();
+ }
+}
\ No newline at end of file
Added:
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/TestEntity.java
===================================================================
---
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/TestEntity.java
(rev 0)
+++
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/TestEntity.java 2010-10-01
15:38:46 UTC (rev 20764)
@@ -0,0 +1,33 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// $Id:$
+package org.hibernate.jpamodelgen.test.generatedannotation;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+/**
+ * @author Hardy Ferentschik
+ */
+@Entity
+public class TestEntity {
+ @Id
+ private long id;
+}
+
+
Modified:
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/util/TestUtil.java
===================================================================
---
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/util/TestUtil.java 2010-10-01
14:18:53 UTC (rev 20763)
+++
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/util/TestUtil.java 2010-10-01
15:38:46 UTC (rev 20764)
@@ -19,8 +19,11 @@
package org.hibernate.jpamodelgen.test.util;
+import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
+import java.io.FileReader;
+import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
@@ -76,6 +79,18 @@
}
}
+ public static Class<?> getMetamodelClassFor(Class<?> entityClass) {
+ String entityModelClassName = entityClass.getName() + META_MODEL_CLASS_POSTFIX;
+
+ try {
+ return Class.forName( entityModelClassName );
+ }
+ catch ( ClassNotFoundException e ) {
+ fail( "Unable to load class " + entityModelClassName );
+ return null;
+ }
+ }
+
/**
* Asserts that a metamodel class for the specified class got generated.
*
@@ -92,16 +107,50 @@
}
}
- public static void assertNoSourceFileGeneratedFor(Class<?> clazz) {
- assertNotNull( clazz, "Class parameter cannot be null" );
+ public static File getMetaModelSourceFileFor(Class<?> clazz) {
String metaModelClassName = clazz.getName() + META_MODEL_CLASS_POSTFIX;
// generate the file name
String fileName = metaModelClassName.replace( PACKAGE_SEPARATOR, PATH_SEPARATOR );
fileName = fileName.concat( ".java" );
- File sourceFile = new File( outBaseDir + PATH_SEPARATOR + fileName );
- assertFalse( sourceFile.exists(), "There should be no source file: " +
fileName );
+ return new File( outBaseDir + PATH_SEPARATOR + fileName );
}
+ public static String getMetaModelSourceAsString(Class<?> clazz) {
+ File sourceFile = getMetaModelSourceFileFor( clazz );
+ StringBuilder contents = new StringBuilder();
+
+ try {
+ BufferedReader input = new BufferedReader( new FileReader( sourceFile ) );
+ try {
+ String line = null; //not declared within while loop
+ /*
+ * readLine is a bit quirky :
+ * it returns the content of a line MINUS the newline.
+ * it returns null only for the END of the stream.
+ * it returns an empty String if two newlines appear in a row.
+ */
+ while ( ( line = input.readLine() ) != null ) {
+ contents.append( line );
+ contents.append( System.getProperty( "line.separator" ) );
+ }
+ }
+ finally {
+ input.close();
+ }
+ }
+ catch ( IOException ex ) {
+ ex.printStackTrace();
+ }
+
+ return contents.toString();
+ }
+
+ public static void assertNoSourceFileGeneratedFor(Class<?> clazz) {
+ assertNotNull( clazz, "Class parameter cannot be null" );
+ File sourceFile = getMetaModelSourceFileFor( clazz );
+ assertFalse( sourceFile.exists(), "There should be no source file: " +
sourceFile.getName() );
+ }
+
public static void assertAbsenceOfFieldInMetamodelFor(Class<?> clazz, String
fieldName) {
assertAbsenceOfFieldInMetamodelFor( clazz, fieldName, "field should not be
persistent" );
}
@@ -165,15 +214,11 @@
}
public static Field getFieldFromMetamodelFor(Class<?> entityClass, String
fieldName) {
- String entityModelClassName = entityClass.getName() + META_MODEL_CLASS_POSTFIX;
- Field field = null;
+ Class<?> metaModelClass = getMetamodelClassFor( entityClass );
+ Field field;
try {
- Class<?> clazz = Class.forName( entityModelClassName );
- field = clazz.getField( fieldName );
+ field = metaModelClass.getField( fieldName );
}
- catch ( ClassNotFoundException e ) {
- fail( "Unable to load class " + entityModelClassName );
- }
catch ( NoSuchFieldException e ) {
field = null;
}