Skip to content

Commit 05c995c

Browse files
committed
DecoratingClassLoader and its subclasses register themselves as parallel capable on Java 7+
Issue: SPR-12285
1 parent 25d13ac commit 05c995c

File tree

5 files changed

+67
-19
lines changed

5 files changed

+67
-19
lines changed

spring-context/src/main/java/org/springframework/context/support/ContextTypeMatchClassLoader.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,12 +17,13 @@
1717
package org.springframework.context.support;
1818

1919
import java.lang.reflect.Method;
20-
import java.util.HashMap;
2120
import java.util.Map;
21+
import java.util.concurrent.ConcurrentHashMap;
2222

2323
import org.springframework.core.DecoratingClassLoader;
2424
import org.springframework.core.OverridingClassLoader;
2525
import org.springframework.core.SmartClassLoader;
26+
import org.springframework.lang.UsesJava7;
2627
import org.springframework.util.ReflectionUtils;
2728

2829
/**
@@ -36,13 +37,21 @@
3637
* @see AbstractApplicationContext
3738
* @see org.springframework.beans.factory.config.ConfigurableBeanFactory#setTempClassLoader
3839
*/
40+
@UsesJava7
3941
class ContextTypeMatchClassLoader extends DecoratingClassLoader implements SmartClassLoader {
4042

43+
static {
44+
if (parallelCapableClassLoaderAvailable) {
45+
ClassLoader.registerAsParallelCapable();
46+
}
47+
}
48+
49+
4150
private static Method findLoadedClassMethod;
4251

4352
static {
4453
try {
45-
findLoadedClassMethod = ClassLoader.class.getDeclaredMethod("findLoadedClass", new Class<?>[] {String.class});
54+
findLoadedClassMethod = ClassLoader.class.getDeclaredMethod("findLoadedClass", String.class);
4655
}
4756
catch (NoSuchMethodException ex) {
4857
throw new IllegalStateException("Invalid [java.lang.ClassLoader] class: no 'findLoadedClass' method defined!");
@@ -51,7 +60,7 @@ class ContextTypeMatchClassLoader extends DecoratingClassLoader implements Smart
5160

5261

5362
/** Cache for byte array per class name */
54-
private final Map<String, byte[]> bytesCache = new HashMap<String, byte[]>();
63+
private final Map<String, byte[]> bytesCache = new ConcurrentHashMap<String, byte[]>(256);
5564

5665

5766
public ContextTypeMatchClassLoader(ClassLoader parent) {

spring-context/src/main/java/org/springframework/instrument/classloading/SimpleInstrumentableClassLoader.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.lang.instrument.ClassFileTransformer;
2020

2121
import org.springframework.core.OverridingClassLoader;
22+
import org.springframework.lang.UsesJava7;
2223

2324
/**
2425
* Simplistic implementation of an instrumentable {@code ClassLoader}.
@@ -29,16 +30,22 @@
2930
* @author Costin Leau
3031
* @since 2.0
3132
*/
33+
@UsesJava7
3234
public class SimpleInstrumentableClassLoader extends OverridingClassLoader {
3335

36+
static {
37+
if (parallelCapableClassLoaderAvailable) {
38+
ClassLoader.registerAsParallelCapable();
39+
}
40+
}
41+
42+
3443
private final WeavingTransformer weavingTransformer;
3544

3645

3746
/**
38-
* Create a new {@code SimpleLoadTimeWeaver} for the given
39-
* {@code ClassLoader}.
40-
* @param parent the {@code ClassLoader} to build a simple
41-
* instrumentable {@code ClassLoader} for
47+
* Create a new SimpleInstrumentableClassLoader for the given ClassLoader.
48+
* @param parent the ClassLoader to build an instrumentable ClassLoader for
4249
*/
4350
public SimpleInstrumentableClassLoader(ClassLoader parent) {
4451
super(parent);
@@ -47,9 +54,8 @@ public SimpleInstrumentableClassLoader(ClassLoader parent) {
4754

4855

4956
/**
50-
* Add a {@code ClassFileTransformer} to be applied by this
51-
* {@code ClassLoader}.
52-
* @param transformer the {@code ClassFileTransformer} to register
57+
* Add a {@link ClassFileTransformer} to be applied by this ClassLoader.
58+
* @param transformer the {@link ClassFileTransformer} to register
5359
*/
5460
public void addTransformer(ClassFileTransformer transformer) {
5561
this.weavingTransformer.addTransformer(transformer);

spring-context/src/main/java/org/springframework/instrument/classloading/SimpleThrowawayClassLoader.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2006 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.instrument.classloading;
1818

1919
import org.springframework.core.OverridingClassLoader;
20+
import org.springframework.lang.UsesJava7;
2021

2122
/**
2223
* ClassLoader that can be used to load classes without bringing them
@@ -26,10 +27,18 @@
2627
* @author Rod Johnson
2728
* @since 2.0
2829
*/
30+
@UsesJava7
2931
public class SimpleThrowawayClassLoader extends OverridingClassLoader {
3032

33+
static {
34+
if (parallelCapableClassLoaderAvailable) {
35+
ClassLoader.registerAsParallelCapable();
36+
}
37+
}
38+
39+
3140
/**
32-
* Create a new SimpleThrowawayClassLoader for the given class loader.
41+
* Create a new SimpleThrowawayClassLoader for the given ClassLoader.
3342
* @param parent the ClassLoader to build a throwaway ClassLoader for
3443
*/
3544
public SimpleThrowawayClassLoader(ClassLoader parent) {

spring-core/src/main/java/org/springframework/core/DecoratingClassLoader.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,9 @@
1919
import java.util.HashSet;
2020
import java.util.Set;
2121

22+
import org.springframework.lang.UsesJava7;
2223
import org.springframework.util.Assert;
24+
import org.springframework.util.ClassUtils;
2325

2426
/**
2527
* Base class for decorating ClassLoaders such as {@link OverridingClassLoader}
@@ -30,8 +32,23 @@
3032
* @author Rod Johnson
3133
* @since 2.5.2
3234
*/
35+
@UsesJava7
3336
public abstract class DecoratingClassLoader extends ClassLoader {
3437

38+
/**
39+
* Java 7+ {@code ClassLoader.registerAsParallelCapable()} available?
40+
* @since 4.1.2
41+
*/
42+
protected static final boolean parallelCapableClassLoaderAvailable =
43+
ClassUtils.hasMethod(ClassLoader.class, "registerAsParallelCapable");
44+
45+
static {
46+
if (parallelCapableClassLoaderAvailable) {
47+
ClassLoader.registerAsParallelCapable();
48+
}
49+
}
50+
51+
3552
private final Set<String> excludedPackages = new HashSet<String>();
3653

3754
private final Set<String> excludedClasses = new HashSet<String>();

spring-core/src/main/java/org/springframework/core/OverridingClassLoader.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.io.InputStream;
2121

22+
import org.springframework.lang.UsesJava7;
2223
import org.springframework.util.FileCopyUtils;
2324

2425
/**
@@ -33,17 +34,23 @@
3334
* @author Juergen Hoeller
3435
* @since 2.0.1
3536
*/
37+
@UsesJava7
3638
public class OverridingClassLoader extends DecoratingClassLoader {
3739

3840
/** Packages that are excluded by default */
39-
public static final String[] DEFAULT_EXCLUDED_PACKAGES =
40-
new String[] {"java.", "javax.", "sun.", "oracle."};
41+
public static final String[] DEFAULT_EXCLUDED_PACKAGES = new String[] {"java.", "javax.", "sun.", "oracle."};
4142

4243
private static final String CLASS_FILE_SUFFIX = ".class";
4344

45+
static {
46+
if (parallelCapableClassLoaderAvailable) {
47+
ClassLoader.registerAsParallelCapable();
48+
}
49+
}
50+
4451

4552
/**
46-
* Create a new OverridingClassLoader for the given class loader.
53+
* Create a new OverridingClassLoader for the given ClassLoader.
4754
* @param parent the ClassLoader to build an overriding ClassLoader for
4855
*/
4956
public OverridingClassLoader(ClassLoader parent) {

0 commit comments

Comments
 (0)