java.lang.Class.getSuperclass()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(7.7k)|赞(0)|评价(0)|浏览(278)

本文整理了Java中java.lang.Class.getSuperclass()方法的一些代码示例,展示了Class.getSuperclass()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Class.getSuperclass()方法的具体详情如下:
包路径:java.lang.Class
类名称:Class
方法名:getSuperclass

Class.getSuperclass介绍

[英]Returns the Class object which represents the superclass of the class represented by this Class. If this Class represents the Object class, a primitive type, an interface or void then the method returns null. If this Class represents an array class then the Object class is returned.
[中]返回表示该类所表示的类的超类的Class对象。如果该类表示对象类、基元类型、接口或void,则该方法返回null。如果该类表示数组类,则返回对象类。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
public boolean hasSuperClass() {
  return (this.introspectedClass.getSuperclass() != null);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public String getSuperClassName() {
  Class<?> superClass = this.introspectedClass.getSuperclass();
  return (superClass != null ? superClass.getName() : null);
}

代码示例来源:origin: apache/incubator-dubbo

public EnumSerializer(Class cl) {
  // hessian/32b[12], hessian/3ab[23]
  if (!cl.isEnum() && cl.getSuperclass().isEnum())
    cl = cl.getSuperclass();
  try {
    _name = cl.getMethod("name", new Class[0]);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: spring-projects/spring-framework

public static Class<?> getEnumType(Class<?> targetType) {
  Class<?> enumType = targetType;
  while (enumType != null && !enumType.isEnum()) {
    enumType = enumType.getSuperclass();
  }
  Assert.notNull(enumType, () -> "The target type " + targetType.getName() + " does not refer to an enum");
  return enumType;
}

代码示例来源:origin: spring-projects/spring-framework

public static Method findDeclaredMethod(final Class type,
    final String methodName, final Class[] parameterTypes)
    throws NoSuchMethodException {
  Class cl = type;
  while (cl != null) {
    try {
      return cl.getDeclaredMethod(methodName, parameterTypes);
    }
    catch (NoSuchMethodException e) {
      cl = cl.getSuperclass();
    }
  }
  throw new NoSuchMethodException(methodName);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Return the user-defined class for the given class: usually simply the given
 * class, but the original class in case of a CGLIB-generated subclass.
 * @param clazz the class to check
 * @return the user-defined class
 */
public static Class<?> getUserClass(Class<?> clazz) {
  if (clazz.getName().contains(CGLIB_CLASS_SEPARATOR)) {
    Class<?> superclass = clazz.getSuperclass();
    if (superclass != null && superclass != Object.class) {
      return superclass;
    }
  }
  return clazz;
}

代码示例来源:origin: apache/incubator-dubbo

/**
 * Returns the writeReplace method
 */
protected Method getWriteReplace(Class cl, Class param) {
  for (; cl != null; cl = cl.getSuperclass()) {
    for (Method method : cl.getDeclaredMethods()) {
      if (method.getName().equals("writeReplace")
          && method.getParameterTypes().length == 1
          && param.equals(method.getParameterTypes()[0]))
        return method;
    }
  }
  return null;
}

代码示例来源:origin: apache/incubator-dubbo

/**
 * Returns the writeReplace method
 */
protected Method getWriteReplace(Class cl, Class param) {
  for (; cl != null; cl = cl.getSuperclass()) {
    for (Method method : cl.getDeclaredMethods()) {
      if (method.getName().equals("writeReplace")
          && method.getParameterTypes().length == 1
          && param.equals(method.getParameterTypes()[0]))
        return method;
    }
  }
  return null;
}

代码示例来源:origin: google/guava

@Override
 @Nullable
 Class<?> getSuperclass(Class<?> type) {
  return type.getSuperclass();
 }
};

代码示例来源:origin: spring-projects/spring-framework

private static Class<?> findParameterizedTypeReferenceSubclass(Class<?> child) {
  Class<?> parent = child.getSuperclass();
  if (Object.class == parent) {
    throw new IllegalStateException("Expected ParameterizedTypeReference superclass");
  }
  else if (ParameterizedTypeReference.class == parent) {
    return child;
  }
  else {
    return findParameterizedTypeReferenceSubclass(parent);
  }
}

代码示例来源:origin: spring-projects/spring-framework

private int getDepth(String exceptionMapping, Class<?> exceptionClass, int depth) {
  if (exceptionClass.getName().contains(exceptionMapping)) {
    // Found it!
    return depth;
  }
  // If we've gone as far as we can go and haven't found it...
  if (exceptionClass == Throwable.class) {
    return -1;
  }
  return getDepth(exceptionMapping, exceptionClass.getSuperclass(), depth + 1);
}

代码示例来源:origin: spring-projects/spring-framework

public static List addAllInterfaces(Class type, List list) {
  Class superclass = type.getSuperclass();
  if (superclass != null) {
    list.addAll(Arrays.asList(type.getInterfaces()));
    addAllInterfaces(superclass, list);
  }
  return list;
}

代码示例来源:origin: spring-projects/spring-framework

private int getDepth(Class<?> exceptionClass, int depth) {
  if (exceptionClass.getName().contains(this.exceptionName)) {
    // Found it!
    return depth;
  }
  // If we've gone as far as we can go and haven't found it...
  if (exceptionClass == Throwable.class) {
    return -1;
  }
  return getDepth(exceptionClass.getSuperclass(), depth + 1);
}

代码示例来源:origin: spring-projects/spring-framework

private int getDepth(Class<?> declaredException, Class<?> exceptionToMatch, int depth) {
  if (exceptionToMatch.equals(declaredException)) {
    // Found it!
    return depth;
  }
  // If we've gone as far as we can go and haven't found it...
  if (exceptionToMatch == Throwable.class) {
    return Integer.MAX_VALUE;
  }
  return getDepth(declaredException, exceptionToMatch.getSuperclass(), depth + 1);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
  Field field = ReflectionUtils.findField(obj.getClass(), BEAN_FACTORY_FIELD);
  Assert.state(field != null, "Unable to find generated BeanFactory field");
  field.set(obj, args[0]);
  // Does the actual (non-CGLIB) superclass implement BeanFactoryAware?
  // If so, call its setBeanFactory() method. If not, just exit.
  if (BeanFactoryAware.class.isAssignableFrom(ClassUtils.getUserClass(obj.getClass().getSuperclass()))) {
    return proxy.invokeSuper(obj, args);
  }
  return null;
}

代码示例来源:origin: spring-projects/spring-framework

@Override
  public Object postProcessBeforeInitialization(Object bean, String beanName)  {
    if (bean instanceof ImportAware) {
      ImportRegistry ir = this.beanFactory.getBean(IMPORT_REGISTRY_BEAN_NAME, ImportRegistry.class);
      AnnotationMetadata importingClass = ir.getImportingClassFor(bean.getClass().getSuperclass().getName());
      if (importingClass != null) {
        ((ImportAware) bean).setImportMetadata(importingClass);
      }
    }
    return bean;
  }
}

代码示例来源:origin: google/guava

/** [descendant, ancestor) */
private static Set<Class<?>> getClassesBetween(Class<?> descendant, Class<?> ancestor) {
 Set<Class<?>> classes = newHashSet();
 while (!descendant.equals(ancestor)) {
  classes.add(descendant);
  descendant = descendant.getSuperclass();
 }
 return classes;
}

代码示例来源:origin: spring-projects/spring-framework

public SourceClass getSuperClass() throws IOException {
  if (this.source instanceof Class) {
    return asSourceClass(((Class<?>) this.source).getSuperclass());
  }
  return asSourceClass(((MetadataReader) this.source).getClassMetadata().getSuperClassName());
}

代码示例来源:origin: google/guava

public void testGetGenericSuperclass_withSuperclass() {
 TypeToken<? super ArrayList<String>> superToken =
   new TypeToken<ArrayList<String>>() {}.getGenericSuperclass();
 assertEquals(ArrayList.class.getSuperclass(), superToken.getRawType());
 assertEquals(
   String.class, ((ParameterizedType) superToken.getType()).getActualTypeArguments()[0]);
 assertEquals(TypeToken.of(Base.class), TypeToken.of(Sub.class).getGenericSuperclass());
 assertEquals(TypeToken.of(Object.class), TypeToken.of(Sub[].class).getGenericSuperclass());
}

代码示例来源:origin: google/guava

public <T, T1 extends T> void testAssignableGenericArrayToClass() {
 assertTrue(TypeToken.of(Object[].class.getSuperclass()).isSupertypeOf(new TypeToken<T[]>() {}));
 for (Class<?> interfaceType : Object[].class.getInterfaces()) {
  assertTrue(TypeToken.of(interfaceType).isSupertypeOf(new TypeToken<T[]>() {}));
 }
 assertTrue(TypeToken.of(Object.class).isSupertypeOf(new TypeToken<T[]>() {}));
 assertFalse(TypeToken.of(String.class).isSupertypeOf(new TypeToken<T[]>() {}));
}

相关文章

微信公众号

最新文章

更多

Class类方法