Author: epbernard
Date: 2006-10-24 21:18:29 -0400 (Tue, 24 Oct 2006)
New Revision: 10650
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/annotationfactory/AnnotationFactory.java
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/AnnotationConfiguration.java
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3OverridenAnnotationReader.java
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/JavaXAnnotatedElement.java
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/JavaXClass.java
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/JavaXFactory.java
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/xml/XMLContext.java
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/ClassValidator.java
Log:
Various optimizations, mostly in the XLayer
ANN-475 EJBOverridenAnnotationReader is used only if there is an XMLContext
ANN-456 Done and used for the ClassValidator used for apply() in AnnotationConfiguration
ANN-474
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/annotationfactory/AnnotationFactory.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/annotationfactory/AnnotationFactory.java 2006-10-24
22:55:36 UTC (rev 10649)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/annotationfactory/AnnotationFactory.java 2006-10-25
01:18:29 UTC (rev 10650)
@@ -18,7 +18,9 @@
@SuppressWarnings("unchecked")
public static <T extends Annotation> T create(AnnotationDescriptor descriptor) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
- Class<T> proxyClass = (Class<T>) Proxy.getProxyClass( classLoader,
descriptor.type() );
+ //TODO round 34ms to generate the proxy, hug! is Javassist Faster?
+ //TODO prebuild the javax.persistence and org.hibernate.annotations classes?
+ Class<T> proxyClass = (Class<T>) Proxy.getProxyClass( classLoader,
descriptor.type() );
InvocationHandler handler = new AnnotationProxy( descriptor );
try {
return getProxyInstance( proxyClass, handler );
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/AnnotationConfiguration.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/AnnotationConfiguration.java 2006-10-24
22:55:36 UTC (rev 10649)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/AnnotationConfiguration.java 2006-10-25
01:18:29 UTC (rev 10650)
@@ -312,11 +312,11 @@
}
for ( PersistentClass persistentClazz : (Collection<PersistentClass>)
classes.values() ) {
//integrate the validate framework
- // TODO: migrate the Validator to the X layer
String className = persistentClazz.getClassName();
if ( StringHelper.isNotEmpty( className ) ) {
try {
- new ClassValidator( ReflectHelper.classForName( className ) ).apply( persistentClazz
);
+ new ClassValidator( ReflectHelper.classForName( className ), null, null, null,
reflectionManager )
+ .apply( persistentClazz );
}
catch (ClassNotFoundException e) {
//swallow them
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3OverridenAnnotationReader.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3OverridenAnnotationReader.java 2006-10-24
22:55:36 UTC (rev 10649)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3OverridenAnnotationReader.java 2006-10-25
01:18:29 UTC (rev 10650)
@@ -354,9 +354,9 @@
getAssociation( OneToOne.class, annotationList, defaults );
getAssociation( OneToMany.class, annotationList, defaults );
getAssociation( ManyToMany.class, annotationList, defaults );
- Annotation current = getSequenceGenerator( tree, defaults );
+ Annotation current = getSequenceGenerator( elementsForProperty, defaults );
if ( current != null ) annotationList.add( current );
- current = getTableGenerator( tree, defaults );
+ current = getTableGenerator( elementsForProperty, defaults );
if ( current != null ) annotationList.add( current );
}
@@ -376,7 +376,39 @@
}
}
- private void processEventAnnotations(List<Annotation> annotationList,
XMLContext.Default defaults) {
+ //TODO mutualize the next 2 methods
+ private Annotation getTableGenerator(List<Element> elementsForProperty,
XMLContext.Default defaults) {
+ for (Element element : elementsForProperty) {
+ Element subelement = element != null ? element.element( annotationToXml.get(
TableGenerator.class ) ) : null;
+ if ( subelement != null ) {
+ return buildTableGeneratorAnnotation( subelement, defaults );
+ }
+ }
+ if ( elementsForProperty.size() == 0 && defaults.canUseJavaAnnotations()
) {
+ return super.getAnnotation( TableGenerator.class );
+ }
+ else {
+ return null;
+ }
+ }
+
+
+ private Annotation getSequenceGenerator(List<Element> elementsForProperty,
XMLContext.Default defaults) {
+ for (Element element : elementsForProperty) {
+ Element subelement = element != null ? element.element( annotationToXml.get(
SequenceGenerator.class ) ) : null;
+ if ( subelement != null ) {
+ return buildSequenceGeneratorAnnotation( subelement );
+ }
+ }
+ if ( elementsForProperty.size() == 0 && defaults.canUseJavaAnnotations()
) {
+ return super.getAnnotation( SequenceGenerator.class );
+ }
+ else {
+ return null;
+ }
+ }
+
+ private void processEventAnnotations(List<Annotation> annotationList,
XMLContext.Default defaults) {
boolean eventElement = false;
for ( Element element : elementsForProperty ) {
String elementName = element.getName();
@@ -546,7 +578,7 @@
AnnotationDescriptor ad = new AnnotationDescriptor( annotationType );
String className = element.attributeValue( "target-entity" );
if ( className != null ) {
- Class clazz = null;
+ Class clazz;
try {
clazz = ReflectHelper.classForName(
XMLContext.buildSafeClassName( className, defaults ),
@@ -1358,7 +1390,8 @@
}
private NamedQueries getNamedQueries(Element tree, XMLContext.Default defaults) {
- List<NamedQuery> queries = (List<NamedQuery>) buildNamedQueries( tree,
false, defaults );
+ //TODO avoid the Proxy Creation (@NamedQueries) when possible
+ List<NamedQuery> queries = (List<NamedQuery>) buildNamedQueries(
tree, false, defaults );
if ( defaults.canUseJavaAnnotations() ) {
NamedQuery annotation = super.getAnnotation( NamedQuery.class );
addNamedQueryIfNeeded( annotation, queries );
@@ -1435,7 +1468,6 @@
element.elements( "named-native-query" ) :
element.elements( "named-query" );
List namedQueries = new ArrayList();
- int index = 0;
Iterator it = namedQueryElementList.listIterator();
while ( it.hasNext() ) {
Element subelement = (Element) it.next();
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/JavaXAnnotatedElement.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/JavaXAnnotatedElement.java 2006-10-24
22:55:36 UTC (rev 10649)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/JavaXAnnotatedElement.java 2006-10-25
01:18:29 UTC (rev 10650)
@@ -19,8 +19,8 @@
private final AnnotatedElement annotatedElement;
public JavaXAnnotatedElement(AnnotatedElement annotatedElement, JavaXFactory factory) {
- this.annotationReader = new EJB3OverridenAnnotationReader( annotatedElement,
factory.getXMLContext() );
- this.factory = factory;
+ //this.annotationReader = new EJB3OverridenAnnotationReader( annotatedElement,
factory.getXMLContext() );
+ this.factory = factory;
this.annotatedElement = annotatedElement;
}
@@ -29,19 +29,22 @@
}
private JavaAnnotationReader getAnnotationReader() {
- return annotationReader;
+ if (annotationReader == null) {
+ annotationReader = factory.buildAnnotationReader(annotatedElement);
+ }
+ return annotationReader;
}
public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
- return annotationReader.getAnnotation( annotationType );
+ return getAnnotationReader().getAnnotation( annotationType );
}
public <T extends Annotation> boolean isAnnotationPresent(Class<T>
annotationType) {
- return annotationReader.isAnnotationPresent( annotationType );
+ return getAnnotationReader().isAnnotationPresent( annotationType );
}
public Annotation[] getAnnotations() {
- return annotationReader.getAnnotations();
+ return getAnnotationReader().getAnnotations();
}
AnnotatedElement toAnnotatedElement() {
@@ -50,7 +53,7 @@
@Override
public boolean equals(Object obj) {
- if ( obj == null || ! ( obj instanceof JavaXAnnotatedElement ) ) return false;
+ if ( ! ( obj instanceof JavaXAnnotatedElement ) ) return false;
JavaXAnnotatedElement other = (JavaXAnnotatedElement) obj;
//FIXME yuk this defeat the type environment
return annotatedElement.equals( other.toAnnotatedElement() );
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/JavaXClass.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/JavaXClass.java 2006-10-24
22:55:36 UTC (rev 10649)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/JavaXClass.java 2006-10-25
01:18:29 UTC (rev 10650)
@@ -45,12 +45,14 @@
int length = classes.length;
XClass[] xClasses = new XClass[length];
//TypeEnvironment environment = getFactory().getTypeEnvironment( toClass() );
- TypeEnvironment environment =
- new CompoundTypeEnvironment( getTypeEnvironment(), getFactory().getTypeEnvironment(
toClass() ) );
- for ( int index = 0; index < length ; index++ ) {
- xClasses[index] = getFactory().toXClass( classes[index], environment );
- }
- return xClasses;
+ if (length != 0) {
+ TypeEnvironment environment =
+ new CompoundTypeEnvironment( getTypeEnvironment(),
getFactory().getTypeEnvironment( toClass() ) );
+ for ( int index = 0; index < length ; index++ ) {
+ xClasses[index] = getFactory().toXClass( classes[index], environment );
+ }
+ }
+ return xClasses;
}
public boolean isInterface() {
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/JavaXFactory.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/JavaXFactory.java 2006-10-24
22:55:36 UTC (rev 10649)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/JavaXFactory.java 2006-10-25
01:18:29 UTC (rev 10650)
@@ -4,6 +4,9 @@
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -28,6 +31,7 @@
import org.hibernate.reflection.java.generics.TypeUtils;
import org.hibernate.reflection.java.xml.XMLContext;
import org.hibernate.util.ReflectHelper;
+import org.hibernate.AssertionFailure;
/**
* The factory for all the objects in this package.
@@ -40,8 +44,59 @@
private XMLContext xmlContext;
private HashMap defaults;
+ private static Constructor[] annotationReaderConstructors;
- private static class TypeKey extends Pair<Type, TypeEnvironment> {
+ static {
+ buildAnnotationReaderConstructor();
+ }
+
+
+ public JavaAnnotationReader buildAnnotationReader(AnnotatedElement annotatedElement)
{
+ try {
+ if ( xmlContext.hasContext() ) {
+ return (JavaAnnotationReader)
annotationReaderConstructors[1].newInstance(annotatedElement, xmlContext);
+ }
+ else {
+ return (JavaAnnotationReader)
annotationReaderConstructors[0].newInstance(annotatedElement);
+ }
+ }
+ catch (IllegalAccessException e) {
+ throw new AssertionFailure( "Unable to call AnnotationReader
constructor", e );
+ }
+ catch (InvocationTargetException e) {
+ throw new AssertionFailure( "Unable to call AnnotationReader
constructor", e );
+ }
+ catch (InstantiationException e) {
+ throw new AssertionFailure( "Unable to call AnnotationReader
constructor", e );
+ }
+ }
+
+ private static void buildAnnotationReaderConstructor() {
+ annotationReaderConstructors = new Constructor[2];
+ try {
+ Class readerClass = ReflectHelper.classForName(
"org.hibernate.reflection.java.JavaAnnotationReader");
+ annotationReaderConstructors[0] = readerClass.getConstructor(
AnnotatedElement.class );
+ }
+ catch (ClassNotFoundException e) {
+ throw new AssertionFailure( "Unable to load
org.hibernate.reflection.java.JavaAnnotationReader", e );
+ }
+ catch (NoSuchMethodException e) {
+ throw new AssertionFailure( "Unable to call constructor
org.hibernate.reflection.java.JavaAnnotationReader(AnnotatedElement)", e );
+ }
+
+ try {
+ Class readerClass = ReflectHelper.classForName(
"org.hibernate.reflection.java.EJB3OverridenAnnotationReader");
+ annotationReaderConstructors[1] = readerClass.getConstructor(
AnnotatedElement.class, XMLContext.class );
+ }
+ catch (ClassNotFoundException e) {
+ throw new AssertionFailure( "Unable to load
org.hibernate.reflection.java.EJB3OverridenAnnotationReader", e );
+ }
+ catch (NoSuchMethodException e) {
+ throw new AssertionFailure( "Unable to call constructor
org.hibernate.reflection.java.EJB3OverridenAnnotationReader(AnnotatedElement,
XMLContext)", e );
+ }
+ }
+
+ private static class TypeKey extends Pair<Type, TypeEnvironment> {
TypeKey(Type t, TypeEnvironment context) {
super( t, context );
}
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/xml/XMLContext.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/xml/XMLContext.java 2006-10-24
22:55:36 UTC (rev 10649)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/xml/XMLContext.java 2006-10-25
01:18:29 UTC (rev 10650)
@@ -22,12 +22,14 @@
private Map<String, Default> defaultsOverriding = new HashMap<String,
Default>();
private List<Element> defaultElements = new ArrayList<Element>();
private List<String> defaultEntityListeners = new ArrayList<String>();
+ private boolean hasContext = false;
- /**
+ /**
* Add a document and return the list of added classes names
*/
public List<String> addDocument(Document doc) {
- List<String> addedClasses = new ArrayList<String>();
+ hasContext = true;
+ List<String> addedClasses = new ArrayList<String>();
Element root = doc.getRootElement();
//global defaults
Element metadata = root.element( "persistence-unit-metadata" );
@@ -163,7 +165,11 @@
return defaultElements;
}
- public static class Default {
+ public boolean hasContext() {
+ return hasContext;
+ }
+
+ public static class Default {
private String access;
private String packageName;
private String schema;
@@ -213,7 +219,7 @@
}
public boolean canUseJavaAnnotations() {
- return ! Boolean.TRUE.equals( metadataComplete );
+ return metadataComplete == null || ! metadataComplete.booleanValue();
}
protected void setMetadataComplete(Boolean metadataComplete) {
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/ClassValidator.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/ClassValidator.java 2006-10-24
22:55:36 UTC (rev 10649)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/ClassValidator.java 2006-10-25
01:18:29 UTC (rev 10650)
@@ -71,7 +71,7 @@
private transient ResourceBundle messageBundle;
private transient ResourceBundle defaultMessageBundle;
private transient boolean isUserProvidedResourceBundle;
- private transient ReflectionManager reflectionManager = new JavaXFactory();
+ private transient ReflectionManager reflectionManager;
private final transient Map<XClass, ClassValidator> childClassValidators;
private transient List<Validator> beanValidators;
@@ -102,7 +102,7 @@
* for message rendering on violation
*/
public ClassValidator(Class<T> beanClass, ResourceBundle resourceBundle) {
- this( beanClass, resourceBundle, null, new HashMap<XClass, ClassValidator>() );
+ this( beanClass, resourceBundle, null, new HashMap<XClass, ClassValidator>(),
null );
}
/**
@@ -110,27 +110,34 @@
* for message rendering on violation
*/
public ClassValidator(Class<T> beanClass, MessageInterpolator interpolator) {
- this( beanClass, null, interpolator, new HashMap<XClass, ClassValidator>() );
+ this( beanClass, null, interpolator, new HashMap<XClass, ClassValidator>(), null
);
}
- protected ClassValidator(
- Class<T> beanClass, ResourceBundle resourceBundle, MessageInterpolator
interpolator, Map<XClass, ClassValidator> childClassValidators
- ) {
- XClass beanXClass = reflectionManager.toXClass( beanClass );
+ /**
+ * Not a public API
+ */
+ public ClassValidator(
+ Class<T> beanClass, ResourceBundle resourceBundle, MessageInterpolator
interpolator,
+ Map<XClass, ClassValidator> childClassValidators, ReflectionManager
reflectionManager
+ ) {
+ this.reflectionManager = reflectionManager != null ? reflectionManager : new
JavaXFactory();
+ XClass beanXClass = this.reflectionManager.toXClass( beanClass );
this.beanClass = beanClass;
this.messageBundle = resourceBundle == null ?
getDefaultResourceBundle() :
resourceBundle;
this.defaultMessageBundle = ResourceBundle.getBundle( DEFAULT_VALIDATOR_MESSAGE );
this.userInterpolator = interpolator;
- this.childClassValidators = childClassValidators;
- initValidator( beanXClass, childClassValidators );
+ this.childClassValidators = childClassValidators != null ?
+ childClassValidators :
+ new HashMap<XClass, ClassValidator>();
+ initValidator( beanXClass, this.childClassValidators );
}
@SuppressWarnings("unchecked")
protected ClassValidator(
XClass beanXClass, ResourceBundle resourceBundle, MessageInterpolator
userInterpolator,
- Map<XClass, ClassValidator> childClassValidators
+ Map<XClass, ClassValidator> childClassValidators, ReflectionManager
reflectionManager
) {
this.beanClass = reflectionManager.toClass( beanXClass );
this.messageBundle = resourceBundle == null ?
@@ -242,7 +249,7 @@
clazz = member.getType();
}
if ( !childClassValidators.containsKey( clazz ) ) {
- new ClassValidator( clazz, messageBundle, userInterpolator, childClassValidators );
+ new ClassValidator( clazz, messageBundle, userInterpolator, childClassValidators,
reflectionManager );
}
}
}