diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/ClassFileTransformerAdapter.java b/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/ClassFileTransformerAdapter.java index 9a62f076a1e..9db7454c434 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/ClassFileTransformerAdapter.java +++ b/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"); * 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) */ @@ -38,8 +39,11 @@ class ClassFileTransformerAdapter implements ClassFileTransformer { private static final Log logger = LogFactory.getLog(ClassFileTransformerAdapter.class); + private final ClassTransformer classTransformer; + private boolean currentlyTransforming = false; + public ClassFileTransformerAdapter(ClassTransformer classTransformer) { Assert.notNull(classTransformer, "ClassTransformer must not be null"); @@ -52,28 +56,42 @@ class ClassFileTransformerAdapter implements ClassFileTransformer { ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) { - try { - byte[] transformed = this.classTransformer.transform( - loader, className, classBeingRedefined, protectionDomain, classfileBuffer); - if (transformed != null && logger.isDebugEnabled()) { - logger.debug("Transformer of class [" + this.classTransformer.getClass().getName() + - "] transformed class [" + className + "]; bytes in=" + - classfileBuffer.length + "; bytes out=" + transformed.length); + synchronized (this) { + if (this.currentlyTransforming) { + // Defensively back out when called from within the transform delegate below: + // in particular, for the over-eager transformer implementation in Hibernate 5. + return null; } - 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); - } - catch (Throwable ex) { - if (logger.isWarnEnabled()) { - logger.warn("Error weaving class [" + className + "] with " + - "transformer of class [" + this.classTransformer.getClass().getName() + "]", ex); + + this.currentlyTransforming = true; + try { + byte[] transformed = this.classTransformer.transform( + loader, className, classBeingRedefined, protectionDomain, classfileBuffer); + if (transformed != null && logger.isDebugEnabled()) { + logger.debug("Transformer of class [" + this.classTransformer.getClass().getName() + + "] transformed class [" + className + "]; bytes in=" + + classfileBuffer.length + "; bytes out=" + transformed.length); + } + 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); } }