[Arakhnę-Dev] [185] * Add ReflectionUtil#getCommonType().

[ Thread Index | Date Index | More arakhne.org/dev Archives ]


Revision: 185
Author:   galland
Date:     2010-09-19 22:31:34 +0200 (Sun, 19 Sep 2010)
Log Message:
-----------
* Add ReflectionUtil#getCommonType().

Modified Paths:
--------------
    trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ReflectionUtil.java

Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ReflectionUtil.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ReflectionUtil.java	2010-09-16 17:34:54 UTC (rev 184)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ReflectionUtil.java	2010-09-19 20:31:34 UTC (rev 185)
@@ -596,5 +596,43 @@
 		}
 		return list;
 	}
+	
+	/** Replies the top-most type which is common to both given types.
+	 * 
+	 * @param type1
+	 * @param type2
+	 * @return the top-most type which is common to both given types.
+	 * @since 6.0
+	 */
+	public static Class<?> getCommonType(Class<?> type1, Class<?> type2) {
+		if (type1==null) return type2;
+		if (type2==null) return type1;
+		Class<?> top = type1;
+		while (!top.isAssignableFrom(type2)) {
+			top = top.getSuperclass();
+			assert(top!=null);
+		}
+		return top;
+	}
 
+	/** Replies the top-most type which is common to both given objects.
+	 * 
+	 * @param instance1
+	 * @param instance2
+	 * @return the top-most type which is common to both given objects.
+	 * @since 6.0
+	 */
+	public static Class<?> getCommonType(Object instance1, Object instance2) {
+		if (instance1==null ) {
+			return instance2==null ? null : instance2.getClass();
+		}
+		if (instance2==null) return instance1.getClass();
+		Class<?> top = instance1.getClass();
+		while (!top.isInstance(instance2)) {
+			top = top.getSuperclass();
+			assert(top!=null);
+		}
+		return top;
+	}
+
 }


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/