我正在摆弄Java的反射API,并试图处理一些字段。现在我不得不识别字段的类型。字符串很简单,只需执行myField.getType().equals(String.class)。这同样适用于其他非派生类。但是如何检查派生类呢?例如,LinkedList作为List的子类。我找不到任何isSubclassOf(…)或extends(…)方法。我需要遍历所有getSuperClass()并通过自己的超类找到我的超类吗?
当前回答
这是@schuttek的答案的改进版本。改进的原因是它正确地为原语返回false(例如isSubclassOf(int.class, Object.class) => false),也正确地处理接口(例如isSubclassOf(HashMap.class, Map.class) => true)。
static public boolean isSubclassOf(final Class<?> clazz, final Class<?> possibleSuperClass)
{
if (clazz == null || possibleSuperClass == null)
{
return false;
}
else if (clazz.equals(possibleSuperClass))
{
return true;
}
else
{
final boolean isSubclass = isSubclassOf(clazz.getSuperclass(), possibleSuperClass);
if (!isSubclass && clazz.getInterfaces() != null)
{
for (final Class<?> inter : clazz.getInterfaces())
{
if (isSubclassOf(inter, possibleSuperClass))
{
return true;
}
}
}
return isSubclass;
}
}
其他回答
这对我来说很管用:
protected boolean isTypeOf(String myClass, Class<?> superClass) {
boolean isSubclassOf = false;
try {
Class<?> clazz = Class.forName(myClass);
if (!clazz.equals(superClass)) {
clazz = clazz.getSuperclass();
isSubclassOf = isTypeOf(clazz.getName(), superClass);
} else {
isSubclassOf = true;
}
} catch(ClassNotFoundException e) {
/* Ignore */
}
return isSubclassOf;
}
/ /继承
class A {
int i = 10;
public String getVal() {
return "I'm 'A'";
}
}
class B extends A {
int j = 20;
public String getVal() {
return "I'm 'B'";
}
}
class C extends B {
int k = 30;
public String getVal() {
return "I'm 'C'";
}
}
/ /方法
public static boolean isInheritedClass(Object parent, Object child) {
if (parent == null || child == null) {
return false;
} else {
return isInheritedClass(parent.getClass(), child.getClass());
}
}
public static boolean isInheritedClass(Class<?> parent, Class<?> child) {
if (parent == null || child == null) {
return false;
} else {
if (parent.isAssignableFrom(child)) {
// is child or same class
return parent.isAssignableFrom(child.getSuperclass());
} else {
return false;
}
}
}
//测试代码
System.out.println("isInheritedClass(new A(), new B()):" + isInheritedClass(new A(), new B()));
System.out.println("isInheritedClass(new A(), new C()):" + isInheritedClass(new A(), new C()));
System.out.println("isInheritedClass(new A(), new A()):" + isInheritedClass(new A(), new A()));
System.out.println("isInheritedClass(new B(), new A()):" + isInheritedClass(new B(), new A()));
System.out.println("isInheritedClass(A.class, B.class):" + isInheritedClass(A.class, B.class));
System.out.println("isInheritedClass(A.class, C.class):" + isInheritedClass(A.class, C.class));
System.out.println("isInheritedClass(A.class, A.class):" + isInheritedClass(A.class, A.class));
System.out.println("isInheritedClass(B.class, A.class):" + isInheritedClass(B.class, A.class));
/ /结果
isInheritedClass(new A(), new B()):true
isInheritedClass(new A(), new C()):true
isInheritedClass(new A(), new A()):false
isInheritedClass(new B(), new A()):false
isInheritedClass(A.class, B.class):true
isInheritedClass(A.class, C.class):true
isInheritedClass(A.class, A.class):false
isInheritedClass(B.class, A.class):false
除了@To-kra的回答。如果有人不喜欢递归:
public static boolean isSubClassOf(Class<?> clazz, Class<?> superClass) {
if(Object.class.equals(superClass)) {
return true;
}
for(; !Object.class.equals(clazz); clazz = clazz.getSuperclass()) {
if(clazz.getSuperclass().equals(superClass)) {
return true;
}
}
return false;
}
注意:没有空检查的清晰度。
另一个选项是instanceof:
Object o =...
if (o instanceof Number) {
double d = ((Number)o).doubleValue(); //this cast is safe
}
这是@schuttek的答案的改进版本。改进的原因是它正确地为原语返回false(例如isSubclassOf(int.class, Object.class) => false),也正确地处理接口(例如isSubclassOf(HashMap.class, Map.class) => true)。
static public boolean isSubclassOf(final Class<?> clazz, final Class<?> possibleSuperClass)
{
if (clazz == null || possibleSuperClass == null)
{
return false;
}
else if (clazz.equals(possibleSuperClass))
{
return true;
}
else
{
final boolean isSubclass = isSubclassOf(clazz.getSuperclass(), possibleSuperClass);
if (!isSubclass && clazz.getInterfaces() != null)
{
for (final Class<?> inter : clazz.getInterfaces())
{
if (isSubclassOf(inter, possibleSuperClass))
{
return true;
}
}
}
return isSubclass;
}
}
推荐文章
- 将所有非字母数字字符替换为空字符串
- 漂亮地打印Java集合(toString不返回漂亮输出)
- 静态嵌套类在Java,为什么?
- 如何防止Eclipse在启动时挂起?
- Optional和Optional的区别是什么?flatMap和Optional.map?
- Java中枚举的命名:单数还是复数?
- 盎格鲁- ngcloak / ngg展示blink元素
- 如何在Python中使用方法重载?
- 将类代码分离为头文件和cpp文件
- JavaBean和POJO之间的区别是什么?
- 注释在Java中如何使用,在哪里使用?
- 如何在Ubuntu下安装JDK 11 ?
- Spring Boot -无法确定数据库类型为NONE的嵌入式数据库驱动程序类
- 'datetime'模块没有'strptime'属性
- 如何转换/解析从字符串到字符在java?