incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r1494195 - in /sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl: ./ lang/ reflection/
Date Tue, 18 Jun 2013 16:50:43 GMT
Author: dklco
Date: Tue Jun 18 16:50:43 2013
New Revision: 1494195

URL: http://svn.apache.org/r1494195
Log:
Made some improvements to the hashcode implementations to make it so that it should be closer
to the equals implementation as well as removing some code which wasn't really needed

Removed:
    sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/PrimeNumbers.java
Modified:
    sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/SlingInvocationHandler.java
    sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/GetMethodToStringImpl.java
    sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/JDPEqualsImpl.java
    sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/JDPHashCodeImpl.java
    sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/MethodType.java
    sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/reflection/Classes.java
    sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/reflection/Methods.java

Modified: sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/SlingInvocationHandler.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/SlingInvocationHandler.java?rev=1494195&r1=1494194&r2=1494195&view=diff
==============================================================================
--- sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/SlingInvocationHandler.java
(original)
+++ sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/SlingInvocationHandler.java
Tue Jun 18 16:50:43 2013
@@ -39,7 +39,6 @@ import org.apache.sling.commons.proxy.im
 import org.apache.sling.commons.proxy.impl.lang.JDPEqualsImpl;
 import org.apache.sling.commons.proxy.impl.lang.JDPHashCodeImpl;
 import org.apache.sling.commons.proxy.impl.lang.MethodType;
-import org.apache.sling.commons.proxy.impl.lang.PrimeNumbers;
 import org.apache.sling.commons.proxy.impl.reflection.Annotations;
 import org.apache.sling.commons.proxy.impl.to.BaseInvokedTO;
 import org.apache.sling.commons.proxy.impl.to.InvokedChildrenTO;
@@ -282,9 +281,8 @@ public class SlingInvocationHandler impl
 	 */
 	@Override
 	public int hashCode() {
-		final int hashCode = (SlingInvocationHandler.class.hashCode() * PrimeNumbers
-				.getInstance().get(2)) + this.r.getPath().hashCode();
-		return hashCode;
+		return SlingInvocationHandler.class.hashCode() * 37
+				+ this.r.getPath().hashCode();
 	}
 
 	/*

Modified: sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/GetMethodToStringImpl.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/GetMethodToStringImpl.java?rev=1494195&r1=1494194&r2=1494195&view=diff
==============================================================================
--- sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/GetMethodToStringImpl.java
(original)
+++ sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/GetMethodToStringImpl.java
Tue Jun 18 16:50:43 2013
@@ -103,7 +103,7 @@ public final class GetMethodToStringImpl
 
 		if (obj != null) {
 			this.addInterfaceInfo(obj, sb);
-			this.addMethodReturnValues(obj, Methods.getterMethods(obj), sb);
+			this.addMethodReturnValues(obj, Methods.getBeanGetterMethods(obj), sb);
 		} else {
 			sb.append("null");
 		}

Modified: sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/JDPEqualsImpl.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/JDPEqualsImpl.java?rev=1494195&r1=1494194&r2=1494195&view=diff
==============================================================================
--- sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/JDPEqualsImpl.java
(original)
+++ sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/JDPEqualsImpl.java
Tue Jun 18 16:50:43 2013
@@ -44,7 +44,7 @@ public class JDPEqualsImpl {
 		if (o1 == null || o2 == null) {
 			return false;
 		}
-		if (!areProxies(o1, o2)) {
+		if (!Proxy.isProxyClass(o2.getClass())) {
 			return false;
 		}
 
@@ -81,18 +81,4 @@ public class JDPEqualsImpl {
 		}
 		return s1.equals(s2);
 	}
-
-	/**
-	 * Checks to see if the two objects are both JDP proxies.
-	 * 
-	 * @param o1
-	 *            the first object
-	 * @param o2
-	 *            the second object
-	 * @return whether or not the objects are proxies
-	 */
-	private static boolean areProxies(Object o1, Object o2) {
-		return Proxy.isProxyClass(o1.getClass())
-				&& Proxy.isProxyClass(o2.getClass());
-	}
 }

Modified: sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/JDPHashCodeImpl.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/JDPHashCodeImpl.java?rev=1494195&r1=1494194&r2=1494195&view=diff
==============================================================================
--- sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/JDPHashCodeImpl.java
(original)
+++ sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/JDPHashCodeImpl.java
Tue Jun 18 16:50:43 2013
@@ -18,25 +18,19 @@
  */
 package org.apache.sling.commons.proxy.impl.lang;
 
-import java.lang.reflect.Field;
-import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Proxy;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.Iterator;
-import java.util.List;
 
