diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/AnnotationBinder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/AnnotationBinder.java index a9c70603c4..d6c712ba1f 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/AnnotationBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/AnnotationBinder.java @@ -32,6 +32,8 @@ import org.jboss.jandex.Index; import org.slf4j.Logger; 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; /** diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClass.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClass.java index bf8a4d1eb9..7e2bf98df3 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClass.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClass.java @@ -41,6 +41,8 @@ import org.jboss.jandex.MethodInfo; import org.hibernate.AnnotationException; 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.classloading.spi.ClassLoaderService; diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClassHierarchy.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClassHierarchy.java index 02340b088e..af56df1b75 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClassHierarchy.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClassHierarchy.java @@ -35,6 +35,7 @@ import org.jboss.jandex.FieldInfo; import org.jboss.jandex.MethodInfo; import org.hibernate.AnnotationException; +import org.hibernate.metamodel.source.annotations.util.JandexHelper; import org.hibernate.service.ServiceRegistry; /** @@ -47,7 +48,11 @@ public class ConfiguredClassHierarchy implements Iterable { private final InheritanceType inheritanceType; private final List configuredClasses; - ConfiguredClassHierarchy(List classes, ServiceRegistry serviceRegistry) { + public static ConfiguredClassHierarchy create(List classes, ServiceRegistry serviceRegistry) { + return new ConfiguredClassHierarchy( classes, serviceRegistry ); + } + + private ConfiguredClassHierarchy(List classes, ServiceRegistry serviceRegistry) { defaultAccessType = determineDefaultAccessType( classes ); inheritanceType = determineInheritanceType( classes ); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClassHierarchyBuilder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/util/ConfiguredClassHierarchyBuilder.java similarity index 94% rename from hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClassHierarchyBuilder.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/util/ConfiguredClassHierarchyBuilder.java index 5bf896f1a9..bf5aa190c8 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClassHierarchyBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/util/ConfiguredClassHierarchyBuilder.java @@ -21,7 +21,7 @@ * 51 Franklin Street, Fifth Floor * 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.HashMap; @@ -34,6 +34,7 @@ import org.jboss.jandex.ClassInfo; import org.jboss.jandex.DotName; import org.jboss.jandex.Index; +import org.hibernate.metamodel.source.annotations.ConfiguredClassHierarchy; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.classloading.spi.ClassLoaderService; @@ -90,7 +91,7 @@ public class ConfiguredClassHierarchyBuilder { List> processedList = new ArrayList>(); for ( List classInfoList : processedClassInfos.values() ) { if ( !processedList.contains( classInfoList ) ) { - hierarchies.add( new ConfiguredClassHierarchy( classInfoList, serviceRegistry ) ); + hierarchies.add( ConfiguredClassHierarchy.create( classInfoList, serviceRegistry ) ); processedList.add( classInfoList ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/JandexHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/util/JandexHelper.java similarity index 61% rename from hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/JandexHelper.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/util/JandexHelper.java index 7694420ae6..06a290c114 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/JandexHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/util/JandexHelper.java @@ -21,15 +21,21 @@ * 51 Franklin Street, Fifth Floor * 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 org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.ClassInfo; import org.jboss.jandex.DotName; +import org.jboss.jandex.Index; +import org.jboss.jandex.Indexer; import org.hibernate.AssertionFailure; +import org.hibernate.HibernateException; +import org.hibernate.service.classloading.spi.ClassLoaderService; /** * Utility methods for working with the jandex annotation index. @@ -48,7 +54,7 @@ public class JandexHelper { * * @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 { List annotationList = classInfo.annotations().get( annotationName ); 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(); + } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ReflectionHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/util/ReflectionHelper.java similarity index 95% rename from hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ReflectionHelper.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/util/ReflectionHelper.java index 3620378ea8..63bce75198 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ReflectionHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/util/ReflectionHelper.java @@ -1,4 +1,4 @@ -package org.hibernate.metamodel.source.annotations; +package org.hibernate.metamodel.source.annotations.util; import java.beans.Introspector; import java.lang.reflect.Field; diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/util/package-info.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/util/package-info.java new file mode 100644 index 0000000000..3f4755c699 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/util/package-info.java @@ -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. + */ \ No newline at end of file diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/OrmXmlParser.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/OrmXmlParser.java similarity index 97% rename from hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/OrmXmlParser.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/OrmXmlParser.java index e424313f48..4685ac45a2 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/OrmXmlParser.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/OrmXmlParser.java @@ -1,4 +1,4 @@ -package org.hibernate.metamodel.source.annotations; +package org.hibernate.metamodel.source.annotations.xml; import java.io.InputStream; import java.util.HashSet; diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/package-info.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/package-info.java new file mode 100644 index 0000000000..5f763a0087 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/package-info.java @@ -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 + */ \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/ConfiguredClassHierarchyBuilderTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/ConfiguredClassHierarchyBuilderTest.java similarity index 84% rename from hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/ConfiguredClassHierarchyBuilderTest.java rename to hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/ConfiguredClassHierarchyBuilderTest.java index 0a492c679f..8d0763b212 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/ConfiguredClassHierarchyBuilderTest.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/ConfiguredClassHierarchyBuilderTest.java @@ -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.Iterator; import java.util.Set; @@ -16,20 +14,20 @@ import javax.persistence.MappedSuperclass; import org.jboss.jandex.ClassInfo; import org.jboss.jandex.DotName; import org.jboss.jandex.Index; -import org.jboss.jandex.Indexer; import org.junit.After; import org.junit.Before; import org.junit.Test; 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.testing.junit4.BaseUnitTestCase; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertTrue; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.fail; /** * @author Hardy Ferentschik @@ -37,10 +35,12 @@ import static org.junit.Assert.fail; public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { private BasicServiceRegistryImpl serviceRegistry; + private ClassLoaderService service; @Before public void setUp() { serviceRegistry = new BasicServiceRegistryImpl( Collections.emptyMap() ); + service = serviceRegistry.getService( ClassLoaderService.class ); } @After @@ -50,7 +50,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { @Test public void testSingleEntity() { - Index index = indexForClass( Foo.class ); + Index index = JandexHelper.indexForClass( service, Foo.class ); Set hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry ); @@ -64,7 +64,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { @Test public void testSimpleInheritance() { - Index index = indexForClass( B.class, A.class ); + Index index = JandexHelper.indexForClass( service, B.class, A.class ); Set hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry ); @@ -80,7 +80,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { @Test public void testMultipleHierarchies() { - Index index = indexForClass( B.class, A.class, Foo.class ); + Index index = JandexHelper.indexForClass( service, B.class, A.class, Foo.class ); Set hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry ); @@ -105,7 +105,9 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { private String mappedProperty; } - Index index = indexForClass( MappedSubClass.class, MappedSuperClass.class, UnmappedSubClass.class ); + Index index = JandexHelper.indexForClass( + service, MappedSubClass.class, MappedSuperClass.class, UnmappedSubClass.class + ); Set hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry ); @@ -128,7 +130,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { class EntityAndMappedSuperClass { } - Index index = indexForClass( EntityAndMappedSuperClass.class ); + Index index = JandexHelper.indexForClass( service, EntityAndMappedSuperClass.class ); ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry ); } @@ -144,7 +146,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { class B extends A { } - Index index = indexForClass( B.class, A.class ); + Index index = JandexHelper.indexForClass( service, B.class, A.class ); ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry ); } @@ -160,7 +162,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { class B extends A { } - Index index = indexForClass( B.class, A.class ); + Index index = JandexHelper.indexForClass( service, B.class, A.class ); Set hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry ); @@ -189,7 +191,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { class B extends A { } - Index index = indexForClass( B.class, A.class ); + Index index = JandexHelper.indexForClass( service, B.class, A.class ); Set hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry ); @@ -210,7 +212,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { class B extends A { } - Index index = indexForClass( B.class, A.class ); + Index index = JandexHelper.indexForClass( service, B.class, A.class ); Set hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry ); @@ -238,7 +240,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { 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 hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry ); @@ -261,26 +263,10 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { class B extends A { } - Index index = indexForClass( B.class, A.class ); + Index index = JandexHelper.indexForClass( service, B.class, A.class ); 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 public class Foo { @Id diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/OrmXmlParserTests.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/xml/OrmXmlParserTests.java similarity index 83% rename from hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/OrmXmlParserTests.java rename to hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/xml/OrmXmlParserTests.java index 71f219680d..158831e4bc 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/OrmXmlParserTests.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/xml/OrmXmlParserTests.java @@ -1,4 +1,4 @@ -package org.hibernate.metamodel.source.annotations; +package org.hibernate.metamodel.source.annotations.xml; import java.util.Collections; import java.util.HashSet; @@ -20,7 +20,7 @@ public class OrmXmlParserTests extends BaseUnitTestCase { MetadataImpl metadata = (MetadataImpl) new MetadataSources( new BasicServiceRegistryImpl( Collections.emptyMap() ) ).buildMetadata(); OrmXmlParser parser = new OrmXmlParser( metadata ); Set xmlFiles = new HashSet(); - xmlFiles.add( "org/hibernate/metamodel/source/annotations/orm.xml" ); + xmlFiles.add( "org/hibernate/metamodel/source/annotations/xml/orm.xml" ); parser.parseAndUpdateIndex( xmlFiles, null ); } @@ -29,7 +29,7 @@ public class OrmXmlParserTests extends BaseUnitTestCase { MetadataImpl metadata = (MetadataImpl) new MetadataSources( new BasicServiceRegistryImpl( Collections.emptyMap() ) ).buildMetadata(); OrmXmlParser parser = new OrmXmlParser( metadata ); Set xmlFiles = new HashSet(); - xmlFiles.add( "org/hibernate/metamodel/source/annotations/orm2.xml" ); + xmlFiles.add( "org/hibernate/metamodel/source/annotations/xml/orm-star.xml" ); parser.parseAndUpdateIndex( xmlFiles, null ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/Star.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/xml/Star.java similarity index 68% rename from hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/Star.java rename to hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/xml/Star.java index 6592f0dea1..2bc24b6cb9 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/Star.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/xml/Star.java @@ -1,4 +1,4 @@ -package org.hibernate.metamodel.source.annotations; +package org.hibernate.metamodel.source.annotations.xml; import javax.persistence.Entity; diff --git a/hibernate-core/src/test/resources/org/hibernate/metamodel/source/annotations/orm2.xml b/hibernate-core/src/test/resources/org/hibernate/metamodel/source/annotations/xml/orm-star.xml similarity index 89% rename from hibernate-core/src/test/resources/org/hibernate/metamodel/source/annotations/orm2.xml rename to hibernate-core/src/test/resources/org/hibernate/metamodel/source/annotations/xml/orm-star.xml index 82bb60b48e..6d367af66a 100644 --- a/hibernate-core/src/test/resources/org/hibernate/metamodel/source/annotations/orm2.xml +++ b/hibernate-core/src/test/resources/org/hibernate/metamodel/source/annotations/xml/orm-star.xml @@ -6,7 +6,7 @@ version="1.0" > - org.hibernate.metamodel.source.annotations + org.hibernate.metamodel.source.annotations.xml diff --git a/hibernate-core/src/test/resources/org/hibernate/metamodel/source/annotations/orm.xml b/hibernate-core/src/test/resources/org/hibernate/metamodel/source/annotations/xml/orm.xml similarity index 100% rename from hibernate-core/src/test/resources/org/hibernate/metamodel/source/annotations/orm.xml rename to hibernate-core/src/test/resources/org/hibernate/metamodel/source/annotations/xml/orm.xml