HCANN-5 Introdice the MetadataProvider and MetadataProviderInjector facility

ANN-810 Use the new MetadataProvider facility to implement EJB3 overriding

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@16206 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Emmanuel Bernard 2009-03-23 01:53:05 +00:00
parent 996f87451b
commit 1c42279492
13 changed files with 285 additions and 207 deletions

View File

@ -41,47 +41,50 @@ import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.StringTokenizer;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.MappedSuperclass;
import javax.persistence.Embeddable;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.hibernate.AnnotationException;
import org.hibernate.HibernateException;
import org.hibernate.Interceptor;
import org.hibernate.MappingException;
import org.hibernate.SessionFactory;
import org.hibernate.DuplicateMappingException;
import org.hibernate.engine.NamedQueryDefinition;
import org.hibernate.engine.NamedSQLQueryDefinition;
import org.hibernate.engine.ResultSetMappingDefinition;
import org.hibernate.annotations.AnyMetaDef;
import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.cfg.annotations.Version;
import org.hibernate.cfg.annotations.reflection.EJB3ReflectionManager;
import org.hibernate.event.EventListeners;
import org.hibernate.event.PreInsertEventListener;
import org.hibernate.event.PreUpdateEventListener;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Join;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Table;
import org.hibernate.mapping.UniqueKey;
import org.hibernate.mapping.IdGenerator;
import org.hibernate.util.JoinedIterator;
import org.hibernate.util.ReflectHelper;
import org.hibernate.util.StringHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.hibernate.AnnotationException;
import org.hibernate.DuplicateMappingException;
import org.hibernate.HibernateException;
import org.hibernate.Interceptor;
import org.hibernate.MappingException;
import org.hibernate.SessionFactory;
import org.hibernate.annotations.AnyMetaDef;
import org.hibernate.annotations.common.reflection.MetadataProvider;
import org.hibernate.annotations.common.reflection.MetadataProviderInjector;
import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.annotations.common.reflection.java.JavaReflectionManager;
import org.hibernate.cfg.annotations.Version;
import org.hibernate.cfg.annotations.reflection.JPAMetadataProvider;
import org.hibernate.engine.NamedQueryDefinition;
import org.hibernate.engine.NamedSQLQueryDefinition;
import org.hibernate.engine.ResultSetMappingDefinition;
import org.hibernate.event.EventListeners;
import org.hibernate.event.PreInsertEventListener;
import org.hibernate.event.PreUpdateEventListener;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.IdGenerator;
import org.hibernate.mapping.Join;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Table;
import org.hibernate.mapping.UniqueKey;
import org.hibernate.util.JoinedIterator;
import org.hibernate.util.ReflectHelper;
import org.hibernate.util.StringHelper;
/**
* Similar to the {@link Configuration} object but handles EJB3 and Hibernate
* specific annotations as a metadata facility.
@ -250,7 +253,9 @@ public class AnnotationConfiguration extends Configuration {
namingStrategy = EJB3NamingStrategy.INSTANCE;
setEntityResolver( new EJB3DTDEntityResolver() );
anyMetaDefs = new HashMap<String, AnyMetaDef>();
reflectionManager = new EJB3ReflectionManager();
reflectionManager = new JavaReflectionManager();
((MetadataProviderInjector) reflectionManager).setMetadataProvider( new JPAMetadataProvider() );
}
@Override
@ -688,7 +693,9 @@ public class AnnotationConfiguration extends Configuration {
hbmDocuments.add( doc );
}
else {
List<String> classnames = ( (EJB3ReflectionManager) reflectionManager ).getXMLContext().addDocument( doc );
final MetadataProvider metadataProvider = ( ( MetadataProviderInjector ) reflectionManager ).getMetadataProvider();
JPAMetadataProvider jpaMetadataProvider = (JPAMetadataProvider) metadataProvider;
List<String> classnames = jpaMetadataProvider.getXMLContext().addDocument( doc );
for (String classname : classnames) {
try {
annotatedClasses.add( reflectionManager.classForName( classname, this.getClass() ) );

View File

@ -30,7 +30,7 @@ import org.slf4j.LoggerFactory;
* @author Emmanuel Bernard
*/
public class Version {
public static final String VERSION = "3.4.0.GA";
public static final String VERSION = "3.5.0-SNAPSHOT";
private static Logger log = LoggerFactory.getLogger( Version.class );
static {

View File

@ -1,124 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC 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 Middleware LLC.
*
* 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.cfg.annotations.reflection;
import java.lang.reflect.AnnotatedElement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityListeners;
import javax.persistence.NamedNativeQuery;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.TableGenerator;
import org.dom4j.Element;
import org.hibernate.annotations.common.reflection.AnnotationReader;
import org.hibernate.annotations.common.reflection.java.JavaReflectionManager;
import org.hibernate.util.ReflectHelper;
public class EJB3ReflectionManager extends JavaReflectionManager {
private XMLContext xmlContext = new XMLContext();
private HashMap defaults = null;
public AnnotationReader buildAnnotationReader(AnnotatedElement annotatedElement) {
if ( xmlContext.hasContext() ) {
return new EJB3OverridenAnnotationReader( annotatedElement, xmlContext );
}
else {
return super.buildAnnotationReader( annotatedElement );
}
}
public Map getDefaults() {
if ( defaults == null ) {
defaults = new HashMap();
XMLContext.Default xmlDefaults = xmlContext.getDefault( null );
List<Class> entityListeners = new ArrayList<Class>();
for (String className : xmlContext.getDefaultEntityListeners()) {
try {
entityListeners.add( ReflectHelper.classForName( className, this.getClass() ) );
}
catch (ClassNotFoundException e) {
throw new IllegalStateException( "Default entity listener class not found: " + className );
}
}
defaults.put( EntityListeners.class, entityListeners );
for (Element element : xmlContext.getAllDocuments()) {
List<Element> elements = element.elements( "sequence-generator" );
List<SequenceGenerator> sequenceGenerators = (List<SequenceGenerator>) defaults.get( SequenceGenerator.class );
if ( sequenceGenerators == null ) {
sequenceGenerators = new ArrayList<SequenceGenerator>();
defaults.put( SequenceGenerator.class, sequenceGenerators );
}
for (Element subelement : elements) {
sequenceGenerators.add( EJB3OverridenAnnotationReader.buildSequenceGeneratorAnnotation( subelement ) );
}
elements = element.elements( "table-generator" );
List<TableGenerator> tableGenerators = (List<TableGenerator>) defaults.get( TableGenerator.class );
if ( tableGenerators == null ) {
tableGenerators = new ArrayList<TableGenerator>();
defaults.put( TableGenerator.class, tableGenerators );
}
for (Element subelement : elements) {
tableGenerators.add( EJB3OverridenAnnotationReader.buildTableGeneratorAnnotation( subelement, xmlDefaults ) );
}
List<NamedQuery> namedQueries = (List<NamedQuery>) defaults.get( NamedQuery.class );
if ( namedQueries == null ) {
namedQueries = new ArrayList<NamedQuery>();
defaults.put( NamedQuery.class, namedQueries );
}
List<NamedQuery> currentNamedQueries = EJB3OverridenAnnotationReader.buildNamedQueries( element, false, xmlDefaults );
namedQueries.addAll( currentNamedQueries );
List<NamedNativeQuery> namedNativeQueries = (List<NamedNativeQuery>) defaults.get( NamedNativeQuery.class );
if ( namedNativeQueries == null ) {
namedNativeQueries = new ArrayList<NamedNativeQuery>();
defaults.put( NamedNativeQuery.class, namedNativeQueries );
}
List<NamedNativeQuery> currentNamedNativeQueries = EJB3OverridenAnnotationReader.buildNamedQueries( element, true, xmlDefaults );
namedNativeQueries.addAll( currentNamedNativeQueries );
List<SqlResultSetMapping> sqlResultSetMappings = (List<SqlResultSetMapping>) defaults.get( SqlResultSetMapping.class );
if ( sqlResultSetMappings == null ) {
sqlResultSetMappings = new ArrayList<SqlResultSetMapping>();
defaults.put( SqlResultSetMapping.class, sqlResultSetMappings );
}
List<SqlResultSetMapping> currentSqlResultSetMappings = EJB3OverridenAnnotationReader.buildSqlResultsetMappings( element, xmlDefaults );
sqlResultSetMappings.addAll( currentSqlResultSetMappings );
}
}
return defaults;
}
public XMLContext getXMLContext() {
return xmlContext;
}
}

View File

@ -0,0 +1,126 @@
package org.hibernate.cfg.annotations.reflection;
import java.lang.reflect.AnnotatedElement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityListeners;
import javax.persistence.NamedNativeQuery;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.TableGenerator;
import org.dom4j.Element;
import org.hibernate.annotations.common.reflection.AnnotationReader;
import org.hibernate.annotations.common.reflection.MetadataProvider;
import org.hibernate.annotations.common.reflection.java.JavaMetadataProvider;
import org.hibernate.util.ReflectHelper;
/**
* MetadataProvider aware of the JPA Deployment descriptor
*
* @author Emmanuel Bernard
*/
public class JPAMetadataProvider implements MetadataProvider {
private MetadataProvider delegate = new JavaMetadataProvider();
private XMLContext xmlContext = new XMLContext();
private Map<Object, Object> defaults;
private Map<AnnotatedElement, AnnotationReader> cache = new HashMap<AnnotatedElement, AnnotationReader>(100);
public AnnotationReader getAnnotationReader(AnnotatedElement annotatedElement) {
AnnotationReader reader = cache.get( annotatedElement );
if (reader == null) {
if ( xmlContext.hasContext() ) {
reader = new JPAOverridenAnnotationReader( annotatedElement, xmlContext );
}
else {
reader = delegate.getAnnotationReader( annotatedElement );
}
cache.put(annotatedElement, reader);
}
return reader;
}
public Map<Object, Object> getDefaults() {
if ( defaults == null ) {
defaults = new HashMap<Object, Object>();
XMLContext.Default xmlDefaults = xmlContext.getDefault( null );
List<Class> entityListeners = new ArrayList<Class>();
for ( String className : xmlContext.getDefaultEntityListeners() ) {
try {
entityListeners.add( ReflectHelper.classForName( className, this.getClass() ) );
}
catch ( ClassNotFoundException e ) {
throw new IllegalStateException( "Default entity listener class not found: " + className );
}
}
defaults.put( EntityListeners.class, entityListeners );
for ( Element element : xmlContext.getAllDocuments() ) {
List<Element> elements = element.elements( "sequence-generator" );
List<SequenceGenerator> sequenceGenerators = ( List<SequenceGenerator> ) defaults.get( SequenceGenerator.class );
if ( sequenceGenerators == null ) {
sequenceGenerators = new ArrayList<SequenceGenerator>();
defaults.put( SequenceGenerator.class, sequenceGenerators );
}
for ( Element subelement : elements ) {
sequenceGenerators.add( JPAOverridenAnnotationReader.buildSequenceGeneratorAnnotation( subelement ) );
}
elements = element.elements( "table-generator" );
List<TableGenerator> tableGenerators = ( List<TableGenerator> ) defaults.get( TableGenerator.class );
if ( tableGenerators == null ) {
tableGenerators = new ArrayList<TableGenerator>();
defaults.put( TableGenerator.class, tableGenerators );
}
for ( Element subelement : elements ) {
tableGenerators.add(
JPAOverridenAnnotationReader.buildTableGeneratorAnnotation(
subelement, xmlDefaults
)
);
}
List<NamedQuery> namedQueries = ( List<NamedQuery> ) defaults.get( NamedQuery.class );
if ( namedQueries == null ) {
namedQueries = new ArrayList<NamedQuery>();
defaults.put( NamedQuery.class, namedQueries );
}
List<NamedQuery> currentNamedQueries = JPAOverridenAnnotationReader.buildNamedQueries(
element, false, xmlDefaults
);
namedQueries.addAll( currentNamedQueries );
List<NamedNativeQuery> namedNativeQueries = ( List<NamedNativeQuery> ) defaults.get( NamedNativeQuery.class );
if ( namedNativeQueries == null ) {
namedNativeQueries = new ArrayList<NamedNativeQuery>();
defaults.put( NamedNativeQuery.class, namedNativeQueries );
}
List<NamedNativeQuery> currentNamedNativeQueries = JPAOverridenAnnotationReader.buildNamedQueries(
element, true, xmlDefaults
);
namedNativeQueries.addAll( currentNamedNativeQueries );
List<SqlResultSetMapping> sqlResultSetMappings = ( List<SqlResultSetMapping> ) defaults.get(
SqlResultSetMapping.class
);
if ( sqlResultSetMappings == null ) {
sqlResultSetMappings = new ArrayList<SqlResultSetMapping>();
defaults.put( SqlResultSetMapping.class, sqlResultSetMappings );
}
List<SqlResultSetMapping> currentSqlResultSetMappings = JPAOverridenAnnotationReader.buildSqlResultsetMappings(
element, xmlDefaults
);
sqlResultSetMappings.addAll( currentSqlResultSetMappings );
}
}
return defaults;
}
public XMLContext getXMLContext() {
return xmlContext;
}
}

View File

@ -127,8 +127,8 @@ import org.slf4j.LoggerFactory;
* @author Emmanuel Bernard
*/
@SuppressWarnings("unchecked")
public class EJB3OverridenAnnotationReader implements AnnotationReader {
private Logger log = LoggerFactory.getLogger( EJB3OverridenAnnotationReader.class );
public class JPAOverridenAnnotationReader implements AnnotationReader {
private Logger log = LoggerFactory.getLogger( JPAOverridenAnnotationReader.class );
private static final Map<Class, String> annotationToXml;
private static final String SCHEMA_VALIDATION = "Activate schema validation for more informations";
private static final Filter FILTER = new Filter() {
@ -218,7 +218,7 @@ public class EJB3OverridenAnnotationReader implements AnnotationReader {
METHOD
}
public EJB3OverridenAnnotationReader(AnnotatedElement el, XMLContext xmlContext) {
public JPAOverridenAnnotationReader(AnnotatedElement el, XMLContext xmlContext) {
this.element = el;
this.xmlContext = xmlContext;
if ( el instanceof Class ) {
@ -1399,7 +1399,7 @@ public class EJB3OverridenAnnotationReader implements AnnotationReader {
try {
clazz = ReflectHelper.classForName(
XMLContext.buildSafeClassName( clazzName, defaults ),
EJB3OverridenAnnotationReader.class
JPAOverridenAnnotationReader.class
);
}
catch (ClassNotFoundException e) {
@ -1436,7 +1436,7 @@ public class EJB3OverridenAnnotationReader implements AnnotationReader {
try {
clazz = ReflectHelper.classForName(
XMLContext.buildSafeClassName( clazzName, defaults ),
EJB3OverridenAnnotationReader.class
JPAOverridenAnnotationReader.class
);
}
catch (ClassNotFoundException e) {
@ -1572,7 +1572,7 @@ public class EJB3OverridenAnnotationReader implements AnnotationReader {
try {
clazz = ReflectHelper.classForName(
XMLContext.buildSafeClassName( clazzName, defaults ),
EJB3OverridenAnnotationReader.class
JPAOverridenAnnotationReader.class
);
}
catch (ClassNotFoundException e) {

View File

@ -61,7 +61,7 @@ import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.hibernate.annotations.Columns;
import org.hibernate.cfg.EJB3DTDEntityResolver;
import org.hibernate.cfg.annotations.reflection.EJB3OverridenAnnotationReader;
import org.hibernate.cfg.annotations.reflection.JPAOverridenAnnotationReader;
import org.hibernate.cfg.annotations.reflection.XMLContext;
import org.hibernate.util.XMLHelper;
import org.xml.sax.InputSource;
@ -72,17 +72,17 @@ import org.xml.sax.SAXNotSupportedException;
/**
* @author Emmanuel Bernard
*/
public class EJB3OverridenAnnotationReaderTest extends TestCase {
public class JPAOverridenAnnotationReaderTest extends TestCase {
public void testMappedSuperclassAnnotations() throws Exception {
XMLContext context = buildContext(
"org/hibernate/test/annotations/reflection/metadata-complete.xml" );
EJB3OverridenAnnotationReader reader = new EJB3OverridenAnnotationReader( Organization.class, context );
JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( Organization.class, context );
assertTrue( reader.isAnnotationPresent( MappedSuperclass.class ) );
}
public void testEntityRelatedAnnotations() throws Exception {
XMLContext context = buildContext( "org/hibernate/test/annotations/reflection/orm.xml" );
EJB3OverridenAnnotationReader reader = new EJB3OverridenAnnotationReader( Administration.class, context );
JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( Administration.class, context );
assertNotNull( reader.getAnnotation( Entity.class ) );
assertEquals(
"Default value in xml entity should not override @Entity.name", "JavaAdministration",
@ -116,7 +116,7 @@ public class EJB3OverridenAnnotationReaderTest extends TestCase {
assertEquals( "wrong tble name", "tablehilo", reader.getAnnotation( TableGenerator.class ).table() );
assertEquals( "no schema overriding", "myschema", reader.getAnnotation( TableGenerator.class ).schema() );
reader = new EJB3OverridenAnnotationReader( Match.class, context );
reader = new JPAOverridenAnnotationReader( Match.class, context );
assertNotNull( reader.getAnnotation( Table.class ) );
assertEquals(
"Java annotation not taken into account", "matchtable", reader.getAnnotation( Table.class ).name()
@ -164,10 +164,10 @@ public class EJB3OverridenAnnotationReaderTest extends TestCase {
assertNotNull( reader.getAnnotation( ExcludeSuperclassListeners.class ) );
assertNotNull( reader.getAnnotation( ExcludeDefaultListeners.class ) );
reader = new EJB3OverridenAnnotationReader( Competition.class, context );
reader = new JPAOverridenAnnotationReader( Competition.class, context );
assertNotNull( reader.getAnnotation( MappedSuperclass.class ) );
reader = new EJB3OverridenAnnotationReader( TennisMatch.class, context );
reader = new JPAOverridenAnnotationReader( TennisMatch.class, context );
assertNull( "Mutualize PKJC into PKJCs", reader.getAnnotation( PrimaryKeyJoinColumn.class ) );
assertNotNull( reader.getAnnotation( PrimaryKeyJoinColumns.class ) );
assertEquals(
@ -194,7 +194,7 @@ public class EJB3OverridenAnnotationReaderTest extends TestCase {
);
reader = new EJB3OverridenAnnotationReader( SocialSecurityPhysicalAccount.class, context );
reader = new JPAOverridenAnnotationReader( SocialSecurityPhysicalAccount.class, context );
assertNotNull( reader.getAnnotation( IdClass.class ) );
assertEquals( "id-class not used", SocialSecurityNumber.class, reader.getAnnotation( IdClass.class ).value() );
assertEquals(
@ -213,7 +213,7 @@ public class EJB3OverridenAnnotationReaderTest extends TestCase {
public void testEntityRelatedAnnotationsMetadataComplete() throws Exception {
XMLContext context = buildContext(
"org/hibernate/test/annotations/reflection/metadata-complete.xml" );
EJB3OverridenAnnotationReader reader = new EJB3OverridenAnnotationReader( Administration.class, context );
JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( Administration.class, context );
assertNotNull( reader.getAnnotation( Entity.class ) );
assertEquals(
"Metadata complete should ignore java annotations", "", reader.getAnnotation( Entity.class ).name()
@ -222,7 +222,7 @@ public class EJB3OverridenAnnotationReaderTest extends TestCase {
assertEquals( "@Table should not be used", "", reader.getAnnotation( Table.class ).name() );
assertEquals( "Default schema not overriden", "myschema", reader.getAnnotation( Table.class ).schema() );
reader = new EJB3OverridenAnnotationReader( Match.class, context );
reader = new JPAOverridenAnnotationReader( Match.class, context );
assertNotNull( reader.getAnnotation( Table.class ) );
assertEquals( "@Table should not be used", "", reader.getAnnotation( Table.class ).name() );
assertEquals( "Overriding not taken into account", "myschema", reader.getAnnotation( Table.class ).schema() );
@ -233,14 +233,14 @@ public class EJB3OverridenAnnotationReaderTest extends TestCase {
assertNull( reader.getAnnotation( NamedQueries.class ) );
assertNull( reader.getAnnotation( NamedNativeQueries.class ) );
reader = new EJB3OverridenAnnotationReader( TennisMatch.class, context );
reader = new JPAOverridenAnnotationReader( TennisMatch.class, context );
assertNull( reader.getAnnotation( PrimaryKeyJoinColumn.class ) );
assertNull( reader.getAnnotation( PrimaryKeyJoinColumns.class ) );
reader = new EJB3OverridenAnnotationReader( Competition.class, context );
reader = new JPAOverridenAnnotationReader( Competition.class, context );
assertNull( reader.getAnnotation( MappedSuperclass.class ) );
reader = new EJB3OverridenAnnotationReader( SocialSecurityMoralAccount.class, context );
reader = new JPAOverridenAnnotationReader( SocialSecurityMoralAccount.class, context );
assertNull( reader.getAnnotation( IdClass.class ) );
assertNull( reader.getAnnotation( DiscriminatorValue.class ) );
assertNull( reader.getAnnotation( DiscriminatorColumn.class ) );
@ -251,11 +251,11 @@ public class EJB3OverridenAnnotationReaderTest extends TestCase {
public void testIdRelatedAnnotations() throws Exception {
XMLContext context = buildContext( "org/hibernate/test/annotations/reflection/orm.xml" );
Method method = Administration.class.getDeclaredMethod( "getId" );
EJB3OverridenAnnotationReader reader = new EJB3OverridenAnnotationReader( method, context );
JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( method, context );
assertNull( reader.getAnnotation( Id.class ) );
assertNull( reader.getAnnotation( Column.class ) );
Field field = Administration.class.getDeclaredField( "id" );
reader = new EJB3OverridenAnnotationReader( field, context );
reader = new JPAOverridenAnnotationReader( field, context );
assertNotNull( reader.getAnnotation( Id.class ) );
assertNotNull( reader.getAnnotation( GeneratedValue.class ) );
assertEquals( GenerationType.SEQUENCE, reader.getAnnotation( GeneratedValue.class ).strategy() );
@ -271,23 +271,23 @@ public class EJB3OverridenAnnotationReaderTest extends TestCase {
context = buildContext(
"org/hibernate/test/annotations/reflection/metadata-complete.xml" );
method = Administration.class.getDeclaredMethod( "getId" );
reader = new EJB3OverridenAnnotationReader( method, context );
reader = new JPAOverridenAnnotationReader( method, context );
assertNotNull(
"Default access type when not defined in metadata complete should be property",
reader.getAnnotation( Id.class )
);
field = Administration.class.getDeclaredField( "id" );
reader = new EJB3OverridenAnnotationReader( field, context );
reader = new JPAOverridenAnnotationReader( field, context );
assertNull(
"Default access type when not defined in metadata complete should be property",
reader.getAnnotation( Id.class )
);
method = BusTrip.class.getDeclaredMethod( "getId" );
reader = new EJB3OverridenAnnotationReader( method, context );
reader = new JPAOverridenAnnotationReader( method, context );
assertNull( reader.getAnnotation( EmbeddedId.class ) );
field = BusTrip.class.getDeclaredField( "id" );
reader = new EJB3OverridenAnnotationReader( field, context );
reader = new JPAOverridenAnnotationReader( field, context );
assertNotNull( reader.getAnnotation( EmbeddedId.class ) );
assertNotNull( reader.getAnnotation( AttributeOverrides.class ) );
assertEquals( 1, reader.getAnnotation( AttributeOverrides.class ).value().length );
@ -297,33 +297,33 @@ public class EJB3OverridenAnnotationReaderTest extends TestCase {
XMLContext context = buildContext(
"org/hibernate/test/annotations/reflection/metadata-complete.xml" );
Field field = BusTrip.class.getDeclaredField( "status" );
EJB3OverridenAnnotationReader reader = new EJB3OverridenAnnotationReader( field, context );
JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( field, context );
assertNotNull( reader.getAnnotation( Enumerated.class ) );
assertEquals( EnumType.STRING, reader.getAnnotation( Enumerated.class ).value() );
assertEquals( false, reader.getAnnotation( Basic.class ).optional() );
field = BusTrip.class.getDeclaredField( "serial" );
reader = new EJB3OverridenAnnotationReader( field, context );
reader = new JPAOverridenAnnotationReader( field, context );
assertNotNull( reader.getAnnotation( Lob.class ) );
assertEquals( "serialbytes", reader.getAnnotation( Columns.class ).columns()[0].name() );
field = BusTrip.class.getDeclaredField( "terminusTime" );
reader = new EJB3OverridenAnnotationReader( field, context );
reader = new JPAOverridenAnnotationReader( field, context );
assertNotNull( reader.getAnnotation( Temporal.class ) );
assertEquals( TemporalType.TIMESTAMP, reader.getAnnotation( Temporal.class ).value() );
assertEquals( FetchType.LAZY, reader.getAnnotation( Basic.class ).fetch() );
field = BusTripPk.class.getDeclaredField( "busDriver" );
reader = new EJB3OverridenAnnotationReader( field, context );
reader = new JPAOverridenAnnotationReader( field, context );
assertNotNull( reader.isAnnotationPresent( Basic.class ) );
}
public void testVersionRelatedAnnotations() throws Exception {
XMLContext context = buildContext( "org/hibernate/test/annotations/reflection/orm.xml" );
Method method = Administration.class.getDeclaredMethod( "getVersion" );
EJB3OverridenAnnotationReader reader = new EJB3OverridenAnnotationReader( method, context );
JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( method, context );
assertNotNull( reader.getAnnotation( Version.class ) );
Field field = Match.class.getDeclaredField( "version" );
reader = new EJB3OverridenAnnotationReader( field, context );
reader = new JPAOverridenAnnotationReader( field, context );
assertNotNull( reader.getAnnotation( Version.class ) );
}
@ -331,12 +331,12 @@ public class EJB3OverridenAnnotationReaderTest extends TestCase {
XMLContext context = buildContext( "org/hibernate/test/annotations/reflection/orm.xml" );
Field field = Administration.class.getDeclaredField( "transientField" );
EJB3OverridenAnnotationReader reader = new EJB3OverridenAnnotationReader( field, context );
JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( field, context );
assertNotNull( reader.getAnnotation( Transient.class ) );
assertNull( reader.getAnnotation( Basic.class ) );
field = Match.class.getDeclaredField( "playerASSN" );
reader = new EJB3OverridenAnnotationReader( field, context );
reader = new JPAOverridenAnnotationReader( field, context );
assertNotNull( reader.getAnnotation( Embedded.class ) );
}
@ -344,7 +344,7 @@ public class EJB3OverridenAnnotationReaderTest extends TestCase {
XMLContext context = buildContext( "org/hibernate/test/annotations/reflection/orm.xml" );
Field field = Administration.class.getDeclaredField( "defaultBusTrip" );
EJB3OverridenAnnotationReader reader = new EJB3OverridenAnnotationReader( field, context );
JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( field, context );
assertNotNull( reader.getAnnotation( OneToOne.class ) );
assertNull( reader.getAnnotation( JoinColumns.class ) );
assertNotNull( reader.getAnnotation( PrimaryKeyJoinColumns.class ) );
@ -356,7 +356,7 @@ public class EJB3OverridenAnnotationReaderTest extends TestCase {
context = buildContext(
"org/hibernate/test/annotations/reflection/metadata-complete.xml" );
field = BusTrip.class.getDeclaredField( "players" );
reader = new EJB3OverridenAnnotationReader( field, context );
reader = new JPAOverridenAnnotationReader( field, context );
assertNotNull( reader.getAnnotation( OneToMany.class ) );
assertNotNull( reader.getAnnotation( JoinColumns.class ) );
assertEquals( 2, reader.getAnnotation( JoinColumns.class ).value().length );
@ -365,7 +365,7 @@ public class EJB3OverridenAnnotationReaderTest extends TestCase {
assertEquals( "name", reader.getAnnotation( MapKey.class ).name() );
field = BusTrip.class.getDeclaredField( "roads" );
reader = new EJB3OverridenAnnotationReader( field, context );
reader = new JPAOverridenAnnotationReader( field, context );
assertNotNull( reader.getAnnotation( ManyToMany.class ) );
assertNotNull( reader.getAnnotation( JoinTable.class ) );
assertEquals( "bus_road", reader.getAnnotation( JoinTable.class ).name() );
@ -380,20 +380,20 @@ public class EJB3OverridenAnnotationReaderTest extends TestCase {
XMLContext context = buildContext( "org/hibernate/test/annotations/reflection/orm.xml" );
Method method = Administration.class.getDeclaredMethod( "calculate" );
EJB3OverridenAnnotationReader reader = new EJB3OverridenAnnotationReader( method, context );
JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( method, context );
assertTrue( reader.isAnnotationPresent( PrePersist.class ) );
reader = new EJB3OverridenAnnotationReader( Administration.class, context );
reader = new JPAOverridenAnnotationReader( Administration.class, context );
assertTrue( reader.isAnnotationPresent( EntityListeners.class ) );
assertEquals( 1, reader.getAnnotation( EntityListeners.class ).value().length );
assertEquals( LogListener.class, reader.getAnnotation( EntityListeners.class ).value()[0] );
method = LogListener.class.getDeclaredMethod( "noLog", Object.class );
reader = new EJB3OverridenAnnotationReader( method, context );
reader = new JPAOverridenAnnotationReader( method, context );
assertTrue( reader.isAnnotationPresent( PostLoad.class ) );
method = LogListener.class.getDeclaredMethod( "log", Object.class );
reader = new EJB3OverridenAnnotationReader( method, context );
reader = new JPAOverridenAnnotationReader( method, context );
assertTrue( reader.isAnnotationPresent( PrePersist.class ) );
assertFalse( reader.isAnnotationPresent( PostPersist.class ) );

View File

@ -30,7 +30,7 @@ import org.slf4j.LoggerFactory;
* @author Emmanuel Bernard
*/
public class Version {
public static final String VERSION = "3.1.0.GA";
public static final String VERSION = "3.2.0-SNAPSHOT";
private static Logger log = LoggerFactory.getLogger( Version.class );
static {

View File

@ -0,0 +1,22 @@
package org.hibernate.annotations.common.reflection;
import java.util.Map;
import java.lang.reflect.AnnotatedElement;
/**
* Provides metadata
*
* @author Emmanuel Bernard
*/
public interface MetadataProvider {
/**
* provide default metadata
*/
Map<Object, Object> getDefaults();
/**
* provide metadata for a gien annotated element
*/
AnnotationReader getAnnotationReader(AnnotatedElement annotatedElement);
}

View File

@ -0,0 +1,15 @@
package org.hibernate.annotations.common.reflection;
/**
* Offers access to and the ability to change the metadata provider
*
* @author Emmanuel Bernard
*/
public interface MetadataProviderInjector {
MetadataProvider getMetadataProvider();
/**
* Defines the metadata provider for a given Reflection Manager
*/
void setMetadataProvider(MetadataProvider metadataProvider);
}

View File

@ -0,0 +1,22 @@
package org.hibernate.annotations.common.reflection.java;
import java.util.Map;
import java.util.Collections;
import java.lang.reflect.AnnotatedElement;
import org.hibernate.annotations.common.reflection.MetadataProvider;
import org.hibernate.annotations.common.reflection.AnnotationReader;
/**
* @author Emmanuel Bernard
*/
public class JavaMetadataProvider implements MetadataProvider {
public Map<Object, Object> getDefaults() {
return Collections.emptyMap();
}
public AnnotationReader getAnnotationReader(AnnotatedElement annotatedElement) {
return new JavaAnnotationReader(annotatedElement);
}
}

View File

@ -37,6 +37,8 @@ import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.annotations.common.reflection.XMethod;
import org.hibernate.annotations.common.reflection.XPackage;
import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.annotations.common.reflection.MetadataProviderInjector;
import org.hibernate.annotations.common.reflection.MetadataProvider;
import org.hibernate.annotations.common.reflection.java.generics.IdentityTypeEnvironment;
import org.hibernate.annotations.common.reflection.java.generics.TypeEnvironment;
import org.hibernate.annotations.common.reflection.java.generics.TypeEnvironmentFactory;
@ -52,7 +54,20 @@ import org.hibernate.annotations.common.Version;
* @author Davide Marchignoli
* @author Emmanuel Bernard
*/
public class JavaReflectionManager implements ReflectionManager {
public class JavaReflectionManager implements ReflectionManager, MetadataProviderInjector {
private MetadataProvider metadataProvider;
public MetadataProvider getMetadataProvider() {
if (metadataProvider == null) {
setMetadataProvider( new JavaMetadataProvider() );
}
return metadataProvider;
}
public void setMetadataProvider(MetadataProvider metadataProvider) {
this.metadataProvider = metadataProvider;
}
static {
Version.touch();
@ -204,10 +219,11 @@ public class JavaReflectionManager implements ReflectionManager {
}
public AnnotationReader buildAnnotationReader(AnnotatedElement annotatedElement) {
return new JavaAnnotationReader(annotatedElement);
return getMetadataProvider().getAnnotationReader( annotatedElement );
}
public Map getDefaults() {
return new HashMap();
return getMetadataProvider().getDefaults();
}
}

View File

@ -35,9 +35,6 @@ import org.hibernate.annotations.common.reflection.XAnnotatedElement;
*/
abstract class JavaXAnnotatedElement implements XAnnotatedElement {
// responsible for extracting annotations
private AnnotationReader annotationReader;
private final JavaReflectionManager factory;
private final AnnotatedElement annotatedElement;
@ -52,10 +49,7 @@ abstract class JavaXAnnotatedElement implements XAnnotatedElement {
}
private AnnotationReader getAnnotationReader() {
if (annotationReader == null) {
annotationReader = factory.buildAnnotationReader(annotatedElement);
}
return annotationReader;
return factory.buildAnnotationReader(annotatedElement);
}
public <T extends Annotation> T getAnnotation(Class<T> annotationType) {

View File

@ -9,7 +9,7 @@ import org.slf4j.LoggerFactory;
* @author Emmanuel Bernard
*/
public class Version {
public static final String VERSION = "3.4.0.GA";
public static final String VERSION = "3.5.0-SNAPSHOT";
private static final Logger log = LoggerFactory.getLogger( Version.class );
static {