Skip to content

Commit

Permalink
Replaced "local first" class loading approach by "missing only" appro…
Browse files Browse the repository at this point in the history
…ach.
  • Loading branch information
fvgh committed Jan 12, 2019
1 parent 39d64ca commit 0411b78
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 124 deletions.
71 changes: 71 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2016 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless;

import java.net.URL;
import java.net.URLClassLoader;
import java.util.Objects;

/**
* The requests to load a class for a feature is first delegated to the system class loader,
* before the feature class loader searches the provided URLs. In case the class resolution or
* verification fails, the request is delegated to the class loader of the build tool.
* <br/>
* Features shall be independent from build tools. Framework capabilities provided by
* build tools can be explicitly accessed by omitting the run-time dependencies for these
* classes in the provided URLs.
* <br/>
* For build tools not supporting framework capabilities required by certain features,
* the Spotless plugin for the specific build tool provides the missing framework capabilities.
*/
public class FeatureClassLoader extends URLClassLoader {
static {
try {
ClassLoader.registerAsParallelCapable();
} catch (NoSuchMethodError ignore) {
// Not supported on Java 6
}
}

private final ClassLoader buildToolClassLoader;

/**
* Constructs a new FeatureClassLoader for the given URLs, based on an {@code URLClassLoader},
* using the system class loader as parent. In case the class resolution or verification fails,
* the the class loader of the build tool is used.
*
* @param urls the URLs from which to load classes and resources
* @param buildToolClassLoader The build tool class loader
* @exception SecurityException If a security manager exists and prevents the creation of a class loader.
* @exception NullPointerException if {@code urls} is {@code null}.
*/

public FeatureClassLoader(URL[] urls, ClassLoader buildToolClassLoader) {
super(urls, null);
Objects.requireNonNull(buildToolClassLoader);
this.buildToolClassLoader = buildToolClassLoader;
}

@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
try {
return super.findClass(name);
} catch (ClassNotFoundException e) {
return buildToolClassLoader.loadClass(name);
}
}

}
4 changes: 0 additions & 4 deletions lib/src/main/java/com/diffplug/spotless/JarState.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,6 @@ public ClassLoader getClassLoader() {
return SpotlessCache.instance().classloader(this);
}

public ClassLoader getClassLoaderOld() {
return SpotlessCache.instance().classloaderOld(this);
}

/**
* Returns a classloader containing only the jars in this JarState.
*
Expand Down
111 changes: 0 additions & 111 deletions lib/src/main/java/com/diffplug/spotless/LocalFirstClassLoader.java

This file was deleted.

9 changes: 1 addition & 8 deletions lib/src/main/java/com/diffplug/spotless/SpotlessCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,11 @@ synchronized ClassLoader classloader(JarState state) {
return classloader(state, state);
}

@SuppressFBWarnings("DP_CREATE_CLASSLOADER_INSIDE_DO_PRIVILEGED")
synchronized ClassLoader classloaderOld(JarState state) {
SerializedKey serializedKey = new SerializedKey(state);
return cache
.computeIfAbsent(serializedKey, k -> new URLClassLoader(state.jarUrls(), null));
}

@SuppressFBWarnings("DP_CREATE_CLASSLOADER_INSIDE_DO_PRIVILEGED")
synchronized ClassLoader classloader(Serializable key, JarState state) {
SerializedKey serializedKey = new SerializedKey(key);
return cache
.computeIfAbsent(serializedKey, k -> new LocalFirstClassLoader(state.jarUrls(), this.getClass().getClassLoader()));
.computeIfAbsent(serializedKey, k -> new FeatureClassLoader(state.jarUrls(), this.getClass().getClassLoader()));
}

static SpotlessCache instance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ FormatterFunc createFormat() throws Exception {
Logger logger = Logger.getLogger(FreshMarkStep.class.getName());
Consumer<String> loggingStream = logger::warning;

ClassLoader classLoader = jarState.getClassLoaderOld();
ClassLoader classLoader = jarState.getClassLoader();

// instantiate the formatter and get its format method
Class<?> formatterClazz = classLoader.loadClass(FORMATTER_CLASS);
Expand Down

0 comments on commit 0411b78

Please sign in to comment.