Polishing

This commit is contained in:
Juergen Hoeller
2016-04-12 23:33:58 +02:00
parent 33dcef3583
commit b1c70729c2
4 changed files with 49 additions and 41 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 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.
@@ -97,15 +97,14 @@ public class ReflectiveLoadTimeWeaver implements LoadTimeWeaver {
Assert.notNull(classLoader, "ClassLoader must not be null");
this.classLoader = classLoader;
this.addTransformerMethod = ClassUtils.getMethodIfAvailable(
this.classLoader.getClass(), ADD_TRANSFORMER_METHOD_NAME,
new Class<?>[] {ClassFileTransformer.class});
this.classLoader.getClass(), ADD_TRANSFORMER_METHOD_NAME, ClassFileTransformer.class);
if (this.addTransformerMethod == null) {
throw new IllegalStateException(
"ClassLoader [" + classLoader.getClass().getName() + "] does NOT provide an " +
"'addTransformer(ClassFileTransformer)' method.");
}
this.getThrowawayClassLoaderMethod = ClassUtils.getMethodIfAvailable(
this.classLoader.getClass(), GET_THROWAWAY_CLASS_LOADER_METHOD_NAME, new Class<?>[0]);
this.classLoader.getClass(), GET_THROWAWAY_CLASS_LOADER_METHOD_NAME);
// getThrowawayClassLoader method is optional
if (this.getThrowawayClassLoaderMethod == null) {
if (logger.isInfoEnabled()) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 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.
@@ -29,47 +29,57 @@ import org.springframework.util.Assert;
/**
*
* Reflective wrapper around a WebSphere 7 class loader. Used to
* Reflective wrapper around a WebSphere 7+ class loader. Used to
* encapsulate the classloader-specific methods (discovered and
* called through reflection) from the load-time weaver.
*
* @author Costin Leau
* @author Juergen Hoeller
* @since 3.1
*/
class WebSphereClassLoaderAdapter {
private static final String COMPOUND_CLASS_LOADER_NAME = "com.ibm.ws.classloader.CompoundClassLoader";
private static final String CLASS_PRE_PROCESSOR_NAME = "com.ibm.websphere.classloader.ClassLoaderInstancePreDefinePlugin";
private static final String PLUGINS_FIELD = "preDefinePlugins";
private ClassLoader classLoader;
private Class<?> wsPreProcessorClass;
private Method addPreDefinePlugin;
private Constructor<? extends ClassLoader> cloneConstructor;
private Field transformerList;
public WebSphereClassLoaderAdapter(ClassLoader classLoader) {
Class<?> wsCompoundClassLoaderClass = null;
Class<?> wsCompoundClassLoaderClass;
try {
wsCompoundClassLoaderClass = classLoader.loadClass(COMPOUND_CLASS_LOADER_NAME);
cloneConstructor = classLoader.getClass().getDeclaredConstructor(wsCompoundClassLoaderClass);
cloneConstructor.setAccessible(true);
this.cloneConstructor = classLoader.getClass().getDeclaredConstructor(wsCompoundClassLoaderClass);
this.cloneConstructor.setAccessible(true);
wsPreProcessorClass = classLoader.loadClass(CLASS_PRE_PROCESSOR_NAME);
addPreDefinePlugin = classLoader.getClass().getMethod("addPreDefinePlugin", wsPreProcessorClass);
transformerList = wsCompoundClassLoaderClass.getDeclaredField(PLUGINS_FIELD);
transformerList.setAccessible(true);
this.wsPreProcessorClass = classLoader.loadClass(CLASS_PRE_PROCESSOR_NAME);
this.addPreDefinePlugin = classLoader.getClass().getMethod("addPreDefinePlugin", this.wsPreProcessorClass);
this.transformerList = wsCompoundClassLoaderClass.getDeclaredField(PLUGINS_FIELD);
this.transformerList.setAccessible(true);
}
catch (Exception ex) {
throw new IllegalStateException(
"Could not initialize WebSphere LoadTimeWeaver because WebSphere 7 API classes are not available",
ex);
"Could not initialize WebSphere LoadTimeWeaver because WebSphere API classes are not available", ex);
}
if (!wsCompoundClassLoaderClass.isInstance(classLoader)) {
throw new IllegalArgumentException("ClassLoader must be instance of [" + COMPOUND_CLASS_LOADER_NAME + "]");
}
Assert.isInstanceOf(wsCompoundClassLoaderClass, classLoader,
"ClassLoader must be instance of [" + COMPOUND_CLASS_LOADER_NAME + "]");
this.classLoader = classLoader;
}
public ClassLoader getClassLoader() {
return this.classLoader;
}
@@ -79,9 +89,8 @@ class WebSphereClassLoaderAdapter {
try {
InvocationHandler adapter = new WebSphereClassPreDefinePlugin(transformer);
Object adapterInstance = Proxy.newProxyInstance(this.wsPreProcessorClass.getClassLoader(),
new Class<?>[] { this.wsPreProcessorClass }, adapter);
new Class<?>[] {this.wsPreProcessorClass}, adapter);
this.addPreDefinePlugin.invoke(this.classLoader, adapterInstance);
}
catch (InvocationTargetException ex) {
throw new IllegalStateException("WebSphere addPreDefinePlugin method threw exception", ex.getCause());
@@ -93,9 +102,9 @@ class WebSphereClassLoaderAdapter {
public ClassLoader getThrowawayClassLoader() {
try {
ClassLoader loader = cloneConstructor.newInstance(getClassLoader());
// clear out the transformers (copied as well)
List<?> list = (List<?>) transformerList.get(loader);
ClassLoader loader = this.cloneConstructor.newInstance(getClassLoader());
// Clear out the transformers (copied as well)
List<?> list = (List<?>) this.transformerList.get(loader);
list.clear();
return loader;
}

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.
@@ -63,19 +63,16 @@ public class OverridingClassLoader extends DecoratingClassLoader {
@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);
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 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.
@@ -27,10 +27,11 @@ import org.springframework.util.Assert;
/**
* Simple adapter that implements the {@code java.lang.instrument.ClassFileTransformer}
* interface based on a JPA ClassTransformer which a JPA PersistenceProvider asks the
* PersistenceUnitInfo to install in the current runtime.
* interface based on a JPA {@code ClassTransformer} which a JPA PersistenceProvider
* asks the {@code PersistenceUnitInfo} to install in the current runtime.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @since 2.0
* @see javax.persistence.spi.PersistenceUnitInfo#addTransformer(javax.persistence.spi.ClassTransformer)
*/
@@ -63,14 +64,16 @@ class ClassFileTransformerAdapter implements ClassFileTransformer {
return transformed;
}
catch (ClassCircularityError ex) {
logger.error("Error weaving class [" + className + "] with " +
"transformer of class [" + this.classTransformer.getClass().getName() + "]", ex);
throw new IllegalStateException("Could not weave class [" + className + "]", ex);
if (logger.isErrorEnabled()) {
logger.error("Circularity error while weaving class [" + className + "] with " +
"transformer of class [" + this.classTransformer.getClass().getName() + "]", ex);
}
throw new IllegalStateException("Failed to weave class [" + className + "]", ex);
}
catch (Throwable ex) {
if (logger.isWarnEnabled()) {
logger.warn("Error weaving class [" + className + "] with " +
"transformer of class [" + this.classTransformer.getClass().getName() + "]", ex);
logger.warn("Error weaving class [" + className + "] with transformer of class [" +
this.classTransformer.getClass().getName() + "]", ex);
}
// The exception will be ignored by the class loader, anyway...
throw new IllegalStateException("Could not weave class [" + className + "]", ex);