com.talk2object.util.ClassAssignmentUtils Maven / Gradle / Ivy
The newest version!
package com.talk2object.util;
import java.util.ArrayList;
import java.util.Set;
public class ClassAssignmentUtils {
public static final int INTERFACE_COST = 10000;
public static final int OBJECT_COST = 20000;
/**
* interface cost is more than class inheritance.
*
* @param assigneeClass
* @param assignerClass
* @return -1 if not assignable. bigger means more inheritance levels.
*/
public static int getAssignmentCost(Class assigneeClass, Class assignerClass) {
if (assigneeClass.isAssignableFrom(assignerClass)) {
if (assigneeClass.isInterface()) {
return INTERFACE_COST;
} else {
if (assigneeClass == Object.class)
return OBJECT_COST;
else
return getInheritanceLevel(assigneeClass, assignerClass);
}
} else {
return -1;
}
}
/**
*
*
* @param assignee
* @param assigner
* @return
*/
public static int getInheritanceLevel(Class superClass, Class subClass) {
int count = 0;
Class tempClass = subClass;
while (tempClass != superClass) {
count++;
tempClass = tempClass.getSuperclass();
}
if (count >= INTERFACE_COST)
throw new RuntimeException("too many inheritance levels.");
return count;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy