Skip to content

Commit

Permalink
METAGEN-22 Some cleanup and optimisatons prior adding new functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
hferentschik authored and stliu committed Nov 11, 2013
1 parent 7856f66 commit 6e039d7
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 116 deletions.
Expand Up @@ -116,7 +116,7 @@ private static void printClassDeclaration(MetaEntity entity, PrintWriter pw, Con
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 + "_" );
}
}
Expand Down
Expand Up @@ -31,17 +31,27 @@
import javax.tools.Diagnostic;

import org.hibernate.jpamodelgen.model.MetaEntity;
import org.hibernate.jpamodelgen.util.Constants;

/**
* @author Max Andersen
* @author Hardy Ferentschik
* @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;
Expand All @@ -57,8 +67,8 @@ public Context(ProcessingEnvironment pe) {

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;
}
Expand All @@ -70,8 +80,8 @@ public Context(ProcessingEnvironment pe) {
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 );
}
Expand All @@ -81,7 +91,6 @@ public Context(ProcessingEnvironment pe) {
}

logDebug = Boolean.parseBoolean( pe.getOptions().get( JPAMetaModelEntityProcessor.DEBUG_OPTION ) );

}

public ProcessingEnvironment getProcessingEnvironment() {
Expand Down Expand Up @@ -120,20 +129,20 @@ public void addMetaEntity(String fcqn, MetaEntity metaEntity) {
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) {
Expand Down
Expand Up @@ -128,7 +128,7 @@ private void createMetaModelClasses() {
// 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 ) {
Expand Down Expand Up @@ -218,7 +218,7 @@ private MetaEntity tryGettingExistingEntityFromContext(AnnotationMirror mirror,
}
else if ( TypeUtils.isAnnotationMirrorOfType( mirror, MappedSuperclass.class )
|| TypeUtils.isAnnotationMirrorOfType( mirror, Embeddable.class ) ) {
alreadyExistingMetaEntity = context.getMetaSuperclassOrEmbeddable( fqn );
alreadyExistingMetaEntity = context.getMetaEmbeddable( fqn );
}
return alreadyExistingMetaEntity;
}
Expand All @@ -228,10 +228,10 @@ private void addMetaEntityToContext(AnnotationMirror mirror, AnnotationMetaEntit
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 );
}
}

Expand Down
Expand Up @@ -63,7 +63,10 @@ public class Constants {
BASIC_ARRAY_TYPES.add( "java.lang.Byte" );
}

private Constants(){}
public static final String PATH_SEPARATOR = "/";

private Constants() {
}
}


Expand Up @@ -90,6 +90,8 @@ protected XmlMetaEntity(MappedSuperclass mappedSuperclass, String packageName, T
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) {
Expand Down

This file was deleted.

Expand Up @@ -39,6 +39,7 @@

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;
Expand All @@ -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;
Expand Down Expand Up @@ -154,13 +154,13 @@ private void parseEmbeddable(Collection<org.hibernate.jpamodelgen.xml.jaxb.Embed
}

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

Expand All @@ -180,17 +180,17 @@ private void parseMappedSuperClass(Collection<org.hibernate.jpamodelgen.xml.jaxb
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 );
}
}

Expand Down Expand Up @@ -263,7 +263,7 @@ private InputStream getInputStreamForResource(String resource) {
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.
Expand All @@ -274,20 +274,20 @@ private InputStream getInputStreamForResource(String resource) {
}

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

Expand Down Expand Up @@ -329,26 +329,25 @@ private void determineXmlAccessTypes() {
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 );
}
AccessTypeInformation accessInfo = new AccessTypeInformation(
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 );
}
Expand Down
Expand Up @@ -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>
Expand Down

0 comments on commit 6e039d7

Please sign in to comment.