[hibernate-commits] Hibernate SVN: r17945 - in jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen: annotation and 1 other directory.
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Fri Nov 6 11:02:26 EST 2009
Author: hardy.ferentschik
Date: 2009-11-06 11:02:26 -0500 (Fri, 06 Nov 2009)
New Revision: 17945
Modified:
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/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java
Log:
METAGEN-5
Added an additonal option which can be passed to the processor in order to print debug messages. Passing of the parameter is via -Adebug=true
Modified: jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/ClassWriter.java
===================================================================
--- jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/ClassWriter.java 2009-11-06 13:59:37 UTC (rev 17944)
+++ jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/ClassWriter.java 2009-11-06 16:02:26 UTC (rev 17945)
@@ -23,7 +23,6 @@
import java.io.StringWriter;
import java.util.List;
import javax.annotation.processing.FilerException;
-import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
@@ -37,13 +36,13 @@
*/
public class ClassWriter {
- public static void writeFile(MetaEntity entity, ProcessingEnvironment processingEnv, Context context) {
+ public static void writeFile(MetaEntity entity, Context context) {
try {
String metaModelPackage = entity.getPackageName();
StringBuffer body = generateBody( entity, context );
- FileObject fo = processingEnv.getFiler().createSourceFile(
+ FileObject fo = context.getProcessingEnvironment().getFiler().createSourceFile(
metaModelPackage + "." + entity.getSimpleName() + "_"
);
OutputStream os = fo.openOutputStream();
@@ -62,17 +61,14 @@
}
catch ( FilerException filerEx ) {
- processingEnv.getMessager().printMessage(
- Diagnostic.Kind.ERROR,
- "Problem with Processing Environment Filer: "
- + filerEx.getMessage()
+ context.logMessage(
+ Diagnostic.Kind.ERROR, "Problem with Processing Environment Filer: " + filerEx.getMessage()
);
}
catch ( IOException ioEx ) {
- processingEnv.getMessager().printMessage(
+ context.logMessage(
Diagnostic.Kind.ERROR,
- "Problem opening file to write MetaModel for " + entity.getSimpleName()
- + ioEx.getMessage()
+ "Problem opening file to write MetaModel for " + entity.getSimpleName() + ioEx.getMessage()
);
}
}
Modified: jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/Context.java
===================================================================
--- jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/Context.java 2009-11-06 13:59:37 UTC (rev 17944)
+++ jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/Context.java 2009-11-06 16:02:26 UTC (rev 17945)
@@ -17,13 +17,13 @@
*/
package org.hibernate.jpamodelgen;
+import java.util.HashMap;
+import java.util.HashSet;
import java.util.Map;
-import java.util.HashMap;
import java.util.Set;
-import java.util.HashSet;
+import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.TypeElement;
import javax.persistence.AccessType;
-import javax.annotation.processing.ProcessingEnvironment;
import javax.tools.Diagnostic;
import org.hibernate.jpamodelgen.annotation.AnnotationMetaEntity;
@@ -34,12 +34,16 @@
* @author Emmanuel Bernard
*/
public class Context {
+ private static final String DEBUG_PARAMETER = "debug";
+ private final Map<String, MetaEntity> metaEntitiesToProcess = new HashMap<String, MetaEntity>();
+ private final Map<String, MetaEntity> metaSuperclassAndEmbeddableToProcess = new HashMap<String, MetaEntity>();
+
+ private ProcessingEnvironment pe;
+ private boolean logDebug = false;
+
//used to cache access types
private Map<TypeElement, AccessTypeHolder> accessTypes = new HashMap<TypeElement, AccessTypeHolder>();
private Set<String> elementsAlreadyProcessed = new HashSet<String>();
- private ProcessingEnvironment pe;
- private final Map<String, MetaEntity> metaEntitiesToProcess = new HashMap<String, MetaEntity>();
- private final Map<String, MetaEntity> metaSuperclassAndEmbeddableToProcess = new HashMap<String, MetaEntity>();
private static class AccessTypeHolder {
public AccessType elementAccessType;
@@ -48,8 +52,16 @@
public Context(ProcessingEnvironment pe) {
this.pe = pe;
+ String debugParam = pe.getOptions().get( DEBUG_PARAMETER );
+ if ( debugParam != null && "true".equals( debugParam ) ) {
+ logDebug = true;
+ }
}
+ public ProcessingEnvironment getProcessingEnvironment() {
+ return pe;
+ }
+
public Map<String, MetaEntity> getMetaEntitiesToProcess() {
return metaEntitiesToProcess;
}
@@ -94,11 +106,17 @@
//does not work for Entity (risk of circularity)
public void processElement(TypeElement element, AccessType defaultAccessTypeForHierarchy) {
if ( elementsAlreadyProcessed.contains( element.getQualifiedName().toString() ) ) {
- pe.getMessager().printMessage( Diagnostic.Kind.WARNING, "Element already processed (ignoring): " + element );
+ logMessage( Diagnostic.Kind.WARNING, "Element already processed (ignoring): " + element );
return;
}
-
- ClassWriter.writeFile( new AnnotationMetaEntity( pe, element, this, defaultAccessTypeForHierarchy ), pe, this );
+ ClassWriter.writeFile( new AnnotationMetaEntity( pe, element, this, defaultAccessTypeForHierarchy ), this );
elementsAlreadyProcessed.add( element.getQualifiedName().toString() );
}
+
+ public void logMessage(Diagnostic.Kind type, String message) {
+ if ( !logDebug && type.equals( Diagnostic.Kind.NOTE ) ) {
+ return;
+ }
+ pe.getMessager().printMessage( type, message );
+ }
}
Modified: jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java
===================================================================
--- jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java 2009-11-06 13:59:37 UTC (rev 17944)
+++ jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java 2009-11-06 16:02:26 UTC (rev 17945)
@@ -64,10 +64,8 @@
* @author Hardy Ferentschik
* @author Emmanuel Bernard
*/
-//@SupportedAnnotationTypes("javax.persistence.Entity")
@SupportedAnnotationTypes("*")
@SupportedSourceVersion(RELEASE_6)
-// TODO Extract all the XML parsing into a separate class
public class JPAMetaModelEntityProcessor extends AbstractProcessor {
private static final String PATH_SEPARATOR = "/";
@@ -87,7 +85,7 @@
public void init(ProcessingEnvironment env) {
super.init( env );
context = new Context( env );
- processingEnv.getMessager().printMessage( Diagnostic.Kind.NOTE, "Init Processor " + this );
+ context.logMessage( Diagnostic.Kind.NOTE, "Init Processor " + this );
}
@Override
@@ -95,13 +93,9 @@
final RoundEnvironment roundEnvironment) {
if ( roundEnvironment.processingOver() ) {
- processingEnv.getMessager()
- .printMessage( Diagnostic.Kind.NOTE, "Last processing round." );
-
+ context.logMessage( Diagnostic.Kind.NOTE, "Last processing round." );
createMetaModelClasses();
-
- processingEnv.getMessager()
- .printMessage( Diagnostic.Kind.NOTE, "Finished processing" );
+ context.logMessage( Diagnostic.Kind.NOTE, "Finished processing" );
return ALLOW_OTHER_PROCESSORS_TO_CLAIM_ANNOTATIONS;
}
@@ -110,14 +104,13 @@
}
if ( !hostJPAAnnotations( annotations ) ) {
- processingEnv.getMessager()
- .printMessage( Diagnostic.Kind.NOTE, "Current processing round does not contain entities" );
+ context.logMessage( Diagnostic.Kind.NOTE, "Current processing round does not contain entities" );
return ALLOW_OTHER_PROCESSORS_TO_CLAIM_ANNOTATIONS;
}
Set<? extends Element> elements = roundEnvironment.getRootElements();
for ( Element element : elements ) {
- processingEnv.getMessager().printMessage( Diagnostic.Kind.NOTE, "Processing " + element.toString() );
+ context.logMessage( Diagnostic.Kind.NOTE, "Processing " + element.toString() );
handleRootElementAnnotationMirrors( element );
}
@@ -126,9 +119,8 @@
private void createMetaModelClasses() {
for ( MetaEntity entity : context.getMetaEntitiesToProcess().values() ) {
- processingEnv.getMessager()
- .printMessage( Diagnostic.Kind.NOTE, "Writing meta model for " + entity );
- ClassWriter.writeFile( entity, processingEnv, context );
+ context.logMessage( Diagnostic.Kind.NOTE, "Writing meta model for " + entity );
+ ClassWriter.writeFile( entity, context );
}
//process left over, in most cases is empty
@@ -137,9 +129,8 @@
}
for ( MetaEntity entity : context.getMetaSuperclassAndEmbeddableToProcess().values() ) {
- processingEnv.getMessager()
- .printMessage( Diagnostic.Kind.NOTE, "Writing meta model for " + entity );
- ClassWriter.writeFile( entity, processingEnv, context );
+ context.logMessage( Diagnostic.Kind.NOTE, "Writing meta model for " + entity );
+ ClassWriter.writeFile( entity, context );
}
}
@@ -161,8 +152,7 @@
private void parsePersistenceXml() {
Persistence persistence = parseXml( PERSISTENCE_XML, Persistence.class, PERSISTENCE_XML_XSD );
- if ( persistence != null )
- {
+ if ( persistence != null ) {
List<Persistence.PersistenceUnit> persistenceUnits = persistence.getPersistenceUnit();
for ( Persistence.PersistenceUnit unit : persistenceUnits ) {
List<String> mappingFiles = unit.getMappingFile();
@@ -228,7 +218,7 @@
String fullyQualifiedClassName = packageName + "." + entity.getClazz();
if ( !xmlMappedTypeExists( fullyQualifiedClassName ) ) {
- processingEnv.getMessager().printMessage(
+ context.logMessage(
Diagnostic.Kind.WARNING,
fullyQualifiedClassName + " is mapped in xml, but class does not exists. Skipping meta model generation."
);
@@ -240,7 +230,7 @@
);
if ( context.getMetaEntitiesToProcess().containsKey( fullyQualifiedClassName ) ) {
- processingEnv.getMessager().printMessage(
+ context.logMessage(
Diagnostic.Kind.WARNING,
fullyQualifiedClassName + " was already processed once. Skipping second occurance."
);
@@ -266,7 +256,7 @@
String fullyQualifiedClassName = packageName + "." + embeddable.getClazz();
if ( !xmlMappedTypeExists( fullyQualifiedClassName ) ) {
- processingEnv.getMessager().printMessage(
+ context.logMessage(
Diagnostic.Kind.WARNING,
fullyQualifiedClassName + " is mapped in xml, but class does not exists. Skipping meta model generation."
);
@@ -278,7 +268,7 @@
);
if ( context.getMetaSuperclassAndEmbeddableToProcess().containsKey( fullyQualifiedClassName ) ) {
- processingEnv.getMessager().printMessage(
+ context.logMessage(
Diagnostic.Kind.WARNING,
fullyQualifiedClassName + " was already processed once. Skipping second occurance."
);
@@ -294,7 +284,7 @@
String fullyQualifiedClassName = packageName + "." + mappedSuperClass.getClazz();
if ( !xmlMappedTypeExists( fullyQualifiedClassName ) ) {
- processingEnv.getMessager().printMessage(
+ context.logMessage(
Diagnostic.Kind.WARNING,
fullyQualifiedClassName + " is mapped in xml, but class does not exists. Skipping meta model generation."
);
@@ -306,7 +296,7 @@
);
if ( context.getMetaSuperclassAndEmbeddableToProcess().containsKey( fullyQualifiedClassName ) ) {
- processingEnv.getMessager().printMessage(
+ context.logMessage(
Diagnostic.Kind.WARNING,
fullyQualifiedClassName + " was already processed once. Skipping second occurance."
);
@@ -325,13 +315,17 @@
if ( element.getKind() == ElementKind.CLASS ) {
if ( annotationType.equals( ENTITY_ANN ) ) {
- AnnotationMetaEntity metaEntity = new AnnotationMetaEntity( processingEnv, ( TypeElement ) element, context );
+ AnnotationMetaEntity metaEntity = new AnnotationMetaEntity(
+ processingEnv, ( TypeElement ) element, context
+ );
// TODO instead of just adding the entity we have to do some merging.
context.getMetaEntitiesToProcess().put( metaEntity.getQualifiedName(), metaEntity );
}
else if ( annotationType.equals( MAPPED_SUPERCLASS_ANN )
|| annotationType.equals( EMBEDDABLE_ANN ) ) {
- AnnotationMetaEntity metaEntity = new AnnotationMetaEntity( processingEnv, ( TypeElement ) element, context );
+ AnnotationMetaEntity metaEntity = new AnnotationMetaEntity(
+ processingEnv, ( TypeElement ) element, context
+ );
// TODO instead of just adding the entity we have to do some merging.
context.getMetaSuperclassAndEmbeddableToProcess().put( metaEntity.getQualifiedName(), metaEntity );
@@ -343,8 +337,7 @@
private InputStream getInputStreamForResource(String resource) {
String pkg = getPackage( resource );
String name = getRelativeName( resource );
- processingEnv.getMessager()
- .printMessage( Diagnostic.Kind.NOTE, "Reading resource " + resource );
+ context.logMessage( Diagnostic.Kind.NOTE, "Reading resource " + resource );
InputStream ormStream;
try {
FileObject fileObject = processingEnv.getFiler().getResource( StandardLocation.CLASS_OUTPUT, pkg, name );
@@ -381,7 +374,7 @@
InputStream stream = getInputStreamForResource( resource );
if ( stream == null ) {
- processingEnv.getMessager().printMessage( Diagnostic.Kind.NOTE, resource + " not found." );
+ context.logMessage( Diagnostic.Kind.NOTE, resource + " not found." );
return null;
}
try {
@@ -394,12 +387,12 @@
}
catch ( JAXBException e ) {
String message = "Error unmarshalling " + resource + " with exception :\n " + e;
- processingEnv.getMessager().printMessage( Diagnostic.Kind.WARNING, message );
+ context.logMessage( Diagnostic.Kind.WARNING, message );
return null;
}
catch ( Exception e ) {
String message = "Error reading " + resource + " with exception :\n " + e;
- processingEnv.getMessager().printMessage( Diagnostic.Kind.WARNING, message );
+ context.logMessage( Diagnostic.Kind.WARNING, message );
return null;
}
}
@@ -426,15 +419,15 @@
Schema schema = null;
URL schemaUrl = this.getClass().getClassLoader().getResource( schemaName );
if ( schemaUrl == null ) {
- return schema;
+ return schema;
}
-
+
SchemaFactory sf = SchemaFactory.newInstance( javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI );
try {
schema = sf.newSchema( schemaUrl );
}
catch ( SAXException e ) {
- processingEnv.getMessager().printMessage(
+ context.logMessage(
Diagnostic.Kind.WARNING, "Unable to create schema for " + schemaName + ": " + e.getMessage()
);
}
Modified: jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java
===================================================================
--- jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java 2009-11-06 13:59:37 UTC (rev 17944)
+++ jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java 2009-11-06 16:02:26 UTC (rev 17945)
@@ -192,15 +192,14 @@
}
private AccessType getAccessTypeForClass(TypeElement searchedElement) {
- pe.getMessager().printMessage( Diagnostic.Kind.NOTE, "check class" + searchedElement );
+ context.logMessage( Diagnostic.Kind.NOTE, "check class " + searchedElement );
AccessType accessType = context.getAccessType( searchedElement );
if ( defaultAccessTypeForHierarchy == null ) {
this.defaultAccessTypeForHierarchy = context.getDefaultAccessTypeForHerarchy( searchedElement );
}
if ( accessType != null ) {
- pe.getMessager()
- .printMessage( Diagnostic.Kind.NOTE, "Found in cache" + searchedElement + ":" + accessType );
+ context.logMessage( Diagnostic.Kind.NOTE, "Found in cache" + searchedElement + ":" + accessType );
return accessType;
}
@@ -211,8 +210,7 @@
final Access accessAnn = searchedElement.getAnnotation( Access.class );
AccessType forcedAccessType = accessAnn != null ? accessAnn.value() : null;
if ( forcedAccessType != null ) {
- pe.getMessager()
- .printMessage( Diagnostic.Kind.NOTE, "access type " + searchedElement + ":" + forcedAccessType );
+ context.logMessage( Diagnostic.Kind.NOTE, "access type " + searchedElement + ":" + forcedAccessType );
context.addAccessType( searchedElement, forcedAccessType );
}
@@ -232,7 +230,7 @@
//FIXME consider XML
if ( annotationType.equals( Id.class.getName() )
|| annotationType.equals( EmbeddedId.class.getName() ) ) {
- pe.getMessager().printMessage( Diagnostic.Kind.NOTE, "Found id on" + searchedElement );
+ context.logMessage( Diagnostic.Kind.NOTE, "Found id on" + searchedElement );
final ElementKind kind = subElement.getKind();
if ( kind == ElementKind.FIELD || kind == ElementKind.METHOD ) {
accessType = kind == ElementKind.FIELD ? AccessType.FIELD : AccessType.PROPERTY;
@@ -251,7 +249,7 @@
}
if ( forcedAccessType == null ) {
context.addAccessType( searchedElement, accessType );
- pe.getMessager().printMessage(
+ context.logMessage(
Diagnostic.Kind.NOTE, "access type " + searchedElement + ":" + accessType
);
return accessType;
More information about the hibernate-commits
mailing list