简体   繁体   English

Guava Google在Java中使用Reflections

[英]Guava Google using Reflections in java

I am trying to use Reflections in my Java project (also using Spring boot), and I have to get all the classes of a package that implements an interface. 我试图在我的Java项目(也使用Spring boot)中使用Reflections,并且我必须获取实现接口的包的所有类。

public static Object[] getClasses(String packageName) throws IOException
  {
    Reflections r = new Reflections(packageName);
    Set<Class<? extends EntityXML>> allClasses = r.getSubTypesOf(EntityXML.class);
    return (Object[]) allClasses.toArray();
  }

But it returns me always this error. 但这总是使我返回此错误。

java.lang.NoSuchMethodError: com.google.common.collect.Sets$SetView.iterator()Lcom/google/common/collect/UnmodifiableIterator;
        at org.reflections.Reflections.expandSuperTypes(Reflections.java:380)
        at org.reflections.Reflections.<init>(Reflections.java:126)
        at org.reflections.Reflections.<init>(Reflections.java:168)
        at org.reflections.Reflections.<init>(Reflections.java:141)
        at gcs.fds.focus.lib.utils.FindPackages.getClasses(FindPackages.java:14)
        ...

I try to import the google.guava package to avoid this error, and it fix it, but I would like not to import it, because it is a package without use in my project. 我尝试导入google.guava程序包以避免此错误,并对其进行了修复,但我不希望将其导入,因为它是未在我的项目中使用的程序包。

Any idea about how to avoid this error? 关于如何避免此错误的任何想法?

Okey I solved! 我解决了!

I deleted the google.guava dependency, and I make a 我删除了google.guava依赖项,

mvn dependency:tree

where I could see that other of my dependencies imports also google.guava, but version 19 which I read that makes this conflict. 在这里我可以看到我的其他依赖项也导入了google.guava,但是我阅读的版本19导致了这种冲突。 (everit.json dependency), so I exclude the google.guava from this dependency and it works! (everit.json依赖项),因此我从此依赖项中排除了google.guava,并且可以使用!

<dependency>
    <groupId>org.everit.json</groupId>
    <artifactId>org.everit.json.schema</artifactId>
    <version>1.3.0</version>
    <exclusions>
        <exclusion>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
        </exclusion>
    </exclusions>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM