METAGEN-73 Adding the fqcn of the processor as value of @Generated.
Also adding an option to add the generation date.
This commit is contained in:
parent
802cbcd546
commit
8177e5b840
|
@ -500,7 +500,7 @@ cq.where( cb.equal(itemNode.get(Item_.id), 5 ) ).distinct(true);
|
||||||
<row>
|
<row>
|
||||||
<entry>debug</entry>
|
<entry>debug</entry>
|
||||||
|
|
||||||
<entry>if set to <literal>true</literal> additional trace
|
<entry>If set to <literal>true</literal> additional trace
|
||||||
information will be outputted by the processor</entry>
|
information will be outputted by the processor</entry>
|
||||||
</row>
|
</row>
|
||||||
|
|
||||||
|
@ -542,7 +542,7 @@ cq.where( cb.equal(itemNode.get(Item_.id), 5 ) ).distinct(true);
|
||||||
<row>
|
<row>
|
||||||
<entry>fullyAnnotationConfigured</entry>
|
<entry>fullyAnnotationConfigured</entry>
|
||||||
|
|
||||||
<entry>if set to <literal>true</literal> the processor will
|
<entry>If set to <literal>true</literal> the processor will
|
||||||
ignore <filename>orm.xml</filename> and
|
ignore <filename>orm.xml</filename> and
|
||||||
<filename>persistence.xml.</filename></entry>
|
<filename>persistence.xml.</filename></entry>
|
||||||
</row>
|
</row>
|
||||||
|
@ -558,7 +558,7 @@ cq.where( cb.equal(itemNode.get(Item_.id), 5 ) ).distinct(true);
|
||||||
<row>
|
<row>
|
||||||
<entry>skipGeneratedAnnotation</entry>
|
<entry>skipGeneratedAnnotation</entry>
|
||||||
|
|
||||||
<entry>if set to <literal>true</literal> the processor will
|
<entry>If set to <literal>true</literal> the processor will
|
||||||
not the <classname>@Generated</classname> to the generated
|
not the <classname>@Generated</classname> to the generated
|
||||||
Java source file. The default for this option is
|
Java source file. The default for this option is
|
||||||
<constant>false</constant> and the
|
<constant>false</constant> and the
|
||||||
|
@ -568,10 +568,21 @@ cq.where( cb.equal(itemNode.get(Item_.id), 5 ) ).distinct(true);
|
||||||
<constant>true</constant>.</entry>
|
<constant>true</constant>.</entry>
|
||||||
</row>
|
</row>
|
||||||
|
|
||||||
|
<row>
|
||||||
|
<entry>addGenerationDate</entry>
|
||||||
|
|
||||||
|
<entry>If set to <constant>true</constant> the generation date
|
||||||
|
of the metamodel class will be inserted in the date parameter
|
||||||
|
of the <classname>@Generated</classname> annotation. The
|
||||||
|
default is <constant>false</constant>. This parameter is
|
||||||
|
ignored if <constant>skipGeneratedAnnotation</constant> is
|
||||||
|
specified.</entry>
|
||||||
|
</row>
|
||||||
|
|
||||||
<row>
|
<row>
|
||||||
<entry>addSuppressWarningsAnnotation</entry>
|
<entry>addSuppressWarningsAnnotation</entry>
|
||||||
|
|
||||||
<entry>if set to <literal>true</literal> the processor will
|
<entry>If set to <literal>true</literal> the processor will
|
||||||
add <emphasis>@SuppressWarnings("all")</emphasis> to the
|
add <emphasis>@SuppressWarnings("all")</emphasis> to the
|
||||||
generated Java source file. Per default this annotation is not
|
generated Java source file. Per default this annotation is not
|
||||||
generated. See also <ulink
|
generated. See also <ulink
|
||||||
|
|
|
@ -20,6 +20,8 @@ import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.annotation.Generated;
|
import javax.annotation.Generated;
|
||||||
import javax.annotation.processing.FilerException;
|
import javax.annotation.processing.FilerException;
|
||||||
|
@ -44,6 +46,12 @@ import org.hibernate.jpamodelgen.util.TypeUtils;
|
||||||
*/
|
*/
|
||||||
public final class ClassWriter {
|
public final class ClassWriter {
|
||||||
private static final String META_MODEL_CLASS_NAME_SUFFIX = "_";
|
private static final String META_MODEL_CLASS_NAME_SUFFIX = "_";
|
||||||
|
private static final ThreadLocal<SimpleDateFormat> SIMPLE_DATE_FORMAT = new ThreadLocal<SimpleDateFormat>() {
|
||||||
|
@Override
|
||||||
|
public SimpleDateFormat initialValue() {
|
||||||
|
return new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSSZ" );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
private ClassWriter() {
|
private ClassWriter() {
|
||||||
}
|
}
|
||||||
|
@ -51,7 +59,9 @@ public final class ClassWriter {
|
||||||
public static void writeFile(MetaEntity entity, Context context) {
|
public static void writeFile(MetaEntity entity, Context context) {
|
||||||
try {
|
try {
|
||||||
String metaModelPackage = entity.getPackageName();
|
String metaModelPackage = entity.getPackageName();
|
||||||
StringBuffer body = generateBody( entity, context );
|
// need to generate the body first, since this will also update the required imports which need to
|
||||||
|
// be written out first
|
||||||
|
String body = generateBody( entity, context ).toString();
|
||||||
|
|
||||||
FileObject fo = context.getProcessingEnvironment().getFiler().createSourceFile(
|
FileObject fo = context.getProcessingEnvironment().getFiler().createSourceFile(
|
||||||
getFullyQualifiedClassName( entity, metaModelPackage )
|
getFullyQualifiedClassName( entity, metaModelPackage )
|
||||||
|
@ -95,19 +105,25 @@ public final class ClassWriter {
|
||||||
PrintWriter pw = null;
|
PrintWriter pw = null;
|
||||||
try {
|
try {
|
||||||
pw = new PrintWriter( sw );
|
pw = new PrintWriter( sw );
|
||||||
|
|
||||||
if ( !context.skipGeneratedAnnotation() ) {
|
if ( !context.skipGeneratedAnnotation() ) {
|
||||||
pw.println( writeGeneratedAnnotation( entity ) );
|
pw.println( writeGeneratedAnnotation( entity, context ) );
|
||||||
}
|
}
|
||||||
if ( context.isAddSuppressWarningsAnnotation() ) {
|
if ( context.isAddSuppressWarningsAnnotation() ) {
|
||||||
pw.println( writeSuppressWarnings() );
|
pw.println( writeSuppressWarnings() );
|
||||||
}
|
}
|
||||||
|
|
||||||
pw.println( writeStaticMetaModelAnnotation( entity ) );
|
pw.println( writeStaticMetaModelAnnotation( entity ) );
|
||||||
|
|
||||||
printClassDeclaration( entity, pw, context );
|
printClassDeclaration( entity, pw, context );
|
||||||
|
|
||||||
pw.println();
|
pw.println();
|
||||||
|
|
||||||
List<MetaAttribute> members = entity.getMembers();
|
List<MetaAttribute> members = entity.getMembers();
|
||||||
for ( MetaAttribute metaMember : members ) {
|
for ( MetaAttribute metaMember : members ) {
|
||||||
pw.println( " " + metaMember.getDeclarationString() );
|
pw.println( " " + metaMember.getDeclarationString() );
|
||||||
}
|
}
|
||||||
|
|
||||||
pw.println();
|
pw.println();
|
||||||
pw.println( "}" );
|
pw.println( "}" );
|
||||||
return sw.getBuffer();
|
return sw.getBuffer();
|
||||||
|
@ -186,8 +202,21 @@ public final class ClassWriter {
|
||||||
return fullyQualifiedClassName;
|
return fullyQualifiedClassName;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String writeGeneratedAnnotation(MetaEntity entity) {
|
private static String writeGeneratedAnnotation(MetaEntity entity, Context context) {
|
||||||
return "@" + entity.importType( Generated.class.getName() ) + "(\"JPA MetaModel for " + entity.getQualifiedName() + "\")";
|
StringBuilder generatedAnnotation = new StringBuilder();
|
||||||
|
generatedAnnotation.append( "@" )
|
||||||
|
.append( entity.importType( Generated.class.getName() ) )
|
||||||
|
.append( "(value = \"" )
|
||||||
|
.append( JPAMetaModelEntityProcessor.class.getName() );
|
||||||
|
if ( context.addGeneratedDate() ) {
|
||||||
|
generatedAnnotation.append( "\", date = \"" )
|
||||||
|
.append( SIMPLE_DATE_FORMAT.get().format( new Date() ) )
|
||||||
|
.append( "\")" );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
generatedAnnotation.append( "\")" );
|
||||||
|
}
|
||||||
|
return generatedAnnotation.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String writeSuppressWarnings() {
|
private static String writeSuppressWarnings() {
|
||||||
|
|
|
@ -62,6 +62,7 @@ public final class Context {
|
||||||
|
|
||||||
private boolean isPersistenceUnitCompletelyXmlConfigured;
|
private boolean isPersistenceUnitCompletelyXmlConfigured;
|
||||||
private boolean skipGeneratedAnnotation;
|
private boolean skipGeneratedAnnotation;
|
||||||
|
private boolean addGenerationDate;
|
||||||
private boolean addSuppressWarningsAnnotation;
|
private boolean addSuppressWarningsAnnotation;
|
||||||
private AccessType persistenceUnitDefaultAccessType;
|
private AccessType persistenceUnitDefaultAccessType;
|
||||||
|
|
||||||
|
@ -109,6 +110,14 @@ public final class Context {
|
||||||
this.skipGeneratedAnnotation = skipGeneratedAnnotation;
|
this.skipGeneratedAnnotation = skipGeneratedAnnotation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean addGeneratedDate() {
|
||||||
|
return addGenerationDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAddGenerationDate(boolean addGenerationDate) {
|
||||||
|
this.addGenerationDate = addGenerationDate;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isAddSuppressWarningsAnnotation() {
|
public boolean isAddSuppressWarningsAnnotation() {
|
||||||
return addSuppressWarningsAnnotation;
|
return addSuppressWarningsAnnotation;
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,6 +74,7 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
|
||||||
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 SKIP_GENERATED_ANNOTATION = "skipGeneratedAnnotation";
|
public static final String SKIP_GENERATED_ANNOTATION = "skipGeneratedAnnotation";
|
||||||
|
public static final String ADD_GENERATION_DATE = "addGenerationDate";
|
||||||
/**
|
/**
|
||||||
* @deprecated since 1.2
|
* @deprecated since 1.2
|
||||||
*/
|
*/
|
||||||
|
@ -96,6 +97,10 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
|
||||||
boolean addGeneratedAnnotation = Boolean.parseBoolean( tmp );
|
boolean addGeneratedAnnotation = Boolean.parseBoolean( tmp );
|
||||||
context.setSkipGeneratedAnnotation( addGeneratedAnnotation );
|
context.setSkipGeneratedAnnotation( addGeneratedAnnotation );
|
||||||
|
|
||||||
|
tmp = env.getOptions().get( JPAMetaModelEntityProcessor.ADD_GENERATION_DATE );
|
||||||
|
boolean addGenerationDate = Boolean.parseBoolean( tmp );
|
||||||
|
context.setAddGenerationDate( addGenerationDate );
|
||||||
|
|
||||||
tmp = env.getOptions().get( JPAMetaModelEntityProcessor.ADD_SUPPRESS_WARNINGS_ANNOTATION );
|
tmp = env.getOptions().get( JPAMetaModelEntityProcessor.ADD_SUPPRESS_WARNINGS_ANNOTATION );
|
||||||
boolean addSuppressWarningsAnnotation = Boolean.parseBoolean( tmp );
|
boolean addSuppressWarningsAnnotation = Boolean.parseBoolean( tmp );
|
||||||
context.setAddSuppressWarningsAnnotation( addSuppressWarningsAnnotation );
|
context.setAddSuppressWarningsAnnotation( addSuppressWarningsAnnotation );
|
||||||
|
|
|
@ -37,7 +37,8 @@ public class GeneratedAnnotationTest extends CompilationTest {
|
||||||
|
|
||||||
// need to check the source because @Generated is not a runtime annotation
|
// need to check the source because @Generated is not a runtime annotation
|
||||||
String metaModelSource = getMetaModelSourceAsString( TestEntity.class );
|
String metaModelSource = getMetaModelSourceAsString( TestEntity.class );
|
||||||
assertTrue( metaModelSource.contains( "@Generated" ), "@Generated should be added to the metamodel." );
|
String generatedString = "@Generated(value = \"org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor\")";
|
||||||
|
assertTrue( metaModelSource.contains( generatedString ), "@Generated should be added to the metamodel." );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -0,0 +1,64 @@
|
||||||
|
/*
|
||||||
|
* 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 org.hibernate.jpamodelgen.test.util.TestForIssue;
|
||||||
|
|
||||||
|
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor;
|
||||||
|
import static org.hibernate.jpamodelgen.test.util.TestUtil.dumpMetaModelSourceFor;
|
||||||
|
import static org.hibernate.jpamodelgen.test.util.TestUtil.getMetaModelSourceAsString;
|
||||||
|
import static org.testng.Assert.assertTrue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Hardy Ferentschik
|
||||||
|
*/
|
||||||
|
public class GenerationDateTest extends CompilationTest {
|
||||||
|
@Test
|
||||||
|
@TestForIssue(jiraKey = "METAGEN-73")
|
||||||
|
public void testGeneratedAnnotationGenerated() {
|
||||||
|
assertMetamodelClassGeneratedFor( TestEntity.class );
|
||||||
|
|
||||||
|
// need to check the source because @Generated is not a runtime annotation
|
||||||
|
String metaModelSource = getMetaModelSourceAsString( TestEntity.class );
|
||||||
|
|
||||||
|
dumpMetaModelSourceFor( TestEntity.class );
|
||||||
|
String generatedString = "@Generated(value = \"org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor\", date = \"";
|
||||||
|
|
||||||
|
assertTrue( metaModelSource.contains( generatedString ), "@Generated should also contain the date parameter." );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Map<String, String> getProcessorOptions() {
|
||||||
|
Map<String, String> properties = new HashMap<String, String>();
|
||||||
|
properties.put( JPAMetaModelEntityProcessor.ADD_GENERATION_DATE, "true" );
|
||||||
|
return properties;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getPackageNameOfCurrentTest() {
|
||||||
|
return GenerationDateTest.class.getPackage().getName();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue