spelhelper/src/main/java/net/abhinavsarkar/spelhelper/InheritenceUtil.java

87 lines
2.5 KiB
Java
Raw Normal View History

2010-05-27 20:24:57 +05:30
/* Copyright 2010 Abhinav Sarkar <abhinav@abhinavsarkar.net>
*
* This file is a part of SpelHelper library.
*
* SpelHelper library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License (GNU LGPL) as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* SpelHelper library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with SpelHelper library. If not, see <http://www.gnu.org/licenses/>.
*/
2010-05-26 19:00:31 +05:30
package net.abhinavsarkar.spelhelper;
import java.util.LinkedHashSet;
import java.util.Set;
final class InheritenceUtil {
2010-05-27 21:56:11 +05:30
private InheritenceUtil() {
}
public static Set<Class<?>> getInheritance(final Class<?> clazz) {
2010-05-26 19:22:11 +05:30
LinkedHashSet<Class<?>> result = new LinkedHashSet<Class<?>>();
2010-05-27 21:56:11 +05:30
result.add(clazz);
getInheritance(clazz, result);
2010-05-26 19:22:11 +05:30
return result;
}
2010-05-26 19:00:31 +05:30
2010-05-26 19:22:11 +05:30
/**
* Get inheritance of type.
2010-05-27 20:24:57 +05:30
*
2010-05-27 21:56:11 +05:30
* @param clazz
2010-05-26 19:22:11 +05:30
* @param result
*/
2010-05-27 21:56:11 +05:30
private static void getInheritance(final Class<?> clazz, final Set<Class<?>> result) {
Class<?> superclass = getSuperclass(clazz);
2010-05-26 19:00:31 +05:30
2010-05-26 19:22:11 +05:30
if (superclass != null) {
result.add(superclass);
getInheritance(superclass, result);
}
2010-05-26 19:00:31 +05:30
2010-05-27 21:56:11 +05:30
getInterfaceInheritance(clazz, result);
2010-05-26 19:22:11 +05:30
}
2010-05-26 19:00:31 +05:30
2010-05-26 19:22:11 +05:30
/**
* Get interfaces that the type inherits from.
2010-05-27 20:24:57 +05:30
*
2010-05-27 21:56:11 +05:30
* @param clazz
2010-05-26 19:22:11 +05:30
* @param result
*/
2010-05-27 21:56:11 +05:30
private static void getInterfaceInheritance(final Class<?> clazz,
2010-05-26 19:22:11 +05:30
final Set<Class<?>> result) {
2010-05-27 21:56:11 +05:30
for (Class<?> c : clazz.getInterfaces()) {
2010-05-26 19:22:11 +05:30
result.add(c);
getInterfaceInheritance(c, result);
}
}
2010-05-26 19:00:31 +05:30
2010-05-26 19:22:11 +05:30
/**
* Get superclass of class.
2010-05-27 20:24:57 +05:30
*
2010-05-27 21:56:11 +05:30
* @param clazz
2010-05-26 19:22:11 +05:30
* @return
*/
2010-05-27 21:56:11 +05:30
private static Class<?> getSuperclass(final Class<?> clazz) {
if (clazz == null) {
2010-05-26 19:22:11 +05:30
return null;
}
2010-05-27 21:56:11 +05:30
if (clazz.isArray() && clazz != Object[].class) {
Class<?> type = clazz.getComponentType();
2010-05-26 19:22:11 +05:30
while (type.isArray()) {
type = type.getComponentType();
}
return type;
}
2010-05-27 21:56:11 +05:30
return clazz.getSuperclass();
2010-05-26 19:22:11 +05:30
}
2010-05-26 19:00:31 +05:30
}