From 1a27b3f9af6883206220e793b7ef71af6613dbab Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Sun, 29 Aug 2021 21:08:01 +0200 Subject: Bug 575698 - Simplify creation of Enumerations Change-Id: Iee43928d4fa726008267411ab8ff733bf4e0bad6 Signed-off-by: Hannes Wellmann Reviewed-on: https://git.eclipse.org/r/c/equinox/rt.equinox.framework/+/184747 Tested-by: Equinox Bot Reviewed-by: Thomas Watson --- .../META-INF/MANIFEST.MF | 2 +- bundles/org.eclipse.equinox.launcher/pom.xml | 2 +- .../src/org/eclipse/equinox/launcher/Main.java | 18 +------ .../connect/DelegatingConnectClassLoader.java | 4 +- .../osgi/internal/container/InternalUtils.java | 14 +++++ .../osgi/internal/framework/ContextFinder.java | 4 +- .../eclipse/osgi/internal/loader/BundleLoader.java | 19 ++----- .../loader/classpath/ClasspathManager.java | 14 ++--- .../log/ExtendedLogReaderServiceFactory.java | 5 +- .../osgi/internal/permadmin/BundlePermissions.java | 22 ++------ .../permadmin/PermissionInfoCollection.java | 3 +- .../osgi/internal/permadmin/SecurityTable.java | 3 +- .../eclipse/osgi/storage/ManifestLocalization.java | 3 +- .../src/org/eclipse/osgi/storage/Storage.java | 59 ++++------------------ .../supplement/META-INF/MANIFEST.MF | 2 +- bundles/org.eclipse.osgi/supplement/pom.xml | 2 +- .../equinox/log/LogPermissionCollection.java | 20 +------- 17 files changed, 53 insertions(+), 143 deletions(-) diff --git a/bundles/org.eclipse.equinox.launcher/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.launcher/META-INF/MANIFEST.MF index e51c25b5b..e236d4ac6 100644 --- a/bundles/org.eclipse.equinox.launcher/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.equinox.launcher/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: org.eclipse.equinox.launcher;singleton:=true -Bundle-Version: 1.6.300.qualifier +Bundle-Version: 1.6.400.qualifier Main-Class: org.eclipse.equinox.launcher.Main Bundle-ClassPath: . Bundle-Vendor: %providerName diff --git a/bundles/org.eclipse.equinox.launcher/pom.xml b/bundles/org.eclipse.equinox.launcher/pom.xml index 5db5cc7fd..cce13f260 100644 --- a/bundles/org.eclipse.equinox.launcher/pom.xml +++ b/bundles/org.eclipse.equinox.launcher/pom.xml @@ -19,6 +19,6 @@ org.eclipse.equinox org.eclipse.equinox.launcher - 1.6.300-SNAPSHOT + 1.6.400-SNAPSHOT eclipse-plugin diff --git a/bundles/org.eclipse.equinox.launcher/src/org/eclipse/equinox/launcher/Main.java b/bundles/org.eclipse.equinox.launcher/src/org/eclipse/equinox/launcher/Main.java index d37984add..6c215bec7 100644 --- a/bundles/org.eclipse.equinox.launcher/src/org/eclipse/equinox/launcher/Main.java +++ b/bundles/org.eclipse.equinox.launcher/src/org/eclipse/equinox/launcher/Main.java @@ -2629,23 +2629,7 @@ public class Main { @Override public Enumeration elements() { - return new Enumeration() { - int cur = 0; - - @Override - public boolean hasMoreElements() { - return cur < 1; - } - - @Override - public Permission nextElement() { - if (cur == 0) { - cur = 1; - return allPermission; - } - throw new NoSuchElementException(); - } - }; + return Collections.enumeration(Collections.singleton(allPermission)); } }; } diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/connect/DelegatingConnectClassLoader.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/connect/DelegatingConnectClassLoader.java index 9b16f162d..e4be48b6b 100644 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/connect/DelegatingConnectClassLoader.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/connect/DelegatingConnectClassLoader.java @@ -56,12 +56,12 @@ public class DelegatingConnectClassLoader extends EquinoxClassLoader { @Override public Enumeration findLocalResources(String resource) { if (connectClassLoader == null) { - return Collections.enumeration(Collections.emptyList()); + return Collections.emptyEnumeration(); } try { return connectClassLoader.getResources(resource); } catch (IOException e) { - return Collections.enumeration(Collections.emptyList()); + return Collections.emptyEnumeration(); } } } diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/container/InternalUtils.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/container/InternalUtils.java index 9f3c7be25..08ac1c32a 100644 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/container/InternalUtils.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/container/InternalUtils.java @@ -18,6 +18,7 @@ import java.security.SecureRandom; import java.util.AbstractList; import java.util.ArrayList; import java.util.Collection; +import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.Random; @@ -196,4 +197,17 @@ public class InternalUtils { return new UUID(mostSignificantBits, leastSignificantBits).toString(); } + public static Enumeration asEnumeration(Iterator it) { + return new Enumeration() { + @Override + public boolean hasMoreElements() { + return it.hasNext(); + } + + @Override + public E nextElement() { + return it.next(); + } + }; + } } diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/ContextFinder.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/ContextFinder.java index 1cf990985..29b05e3f8 100644 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/ContextFinder.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/ContextFinder.java @@ -169,8 +169,8 @@ public class ContextFinder extends ClassLoader implements PrivilegedAction getResources(String arg0) throws IOException { //Shortcut cycle - if (startLoading(arg0) == false) { - return Collections.enumeration(Collections.emptyList()); + if (!startLoading(arg0)) { + return Collections.emptyEnumeration(); } try { List toConsult = findClassLoaders(); diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/loader/BundleLoader.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/loader/BundleLoader.java index b90ca0db1..e966a8229 100644 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/loader/BundleLoader.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/loader/BundleLoader.java @@ -86,10 +86,6 @@ public class BundleLoader extends ModuleLoader { private static final Pattern PACKAGENAME_FILTER = Pattern.compile("\\(osgi.wiring.package\\s*=\\s*([^)]+)\\)"); //$NON-NLS-1$ - // TODO needed instead of using Collections.emptyEnumertion until we no longer support Java 6 - @SuppressWarnings("rawtypes") - private final static Enumeration EMPTY_ENUMERATION = Collections.enumeration(Collections.emptyList()); - private final ModuleWiring wiring; private final EquinoxContainer container; private final Debug debug; @@ -685,7 +681,7 @@ public class BundleLoader extends ModuleLoader { if ((name.length() > 1) && (name.charAt(0) == '/')) /* if name has a leading slash */ name = name.substring(1); /* remove leading slash before search */ String pkgName = getResourcePackageName(name); - Enumeration result = emptyEnumeration(); + Enumeration result = Collections.emptyEnumeration(); boolean bootDelegation = false; // follow the OSGi delegation model // First check the parent classloader for system resources, if it is a java resource. @@ -839,12 +835,10 @@ public class BundleLoader extends ModuleLoader { public static Enumeration compoundEnumerations(Enumeration list1, Enumeration list2) { if (list2 == null || !list2.hasMoreElements()) - return list1 == null ? BundleLoader.emptyEnumeration() : list1; + return list1 == null ? Collections.emptyEnumeration() : list1; if (list1 == null || !list1.hasMoreElements()) - return list2 == null ? BundleLoader.emptyEnumeration() : list2; - List compoundResults = new ArrayList<>(); - while (list1.hasMoreElements()) - compoundResults.add(list1.nextElement()); + return list2 == null ? Collections.emptyEnumeration() : list2; + List compoundResults = Collections.list(list1); while (list2.hasMoreElements()) { E item = list2.nextElement(); if (!compoundResults.contains(item)) //don't add duplicates @@ -853,11 +847,6 @@ public class BundleLoader extends ModuleLoader { return Collections.enumeration(compoundResults); } - @SuppressWarnings("unchecked") - private static Enumeration emptyEnumeration() { - return EMPTY_ENUMERATION; - } - /** * Finds a resource local to this bundle. Only the classloader for this bundle is searched. * @param name The name of the resource to find. diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/loader/classpath/ClasspathManager.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/loader/classpath/ClasspathManager.java index c5c7f36e7..cf684bb44 100644 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/loader/classpath/ClasspathManager.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/loader/classpath/ClasspathManager.java @@ -63,8 +63,6 @@ import org.osgi.framework.namespace.HostNamespace; public class ClasspathManager { private static final FragmentClasspath[] emptyFragments = new FragmentClasspath[0]; private static final String[] DEFAULT_CLASSPATH = new String[] {"."}; //$NON-NLS-1$ - @SuppressWarnings("unchecked") - private static final Enumeration EMPTY_ENUMERATION = Collections.enumeration(Collections.EMPTY_LIST); private final Generation generation; private final ModuleClassLoader classloader; @@ -445,7 +443,7 @@ public class ClasspathManager { ClasspathEntry[] hookEntries = hook.getClassPathEntries(resource, this); if (hookEntries != null) { findLocalResources(resource, hookEntries, m, classPathIndex, resources); - return resources.size() > 0 ? Collections.enumeration(resources) : EMPTY_ENUMERATION; + return resources.size() > 0 ? Collections.enumeration(resources) : Collections.emptyEnumeration(); } } @@ -459,7 +457,7 @@ public class ClasspathManager { if (resources.size() > 0) return Collections.enumeration(resources); - return EMPTY_ENUMERATION; + return Collections.emptyEnumeration(); } private void findLocalResources(String resource, ClasspathEntry[] cpEntries, Module m, int[] classPathIndex, List resources) { @@ -874,15 +872,11 @@ public class ClasspathManager { for (FragmentClasspath fragmentClasspath : currentFragments) generations.add(fragmentClasspath.getGeneration()); - List result = Collections.emptyList(); // now search over all the bundle files Enumeration eURLs = Storage.findEntries(generations, path, filePattern, options); if (eURLs == null) - return result; - result = new ArrayList<>(); - while (eURLs.hasMoreElements()) - result.add(eURLs.nextElement()); - return Collections.unmodifiableList(result); + return Collections.emptyList(); + return Collections.unmodifiableList(Collections.list(eURLs)); } /** diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/log/ExtendedLogReaderServiceFactory.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/log/ExtendedLogReaderServiceFactory.java index 6f8efebaa..b8aa9d2d4 100644 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/log/ExtendedLogReaderServiceFactory.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/log/ExtendedLogReaderServiceFactory.java @@ -57,9 +57,6 @@ public class ExtendedLogReaderServiceFactory implements ServiceFactory EMPTY_ENUMERATION = Collections.enumeration(Collections.EMPTY_LIST); - static final LogFilter NULL_LOGGER_FILTER = (b, loggerName, logLevel) -> true; private static final LogFilter[] ALWAYS_LOG = new LogFilter[0]; @@ -311,7 +308,7 @@ public class ExtendedLogReaderServiceFactory implements ServiceFactory getLog() { if (history == null) { - return EMPTY_ENUMERATION; + return Collections.emptyEnumeration(); } synchronized (history) { return Collections.enumeration(new ArrayList<>(history)); diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/permadmin/BundlePermissions.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/permadmin/BundlePermissions.java index 2d874bd4b..fd7d2a722 100644 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/permadmin/BundlePermissions.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/permadmin/BundlePermissions.java @@ -13,28 +13,17 @@ *******************************************************************************/ package org.eclipse.osgi.internal.permadmin; -import java.security.*; +import java.security.Permission; +import java.security.PermissionCollection; +import java.security.Permissions; +import java.util.Collections; import java.util.Enumeration; -import java.util.NoSuchElementException; import org.osgi.framework.Bundle; import org.osgi.framework.PackagePermission; public final class BundlePermissions extends PermissionCollection { private static final long serialVersionUID = -5443618108312606612L; - // Note that this forces the Enumeration inner class to be loaded as soon as possible (see bug 119069) - static final Enumeration EMPTY_ENUMERATION = new Enumeration() { - @Override - public boolean hasMoreElements() { - return false; - } - - @Override - public Permission nextElement() { - throw new NoSuchElementException(); - } - }; - private final Bundle bundle; private final SecurityAdmin securityAdmin; private final PermissionInfoCollection impliedPermissions; @@ -75,8 +64,7 @@ public final class BundlePermissions extends PermissionCollection { public Enumeration elements() { // TODO return an empty enumeration for now; // It does not seem possible to do this properly with multiple exports and conditional permissions. - // When looking to fix this be sure the Enumeration class is loaded as soon as possible (see bug 119069) - return EMPTY_ENUMERATION; + return Collections.emptyEnumeration(); } @Override diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/permadmin/PermissionInfoCollection.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/permadmin/PermissionInfoCollection.java index 34b895bcf..3c0d38d6b 100644 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/permadmin/PermissionInfoCollection.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/permadmin/PermissionInfoCollection.java @@ -22,6 +22,7 @@ import java.security.Permission; import java.security.PermissionCollection; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; +import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; @@ -70,7 +71,7 @@ public final class PermissionInfoCollection extends PermissionCollection { @Override public Enumeration elements() { // TODO return an empty enumeration for now; - return BundlePermissions.EMPTY_ENUMERATION; + return Collections.emptyEnumeration(); } @Override diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/permadmin/SecurityTable.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/permadmin/SecurityTable.java index 7fc0837a2..55abf9ade 100644 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/permadmin/SecurityTable.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/permadmin/SecurityTable.java @@ -16,6 +16,7 @@ package org.eclipse.osgi.internal.permadmin; import java.security.Permission; import java.security.PermissionCollection; +import java.util.Collections; import java.util.Enumeration; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -199,7 +200,7 @@ public class SecurityTable extends PermissionCollection { @Override public Enumeration elements() { - return BundlePermissions.EMPTY_ENUMERATION; + return Collections.emptyEnumeration(); } @Override diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/storage/ManifestLocalization.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/storage/ManifestLocalization.java index ce0025b21..0d48cb8dc 100644 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/storage/ManifestLocalization.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/storage/ManifestLocalization.java @@ -254,10 +254,9 @@ public class ManifestLocalization { this.localeString = locale; } - @SuppressWarnings("unchecked") @Override public Enumeration getKeys() { - return Collections.enumeration(Collections.EMPTY_LIST); + return Collections.emptyEnumeration(); } @Override diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/storage/Storage.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/storage/Storage.java index 2f6d93984..7af943e77 100644 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/storage/Storage.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/storage/Storage.java @@ -47,11 +47,12 @@ import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; -import java.util.NoSuchElementException; +import java.util.Objects; import java.util.Properties; import java.util.Set; import java.util.StringTokenizer; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.stream.Stream; import org.eclipse.core.runtime.adaptor.EclipseStarter; import org.eclipse.osgi.container.Module; import org.eclipse.osgi.container.ModuleCapability; @@ -68,6 +69,7 @@ import org.eclipse.osgi.framework.log.FrameworkLogEntry; import org.eclipse.osgi.framework.util.FilePath; import org.eclipse.osgi.framework.util.ObjectPool; import org.eclipse.osgi.framework.util.SecureAction; +import org.eclipse.osgi.internal.container.InternalUtils; import org.eclipse.osgi.internal.debug.Debug; import org.eclipse.osgi.internal.framework.EquinoxConfiguration; import org.eclipse.osgi.internal.framework.EquinoxContainer; @@ -1993,50 +1995,10 @@ public class Storage { // return null if no entries found if (pathList.size() == 0) return null; - // create an enumeration to enumerate the pathList - final String[] pathArray = pathList.toArray(new String[pathList.size()]); - final Generation[] generationArray = generations.toArray(new Generation[generations.size()]); - return new Enumeration() { - private int curPathIndex = 0; - private int curDataIndex = 0; - private URL nextElement = null; - - @Override - public boolean hasMoreElements() { - if (nextElement != null) - return true; - getNextElement(); - return nextElement != null; - } - - @Override - public URL nextElement() { - if (!hasMoreElements()) - throw new NoSuchElementException(); - URL result = nextElement; - // force the next element search - getNextElement(); - return result; - } - - private void getNextElement() { - nextElement = null; - if (curPathIndex >= pathArray.length) - // reached the end of the pathArray; no more elements - return; - while (nextElement == null && curPathIndex < pathArray.length) { - String curPath = pathArray[curPathIndex]; - // search the generation until we have searched them all - while (nextElement == null && curDataIndex < generationArray.length) - nextElement = generationArray[curDataIndex++].getEntry(curPath); - // we have searched all datas then advance to the next path - if (curDataIndex >= generationArray.length) { - curPathIndex++; - curDataIndex = 0; - } - } - } - }; + // create an enumeration to enumerate the pathList (generations must not change) + Stream entries = pathList.stream().flatMap(p -> generations.stream().map(g -> g.getEntry(p))) + .filter(Objects::nonNull); + return InternalUtils.asEnumeration(entries.iterator()); } /** @@ -2136,11 +2098,8 @@ public class Storage { private static LinkedHashSet listEntryPaths(BundleFile bundleFile, String path, Filter patternFilter, Hashtable patternProps, int options, LinkedHashSet pathList) { if (pathList == null) pathList = new LinkedHashSet<>(); - Enumeration entryPaths; - if ((options & BundleWiring.FINDENTRIES_RECURSE) != 0) - entryPaths = bundleFile.getEntryPaths(path, true); - else - entryPaths = bundleFile.getEntryPaths(path); + boolean recurse = (options & BundleWiring.FINDENTRIES_RECURSE) != 0; + Enumeration entryPaths = bundleFile.getEntryPaths(path, recurse); if (entryPaths == null) return pathList; while (entryPaths.hasMoreElements()) { diff --git a/bundles/org.eclipse.osgi/supplement/META-INF/MANIFEST.MF b/bundles/org.eclipse.osgi/supplement/META-INF/MANIFEST.MF index b4a9f5f89..547a77372 100644 --- a/bundles/org.eclipse.osgi/supplement/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.osgi/supplement/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: org.eclipse.equinox.supplement -Bundle-Version: 1.10.200.qualifier +Bundle-Version: 1.10.300.qualifier Bundle-Vendor: %providerName Bundle-Localization: plugin Export-Package: org.eclipse.equinox.log;version="1.1", diff --git a/bundles/org.eclipse.osgi/supplement/pom.xml b/bundles/org.eclipse.osgi/supplement/pom.xml index d1345a76d..d430ff8c1 100644 --- a/bundles/org.eclipse.osgi/supplement/pom.xml +++ b/bundles/org.eclipse.osgi/supplement/pom.xml @@ -21,7 +21,7 @@ org.eclipse.equinox org.eclipse.equinox.supplement - 1.10.200-SNAPSHOT + 1.10.300-SNAPSHOT eclipse-plugin diff --git a/bundles/org.eclipse.osgi/supplement/src/org/eclipse/equinox/log/LogPermissionCollection.java b/bundles/org.eclipse.osgi/supplement/src/org/eclipse/equinox/log/LogPermissionCollection.java index f9e2fb0fe..da037a070 100644 --- a/bundles/org.eclipse.osgi/supplement/src/org/eclipse/equinox/log/LogPermissionCollection.java +++ b/bundles/org.eclipse.osgi/supplement/src/org/eclipse/equinox/log/LogPermissionCollection.java @@ -12,8 +12,8 @@ package org.eclipse.equinox.log; import java.security.Permission; import java.security.PermissionCollection; +import java.util.Collections; import java.util.Enumeration; -import java.util.NoSuchElementException; /** * Stores a set of LogPermission permissions. @@ -40,23 +40,7 @@ public final class LogPermissionCollection extends PermissionCollection { @Override public Enumeration elements() { - return new Enumeration() { - private boolean hasMore = (logPermission != null); - - @Override - public boolean hasMoreElements() { - return hasMore; - } - - @Override - public Permission nextElement() { - if (hasMore) { - hasMore = false; - return logPermission; - } - throw new NoSuchElementException(); - } - }; + return logPermission != null ? Collections.enumeration(Collections.singleton(logPermission)) : Collections.emptyEnumeration(); } @Override -- cgit v1.2.3