[Arakhnę-Dev] [131] Add ReflectionUtil.getAllDirectInterfaces() |
[ Thread Index |
Date Index
| More arakhne.org/dev Archives
]
Revision: 131
Author: galland
Date: 2010-02-12 22:59:34 +0100 (Fri, 12 Feb 2010)
Log Message:
-----------
Add ReflectionUtil.getAllDirectInterfaces()
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-02-12 21:44:34 UTC (rev 130)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ReflectionUtil.java 2010-02-12 21:59:34 UTC (rev 131)
@@ -493,21 +493,23 @@
* </ul>
*
* @param <T> is the highest type to explore in type hierarchy.
+ * @param <I> indicates the type of the replied interfaces.
* @param lowestType is the lowest type to explore in type hierarchy.
* @param highestType is the highest type to explore in type hierarchy.
* @param interfaceType indicates the type of the replied interfaces.
* @return the implemented interfaces.
* @since 5.0
*/
- public static <T> Set<Class<?>> getAllDirectInterfaces(Class<? extends T> lowestType, Class<T> highestType, Class<?> interfaceType) {
+ @SuppressWarnings("unchecked")
+ public static <T,I> Set<Class<? extends I>> getAllDirectInterfaces(Class<? extends T> lowestType, Class<T> highestType, Class<I> interfaceType) {
assert(lowestType!=null);
- Set<Class<?>> collection = new TreeSet<Class<?>>();
+ Set<Class<? extends I>> collection = new TreeSet<Class<? extends I>>();
Class<?> type = lowestType;
boolean cont;
do {
for(Class<?> directInterface : type.getInterfaces()) {
if (interfaceType==null || interfaceType.isAssignableFrom(directInterface))
- collection.add(directInterface);
+ collection.add((Class<? extends I>)directInterface);
}
cont = (highestType==null || !type.equals(highestType));
type = type.getSuperclass();
@@ -517,6 +519,56 @@
}
/**
+ * Determines the interfaces implemented by the classes from the lowest type
+ * to the highestType which are extended the given interfaceType.
+ * <p>
+ * Insteed of {@link Class#getInterfaces()}, this function is exploring
+ * the super classes. This function does not explore super-interfaces
+ * of implemented interfaces.
+ * <p>
+ * <pre><code>
+ * interface IA {}
+ * interface IB extends IA {}
+ * interface IC {}
+ * interface ID extends IB, IC {}
+ * class CA implements IC {}
+ * class CB extends CA {}
+ * class CC extends CB implements IB {}
+ * </code></pre>
+ * This function replies for:
+ * <ul>
+ * <li><code>getAllDirectInterfaces(IA,null,null)</code>=<code>{}</code></li>
+ * <li><code>getAllDirectInterfaces(IB,null,null)</code>=<code>{IA}</code></li>
+ * <li><code>getAllDirectInterfaces(IC,null,null)</code>=<code>{}</code></li>
+ * <li><code>getAllDirectInterfaces(ID,null,null)</code>=<code>{IB,IC}</code></li>
+ * <li><code>getAllDirectInterfaces(CA,null,null)</code>=<code>{IC}</code></li>
+ * <li><code>getAllDirectInterfaces(CB,null,null)</code>=<code>{IC}</code></li>
+ * <li><code>getAllDirectInterfaces(CC,null,null)</code>=<code>{IB,IC}</code></li>
+ * </ul>
+ *
+ * @param <T> is the highest type to explore in type hierarchy.
+ * @param lowestType is the lowest type to explore in type hierarchy.
+ * @param highestType is the highest type to explore in type hierarchy.
+ * @return the implemented interfaces.
+ * @since 5.0
+ */
+ public static <T> Set<Class<?>> getAllDirectInterfaces(Class<? extends T> lowestType, Class<T> highestType) {
+ assert(lowestType!=null);
+ Set<Class<?>> collection = new TreeSet<Class<?>>();
+ Class<?> type = lowestType;
+ boolean cont;
+ do {
+ for(Class<?> directInterface : type.getInterfaces()) {
+ collection.add(directInterface);
+ }
+ cont = (highestType==null || !type.equals(highestType));
+ type = type.getSuperclass();
+ }
+ while (type!=null && cont);
+ return collection;
+ }
+
+ /**
* Replies the list of all the superclasses of the given class.
* <p>
* This function does not replies <code>Object.class</code>.