METAGEN-33 Added 'addGeneratedAnnotation' option

This commit is contained in:
Hardy Ferentschik 2010-10-01 15:38:46 +00:00 committed by Strong Liu
parent 000d5533f4
commit e83b0ca9b2
8 changed files with 234 additions and 18 deletions

View File

@ -551,6 +551,17 @@ cq.where( cb.equal(itemNode.get(Item_.id), 5 ) ).distinct(true);
ignore <filename>orm.xml</filename> and ignore <filename>orm.xml</filename> and
<filename>persistence.xml.</filename></entry> <filename>persistence.xml.</filename></entry>
</row> </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> </tbody>
</tgroup> </tgroup>
</table></para> </table></para>

View File

@ -24,6 +24,7 @@ import java.io.OutputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.List; import java.util.List;
import javax.annotation.Generated;
import javax.annotation.processing.FilerException; import javax.annotation.processing.FilerException;
import javax.lang.model.element.Element; import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement; import javax.lang.model.element.TypeElement;
@ -39,9 +40,9 @@ import org.hibernate.jpamodelgen.model.MetaEntity;
/** /**
* @author Emmanuel Bernard * @author Emmanuel Bernard
*/ */
public class ClassWriter { public final class ClassWriter {
private ClassWriter(){ private ClassWriter() {
} }
public static void writeFile(MetaEntity entity, Context context) { public static void writeFile(MetaEntity entity, Context context) {
@ -91,8 +92,9 @@ public class ClassWriter {
PrintWriter pw = null; PrintWriter pw = null;
try { try {
pw = new PrintWriter( sw ); pw = new PrintWriter( sw );
// we cannot use add @Generated into the metamodel class since this would not work in a JDK 5 environment if ( context.isAddGeneratedAnnotation() ) {
//pw.println( "@" + entity.importType( Generated.class.getName() ) + "(\"JPA MetaModel for " + entity.getQualifiedName() + "\")" ); pw.println( "@" + entity.importType( Generated.class.getName() ) + "(\"JPA MetaModel for " + entity.getQualifiedName() + "\")" );
}
pw.println( "@" + entity.importType( "javax.persistence.metamodel.StaticMetamodel" ) + "(" + entity.getSimpleName() + ".class)" ); pw.println( "@" + entity.importType( "javax.persistence.metamodel.StaticMetamodel" ) + "(" + entity.getSimpleName() + ".class)" );
printClassDeclaration( entity, pw, context ); printClassDeclaration( entity, pw, context );
pw.println(); pw.println();

View File

@ -60,9 +60,10 @@ public class Context {
private final boolean logDebug; private final boolean logDebug;
private final boolean lazyXmlParsing; private final boolean lazyXmlParsing;
private final String persistenceXmlLocation; private final String persistenceXmlLocation;
private final List<String> ormXmlFiles; private final List<String> ormXmlFiles;
private boolean isPersistenceUnitCompletelyXmlConfigured; private boolean isPersistenceUnitCompletelyXmlConfigured;
private boolean addGeneratedAnnotation;
private AccessType persistenceUnitDefaultAccessType; private AccessType persistenceUnitDefaultAccessType;
public Context(ProcessingEnvironment pe) { public Context(ProcessingEnvironment pe) {
@ -101,6 +102,14 @@ public class Context {
return pe; return pe;
} }
public boolean isAddGeneratedAnnotation() {
return addGeneratedAnnotation;
}
public void setAddGeneratedAnnotation(boolean addGeneratedAnnotation) {
this.addGeneratedAnnotation = addGeneratedAnnotation;
}
public Elements getElementUtils() { public Elements getElementUtils() {
return pe.getElementUtils(); return pe.getElementUtils();
} }

View File

@ -70,7 +70,8 @@ import static javax.lang.model.SourceVersion.RELEASE_6;
JPAMetaModelEntityProcessor.PERSISTENCE_XML_OPTION, JPAMetaModelEntityProcessor.PERSISTENCE_XML_OPTION,
JPAMetaModelEntityProcessor.ORM_XML_OPTION, JPAMetaModelEntityProcessor.ORM_XML_OPTION,
JPAMetaModelEntityProcessor.FULLY_ANNOTATION_CONFIGURED_OPTION, JPAMetaModelEntityProcessor.FULLY_ANNOTATION_CONFIGURED_OPTION,
JPAMetaModelEntityProcessor.LAZY_XML_PARSING JPAMetaModelEntityProcessor.LAZY_XML_PARSING,
JPAMetaModelEntityProcessor.ADD_GENERATED_ANNOTATION
}) })
public class JPAMetaModelEntityProcessor extends AbstractProcessor { public class JPAMetaModelEntityProcessor extends AbstractProcessor {
public static final String DEBUG_OPTION = "debug"; public static final String DEBUG_OPTION = "debug";
@ -78,6 +79,7 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
public static final String ORM_XML_OPTION = "ormXmlList"; public static final String ORM_XML_OPTION = "ormXmlList";
public static final String FULLY_ANNOTATION_CONFIGURED_OPTION = "fullyAnnotationConfigured"; public static final String FULLY_ANNOTATION_CONFIGURED_OPTION = "fullyAnnotationConfigured";
public static final String LAZY_XML_PARSING = "lazyXmlParsing"; 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; private static final Boolean ALLOW_OTHER_PROCESSORS_TO_CLAIM_ANNOTATIONS = Boolean.FALSE;
@ -90,7 +92,11 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
Diagnostic.Kind.NOTE, "Hibernate JPA 2 Static-Metamodel Generator " + Version.getVersionString() 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 ); boolean fullyAnnotationConfigured = Boolean.parseBoolean( tmp );
if ( !fullyAnnotationConfigured ) { if ( !fullyAnnotationConfigured ) {

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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;
}

View File

@ -19,8 +19,11 @@
package org.hibernate.jpamodelgen.test.util; package org.hibernate.jpamodelgen.test.util;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileFilter; import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType; import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
@ -76,6 +79,18 @@ public class TestUtil {
} }
} }
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. * Asserts that a metamodel class for the specified class got generated.
* *
@ -92,14 +107,48 @@ public class TestUtil {
} }
} }
public static void assertNoSourceFileGeneratedFor(Class<?> clazz) { public static File getMetaModelSourceFileFor(Class<?> clazz) {
assertNotNull( clazz, "Class parameter cannot be null" );
String metaModelClassName = clazz.getName() + META_MODEL_CLASS_POSTFIX; String metaModelClassName = clazz.getName() + META_MODEL_CLASS_POSTFIX;
// generate the file name // generate the file name
String fileName = metaModelClassName.replace( PACKAGE_SEPARATOR, PATH_SEPARATOR ); String fileName = metaModelClassName.replace( PACKAGE_SEPARATOR, PATH_SEPARATOR );
fileName = fileName.concat( ".java" ); fileName = fileName.concat( ".java" );
File sourceFile = new File( outBaseDir + PATH_SEPARATOR + fileName ); return new File( outBaseDir + PATH_SEPARATOR + fileName );
assertFalse( sourceFile.exists(), "There should be no source file: " + 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) { public static void assertAbsenceOfFieldInMetamodelFor(Class<?> clazz, String fieldName) {
@ -165,14 +214,10 @@ public class TestUtil {
} }
public static Field getFieldFromMetamodelFor(Class<?> entityClass, String fieldName) { public static Field getFieldFromMetamodelFor(Class<?> entityClass, String fieldName) {
String entityModelClassName = entityClass.getName() + META_MODEL_CLASS_POSTFIX; Class<?> metaModelClass = getMetamodelClassFor( entityClass );
Field field = null; Field field;
try { try {
Class<?> clazz = Class.forName( entityModelClassName ); field = metaModelClass.getField( fieldName );
field = clazz.getField( fieldName );
}
catch ( ClassNotFoundException e ) {
fail( "Unable to load class " + entityModelClassName );
} }
catch ( NoSuchFieldException e ) { catch ( NoSuchFieldException e ) {
field = null; field = null;