From d8f6d41a4b9a51aca605921a2e12bfd86768eda5 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Mon, 2 Nov 2009 22:09:31 +0000 Subject: [PATCH] account for inconsistencies in the annotation processoer git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@17894 1b8cb986-b30d-0410-93ca-fae66ebed9b2 --- .../ejb/metamodel/MetadataContext.java | 56 ++++++++++++++----- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/entitymanager/src/main/java/org/hibernate/ejb/metamodel/MetadataContext.java b/entitymanager/src/main/java/org/hibernate/ejb/metamodel/MetadataContext.java index e43ca0d43c..79c4fdfeff 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/metamodel/MetadataContext.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/metamodel/MetadataContext.java @@ -34,10 +34,12 @@ import javax.persistence.metamodel.Attribute; import javax.persistence.metamodel.SingularAttribute; import javax.persistence.metamodel.IdentifiableType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import org.hibernate.mapping.MappedSuperclass; import org.hibernate.mapping.PersistentClass; import org.hibernate.mapping.Property; -import org.hibernate.mapping.Component; import org.hibernate.engine.SessionFactoryImplementor; import org.hibernate.annotations.common.AssertionFailure; @@ -55,6 +57,8 @@ import org.hibernate.annotations.common.AssertionFailure; * @author Emmanuel Bernard */ class MetadataContext { + private static final Logger log = LoggerFactory.getLogger( MetadataContext.class ); + private final SessionFactoryImplementor sessionFactory; private final AttributeFactory attributeFactory = new AttributeFactory( this ); @@ -334,21 +338,45 @@ class MetadataContext { final String name = attribute.getName(); try { Field field = metamodelClass.getDeclaredField( name ); - field.setAccessible( true ); // should be public anyway, but to be sure... - field.set( null, attribute ); + try { + if ( ! field.isAccessible() ) { + // should be public anyway, but to be sure... + field.setAccessible( true ); + } + field.set( null, attribute ); + } + catch ( IllegalAccessException e ) { + // todo : exception type? + throw new AssertionFailure( + "Unable to inject static metamodel attribute : " + metamodelClass.getName() + '#' + name, + e + ); + } + catch ( IllegalArgumentException e ) { + // most likely a mismatch in the type we are injecting and the defined field; this represents a + // mismatch in how the annotation processor interpretted the attribute and how our metamodel + // and/or annotation binder did. +// +// This does seem to be an issue currently for ListAttribute and CollectionAttribute for @OneToMany List +// w/o the @Index definition (which is a bag in Hibernate-terms. So for the time being we simply +// log an error +// throw new AssertionFailure( +// "Illegal argument on static metamodel field injection : " + metamodelClass.getName() + '#' + name +// + "; expected type : " + attribute.getClass().getName() +// + "; encountered type : " + field.getType().getName() +// ); + log.error( + "Illegal argument on static metamodel field injection : " + metamodelClass.getName() + '#' + name + + "; expected type : " + attribute.getClass().getName() + + "; encountered type : " + field.getType().getName() + ); + } } catch ( NoSuchFieldException e ) { - // todo : exception type? - throw new AssertionFailure( - "Unable to locate static metamodel field : " + metamodelClass.getName() + '#' + name - ); - } - catch ( IllegalAccessException e ) { - // todo : exception type? - throw new AssertionFailure( - "Unable to inject static metamodel attribute : " + metamodelClass.getName() + '#' + name, - e - ); + log.error( "Unable to locate static metamodel field : " + metamodelClass.getName() + '#' + name ); +// throw new AssertionFailure( +// "Unable to locate static metamodel field : " + metamodelClass.getName() + '#' + name +// ); } }