-import org.apache.sling.commons.proxy.impl.reflection.Classes;
+import org.apache.sling.commons.proxy.impl.SlingInvocationHandler;
 
 /**
- * Generates a HashCode for the provided Object based on: If the Object is not a
- * Proxy Object, the class name of the Object the implemented interfaces class
- * name in ascending Order the Members hashCode value, evaluated in the members
- * class name ascending order. If the value in NULL, a zero value is used.
+ * Generates a HashCode for the provided Object based on:
  * 
- * Any exceptions thrown while accessing the Objects member values will result
- * in a IllegalStateException
+ * <ul>
+ * <li>The hash codes of all of the interfaces for the object</li>
+ * <li>The hash code of the resource path</li>
+ * </ul>
+ * 
+ * The result of the hash code call is added to 37 * the original result.
  */
 public final class JDPHashCodeImpl {
 
@@ -49,160 +43,21 @@ public final class JDPHashCodeImpl {
 	 * @throws IllegalStateException
 	 */
 	public int hashCode(Object obj) throws IllegalStateException {
-		List<Integer> list = new java.util.ArrayList<Integer>();
-
-		Class<?> c = obj.getClass();
-
-		if (Proxy.isProxyClass(c)) {
-			list.add(Proxy.class.hashCode());
-			handleInterfaces(c, list);
-			InvocationHandler ih = Proxy.getInvocationHandler(obj);
-			list.add(ih.hashCode());
-		} else {
-			list.add(c.hashCode());
-			handleInterfaces(c, list);
-			handleFields(obj, list);
-		}
-
-		return calculate(list);
-	}
 
-	/**
-	 * Adds the has code for all of the interfaces in <code>c</code> into the
-	 * <code>list</code>
-	 * 
-	 * @param c
-	 *            the interfaces to handle
-	 * @param list
-	 *            the list for containing the hash codes
-	 */
-	private void handleInterfaces(Class<?> c, List<Integer> list) {
-		Class<?>[] ifcs = c.getInterfaces();
-		if (ifcs != null) {
-			Arrays.sort(ifcs, new ClassNameComparator());
-			for (Class<?> ifc : ifcs) {
-				list.add(ifc.hashCode());
-			}
-		}
-	}
+		int result = 7;
 
-	/**
-	 * Adds the hash codes for all of the fields of <code>obj</code> into the
-	 * <code>list</code>
-	 * 
-	 * @param obj
-	 *            the object whose fields we should check
-	 * @param list
-	 *            the list of hash codes
-	 */
-	private void handleFields(Object obj, List<Integer> list) {
-		List<Field> fields = Classes.getFields(obj);
-		if (fields != null && fields.size() > 0) {
-			Collections.sort(fields, new FieldComparator());
-			for (Field f : fields) {
-				try {
-					boolean unset = false;
-					if (!f.isAccessible()) {
-						unset = true;
-						f.setAccessible(true);
-					}
-					Object member = f.get(obj);
-					if (unset) {
-						f.setAccessible(false);
-					}
-					list.add((member == null ? 0 : member.hashCode()));
-				} catch (Exception ex) {
-					throw new IllegalStateException(ex);
-				}
-			}
+		Class<?>[] interfaces = obj.getClass().getInterfaces();
+		for (Class<?> intf : interfaces) {
+			result = 37 * result + intf.hashCode();
 		}
-	}
 
-	/**
-	 * The first element in the List will always be the primary Class
-	 * 
-	 * @param list
-	 * @return
-	 */
-	private int calculate(List<Integer> list) {
-		Iterator<Integer> i = list.listIterator();
+		SlingInvocationHandler sih = (SlingInvocationHandler) Proxy
+				.getInvocationHandler(obj);
+		result = 37
+				* result
+				+ (sih.getResourcePath() != null ? sih.getResourcePath()
+						.hashCode() : 0);
 
-		/**
-		 * We start at PrimaryNumber index 2 because: 0 - is only a placeholder
-		 * 1 - is the value 2 - which we never want to use
-		 */
-		int hash = i.next(), primeNdx = 2;
-
-		PrimeNumbers pn = PrimeNumbers.getInstance();
-		for (; i.hasNext();) {
-			hash = hash * pn.get(primeNdx) + i.next();
-		}
-
-		return hash;
-	}
-
-	/**
-	 * This will return NULL if both values are not null. Otherwise it will
-	 * return: 0 - if both are NULL or both have the same Object identity -1 -
-	 * if 'o1' is NULL but 'o2' is not 1 - if 'o1' is not null but 'o2' is
-	 * 
-	 * @param o1
-	 *            Object
-	 * @param o2
-	 *            Object
-	 * @return see above
-	 */
-	private static final Integer compareForNull(Object o1, Object o2) {
-		if (o1 == o2) {
-			return 0;
-		}
-		if (o1 == null) {
-			return -1;
-		}
-		if (o2 == null) {
-			return 1;
-		}
-		return null;
-	}
-
-	/**
-	 * Compares two fields by name
-	 */
-	private static final class FieldComparator implements Comparator<Field> {
-
-		/*
-		 * (non-Javadoc)
-		 * 
-		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
-		 */
-		public int compare(Field o1, Field o2) {
-			Integer rtn = compareForNull(o1, o2);
-			if (rtn != null) {
-				return rtn;
-			}
-
-			return o1.getType().getName().compareTo(o2.getType().getName());
-		}
-	}
-
-	/**
-	 * Compares classes by name.
-	 */
-	private static final class ClassNameComparator implements
-			Comparator<Class<?>> {
-
-		/*
-		 * (non-Javadoc)
-		 * 
-		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
-		 */
-		public int compare(Class<?> o1, Class<?> o2) {
-			Integer rtn = compareForNull(o1, o2);
-			if (rtn != null) {
-				return rtn;
-			}
-
-			return o1.getName().compareTo(o2.getName());
-		}
+		return result;
 	}
 }

Modified: sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/MethodType.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/MethodType.java?rev=1494195&r1=1494194&r2=1494195&view=diff
==============================================================================
--- sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/MethodType.java
(original)
+++ sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/MethodType.java
Tue Jun 18 16:50:43 2013
@@ -29,7 +29,7 @@ public enum MethodType {
 
 	ToString("^(toString)$"), HashCode("^(hashCode)$"), Equals("^(equals)$"), BackingResource(
 			"^(getBackingResource)$"), JavaBeanGet("^get([A-Z])(\\w+)$"), JavaBeanIs(
-			"^is([A-Z])(\\w+)$"), JavaBeanSet("^is([A-Z])(\\w+)$"), Unknown(
+			"^is([A-Z])(\\w+)$"), JavaBeanSet("^set([A-Z])(\\w+)$"), Unknown(
 			"^$");
 
 	private final String regex;

Modified: sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/reflection/Classes.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/reflection/Classes.java?rev=1494195&r1=1494194&r2=1494195&view=diff
==============================================================================
--- sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/reflection/Classes.java
(original)
+++ sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/reflection/Classes.java
Tue Jun 18 16:50:43 2013
@@ -20,6 +20,7 @@ package org.apache.sling.commons.proxy.i
 
 import java.lang.reflect.Field;
 import java.lang.reflect.Proxy;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
 
@@ -38,7 +39,7 @@ public final class Classes {
 	 * @return List<Field> the list of Fields from an Object
 	 */
 	public static List<Field> getFields(Object obj) {
-		java.util.ArrayList<Field> list = new java.util.ArrayList<Field>();
+		List<Field> list = new ArrayList<Field>();
 
 		Class<?> c = obj.getClass();
 		for (; c != null && c != Object.class;) {
@@ -50,7 +51,6 @@ public final class Classes {
 			c = c.getSuperclass();
 		}
 
-		list.trimToSize();
 		return list;
 	}
 
@@ -95,7 +95,6 @@ public final class Classes {
 		return set.size() < 1;
 	}
 
-
 	/**
 	 * Checks the size of the class arraym return the size or -1 if the array is
 	 * null.

Modified: sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/reflection/Methods.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/reflection/Methods.java?rev=1494195&r1=1494194&r2=1494195&view=diff
==============================================================================
--- sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/reflection/Methods.java
(original)
+++ sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/reflection/Methods.java
Tue Jun 18 16:50:43 2013
@@ -21,6 +21,8 @@ package org.apache.sling.commons.proxy.i
 import java.lang.reflect.Method;
 import java.util.List;
 
+import org.apache.commons.lang.StringUtils;
+
 /**
  * Methods for simplifying reflection method calls.
  */
@@ -33,12 +35,14 @@ public final class Methods {
 	 *            the object to check
 	 * @return the list of getter methods
 	 */
-	public static List<Method> getterMethods(Object o) {
+	public static List<Method> getBeanGetterMethods(Object o) {
 		List<Method> list = getMethods(o, "^(get|is)\\w*$", true, 0);
 		return list;
 	}
 
 	/**
+	 * Get the methods matching the specified parameters from the specified
+	 * object.
 	 * 
 	 * @param o
 	 *            Object - the target Object
@@ -57,7 +61,7 @@ public final class Methods {
 	 */
 	public static List<Method> getMethods(Object o, String nameRegex,
 			boolean hasReturn, int numberOfParameters) {
-		nameRegex = (nameRegex == null || nameRegex.trim().length() < 1 ? "^.*$"
+		nameRegex = (StringUtils.isEmpty(nameRegex) ? "^.*$"
 				: nameRegex);
 
 		List<Method> list = new java.util.ArrayList<Method>();



Mime
View raw message