LoadTimeWeaver.getThrowawayClassLoader() decorated for exclude support (if necessary)

Issue: SPR-13886
This commit is contained in:
Juergen Hoeller
2016-04-11 19:19:37 +02:00
parent 448621ac58
commit b82df144e4
8 changed files with 97 additions and 62 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -16,8 +16,9 @@
package org.springframework.core;
import java.util.HashSet;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.lang.UsesJava7;
import org.springframework.util.Assert;
@@ -49,11 +50,11 @@ public abstract class DecoratingClassLoader extends ClassLoader {
}
private final Set<String> excludedPackages = new HashSet<String>();
private final Set<String> excludedPackages =
Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>(8));
private final Set<String> excludedClasses = new HashSet<String>();
private final Object exclusionMonitor = new Object();
private final Set<String> excludedClasses =
Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>(8));
/**
@@ -79,9 +80,7 @@ public abstract class DecoratingClassLoader extends ClassLoader {
*/
public void excludePackage(String packageName) {
Assert.notNull(packageName, "Package name must not be null");
synchronized (this.exclusionMonitor) {
this.excludedPackages.add(packageName);
}
this.excludedPackages.add(packageName);
}
/**
@@ -92,9 +91,7 @@ public abstract class DecoratingClassLoader extends ClassLoader {
*/
public void excludeClass(String className) {
Assert.notNull(className, "Class name must not be null");
synchronized (this.exclusionMonitor) {
this.excludedClasses.add(className);
}
this.excludedClasses.add(className);
}
/**
@@ -107,15 +104,13 @@ public abstract class DecoratingClassLoader extends ClassLoader {
* @see #excludeClass
*/
protected boolean isExcluded(String className) {
synchronized (this.exclusionMonitor) {
if (this.excludedClasses.contains(className)) {
if (this.excludedClasses.contains(className)) {
return true;
}
for (String packageName : this.excludedPackages) {
if (className.startsWith(packageName)) {
return true;
}
for (String packageName : this.excludedPackages) {
if (className.startsWith(packageName)) {
return true;
}
}
}
return false;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -38,7 +38,8 @@ import org.springframework.util.FileCopyUtils;
public class OverridingClassLoader extends DecoratingClassLoader {
/** Packages that are excluded by default */
public static final String[] DEFAULT_EXCLUDED_PACKAGES = new String[] {"java.", "javax.", "sun.", "oracle."};
public static final String[] DEFAULT_EXCLUDED_PACKAGES = new String[]
{"java.", "javax.", "sun.", "oracle.", "javassist.", "org.springframework.core."};
private static final String CLASS_FILE_SUFFIX = ".class";
@@ -49,33 +50,52 @@ public class OverridingClassLoader extends DecoratingClassLoader {
}
private final ClassLoader overrideDelegate;
/**
* Create a new OverridingClassLoader for the given ClassLoader.
* @param parent the ClassLoader to build an overriding ClassLoader for
*/
public OverridingClassLoader(ClassLoader parent) {
this(parent, null);
}
/**
* Create a new OverridingClassLoader for the given ClassLoader.
* @param parent the ClassLoader to build an overriding ClassLoader for
* @param overrideDelegate the ClassLoader to delegate to for overriding
* @since 4.3
*/
public OverridingClassLoader(ClassLoader parent, ClassLoader overrideDelegate) {
super(parent);
this.overrideDelegate = overrideDelegate;
for (String packageName : DEFAULT_EXCLUDED_PACKAGES) {
excludePackage(packageName);
}
}
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
if (this.overrideDelegate != null && isEligibleForOverriding(name)) {
return this.overrideDelegate.loadClass(name);
}
return super.loadClass(name);
}
@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
Class<?> result = null;
if (isEligibleForOverriding(name)) {
result = loadClassForOverriding(name);
}
if (result != null) {
if (resolve) {
resolveClass(result);
Class<?> result = loadClassForOverriding(name);
if (result != null) {
if (resolve) {
resolveClass(result);
}
return result;
}
return result;
}
else {
return super.loadClass(name, resolve);
}
return super.loadClass(name, resolve);
}
/**