diff --git a/hibernate-core/src/main/java/org/hibernate/proxy/pojo/bytebuddy/ByteBuddyProxyFactory.java b/hibernate-core/src/main/java/org/hibernate/proxy/pojo/bytebuddy/ByteBuddyProxyFactory.java index 455e9df04f..e7b13aa993 100644 --- a/hibernate-core/src/main/java/org/hibernate/proxy/pojo/bytebuddy/ByteBuddyProxyFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/proxy/pojo/bytebuddy/ByteBuddyProxyFactory.java @@ -8,6 +8,8 @@ package org.hibernate.proxy.pojo.bytebuddy; import java.io.Serializable; import java.lang.reflect.Method; +import java.security.AccessController; +import java.security.PrivilegedAction; import java.util.Set; import org.hibernate.HibernateException; @@ -85,21 +87,39 @@ public class ByteBuddyProxyFactory implements ProxyFactory, Serializable { overridesEquals ); - try { - final HibernateProxy proxy = (HibernateProxy) proxyClass.getConstructor().newInstance(); + + final HibernateProxy proxy = getHibernateProxy(); ( (ProxyConfiguration) proxy ).$$_hibernate_set_interceptor( interceptor ); return proxy; - } - catch (NoSuchMethodException e) { - String logMessage = LOG.bytecodeEnhancementFailedBecauseOfDefaultConstructor( entityName ); - LOG.error( logMessage, e ); - throw new HibernateException( logMessage, e ); - } - catch (Throwable t) { - String logMessage = LOG.bytecodeEnhancementFailed( entityName ); - LOG.error( logMessage, t ); - throw new HibernateException( logMessage, t ); - } + + } + + private HibernateProxy getHibernateProxy() + throws HibernateException { + + final PrivilegedAction action = new PrivilegedAction() { + + @Override + public HibernateProxy run() { + + try { + return (HibernateProxy) proxyClass.getConstructor().newInstance(); + } + catch (NoSuchMethodException e) { + String logMessage = LOG.bytecodeEnhancementFailedBecauseOfDefaultConstructor( entityName ); + LOG.error( logMessage, e ); + throw new HibernateException( logMessage, e ); + } + catch (Throwable t) { + String logMessage = LOG.bytecodeEnhancementFailed( entityName ); + LOG.error( logMessage, t ); + throw new HibernateException( logMessage, t ); + } + + } + }; + return System.getSecurityManager() != null ? AccessController.doPrivileged( action ) : action.run(); + } }