[hibernate-commits] Hibernate SVN: r10639 - in branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java: . generics

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Mon Oct 23 20:07:17 EDT 2006


Author: epbernard
Date: 2006-10-23 20:07:14 -0400 (Mon, 23 Oct 2006)
New Revision: 10639

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/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/Pair.java
   branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/generics/CompoundTypeEnvironment.java
Log:
First set of optimization to the XLayer

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-20 16:31:39 UTC (rev 10638)
+++ branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3OverridenAnnotationReader.java	2006-10-24 00:07:14 UTC (rev 10639)
@@ -11,6 +11,7 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import javax.persistence.AssociationOverride;
 import javax.persistence.AssociationOverrides;
 import javax.persistence.AttributeOverride;
@@ -176,7 +177,8 @@
 	private String propertyName;
 	private PropertyType propertyType;
 	private transient Annotation[] annotations;
-	private static final String WORD_SEPARATOR = "-";
+    private transient Map<Class, Annotation> annotationsMap;
+    private static final String WORD_SEPARATOR = "-";
 	private transient List<Element> elementsForProperty;
 	private AccessibleObject mirroredAttribute;
 
@@ -246,18 +248,12 @@
 
 	public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
 		initAnnotations();
-		for ( Annotation annotation : annotations ) {
-			if ( annotation.annotationType().equals( annotationType ) ) return (T) annotation;
-		}
-		return null;
+        return (T) annotationsMap.get( annotationType );
 	}
 
 	public <T extends Annotation> boolean isAnnotationPresent(Class<T> annotationType) {
 		initAnnotations();
-		for ( Annotation annotation : annotations ) {
-			if ( annotation.annotationType().equals( annotationType ) ) return true;
-		}
-		return false;
+		return (T) annotationsMap.get( annotationType ) != null;
 	}
 
 	public Annotation[] getAnnotations() {
@@ -267,13 +263,14 @@
 
 	private void initAnnotations() {
 		if ( annotations == null ) {
-			XMLContext.Default defaults = xmlContext.getDefault( className );
+            XMLContext.Default defaults = xmlContext.getDefault( className );
 			if ( className != null && propertyName == null ) {
 				//is a class
 				Element tree = xmlContext.getXMLTree( className, null );
 				Annotation[] annotations = super.getAnnotations();
 				List<Annotation> annotationList = new ArrayList<Annotation>( annotations.length + 5 );
-				for ( Annotation annotation : annotations ) {
+                annotationsMap = new HashMap<Class, Annotation>( annotations.length + 5 );
+                for ( Annotation annotation : annotations ) {
 					if ( ! annotationToXml.containsKey( annotation.annotationType() ) ) {
 						//unknown annotations are left over
 						annotationList.add( annotation );
@@ -321,13 +318,18 @@
 				if ( current != null ) annotationList.add( current );
 				current = getEntityListeners( tree, defaults );
 				if ( current != null ) annotationList.add( current );
-				this.annotations = annotationList.toArray( new Annotation[ annotationList.size() ] );
-			}
-			else if ( className != null && propertyName != null ) {
+                //FIXME use annotationsMap rather than annotationList this will be faster since the annotation type is usually known at put() time
+                this.annotations = annotationList.toArray( new Annotation[ annotationList.size() ] );
+                for (Annotation ann : this.annotations) {
+                    annotationsMap.put( ann.annotationType(), ann );
+                }
+            }
+			else if ( className != null ) { //&& propertyName != null ) { //always true but less confusing
 				Element tree = xmlContext.getXMLTree( className, propertyName );
 				Annotation[] annotations = super.getAnnotations();
 				List<Annotation> annotationList = new ArrayList<Annotation>( annotations.length + 5 );
-				for ( Annotation annotation : annotations ) {
+                annotationsMap = new HashMap<Class, Annotation>( annotations.length + 5 );
+                for ( Annotation annotation : annotations ) {
 					if ( ! annotationToXml.containsKey( annotation.annotationType() ) ) {
 						//unknown annotations are left over
 						annotationList.add( annotation );
@@ -359,11 +361,18 @@
 
 				}
 				processEventAnnotations(annotationList, defaults);
-				this.annotations = annotationList.toArray( new Annotation[ annotationList.size() ] );
-			}
+				//FIXME use annotationsMap rather than annotationList this will be faster since the annotation type is usually known at put() time
+                this.annotations = annotationList.toArray( new Annotation[ annotationList.size() ] );
+                for (Annotation ann : this.annotations) {
+                    annotationsMap.put( ann.annotationType(), ann );
+                }
+            }
 			else {
 				this.annotations = super.getAnnotations();
-			}
+                for (Annotation ann : this.annotations) {
+                    annotationsMap.put( ann.annotationType(), ann );
+                }
+            }
 		}
 	}
 
@@ -461,10 +470,11 @@
 		//no element but might have some default or some annotation
 		boolean defaultToJoinTable = ! ( super.isAnnotationPresent( JoinColumn.class )
 				|| super.isAnnotationPresent( JoinColumns.class ) );
-		defaultToJoinTable = defaultToJoinTable &&
-				( ( annotation.annotationType() == ManyToMany.class && StringHelper.isEmpty( ( (ManyToMany) annotation).mappedBy() ) )
-				  || ( annotation.annotationType() == OneToMany.class && StringHelper.isEmpty( ( (OneToMany) annotation).mappedBy() ) )
-				  || ( annotation.annotationType() == CollectionOfElements.class )
+        final Class<? extends Annotation> annotationClass = annotation.annotationType();
+        defaultToJoinTable = defaultToJoinTable &&
+				( ( annotationClass == ManyToMany.class && StringHelper.isEmpty( ( (ManyToMany) annotation).mappedBy() ) )
+				  || ( annotationClass == OneToMany.class && StringHelper.isEmpty( ( (OneToMany) annotation).mappedBy() ) )
+				  || ( annotationClass == CollectionOfElements.class )
 				);
 		final Class<JoinTable> annotationType = JoinTable.class;
 		if ( defaultToJoinTable

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-20 16:31:39 UTC (rev 10638)
+++ branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/JavaXAnnotatedElement.java	2006-10-24 00:07:14 UTC (rev 10639)
@@ -33,15 +33,15 @@
 	}
 
 	public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
-		return getAnnotationReader().getAnnotation( annotationType );
+		return annotationReader.getAnnotation( annotationType );
 	}
 
 	public <T extends Annotation> boolean isAnnotationPresent(Class<T> annotationType) {
-		return getAnnotationReader().isAnnotationPresent( annotationType );
+		return annotationReader.isAnnotationPresent( annotationType );
 	}
 
 	public Annotation[] getAnnotations() {
-		return getAnnotationReader().getAnnotations();
+		return annotationReader.getAnnotations();
 	}
 
 	AnnotatedElement toAnnotatedElement() {
@@ -53,16 +53,16 @@
 		if ( obj == null || ! ( obj instanceof JavaXAnnotatedElement ) ) return false;
 		JavaXAnnotatedElement other = (JavaXAnnotatedElement) obj;
 		//FIXME yuk this defeat the type environment
-		return toAnnotatedElement().equals( other.toAnnotatedElement() );
+		return annotatedElement.equals( other.toAnnotatedElement() );
 	}
 
 	@Override
 	public int hashCode() {
-		return toAnnotatedElement().hashCode();
+		return annotatedElement.hashCode();
 	}
 
 	@Override
 	public String toString() {
-		return toAnnotatedElement().toString();
+		return annotatedElement.toString();
 	}
 }

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-20 16:31:39 UTC (rev 10638)
+++ branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/JavaXClass.java	2006-10-24 00:07:14 UTC (rev 10639)
@@ -20,10 +20,12 @@
 class JavaXClass extends JavaXAnnotatedElement implements XClass {
 
 	private final TypeEnvironment context;
+    private final Class clazz;
 
-	public JavaXClass(Class clazz, TypeEnvironment env, JavaXFactory factory) {
+    public JavaXClass(Class clazz, TypeEnvironment env, JavaXFactory factory) {
 		super( clazz, factory );
-		this.context = env;
+        this.clazz = clazz; //optimization
+        this.context = env;
 	}
 
 	public String getName() {
@@ -110,7 +112,7 @@
 	}
 
 	public Class<?> toClass() {
-		return (Class) toAnnotatedElement();
+		return clazz;
 	}
 
 	public boolean isAssignableFrom(XClass c) {

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-20 16:31:39 UTC (rev 10638)
+++ branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/JavaXFactory.java	2006-10-24 00:07:14 UTC (rev 10639)
@@ -174,8 +174,6 @@
 				JavaXClass result = xClasses.get( key );
 				if ( result == null ) {
 					result = new JavaXClass( classType, context, JavaXFactory.this );
-					//TODO get rid of it
-					//result.setXMLDescriptor( xml );
 					xClasses.put( key, result );
 				}
 				return result;
@@ -192,8 +190,6 @@
 		JavaXPackage xPackage = packagesToXPackages.get( pkg );
 		if ( xPackage == null ) {
 			xPackage = new JavaXPackage( pkg, this );
-			//TODO get rid of it
-			//xPackage.setXMLDescriptor( xml );
 			packagesToXPackages.put( pkg, xPackage );
 		}
 		return xPackage;
@@ -201,8 +197,9 @@
 
 	XProperty getXProperty(Member member, JavaXClass owner) {
 		MemberKey key = new MemberKey( member, owner.toClass(), owner.getTypeEnvironment() );
-		JavaXProperty xProperty = xProperties.get( key );
-		if ( ! xProperties.containsKey( key ) ) {
+        //FIXME get is as expensive as create most time spent in hashCode and equals
+        JavaXProperty xProperty = xProperties.get( key );
+		if ( xProperty == null ) {
 			xProperty = JavaXProperty.create( member, owner.getTypeEnvironment(), this );
 			xProperties.put( key, xProperty );
 		}
@@ -211,8 +208,9 @@
 
 	XMethod getXMethod(Member member, JavaXClass owner) {
 		MemberKey key = new MemberKey( member, owner.toClass(), owner.getTypeEnvironment() );
-		JavaXMethod xMethod = xMethods.get( key );
-		if ( ! xMethods.containsKey( key ) ) {
+        //FIXME get is as expensive as create most time spent in hashCode and equals
+        JavaXMethod xMethod = xMethods.get( key );
+		if ( xMethod == null ) {
 			xMethod = JavaXMethod.create( member, owner.getTypeEnvironment(), this );
 			xMethods.put( key, xMethod );
 		}

Modified: branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/Pair.java
===================================================================
--- branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/Pair.java	2006-10-20 16:31:39 UTC (rev 10638)
+++ branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/Pair.java	2006-10-24 00:07:14 UTC (rev 10639)
@@ -11,11 +11,13 @@
 	private final T o1;
 
 	private final U o2;
+    private final int hashCode;
 
-	Pair(T o1, U o2) {
+    Pair(T o1, U o2) {
 		this.o1 = o1;
 		this.o2 = o2;
-	}
+        this.hashCode = doHashCode();
+    }
 
 	@Override
 	public boolean equals(Object obj) {
@@ -23,11 +25,20 @@
 			 return false;
 		}
 		Pair other = (Pair) obj;
-		return safeEquals( o1, other.o1 ) && safeEquals( o2, other.o2 );
+        return !differentHashCode( other ) && safeEquals( o1, other.o1 ) && safeEquals( o2, other.o2 );
+    }
+
+    private boolean differentHashCode(Pair other) {
+        return hashCode != other.hashCode;
+    }
+
+    @Override
+	public int hashCode() {
+        //cached because the inheritance can be big
+        return hashCode;
 	}
 
-	@Override
-	public int hashCode() {
+    private int doHashCode() {
 		return safeHashCode( o1 ) ^ safeHashCode( o2 );
 	}
 
@@ -42,7 +53,6 @@
 		if ( obj1 == null ) {
 			return obj2 == null;
 		}
-		boolean result = obj1.equals( obj2 );
-		return result;
+        return obj1.equals( obj2 );
 	}
 }

Modified: branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/generics/CompoundTypeEnvironment.java
===================================================================
--- branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/generics/CompoundTypeEnvironment.java	2006-10-20 16:31:39 UTC (rev 10638)
+++ branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/reflection/java/generics/CompoundTypeEnvironment.java	2006-10-24 00:07:14 UTC (rev 10639)
@@ -13,11 +13,13 @@
 	private final TypeEnvironment f;
 
 	private final TypeEnvironment g;
+    private final int hashCode;
 
-	public CompoundTypeEnvironment(TypeEnvironment f, TypeEnvironment g) {
+    public CompoundTypeEnvironment(TypeEnvironment f, TypeEnvironment g) {
 		this.f = f;
 		this.g = g;
-	}
+        hashCode = doHashCode();
+    }
 
 	public Type bind(Type type) {
 		return f.bind( g.bind( type ) );
@@ -25,20 +27,30 @@
 
 	public boolean equals(Object o) {
 		if ( this == o ) return true;
-		if ( o == null || getClass() != o.getClass() ) return false;
+		if ( ! ( o instanceof CompoundTypeEnvironment ) ) return false;
 
 		final CompoundTypeEnvironment that = (CompoundTypeEnvironment) o;
 
-		if ( !f.equals( that.f ) ) return false;
-		if ( !g.equals( that.g ) ) return false;
+        if ( differentHashCode( that ) ) return false;
 
-		return true;
-	}
+        if ( !f.equals( that.f ) ) return false;
+        return g.equals( that.g );
 
-	public int hashCode() {
+    }
+
+    private boolean differentHashCode(CompoundTypeEnvironment that) {
+        return hashCode != that.hashCode;
+    }
+
+    private int doHashCode() {
 		int result;
 		result = f.hashCode();
 		result = 29 * result + g.hashCode();
 		return result;
 	}
+
+    public int hashCode() {
+        //cached because the inheritance can be big
+        return hashCode;
+    }
 }




More information about the hibernate-commits mailing list