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

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

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

Class.asSubclass介绍

[英]Casts this Class to represent a subclass of the given class. If successful, this Class is returned; otherwise a ClassCastException is thrown.
[中]强制转换该类以表示给定类的子类。如果成功,则返回该类;否则将抛出ClassCastException。

代码示例

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

/**
 * Cast the given type to a subtype of {@link Enum}.
 * @param enumType the enum type, never {@code null}
 * @return the given type as subtype of {@link Enum}
 * @throws IllegalArgumentException if the given type is not a subtype of {@link Enum}
 */
@SuppressWarnings("rawtypes")
private static Class<? extends Enum> asEnumType(Class<?> enumType) {
  Assert.notNull(enumType, "Enum type must not be null");
  if (!Enum.class.isAssignableFrom(enumType)) {
    throw new IllegalArgumentException("Supplied type is not an enum: " + enumType.getName());
  }
  return enumType.asSubclass(Enum.class);
}

代码示例来源:origin: junit-team/junit4

public JUnit38ClassRunner(Class<?> klass) {
  this(new TestSuite(klass.asSubclass(TestCase.class)));
}

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

@Override
 protected Boolean computeValue(Class<?> type) {
  checkExceptionClassValidity(type.asSubclass(Exception.class));
  return true;
 }
};

代码示例来源:origin: apache/kafka

/**
 * Look up a class by name.
 * @param klass class name
 * @param base super class of the class for verification
 * @param <T> the type of the base class
 * @return the new class
 */
public static <T> Class<? extends T> loadClass(String klass, Class<T> base) throws ClassNotFoundException {
  return Class.forName(klass, true, Utils.getContextOrKafkaClassLoader()).asSubclass(base);
}

代码示例来源:origin: prestodb/presto

private static AWSCredentialsProvider getCustomAWSCredentialsProvider(Configuration conf, String providerClass)
{
  try {
    return conf.getClassByName(providerClass)
        .asSubclass(AWSCredentialsProvider.class)
        .getConstructor(URI.class, Configuration.class)
        .newInstance(null, conf);
  }
  catch (ReflectiveOperationException e) {
    throw new RuntimeException(format("Error creating an instance of %s", providerClass), e);
  }
}

代码示例来源:origin: prestodb/presto

@Override
 protected Boolean computeValue(Class<?> type) {
  checkExceptionClassValidity(type.asSubclass(Exception.class));
  return true;
 }
};

代码示例来源:origin: prestodb/presto

@SuppressWarnings({"unchecked", "RedundantCast"})
private static Class<? extends InputFormat<?, ?>> getInputFormatClass(JobConf conf, String inputFormatName)
    throws ClassNotFoundException
{
  // CDH uses different names for Parquet
  if ("parquet.hive.DeprecatedParquetInputFormat".equals(inputFormatName) ||
      "parquet.hive.MapredParquetInputFormat".equals(inputFormatName)) {
    return MapredParquetInputFormat.class;
  }
  Class<?> clazz = conf.getClassByName(inputFormatName);
  return (Class<? extends InputFormat<?, ?>>) clazz.asSubclass(InputFormat.class);
}

代码示例来源:origin: junit-team/junit4

private Test testCaseForClass(Class<?> each) {
  if (TestCase.class.isAssignableFrom(each)) {
    return new TestSuite(each.asSubclass(TestCase.class));
  } else {
    return warning(each.getCanonicalName() + " does not extend TestCase");
  }
}

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

private Test testCaseForClass(Class<?> each) {
  if (TestCase.class.isAssignableFrom(each)) {
    return new TestSuite(each.asSubclass(TestCase.class));
  } else {
    return warning(each.getCanonicalName() + " does not extend TestCase");
  }
}

代码示例来源:origin: jenkinsci/jenkins

@Override
public List<? extends ToolInstaller> getDefaultInstallers() {
  try {
    Class<? extends ToolInstaller> jdkInstallerClass = Jenkins.getInstance().getPluginManager()
        .uberClassLoader.loadClass("hudson.tools.JDKInstaller").asSubclass(ToolInstaller.class);
    Constructor<? extends ToolInstaller> constructor = jdkInstallerClass.getConstructor(String.class, boolean.class);
    return Collections.singletonList(constructor.newInstance(null, false));
  } catch (ClassNotFoundException e) {
    return Collections.emptyList();
  } catch (Exception e) {
    LOGGER.log(Level.WARNING, "Unable to get default installer", e);
    return Collections.emptyList();
  }
}

