Changeset 12867 in josm for trunk/src


Ignore:
Timestamp:
2017-09-16T01:15:48+02:00 (7 years ago)
Author:
Don-vip
Message:

see #15159 - optimize PluginClassLoader for complex/redundant dependencies

Location:
trunk/src/org/openstreetmap/josm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/preferences/CachedProperty.java

    r12846 r12867  
    5151    public final boolean put(T value) {
    5252        // Not used
    53         throw new IllegalAccessError("You cannot use put(T). Use put(String) instead.");
     53        throw new UnsupportedOperationException("You cannot use put(T). Use put(String) instead.");
    5454    }
    5555
  • trunk/src/org/openstreetmap/josm/plugins/PluginClassLoader.java

    r12336 r12867  
    55import java.net.URLClassLoader;
    66import java.util.ArrayList;
     7import java.util.Arrays;
    78import java.util.Collection;
     9import java.util.Objects;
    810
    911import org.openstreetmap.josm.tools.Logging;
     
    1315 * <p>
    1416 * In addition to the classes in the plugin jar file, it loads classes of required
    15  * plugins. The JOSM core classes should be provided by the the parent class loader.
     17 * plugins. The JOSM core classes should be provided by the parent class loader.
    1618 * @since 12322
    1719 */
    1820public class PluginClassLoader extends URLClassLoader {
    1921
    20     Collection<PluginClassLoader> dependencies;
     22    private final Collection<PluginClassLoader> dependencies;
    2123
    2224    static {
     
    3941     * This plugin will have access to the classes of the dependent plugin
    4042     * @param dependency the class loader of the required plugin
     43     * @return {@code true} if the collection of dependencies changed as a result of the call
     44     * @since 12867
    4145     */
    42     public void addDependency(PluginClassLoader dependency) {
    43         dependencies.add(dependency);
     46    public boolean addDependency(PluginClassLoader dependency) {
     47        // Add dependency only if not already present (directly or transitively through another one)
     48        boolean result = !dependencies.contains(Objects.requireNonNull(dependency, "dependency"))
     49                && !dependencies.stream().anyMatch(pcl -> pcl.dependencies.contains(dependency))
     50                && dependencies.add(dependency);
     51        if (result) {
     52            // Now, remove top-level single dependencies, which would be children of the added one
     53            dependencies.removeIf(pcl -> pcl.dependencies.isEmpty() && dependency.dependencies.contains(pcl));
     54        }
     55        return result;
    4456    }
    4557
    4658    @Override
    4759    protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
    48         for (PluginClassLoader dep : dependencies) {
    49             try {
    50                 Class<?> result = dep.loadClass(name, resolve);
    51                 if (result != null) {
    52                     return result;
     60        Class<?> result = findLoadedClass(name);
     61        if (result == null) {
     62            for (PluginClassLoader dep : dependencies) {
     63                try {
     64                    result = dep.loadClass(name, resolve);
     65                    if (result != null) {
     66                        return result;
     67                    }
     68                } catch (ClassNotFoundException e) {
     69                    // do nothing
     70                    Logging.trace("Plugin class not found in {0}: {1}", dep, e.getMessage());
    5371                }
    54             } catch (ClassNotFoundException e) {
    55                 // do nothing
    56                 Logging.trace("Plugin class not found in {0}: {1}", dep, e.getMessage());
    5772            }
     73            result = super.loadClass(name, resolve);
    5874        }
    59         Class<?> result = super.loadClass(name, resolve);
    6075        if (result != null) {
    6176            return result;
     
    6378        throw new ClassNotFoundException(name);
    6479    }
     80
     81    @Override
     82    public String toString() {
     83        return "PluginClassLoader [urls=" + Arrays.toString(getURLs()) +
     84                (dependencies.isEmpty() ? "" : ", dependencies=" + dependencies) + ']';
     85    }
    6586}
Note: See TracChangeset for help on using the changeset viewer.