METAGEN-22 Some cleanup and optimisatons prior adding new functionality

This commit is contained in:
Hardy Ferentschik 2010-02-11 19:37:09 +00:00 committed by Strong Liu
parent 7856f66eb9
commit 6e039d710b
8 changed files with 56 additions and 116 deletions

View File

@ -116,7 +116,7 @@ public class ClassWriter {
final Element superClassElement = ( ( DeclaredType ) superClass ).asElement();
String superClassName = ( ( TypeElement ) superClassElement ).getQualifiedName().toString();
if ( context.containsMetaEntity( superClassName )
|| context.containsMetaSuperclassOrEmbeddable( superClassName ) ) {
|| context.containsMetaEmbeddable( superClassName ) ) {
pw.print( " extends " + superClassName + "_" );
}
}

View File

@ -31,6 +31,7 @@ import javax.persistence.AccessType;
import javax.tools.Diagnostic;
import org.hibernate.jpamodelgen.model.MetaEntity;
import org.hibernate.jpamodelgen.util.Constants;
/**
* @author Max Andersen
@ -38,10 +39,19 @@ import org.hibernate.jpamodelgen.model.MetaEntity;
* @author Emmanuel Bernard
*/
public class Context {
private static final String PATH_SEPARATOR = System.getProperty( "file.separator" );
private static final String DEFAULT_PERSISTENCE_XML_LOCATION = "/META-INF/persistence.xml";
/**
* Used for keeping track of parsed entities and mapped super classes (xml + annotations).
*/
private final Map<String, MetaEntity> metaEntities = new HashMap<String, MetaEntity>();
private final Map<String, MetaEntity> metaSuperclassAndEmbeddable = new HashMap<String, MetaEntity>();
/**
* Used for keeping track of parsed embeddable entities. These entities have to be kept separate since
* they are lazily initialized.
*/
private final Map<String, MetaEntity> metaEmbeddables = new HashMap<String, MetaEntity>();
private final Map<String, AccessTypeInformation> accessTypeInformation = new HashMap<String, AccessTypeInformation>();
private final ProcessingEnvironment pe;
@ -57,8 +67,8 @@ public class Context {
if ( pe.getOptions().get( JPAMetaModelEntityProcessor.PERSISTENCE_XML_OPTION ) != null ) {
String tmp = pe.getOptions().get( JPAMetaModelEntityProcessor.PERSISTENCE_XML_OPTION );
if ( !tmp.startsWith( PATH_SEPARATOR ) ) {
tmp = PATH_SEPARATOR + tmp;
if ( !tmp.startsWith( Constants.PATH_SEPARATOR ) ) {
tmp = Constants.PATH_SEPARATOR + tmp;
}
persistenceXmlLocation = tmp;
}
@ -70,8 +80,8 @@ public class Context {
String tmp = pe.getOptions().get( JPAMetaModelEntityProcessor.ORM_XML_OPTION );
ormXmlFiles = new ArrayList<String>();
for ( String ormFile : tmp.split( "," ) ) {
if ( !ormFile.startsWith( PATH_SEPARATOR ) ) {
ormFile = PATH_SEPARATOR + ormFile;
if ( !ormFile.startsWith( Constants.PATH_SEPARATOR ) ) {
ormFile = Constants.PATH_SEPARATOR + ormFile;
}
ormXmlFiles.add( ormFile );
}
@ -81,7 +91,6 @@ public class Context {
}
logDebug = Boolean.parseBoolean( pe.getOptions().get( JPAMetaModelEntityProcessor.DEBUG_OPTION ) );
}
public ProcessingEnvironment getProcessingEnvironment() {
@ -120,20 +129,20 @@ public class Context {
metaEntities.put( fcqn, metaEntity );
}
public boolean containsMetaSuperclassOrEmbeddable(String fqcn) {
return metaSuperclassAndEmbeddable.containsKey( fqcn );
public boolean containsMetaEmbeddable(String fqcn) {
return metaEmbeddables.containsKey( fqcn );
}
public MetaEntity getMetaSuperclassOrEmbeddable(String fqcn) {
return metaSuperclassAndEmbeddable.get( fqcn );
public MetaEntity getMetaEmbeddable(String fqcn) {
return metaEmbeddables.get( fqcn );
}
public void addMetaSuperclassOrEmbeddable(String fcqn, MetaEntity metaEntity) {
metaSuperclassAndEmbeddable.put( fcqn, metaEntity );
public void addMetaEmbeddable(String fqcn, MetaEntity metaEntity) {
metaEmbeddables.put( fqcn, metaEntity );
}
public Collection<MetaEntity> getMetaSuperclassOrEmbeddable() {
return metaSuperclassAndEmbeddable.values();
public Collection<MetaEntity> getMetaEmbeddables() {
return metaEmbeddables.values();
}
public void addAccessTypeInformation(String fqcn, AccessTypeInformation info) {

View File

@ -128,7 +128,7 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
// we cannot process the delayed entities in any order. There might be dependencies between them.
// we need to process the top level entities first
// TODO make sure that we don't run into circular dependencies here
Collection<MetaEntity> toProcessEntities = context.getMetaSuperclassOrEmbeddable();
Collection<MetaEntity> toProcessEntities = context.getMetaEmbeddables();
while ( !toProcessEntities.isEmpty() ) {
Set<MetaEntity> processedEntities = new HashSet<MetaEntity>();
for ( MetaEntity entity : toProcessEntities ) {
@ -218,7 +218,7 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
}
else if ( TypeUtils.isAnnotationMirrorOfType( mirror, MappedSuperclass.class )
|| TypeUtils.isAnnotationMirrorOfType( mirror, Embeddable.class ) ) {
alreadyExistingMetaEntity = context.getMetaSuperclassOrEmbeddable( fqn );
alreadyExistingMetaEntity = context.getMetaEmbeddable( fqn );
}
return alreadyExistingMetaEntity;
}
@ -228,10 +228,10 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
context.addMetaEntity( metaEntity.getQualifiedName(), metaEntity );
}
else if ( TypeUtils.isAnnotationMirrorOfType( mirror, MappedSuperclass.class ) ) {
context.addMetaSuperclassOrEmbeddable( metaEntity.getQualifiedName(), metaEntity );
context.addMetaEntity( metaEntity.getQualifiedName(), metaEntity );
}
else if ( TypeUtils.isAnnotationMirrorOfType( mirror, Embeddable.class ) ) {
context.addMetaSuperclassOrEmbeddable( metaEntity.getQualifiedName(), metaEntity );
context.addMetaEmbeddable( metaEntity.getQualifiedName(), metaEntity );
}
}

View File

@ -63,7 +63,10 @@ public class Constants {
BASIC_ARRAY_TYPES.add( "java.lang.Byte" );
}
private Constants(){}
public static final String PATH_SEPARATOR = "/";
private Constants() {
}
}

View File

@ -90,6 +90,8 @@ public class XmlMetaEntity implements MetaEntity {
this( mappedSuperclass.getClazz(), packageName, element, context, mappedSuperclass.isMetadataComplete() );
this.attributes = mappedSuperclass.getAttributes();
this.embeddableAttributes = null;
// entities can be directly initialised
init();
}
protected XmlMetaEntity(Embeddable embeddable, String packageName, TypeElement element, Context context) {

View File

@ -1,74 +0,0 @@
// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, 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.
*/
package org.hibernate.jpamodelgen.xml;
import java.util.List;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import org.hibernate.jpamodelgen.Context;
import org.hibernate.jpamodelgen.MetaModelGenerationException;
import org.hibernate.jpamodelgen.model.MetaAttribute;
import org.hibernate.jpamodelgen.xml.jaxb.Basic;
import org.hibernate.jpamodelgen.xml.jaxb.ElementCollection;
import org.hibernate.jpamodelgen.xml.jaxb.EmbeddableAttributes;
import org.hibernate.jpamodelgen.xml.jaxb.ManyToMany;
import org.hibernate.jpamodelgen.xml.jaxb.ManyToOne;
import org.hibernate.jpamodelgen.xml.jaxb.MappedSuperclass;
import org.hibernate.jpamodelgen.xml.jaxb.OneToMany;
import org.hibernate.jpamodelgen.xml.jaxb.OneToOne;
/**
* @author Hardy Ferentschik
*/
public class XmlMetaMappedSuperClass extends XmlMetaEntity {
private boolean initialized;
public XmlMetaMappedSuperClass(MappedSuperclass mappedSuperclass, String packageName, TypeElement element, Context context) {
super( mappedSuperclass, packageName, element, context );
}
public List<MetaAttribute> getMembers() {
if ( !initialized ) {
context.logMessage( Diagnostic.Kind.OTHER, "Entity " + getQualifiedName() + "was lazily initialised." );
init();
initialized = true;
}
return members;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append( "XmlMetaEntity" );
sb.append( "{accessTypeInfo=" ).append( accessTypeInfo );
sb.append( ", clazzName='" ).append( clazzName ).append( '\'' );
sb.append( ", members=" );
if ( initialized ) {
sb.append( members );
}
else {
sb.append( "[un-initalized]" );
}
sb.append( ", isMetaComplete=" ).append( isMetaComplete );
sb.append( '}' );
return sb.toString();
}
}

View File

@ -39,6 +39,7 @@ import org.xml.sax.SAXException;
import org.hibernate.jpamodelgen.AccessTypeInformation;
import org.hibernate.jpamodelgen.Context;
import org.hibernate.jpamodelgen.util.Constants;
import org.hibernate.jpamodelgen.util.StringUtil;
import org.hibernate.jpamodelgen.util.TypeUtils;
import org.hibernate.jpamodelgen.xml.jaxb.Entity;
@ -55,7 +56,6 @@ public class XmlParser {
private static final String ORM_XML = "/META-INF/orm.xml";
private static final String PERSISTENCE_XML_XSD = "persistence_2_0.xsd";
private static final String ORM_XSD = "orm_2_0.xsd";
private static final String PATH_SEPARATOR = "/";
private Context context;
private List<EntityMappings> entityMappings;
@ -154,13 +154,13 @@ public class XmlParser {
}
XmlMetaEntity metaEntity = new XmlMetaEmbeddable( embeddable, pkg, getXmlMappedType( fqcn ), context );
if ( context.containsMetaSuperclassOrEmbeddable( fqcn ) ) {
if ( context.containsMetaEmbeddable( fqcn ) ) {
context.logMessage(
Diagnostic.Kind.WARNING,
fqcn + " was already processed once. Skipping second occurance."
);
}
context.addMetaSuperclassOrEmbeddable( fqcn, metaEntity );
context.addMetaEmbeddable( fqcn, metaEntity );
}
}
@ -180,17 +180,17 @@ public class XmlParser {
continue;
}
XmlMetaEntity metaEntity = new XmlMetaMappedSuperClass(
XmlMetaEntity metaEntity = new XmlMetaEntity(
mappedSuperClass, pkg, getXmlMappedType( fqcn ), context
);
if ( context.containsMetaSuperclassOrEmbeddable( fqcn ) ) {
if ( context.containsMetaEmbeddable( fqcn ) ) {
context.logMessage(
Diagnostic.Kind.WARNING,
fqcn + " was already processed once. Skipping second occurance."
);
}
context.addMetaSuperclassOrEmbeddable( fqcn, metaEntity );
context.addMetaEntity( fqcn, metaEntity );
}
}
@ -263,7 +263,7 @@ public class XmlParser {
ormStream = fileObject.openInputStream();
}
catch ( IOException e1 ) {
// TODO
// TODO - METAGEN-12
// unfortunately, the Filer.getResource API seems not to be able to load from /META-INF. One gets a
// FilerException with the message with "Illegal name /META-INF". This means that we have to revert to
// using the classpath. This might mean that we find a persistence.xml which is 'part of another jar.
@ -274,20 +274,20 @@ public class XmlParser {
}
private String getPackage(String resourceName) {
if ( !resourceName.contains( PATH_SEPARATOR ) ) {
if ( !resourceName.contains( Constants.PATH_SEPARATOR ) ) {
return "";
}
else {
return resourceName.substring( 0, resourceName.lastIndexOf( PATH_SEPARATOR ) );
return resourceName.substring( 0, resourceName.lastIndexOf( Constants.PATH_SEPARATOR ) );
}
}
private String getRelativeName(String resourceName) {
if ( !resourceName.contains( PATH_SEPARATOR ) ) {
if ( !resourceName.contains( Constants.PATH_SEPARATOR ) ) {
return resourceName;
}
else {
return resourceName.substring( resourceName.lastIndexOf( PATH_SEPARATOR ) + 1 );
return resourceName.substring( resourceName.lastIndexOf( Constants.PATH_SEPARATOR ) + 1 );
}
}
@ -329,11 +329,11 @@ public class XmlParser {
context.addAccessTypeInformation( fqcn, accessInfo );
}
for ( org.hibernate.jpamodelgen.xml.jaxb.Embeddable embeddable : mappings.getEmbeddable() ) {
String name = embeddable.getClazz();
for ( org.hibernate.jpamodelgen.xml.jaxb.MappedSuperclass mappedSuperClass : mappings.getMappedSuperclass() ) {
String name = mappedSuperClass.getClazz();
fqcn = StringUtil.determineFullyQualifiedClassName( packageName, name );
AccessType explicitAccessType = null;
org.hibernate.jpamodelgen.xml.jaxb.AccessType type = embeddable.getAccess();
org.hibernate.jpamodelgen.xml.jaxb.AccessType type = mappedSuperClass.getAccess();
if ( type != null ) {
explicitAccessType = mapXmlAccessTypeToJpaAccessType( type );
}
@ -341,14 +341,13 @@ public class XmlParser {
fqcn, explicitAccessType, defaultAccessType
);
context.addAccessTypeInformation( fqcn, accessInfo );
}
for ( org.hibernate.jpamodelgen.xml.jaxb.MappedSuperclass mappedSuperClass : mappings.getMappedSuperclass() ) {
String name = mappedSuperClass.getClazz();
for ( org.hibernate.jpamodelgen.xml.jaxb.Embeddable embeddable : mappings.getEmbeddable() ) {
String name = embeddable.getClazz();
fqcn = StringUtil.determineFullyQualifiedClassName( packageName, name );
AccessType explicitAccessType = null;
org.hibernate.jpamodelgen.xml.jaxb.AccessType type = mappedSuperClass.getAccess();
org.hibernate.jpamodelgen.xml.jaxb.AccessType type = embeddable.getAccess();
if ( type != null ) {
explicitAccessType = mapXmlAccessTypeToJpaAccessType( type );
}

View File

@ -5,10 +5,11 @@
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
version="2.0"
>
<package>org.hibernate.jpamodelgen.test.xmlmapped</package>
<mapped-superclass class="Building" access="FIELD" metadata-complete="true"> <!--means ignore annotations-->
<mapped-superclass class="org.hibernate.jpamodelgen.test.xmlmapped.Building"
access="FIELD"
metadata-complete="true">
<attributes>
<one-to-one name="address" fetch="LAZY"/>
<one-to-one name="address"/>
</attributes>
</mapped-superclass>
</entity-mappings>