提交 05c995cf 编写于 作者: J Juergen Hoeller

DecoratingClassLoader and its subclasses register themselves as parallel capable on Java 7+

Issue: SPR-12285
上级 25d13ac4
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -17,12 +17,13 @@ ...@@ -17,12 +17,13 @@
package org.springframework.context.support; package org.springframework.context.support;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.core.DecoratingClassLoader; import org.springframework.core.DecoratingClassLoader;
import org.springframework.core.OverridingClassLoader; import org.springframework.core.OverridingClassLoader;
import org.springframework.core.SmartClassLoader; import org.springframework.core.SmartClassLoader;
import org.springframework.lang.UsesJava7;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
/** /**
...@@ -36,13 +37,21 @@ import org.springframework.util.ReflectionUtils; ...@@ -36,13 +37,21 @@ import org.springframework.util.ReflectionUtils;
* @see AbstractApplicationContext * @see AbstractApplicationContext
* @see org.springframework.beans.factory.config.ConfigurableBeanFactory#setTempClassLoader * @see org.springframework.beans.factory.config.ConfigurableBeanFactory#setTempClassLoader
*/ */
@UsesJava7
class ContextTypeMatchClassLoader extends DecoratingClassLoader implements SmartClassLoader { class ContextTypeMatchClassLoader extends DecoratingClassLoader implements SmartClassLoader {
static {
if (parallelCapableClassLoaderAvailable) {
ClassLoader.registerAsParallelCapable();
}
}
private static Method findLoadedClassMethod; private static Method findLoadedClassMethod;
static { static {
try { try {
findLoadedClassMethod = ClassLoader.class.getDeclaredMethod("findLoadedClass", new Class<?>[] {String.class}); findLoadedClassMethod = ClassLoader.class.getDeclaredMethod("findLoadedClass", String.class);
} }
catch (NoSuchMethodException ex) { catch (NoSuchMethodException ex) {
throw new IllegalStateException("Invalid [java.lang.ClassLoader] class: no 'findLoadedClass' method defined!"); throw new IllegalStateException("Invalid [java.lang.ClassLoader] class: no 'findLoadedClass' method defined!");
...@@ -51,7 +60,7 @@ class ContextTypeMatchClassLoader extends DecoratingClassLoader implements Smart ...@@ -51,7 +60,7 @@ class ContextTypeMatchClassLoader extends DecoratingClassLoader implements Smart
/** Cache for byte array per class name */ /** Cache for byte array per class name */
private final Map<String, byte[]> bytesCache = new HashMap<String, byte[]>(); private final Map<String, byte[]> bytesCache = new ConcurrentHashMap<String, byte[]>(256);
public ContextTypeMatchClassLoader(ClassLoader parent) { public ContextTypeMatchClassLoader(ClassLoader parent) {
......
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -19,6 +19,7 @@ package org.springframework.instrument.classloading; ...@@ -19,6 +19,7 @@ package org.springframework.instrument.classloading;
import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.ClassFileTransformer;
import org.springframework.core.OverridingClassLoader; import org.springframework.core.OverridingClassLoader;
import org.springframework.lang.UsesJava7;
/** /**
* Simplistic implementation of an instrumentable {@code ClassLoader}. * Simplistic implementation of an instrumentable {@code ClassLoader}.
...@@ -29,16 +30,22 @@ import org.springframework.core.OverridingClassLoader; ...@@ -29,16 +30,22 @@ import org.springframework.core.OverridingClassLoader;
* @author Costin Leau * @author Costin Leau
* @since 2.0 * @since 2.0
*/ */
@UsesJava7
public class SimpleInstrumentableClassLoader extends OverridingClassLoader { public class SimpleInstrumentableClassLoader extends OverridingClassLoader {
static {
if (parallelCapableClassLoaderAvailable) {
ClassLoader.registerAsParallelCapable();
}
}
private final WeavingTransformer weavingTransformer; private final WeavingTransformer weavingTransformer;
/** /**
* Create a new {@code SimpleLoadTimeWeaver} for the given * Create a new SimpleInstrumentableClassLoader for the given ClassLoader.
* {@code ClassLoader}. * @param parent the ClassLoader to build an instrumentable ClassLoader for
* @param parent the {@code ClassLoader} to build a simple
* instrumentable {@code ClassLoader} for
*/ */
public SimpleInstrumentableClassLoader(ClassLoader parent) { public SimpleInstrumentableClassLoader(ClassLoader parent) {
super(parent); super(parent);
...@@ -47,9 +54,8 @@ public class SimpleInstrumentableClassLoader extends OverridingClassLoader { ...@@ -47,9 +54,8 @@ public class SimpleInstrumentableClassLoader extends OverridingClassLoader {
/** /**
* Add a {@code ClassFileTransformer} to be applied by this * Add a {@link ClassFileTransformer} to be applied by this ClassLoader.
* {@code ClassLoader}. * @param transformer the {@link ClassFileTransformer} to register
* @param transformer the {@code ClassFileTransformer} to register
*/ */
public void addTransformer(ClassFileTransformer transformer) { public void addTransformer(ClassFileTransformer transformer) {
this.weavingTransformer.addTransformer(transformer); this.weavingTransformer.addTransformer(transformer);
......
/* /*
* Copyright 2002-2006 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package org.springframework.instrument.classloading; package org.springframework.instrument.classloading;
import org.springframework.core.OverridingClassLoader; import org.springframework.core.OverridingClassLoader;
import org.springframework.lang.UsesJava7;
/** /**
* ClassLoader that can be used to load classes without bringing them * ClassLoader that can be used to load classes without bringing them
...@@ -26,10 +27,18 @@ import org.springframework.core.OverridingClassLoader; ...@@ -26,10 +27,18 @@ import org.springframework.core.OverridingClassLoader;
* @author Rod Johnson * @author Rod Johnson
* @since 2.0 * @since 2.0
*/ */
@UsesJava7
public class SimpleThrowawayClassLoader extends OverridingClassLoader { public class SimpleThrowawayClassLoader extends OverridingClassLoader {
static {
if (parallelCapableClassLoaderAvailable) {
ClassLoader.registerAsParallelCapable();
}
}
/** /**
* Create a new SimpleThrowawayClassLoader for the given class loader. * Create a new SimpleThrowawayClassLoader for the given ClassLoader.
* @param parent the ClassLoader to build a throwaway ClassLoader for * @param parent the ClassLoader to build a throwaway ClassLoader for
*/ */
public SimpleThrowawayClassLoader(ClassLoader parent) { public SimpleThrowawayClassLoader(ClassLoader parent) {
......
/* /*
* Copyright 2002-2008 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -19,7 +19,9 @@ package org.springframework.core; ...@@ -19,7 +19,9 @@ package org.springframework.core;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.springframework.lang.UsesJava7;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/** /**
* Base class for decorating ClassLoaders such as {@link OverridingClassLoader} * Base class for decorating ClassLoaders such as {@link OverridingClassLoader}
...@@ -30,8 +32,23 @@ import org.springframework.util.Assert; ...@@ -30,8 +32,23 @@ import org.springframework.util.Assert;
* @author Rod Johnson * @author Rod Johnson
* @since 2.5.2 * @since 2.5.2
*/ */
@UsesJava7
public abstract class DecoratingClassLoader extends ClassLoader { public abstract class DecoratingClassLoader extends ClassLoader {
/**
* Java 7+ {@code ClassLoader.registerAsParallelCapable()} available?
* @since 4.1.2
*/
protected static final boolean parallelCapableClassLoaderAvailable =
ClassUtils.hasMethod(ClassLoader.class, "registerAsParallelCapable");
static {
if (parallelCapableClassLoaderAvailable) {
ClassLoader.registerAsParallelCapable();
}
}
private final Set<String> excludedPackages = new HashSet<String>(); private final Set<String> excludedPackages = new HashSet<String>();
private final Set<String> excludedClasses = new HashSet<String>(); private final Set<String> excludedClasses = new HashSet<String>();
......
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -19,6 +19,7 @@ package org.springframework.core; ...@@ -19,6 +19,7 @@ package org.springframework.core;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import org.springframework.lang.UsesJava7;
import org.springframework.util.FileCopyUtils; import org.springframework.util.FileCopyUtils;
/** /**
...@@ -33,17 +34,23 @@ import org.springframework.util.FileCopyUtils; ...@@ -33,17 +34,23 @@ import org.springframework.util.FileCopyUtils;
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 2.0.1 * @since 2.0.1
*/ */
@UsesJava7
public class OverridingClassLoader extends DecoratingClassLoader { public class OverridingClassLoader extends DecoratingClassLoader {
/** Packages that are excluded by default */ /** Packages that are excluded by default */
public static final String[] DEFAULT_EXCLUDED_PACKAGES = public static final String[] DEFAULT_EXCLUDED_PACKAGES = new String[] {"java.", "javax.", "sun.", "oracle."};
new String[] {"java.", "javax.", "sun.", "oracle."};
private static final String CLASS_FILE_SUFFIX = ".class"; private static final String CLASS_FILE_SUFFIX = ".class";
static {
if (parallelCapableClassLoaderAvailable) {
ClassLoader.registerAsParallelCapable();
}
}
/** /**
* Create a new OverridingClassLoader for the given class loader. * Create a new OverridingClassLoader for the given ClassLoader.
* @param parent the ClassLoader to build an overriding ClassLoader for * @param parent the ClassLoader to build an overriding ClassLoader for
*/ */
public OverridingClassLoader(ClassLoader parent) { public OverridingClassLoader(ClassLoader parent) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册