代码示例来源:origin: prestodb/presto

private static AWSCredentialsProvider getCustomAWSCredentialsProvider(URI uri, Configuration conf, String providerClass)
{
  try {
    log.debug("Using AWS credential provider %s for URI %s", providerClass, uri);
    return conf.getClassByName(providerClass)
        .asSubclass(AWSCredentialsProvider.class)
        .getConstructor(URI.class, Configuration.class)
        .newInstance(uri, conf);
  }
  catch (ReflectiveOperationException e) {
    throw new RuntimeException(format("Error creating an instance of %s for URI %s", providerClass, uri), e);
  }
}

代码示例来源:origin: apache/flink

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    // read the parent fields and the final fields
    in.defaultReadObject();

    try {
      Class<? extends Writable> writableSplit = splitType.asSubclass(Writable.class);
      mapreduceInputSplit = (org.apache.hadoop.mapreduce.InputSplit) WritableFactories.newInstance(writableSplit);
    }

    catch (Exception e) {
      throw new RuntimeException("Unable to instantiate the Hadoop InputSplit", e);
    }

    ((Writable) mapreduceInputSplit).readFields(in);
  }
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * @deprecated Use {@link #getAction(Class)} on {@code AbstractTestResultAction}.
 */
@Deprecated
public Action getTestResultAction() {
  try {
    return getAction(Jenkins.getInstance().getPluginManager().uberClassLoader.loadClass("hudson.tasks.test.AbstractTestResultAction").asSubclass(Action.class));
  } catch (ClassNotFoundException x) {
    return null;
  }
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * @deprecated Use {@link #getAction(Class)} on {@code AggregatedTestResultAction}.
 */
@Deprecated
public Action getAggregatedTestResultAction() {
  try {
    return getAction(Jenkins.getInstance().getPluginManager().uberClassLoader.loadClass("hudson.tasks.test.AggregatedTestResultAction").asSubclass(Action.class));
  } catch (ClassNotFoundException x) {
    return null;
  }
}

代码示例来源:origin: prestodb/presto

private static <T> T newInstance(String className, Class<T> superType)
    throws ReflectiveOperationException
{
  return HiveStorageFormat.class.getClassLoader().loadClass(className).asSubclass(superType).getConstructor().newInstance();
}

代码示例来源:origin: prestodb/presto

private static Constructor<? extends RecordWriter> getOrcWriterConstructor()
{
  try {
    Constructor<? extends RecordWriter> constructor = OrcOutputFormat.class.getClassLoader()
        .loadClass(ORC_RECORD_WRITER)
        .asSubclass(RecordWriter.class)
        .getDeclaredConstructor(Path.class, WriterOptions.class);
    constructor.setAccessible(true);
    return constructor;
  }
  catch (ReflectiveOperationException e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: junit-team/junit4

protected TestResult runSingleMethod(String testCase, String method, boolean wait) throws Exception {
  Class<? extends TestCase> testClass = loadSuiteClass(testCase).asSubclass(TestCase.class);
  Test test = TestSuite.createTest(testClass, method);
  return doRun(test, wait);
}

代码示例来源:origin: junit-team/junit4

static FilterFactory createFilterFactory(String filterFactoryFqcn) throws FilterNotCreatedException {
  Class<? extends FilterFactory> filterFactoryClass;
  try {
    filterFactoryClass = Classes.getClass(filterFactoryFqcn).asSubclass(FilterFactory.class);
  } catch (Exception e) {
    throw new FilterNotCreatedException(e);
  }
  return createFilterFactory(filterFactoryClass);
}

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

protected TestResult runSingleMethod(String testCase, String method, boolean wait) throws Exception {
  Class<? extends TestCase> testClass = loadSuiteClass(testCase).asSubclass(TestCase.class);
  Test test = TestSuite.createTest(testClass, method);
  return doRun(test, wait);
}

代码示例来源:origin: jenkinsci/jenkins

public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    try {
      DescribableList r = (DescribableList) context.getRequiredType().asSubclass(DescribableList.class).newInstance();
      CopyOnWriteList core = copyOnWriteListConverter.unmarshal(reader, context);
      r.data.replaceBy(core);
      return r;
    } catch (InstantiationException e) {
      InstantiationError x = new InstantiationError();
      x.initCause(e);
      throw x;
    } catch (IllegalAccessException e) {
      IllegalAccessError x = new IllegalAccessError();
      x.initCause(e);
      throw x;
    }
  }
}

相关文章

Class类方法