Browse Source
Removed custom JUnit integration as we can just create HidingClassLoader instances in the test and the integration actually causes more code being needed (additional JUnit rule, method level annotations etc.). Tweaked ShadowingClassLoader to make obvious what has been changed over the Spring Framework variant. Created upstream ticket [0] to ask for the tweaks that would allow us to remove the class again. Original pull request: #202. Related ticket: SPR-15439 [0] https://jira.spring.io/browse/SPR-15439pull/347/head
5 changed files with 72 additions and 225 deletions
@ -1,60 +0,0 @@
@@ -1,60 +0,0 @@
|
||||
/* |
||||
* Copyright 2017 the original author or authors. |
||||
* |
||||
* 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 org.springframework.data.classloadersupport; |
||||
|
||||
import java.lang.annotation.ElementType; |
||||
import java.lang.annotation.Retention; |
||||
import java.lang.annotation.RetentionPolicy; |
||||
import java.lang.annotation.Target; |
||||
|
||||
/** |
||||
* configures the {@link ClassLoaderRule}. |
||||
* |
||||
* @author Jens Schauder |
||||
*/ |
||||
@Target({ElementType.METHOD, ElementType.TYPE}) |
||||
@Retention(RetentionPolicy.RUNTIME) |
||||
public @interface ClassLoaderConfiguration { |
||||
|
||||
/** |
||||
* classes of which the package that contains them will be loaded by the {@link HidingClassLoader} not by |
||||
* the normal {@code ClassLoader}. |
||||
* |
||||
* @return list of classes not to shadow. |
||||
*/ |
||||
Class[] shadowPackage() default {}; |
||||
|
||||
/** |
||||
* prefixes of class names that will be loaded by the {@link HidingClassLoader}. |
||||
* |
||||
* @return list of class prefixes which will not be shadowed. |
||||
*/ |
||||
String[] shadowByPrefix() default {}; |
||||
|
||||
/** |
||||
* classes of which the package that contains them will be hidden by the {@link HidingClassLoader}. |
||||
* |
||||
* @return list of classes to hidePackage. |
||||
*/ |
||||
Class[] hidePackage() default {}; |
||||
|
||||
/** |
||||
* classes from packages that will be hidden by the {@link HidingClassLoader}. |
||||
* |
||||
* @return list of classes of which the package will be hidden. |
||||
*/ |
||||
String[] hideByPrefix() default {}; |
||||
} |
||||
@ -1,113 +0,0 @@
@@ -1,113 +0,0 @@
|
||||
/* |
||||
* Copyright 2017 the original author or authors. |
||||
* |
||||
* 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 org.springframework.data.classloadersupport; |
||||
|
||||
import static java.util.Arrays.*; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import org.junit.rules.MethodRule; |
||||
import org.junit.runners.model.FrameworkMethod; |
||||
import org.junit.runners.model.Statement; |
||||
|
||||
/** |
||||
* supports creation of tests that need to load classes with a {@link HidingClassLoader}. |
||||
* |
||||
* @author Jens Schauder |
||||
*/ |
||||
public class ClassLoaderRule implements MethodRule { |
||||
|
||||
public HidingClassLoader classLoader; |
||||
|
||||
@Override |
||||
public Statement apply(final Statement base, FrameworkMethod method, Object target) { |
||||
|
||||
CombinedClassLoaderConfiguration combinedConfiguration = new CombinedClassLoaderConfiguration( |
||||
method.getAnnotation(ClassLoaderConfiguration.class), |
||||
method.getDeclaringClass().getAnnotation(ClassLoaderConfiguration.class) |
||||
); |
||||
|
||||
classLoader = createClassLoader(combinedConfiguration); |
||||
|
||||
return new Statement() { |
||||
|
||||
@Override |
||||
public void evaluate() throws Throwable { |
||||
|
||||
try { |
||||
base.evaluate(); |
||||
} finally { |
||||
classLoader = null; |
||||
} |
||||
} |
||||
}; |
||||
} |
||||
|
||||
private static HidingClassLoader createClassLoader(CombinedClassLoaderConfiguration configuration) { |
||||
|
||||
HidingClassLoader classLoader = new HidingClassLoader(mergeHidden(configuration)); |
||||
|
||||
for (Class shadow : configuration.shadowPackages) { |
||||
classLoader.excludeClass(shadow.getPackage().getName()); |
||||
} |
||||
|
||||
for (String shadow : configuration.shadowByPrefix) { |
||||
classLoader.excludePackage(shadow); |
||||
} |
||||
|
||||
return classLoader; |
||||
} |
||||
|
||||
private static List<String> mergeHidden(CombinedClassLoaderConfiguration configuration) { |
||||
|
||||
List<String> hidden = new ArrayList<String>(); |
||||
|
||||
for (Class aClass : configuration.hidePackages) { |
||||
hidden.add(aClass.getPackage().getName()); |
||||
} |
||||
|
||||
for (String aPackage : configuration.hideByPrefix) { |
||||
hidden.add(aPackage); |
||||
} |
||||
|
||||
return hidden; |
||||
} |
||||
|
||||
private static class CombinedClassLoaderConfiguration { |
||||
|
||||
final List<Class> shadowPackages = new ArrayList<Class>(); |
||||
final List<String> shadowByPrefix = new ArrayList<String>(); |
||||
final List<Class> hidePackages = new ArrayList<Class>(); |
||||
final List<String> hideByPrefix = new ArrayList<String>(); |
||||
|
||||
CombinedClassLoaderConfiguration(ClassLoaderConfiguration methodAnnotation, ClassLoaderConfiguration classAnnotation) { |
||||
|
||||
mergeAnnotation(methodAnnotation); |
||||
mergeAnnotation(classAnnotation); |
||||
} |
||||
|
||||
private void mergeAnnotation(ClassLoaderConfiguration methodAnnotation) { |
||||
|
||||
if (methodAnnotation != null) { |
||||
|
||||
shadowPackages.addAll(asList(methodAnnotation.shadowPackage())); |
||||
shadowByPrefix.addAll(asList(methodAnnotation.shadowByPrefix())); |
||||
hidePackages.addAll(asList(methodAnnotation.hidePackage())); |
||||
hideByPrefix.addAll(asList(methodAnnotation.hideByPrefix())); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue