简体   繁体   English

Java反射-类加载器检测新旧软件包中的已移动类

[英]Java reflection - classloader detecting moved class in old and new package

I have been messing around with the class loader and other reflection methods, and it seems I have run into a weird bug. 我一直在搞弄类加载器和其他反射方法,看来我遇到了一个奇怪的错误。 I used to have a file, lets say "ClassA.java" in one package, lets say "com.a". 我曾经有一个文件,在一个软件包中说“ ClassA.java”,然后说“ com.a”。 However, I had to the need to move it to package "com.b". 但是,我必须将其移动到“ com.b”包中。

Now when I use this code to grab all classes in both packages, 现在,当我使用此代码获取两个软件包中的所有类时,

ClassLoader[] classLoaders = new ClassLoader[] { ClasspathHelper.contextClassLoader(), ClasspathHelper.staticClassLoader() };

    String[] packagePrefixes = {"com.a", "com.b"};
    FilterBuilder filterBuilder = new FilterBuilder();
    for (String packagePrefix : packagePrefixes)
        filterBuilder.includePackage(packagePrefix);

    ConfigurationBuilder configurationBuiler = new ConfigurationBuilder()
            .setScanners(new SubTypesScanner(false), new ResourcesScanner())
            .setUrls(ClasspathHelper.forClassLoader(classLoaders))
            .filterInputsBy(filterBuilder);

    Set<Class<?>> allClasses = new Reflections(configurationBuiler).getSubTypesOf(Object.class);

The "ClassA.java" class appears twice in the allClasses set (Because the classes are ultimately different due to their different packages) “ ClassA.java”类在allClasses集中出现两次(因为这些类最终因包不同而有所不同)

I tried cleaning and deleting/remaking the packages and files, but it seems to believe the file exists in both packages. 我尝试清理和删除/重新制作软件包和文件,但似乎认为文件同时存在于两个软件包中。

When I dragged the file into "com.b", eclipse gave me the normal pop-up and I did not check the "update fully qualified names in non-Java files". 当我将文件拖到“ com.b”中时,eclipse给了我正常的弹出窗口,并且我没有检查“更新非Java文件中的完全限定名称”。

I have a maven project. 我有一个Maven项目。 Any Ideas? 有任何想法吗? Thanks! 谢谢!

If this is a webapp, delete the server and recreate it. 如果这是一个Web应用程序,请删除服务器并重新创建。 In my case, it was a webapp deployed from within Eclipse into a tomcat server. 就我而言,这是一个从Eclipse内部署到tomcat服务器中的Web应用程序。 Nothing worked until I deleted the server and recreated it, then readded the webapp and started the server again. 在删除服务器并重新创建服务器,然后读取Webapp并重新启动服务器之前,什么都没做。 BOOM. 繁荣。 Problem solved. 问题解决了。

Some cache somewhere was not getting cleared. 某些地方的某些缓存没有清除。 Clearing tomcat's the working directory was not enough. 清除tomcat的工作目录还不够。

Hope this helps someone out there. 希望这可以帮助某人。

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

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