我正在使用Javassist库在运行时加载类。

public class FilterClassGenerator<T, DT> {
   private volatile static Long classNameIncrementor = 1L;

   private Class<T> listFilterClass;
   private Class<DT> dataObjectClass;

   public FilterClassGenerator(Class<T> listFilterClass, Class<DT> dataObjectClass) {
       this.listFilterClass = listFilterClass;
       this.dataObjectClass = dataObjectClass;
   }

   public T createFilterClass(ApiFilter filter) {
       try {
           ClassPool pool = ClassPool.getDefault();
           CtClass comparison = pool.makeClass("com.frammo.filterClasses." + generateNewClassName());
           comparison.setSuperclass(pool.get(listFilterClass.getName()));
           // More code

我在以下位置收到以下错误: comparison.setSuperclass(pool.get(listFilterClass.getName()));
javassist.NotFoundException: com.frammo.stocks.api.simulation.IiiAccountComparisonFilter

但显然存在此类!
package com.frammo.stocks.api.simulation;

// Imports

public abstract class IiiAccountComparisonFilter extends ListComparisonFilter<IiiAccount> {

}

有什么想法为什么找不到类(class)?

最佳答案

当类池未在所有可用搜索路径中找到类时,通常会发生这种情况。要解决此问题,您需要将类文件的类路径添加到类池的搜索路径中,如下所示:

    ClassPool.getDefault().insertClassPath(new ClassClassPath(YourCurrentClassName.class));

10-05 19:34