Browse Source

JPA ClassFileTransformerAdapter defensively backs out when called from within a transform call

Issue: SPR-13886
pull/1032/head
Juergen Hoeller 10 years ago
parent
commit
836a321ab2
  1. 64
      spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/ClassFileTransformerAdapter.java

64
spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/ClassFileTransformerAdapter.java

@ -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"); * 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.
@ -27,10 +27,11 @@ import org.springframework.util.Assert;
/** /**
* Simple adapter that implements the {@code java.lang.instrument.ClassFileTransformer} * Simple adapter that implements the {@code java.lang.instrument.ClassFileTransformer}
* interface based on a JPA ClassTransformer which a JPA PersistenceProvider asks the * interface based on a JPA {@code ClassTransformer} which a JPA PersistenceProvider
* PersistenceUnitInfo to install in the current runtime. * asks the {@code PersistenceUnitInfo} to install in the current runtime.
* *
* @author Rod Johnson * @author Rod Johnson
* @author Juergen Hoeller
* @since 2.0 * @since 2.0
* @see javax.persistence.spi.PersistenceUnitInfo#addTransformer(javax.persistence.spi.ClassTransformer) * @see javax.persistence.spi.PersistenceUnitInfo#addTransformer(javax.persistence.spi.ClassTransformer)
*/ */
@ -38,8 +39,11 @@ class ClassFileTransformerAdapter implements ClassFileTransformer {
private static final Log logger = LogFactory.getLog(ClassFileTransformerAdapter.class); private static final Log logger = LogFactory.getLog(ClassFileTransformerAdapter.class);
private final ClassTransformer classTransformer; private final ClassTransformer classTransformer;
private boolean currentlyTransforming = false;
public ClassFileTransformerAdapter(ClassTransformer classTransformer) { public ClassFileTransformerAdapter(ClassTransformer classTransformer) {
Assert.notNull(classTransformer, "ClassTransformer must not be null"); Assert.notNull(classTransformer, "ClassTransformer must not be null");
@ -52,28 +56,42 @@ class ClassFileTransformerAdapter implements ClassFileTransformer {
ClassLoader loader, String className, Class<?> classBeingRedefined, ClassLoader loader, String className, Class<?> classBeingRedefined,
ProtectionDomain protectionDomain, byte[] classfileBuffer) { ProtectionDomain protectionDomain, byte[] classfileBuffer) {
try { synchronized (this) {
byte[] transformed = this.classTransformer.transform( if (this.currentlyTransforming) {
loader, className, classBeingRedefined, protectionDomain, classfileBuffer); // Defensively back out when called from within the transform delegate below:
if (transformed != null && logger.isDebugEnabled()) { // in particular, for the over-eager transformer implementation in Hibernate 5.
logger.debug("Transformer of class [" + this.classTransformer.getClass().getName() + return null;
"] transformed class [" + className + "]; bytes in=" +
classfileBuffer.length + "; bytes out=" + transformed.length);
} }
return transformed;
} this.currentlyTransforming = true;
catch (ClassCircularityError ex) { try {
logger.error("Error weaving class [" + className + "] with " + byte[] transformed = this.classTransformer.transform(
"transformer of class [" + this.classTransformer.getClass().getName() + "]", ex); loader, className, classBeingRedefined, protectionDomain, classfileBuffer);
throw new IllegalStateException("Could not weave class [" + className + "]", ex); if (transformed != null && logger.isDebugEnabled()) {
} logger.debug("Transformer of class [" + this.classTransformer.getClass().getName() +
catch (Throwable ex) { "] transformed class [" + className + "]; bytes in=" +
if (logger.isWarnEnabled()) { classfileBuffer.length + "; bytes out=" + transformed.length);
logger.warn("Error weaving class [" + className + "] with " + }
"transformer of class [" + this.classTransformer.getClass().getName() + "]", ex); return transformed;
}
catch (ClassCircularityError 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);
}
// The exception will be ignored by the class loader, anyway...
throw new IllegalStateException("Could not weave class [" + className + "]", ex);
}
finally {
this.currentlyTransforming = false;
} }
// The exception will be ignored by the class loader, anyway...
throw new IllegalStateException("Could not weave class [" + className + "]", ex);
} }
} }

Loading…
Cancel
Save