首页 > 解决方案 > 为什么返回值对象在 URLClassPath 的 getResources 函数中有两个额外的字段?

问题描述

我正在阅读spring boot的源代码。我在阅读时发现了一个问题SpringFactoriesLoader.loadFactoryNames。返回的URLClassPath.getResources实现Enumeration<Resource>,但值有额外的两个字段namecheck。那么,什么时候将这两个字段添加到返回值中呢?

public Enumeration<Resource> getResources(final String var1, final boolean var2) {
        return new Enumeration<Resource>() {
            private int index = 0;
            private int[] cache = URLClassPath.this.getLookupCache(var1);
            private Resource res = null;

            private boolean next() {
                if (this.res != null) {
                    return true;
                } else {
                    do {
                        URLClassPath.Loader var1x;
                        if ((var1x = URLClassPath.this.getNextLoader(this.cache, this.index++)) == null) {
                            return false;
                        }

                        this.res = var1x.getResource(var1, var2);
                    } while(this.res == null);

                    return true;
                }
            }

            public boolean hasMoreElements() {
                return this.next();
            }

            public Resource nextElement() {
                if (!this.next()) {
                    throw new NoSuchElementException();
                } else {
                    Resource var1x = this.res;
                    this.res = null;
                    return var1x;
                }
            }
        };
    }

我正在使用 Intellij 调试程序,结果是

调试结果

标签: javaspring-boot

解决方案


在这种情况下,结果实例是一个匿名类,它捕获getResources参数的值(final String var1, final boolean var2)


推荐阅读