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
This commit is contained in:
Hardy Ferentschik 2009-11-06 16:02:26 +00:00 committed by Strong Liu
parent 0e9f97ce2f
commit fe0038ce14
4 changed files with 67 additions and 62 deletions

View File

@ -23,7 +23,6 @@ import java.io.PrintWriter;
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 @@ import javax.tools.FileObject;
*/
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 @@ public class ClassWriter {
}
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()
);
}
}

View File

@ -17,13 +17,13 @@
*/
package org.hibernate.jpamodelgen;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
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 @@ import org.hibernate.jpamodelgen.annotation.AnnotationMetaEntity;
* @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,6 +52,14 @@ public class Context {
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() {
@ -94,11 +106,17 @@ public class Context {
//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 );
}
}

View File

@ -64,10 +64,8 @@ import org.hibernate.jpamodelgen.xml.jaxb.PersistenceUnitMetadata;
* @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 class JPAMetaModelEntityProcessor extends AbstractProcessor {
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 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
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 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
}
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 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
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 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
}
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 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
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 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
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 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
);
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 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
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 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
);
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 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
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 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
);
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 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
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 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
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 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
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 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
}
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 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
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()
);
}

View File

@ -192,15 +192,14 @@ public class AnnotationMetaEntity implements MetaEntity {
}
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 @@ public class AnnotationMetaEntity implements MetaEntity {
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 @@ public class AnnotationMetaEntity implements MetaEntity {
//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 @@ public class AnnotationMetaEntity implements MetaEntity {
}
if ( forcedAccessType == null ) {
context.addAccessType( searchedElement, accessType );
pe.getMessager().printMessage(
context.logMessage(
Diagnostic.Kind.NOTE, "access type " + searchedElement + ":" + accessType
);
return accessType;