HHH-6114 Restructuring packages to accommodate for all required annotation binding code

This commit is contained in:
Hardy Ferentschik 2011-04-15 15:03:59 +02:00
parent 74c6773c44
commit e1ba813122
14 changed files with 137 additions and 45 deletions

View File

@ -32,6 +32,8 @@ import org.jboss.jandex.Index;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.hibernate.metamodel.source.annotations.util.ConfiguredClassHierarchyBuilder;
import org.hibernate.metamodel.source.annotations.util.JandexHelper;
import org.hibernate.metamodel.source.internal.MetadataImpl; import org.hibernate.metamodel.source.internal.MetadataImpl;
/** /**

View File

@ -41,6 +41,8 @@ import org.jboss.jandex.MethodInfo;
import org.hibernate.AnnotationException; import org.hibernate.AnnotationException;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.metamodel.source.annotations.util.JandexHelper;
import org.hibernate.metamodel.source.annotations.util.ReflectionHelper;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.classloading.spi.ClassLoaderService; import org.hibernate.service.classloading.spi.ClassLoaderService;

View File

@ -35,6 +35,7 @@ import org.jboss.jandex.FieldInfo;
import org.jboss.jandex.MethodInfo; import org.jboss.jandex.MethodInfo;
import org.hibernate.AnnotationException; import org.hibernate.AnnotationException;
import org.hibernate.metamodel.source.annotations.util.JandexHelper;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
/** /**
@ -47,7 +48,11 @@ public class ConfiguredClassHierarchy implements Iterable<ConfiguredClass> {
private final InheritanceType inheritanceType; private final InheritanceType inheritanceType;
private final List<ConfiguredClass> configuredClasses; private final List<ConfiguredClass> configuredClasses;
ConfiguredClassHierarchy(List<ClassInfo> classes, ServiceRegistry serviceRegistry) { public static ConfiguredClassHierarchy create(List<ClassInfo> classes, ServiceRegistry serviceRegistry) {
return new ConfiguredClassHierarchy( classes, serviceRegistry );
}
private ConfiguredClassHierarchy(List<ClassInfo> classes, ServiceRegistry serviceRegistry) {
defaultAccessType = determineDefaultAccessType( classes ); defaultAccessType = determineDefaultAccessType( classes );
inheritanceType = determineInheritanceType( classes ); inheritanceType = determineInheritanceType( classes );

View File

@ -21,7 +21,7 @@
* 51 Franklin Street, Fifth Floor * 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.metamodel.source.annotations; package org.hibernate.metamodel.source.annotations.util;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -34,6 +34,7 @@ import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName; import org.jboss.jandex.DotName;
import org.jboss.jandex.Index; import org.jboss.jandex.Index;
import org.hibernate.metamodel.source.annotations.ConfiguredClassHierarchy;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.classloading.spi.ClassLoaderService; import org.hibernate.service.classloading.spi.ClassLoaderService;
@ -90,7 +91,7 @@ public class ConfiguredClassHierarchyBuilder {
List<List<ClassInfo>> processedList = new ArrayList<List<ClassInfo>>(); List<List<ClassInfo>> processedList = new ArrayList<List<ClassInfo>>();
for ( List<ClassInfo> classInfoList : processedClassInfos.values() ) { for ( List<ClassInfo> classInfoList : processedClassInfos.values() ) {
if ( !processedList.contains( classInfoList ) ) { if ( !processedList.contains( classInfoList ) ) {
hierarchies.add( new ConfiguredClassHierarchy( classInfoList, serviceRegistry ) ); hierarchies.add( ConfiguredClassHierarchy.create( classInfoList, serviceRegistry ) );
processedList.add( classInfoList ); processedList.add( classInfoList );
} }
} }

View File

@ -21,15 +21,21 @@
* 51 Franklin Street, Fifth Floor * 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.metamodel.source.annotations; package org.hibernate.metamodel.source.annotations.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.List; import java.util.List;
import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.ClassInfo; import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName; import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
import org.jboss.jandex.Indexer;
import org.hibernate.AssertionFailure; import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
import org.hibernate.service.classloading.spi.ClassLoaderService;
/** /**
* Utility methods for working with the jandex annotation index. * Utility methods for working with the jandex annotation index.
@ -48,7 +54,7 @@ public class JandexHelper {
* *
* @throws org.hibernate.AssertionFailure in case there is * @throws org.hibernate.AssertionFailure in case there is
*/ */
static public AnnotationInstance getSingleAnnotation(ClassInfo classInfo, DotName annotationName) public static AnnotationInstance getSingleAnnotation(ClassInfo classInfo, DotName annotationName)
throws AssertionFailure { throws AssertionFailure {
List<AnnotationInstance> annotationList = classInfo.annotations().get( annotationName ); List<AnnotationInstance> annotationList = classInfo.annotations().get( annotationName );
if ( annotationList == null ) { if ( annotationList == null ) {
@ -65,6 +71,40 @@ public class JandexHelper {
); );
} }
} }
/**
* Creates a jandex index for the specified classes
*
* @param classLoaderService class loader service
* @param classes the classes to index
* @return an annotation repository w/ all the annotation discovered in the specified classes
*/
public static Index indexForClass(ClassLoaderService classLoaderService, Class<?>... classes) {
Indexer indexer = new Indexer();
for ( Class<?> clazz : classes ) {
InputStream stream = classLoaderService.locateResourceStream(
clazz.getName().replace( '.', '/' ) + ".class"
);
try {
indexer.index( stream );
}
catch ( IOException e ) {
StringBuilder builder = new StringBuilder();
builder.append( "[" );
int count = 0;
for ( Class<?> c : classes ) {
builder.append( c.getName() );
if ( count < classes.length - 1 ) {
builder.append( "," );
}
count++;
}
builder.append( "]" );
throw new HibernateException( "Unable to create annotation index for " + builder.toString());
}
}
return indexer.complete();
}
} }

