0

我正在尝试建立一个系统,该系统允许我对导出到文本文件的类进行子类化,而无需修改初始类。为此,我正在尝试构建一个回调列表,可以判断它们是否处理特定条目,然后使用该回调获取从文件创建的该类的实例。问题是我得到一个错误

java.lang.NoClassDefFoundError: com/google/common/base/Predicate 当我尝试运行涉及此类的任何内容时。我究竟做错了什么?

public abstract class Visibility {

private static final List<VisibilityCreationCallback> creationCallbacks;
static {
    creationCallbacks = new ArrayList<VisibilityCreationCallback>();

    Reflections reflections = new Reflections(new ConfigurationBuilder()
            .setUrls(ClasspathHelper.forPackage("com.protocase.viewer.utils.visibility"))
            .setScanners(new ResourcesScanner()));
// ... cut ...

public static Visibility importFromFile(Map<String, Object> importMap) {
   for (VisibilityCreationCallback callback: creationCallbacks) {
       if (callback.handles(importMap)) {
           return callback.create(importMap);
       }
   }

   return null;

}

public class CategoryVisibility extends Visibility{

public static VisibilityCreationCallback makeVisibilityCreationCallback () {
    return new VisibilityCreationCallback() {

        @Override
        public boolean handles(Map<String, Object> importMap) {
           return importMap.containsKey(classTag);
        }

        @Override
        public CategoryVisibility create(Map<String, Object> importMap) {
            return importPD(importMap);
        }
    };
}

/**
 * Test of matches method, of class CategoryVisibility.
 */
@Test
public void testMatches1() {
    Visibility other = new UnrestrictedVisibility();
    CategoryVisibility instance = new CategoryVisibility("A Cat");
    boolean expResult = true;
    boolean result = instance.matches(other);
    assertEquals(expResult, result);
}
4

1 回答 1

1

您只是缺少类路径中的 guava 库,而 Reflections 需要它。这是简短的答案。

更好的解决方案是使用适当的构建工具(maven、gradle、...),并让您的传递依赖项没有麻烦。

于 2015-01-26T14:08:12.723 回答