View File

@ -1,4 +1,4 @@
package org.hibernate.metamodel.source.annotations; package org.hibernate.metamodel.source.annotations.util;
import java.beans.Introspector; import java.beans.Introspector;
import java.lang.reflect.Field; import java.lang.reflect.Field;

View File

@ -0,0 +1,28 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.metamodel.source.annotations.util;
/**
* Utility classes for annotation parsing and binding.
*/

View File

@ -1,4 +1,4 @@
package org.hibernate.metamodel.source.annotations; package org.hibernate.metamodel.source.annotations.xml;
import java.io.InputStream; import java.io.InputStream;
import java.util.HashSet; import java.util.HashSet;

View File

@ -0,0 +1,28 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.metamodel.source.annotations.xml;
/**
* Classes related to parsing orm.xml
*/

View File

@ -1,7 +1,5 @@
package org.hibernate.metamodel.source.annotations; package org.hibernate.metamodel.source.annotations.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.Set; import java.util.Set;
@ -16,20 +14,20 @@ import javax.persistence.MappedSuperclass;
import org.jboss.jandex.ClassInfo; import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName; import org.jboss.jandex.DotName;
import org.jboss.jandex.Index; import org.jboss.jandex.Index;
import org.jboss.jandex.Indexer;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.hibernate.AnnotationException; import org.hibernate.AnnotationException;
import org.hibernate.metamodel.source.Metadata; import org.hibernate.metamodel.source.annotations.ConfiguredClass;
import org.hibernate.metamodel.source.annotations.ConfiguredClassHierarchy;
import org.hibernate.service.classloading.spi.ClassLoaderService;
import org.hibernate.service.internal.BasicServiceRegistryImpl; import org.hibernate.service.internal.BasicServiceRegistryImpl;
import org.hibernate.testing.junit4.BaseUnitTestCase; import org.hibernate.testing.junit4.BaseUnitTestCase;
import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
/** /**
* @author Hardy Ferentschik * @author Hardy Ferentschik
@ -37,10 +35,12 @@ import static org.junit.Assert.fail;
public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
private BasicServiceRegistryImpl serviceRegistry; private BasicServiceRegistryImpl serviceRegistry;
private ClassLoaderService service;
@Before @Before
public void setUp() { public void setUp() {
serviceRegistry = new BasicServiceRegistryImpl( Collections.emptyMap() ); serviceRegistry = new BasicServiceRegistryImpl( Collections.emptyMap() );
service = serviceRegistry.getService( ClassLoaderService.class );
} }
@After @After
@ -50,7 +50,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
@Test @Test
public void testSingleEntity() { public void testSingleEntity() {
Index index = indexForClass( Foo.class ); Index index = JandexHelper.indexForClass( service, Foo.class );
Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies(
index, serviceRegistry index, serviceRegistry
); );
@ -64,7 +64,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
@Test @Test
public void testSimpleInheritance() { public void testSimpleInheritance() {
Index index = indexForClass( B.class, A.class ); Index index = JandexHelper.indexForClass( service, B.class, A.class );
Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies(
index, serviceRegistry index, serviceRegistry
); );
@ -80,7 +80,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
@Test @Test
public void testMultipleHierarchies() { public void testMultipleHierarchies() {
Index index = indexForClass( B.class, A.class, Foo.class ); Index index = JandexHelper.indexForClass( service, B.class, A.class, Foo.class );
Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies(
index, serviceRegistry index, serviceRegistry
); );
@ -105,7 +105,9 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
private String mappedProperty; private String mappedProperty;
} }
Index index = indexForClass( MappedSubClass.class, MappedSuperClass.class, UnmappedSubClass.class ); Index index = JandexHelper.indexForClass(
service, MappedSubClass.class, MappedSuperClass.class, UnmappedSubClass.class
);
Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies(
index, serviceRegistry index, serviceRegistry
); );
@ -128,7 +130,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
class EntityAndMappedSuperClass { class EntityAndMappedSuperClass {
} }
Index index = indexForClass( EntityAndMappedSuperClass.class ); Index index = JandexHelper.indexForClass( service, EntityAndMappedSuperClass.class );
ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry ); ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry );
} }
@ -144,7 +146,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
class B extends A { class B extends A {
} }
Index index = indexForClass( B.class, A.class ); Index index = JandexHelper.indexForClass( service, B.class, A.class );
ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry ); ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry );
} }
@ -160,7 +162,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
class B extends A { class B extends A {
} }
Index index = indexForClass( B.class, A.class ); Index index = JandexHelper.indexForClass( service, B.class, A.class );
Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies(
index, serviceRegistry index, serviceRegistry
); );
@ -189,7 +191,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
class B extends A { class B extends A {
} }
Index index = indexForClass( B.class, A.class ); Index index = JandexHelper.indexForClass( service, B.class, A.class );
Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies(
index, serviceRegistry index, serviceRegistry
); );
@ -210,7 +212,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
class B extends A { class B extends A {
} }
Index index = indexForClass( B.class, A.class ); Index index = JandexHelper.indexForClass( service, B.class, A.class );
Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies(
index, serviceRegistry index, serviceRegistry
); );
@ -238,7 +240,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
class B extends A { class B extends A {
} }
Index index = indexForClass( B.class, MappedSuperClass.class, A.class ); Index index = JandexHelper.indexForClass( service, B.class, MappedSuperClass.class, A.class );
Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies(
index, serviceRegistry index, serviceRegistry
); );
@ -261,26 +263,10 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
class B extends A { class B extends A {
} }
Index index = indexForClass( B.class, A.class ); Index index = JandexHelper.indexForClass( service, B.class, A.class );
ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry ); ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry );
} }
private Index indexForClass(Class<?>... classes) {
Indexer indexer = new Indexer();
for ( Class<?> clazz : classes ) {
InputStream stream = getClass().getClassLoader().getResourceAsStream(
clazz.getName().replace( '.', '/' ) + ".class"
);
try {
indexer.index( stream );
}
catch ( IOException e ) {
fail( "Unable to index" );
}
}
return indexer.complete();
}
@Entity @Entity
public class Foo { public class Foo {
@Id @Id

View File

@ -1,4 +1,4 @@
package org.hibernate.metamodel.source.annotations; package org.hibernate.metamodel.source.annotations.xml;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
@ -20,7 +20,7 @@ public class OrmXmlParserTests extends BaseUnitTestCase {
MetadataImpl metadata = (MetadataImpl) new MetadataSources( new BasicServiceRegistryImpl( Collections.emptyMap() ) ).buildMetadata(); MetadataImpl metadata = (MetadataImpl) new MetadataSources( new BasicServiceRegistryImpl( Collections.emptyMap() ) ).buildMetadata();
OrmXmlParser parser = new OrmXmlParser( metadata ); OrmXmlParser parser = new OrmXmlParser( metadata );
Set<String> xmlFiles = new HashSet<String>(); Set<String> xmlFiles = new HashSet<String>();
xmlFiles.add( "org/hibernate/metamodel/source/annotations/orm.xml" ); xmlFiles.add( "org/hibernate/metamodel/source/annotations/xml/orm.xml" );
parser.parseAndUpdateIndex( xmlFiles, null ); parser.parseAndUpdateIndex( xmlFiles, null );
} }
@ -29,7 +29,7 @@ public class OrmXmlParserTests extends BaseUnitTestCase {
MetadataImpl metadata = (MetadataImpl) new MetadataSources( new BasicServiceRegistryImpl( Collections.emptyMap() ) ).buildMetadata(); MetadataImpl metadata = (MetadataImpl) new MetadataSources( new BasicServiceRegistryImpl( Collections.emptyMap() ) ).buildMetadata();
OrmXmlParser parser = new OrmXmlParser( metadata ); OrmXmlParser parser = new OrmXmlParser( metadata );
Set<String> xmlFiles = new HashSet<String>(); Set<String> xmlFiles = new HashSet<String>();
xmlFiles.add( "org/hibernate/metamodel/source/annotations/orm2.xml" ); xmlFiles.add( "org/hibernate/metamodel/source/annotations/xml/orm-star.xml" );
parser.parseAndUpdateIndex( xmlFiles, null ); parser.parseAndUpdateIndex( xmlFiles, null );
} }
} }

View File

@ -1,4 +1,4 @@
package org.hibernate.metamodel.source.annotations; package org.hibernate.metamodel.source.annotations.xml;
import javax.persistence.Entity; import javax.persistence.Entity;

View File

@ -6,7 +6,7 @@
version="1.0" version="1.0"
> >
<!-- use orm_1_0 on purpose to test backward compatibility --> <!-- use orm_1_0 on purpose to test backward compatibility -->
<package>org.hibernate.metamodel.source.annotations</package> <package>org.hibernate.metamodel.source.annotations.xml</package>
<entity class="Star" access="FIELD" metadata-complete="false"> <entity class="Star" access="FIELD" metadata-complete="false">
<attributes> <attributes>
<id name="id"> <id name="id">