From 747d24b7c16bd62473f7a90b2e9f9a8e2aa6f69d Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Thu, 1 Dec 2016 15:35:09 -0800 Subject: [PATCH] HHH-11289 : Lazy-initializing a static Method and making accessible not thread-safe (cherry picked from commit 3a1cbf32805a38bba8d61de50c8b2a4daf4d0f48) Conflicts: hibernate-core/src/main/java/org/hibernate/boot/internal/AttributeConverterDescriptorImpl.java --- .../cfg/annotations/HCANNHelper.java | 43 ++++++++----------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/HCANNHelper.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/HCANNHelper.java index 63f64db068..c2c4ca737c 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/HCANNHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/HCANNHelper.java @@ -6,12 +6,12 @@ */ package org.hibernate.cfg.annotations; -import java.lang.reflect.AnnotatedElement; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Member; import java.lang.reflect.Method; import org.hibernate.AssertionFailure; +import org.hibernate.HibernateException; import org.hibernate.annotations.common.reflection.XProperty; import org.hibernate.annotations.common.reflection.java.JavaXMember; @@ -21,22 +21,31 @@ import org.hibernate.annotations.common.reflection.java.JavaXMember; * @author Steve Ebersole */ public class HCANNHelper { - private static Class javaXMemberClass = JavaXMember.class; private static Method getMemberMethod; + static { + // The following is in a static block to avoid problems lazy-initializing + // and making accessible in a multi-threaded context. See HHH-11289. + final Class javaXMemberClass = JavaXMember.class; + try { + getMemberMethod = javaXMemberClass.getDeclaredMethod( "getMember" ); + getMemberMethod.setAccessible( true ); + } + catch (NoSuchMethodException e) { + throw new AssertionFailure( + "Could not resolve JavaXMember#getMember method in order to access XProperty member signature", + e + ); + } + catch (Exception e) { + throw new HibernateException( "Could not access org.hibernate.annotations.common.reflection.java.JavaXMember#getMember method", e ); + } + } public static String annotatedElementSignature(XProperty xProperty) { - if ( getMemberMethod == null ) { - resolveGetMemberMethod(); - } - return getUnderlyingMember( xProperty ).toString(); } public static Member getUnderlyingMember(XProperty xProperty) { - if ( getMemberMethod == null ) { - resolveGetMemberMethod(); - } - try { return (Member) getMemberMethod.invoke( xProperty ); } @@ -53,18 +62,4 @@ public class HCANNHelper { ); } } - - @SuppressWarnings("unchecked") - private static void resolveGetMemberMethod() { - try { - getMemberMethod = javaXMemberClass.getDeclaredMethod( "getMember" ); - getMemberMethod.setAccessible( true ); - } - catch (NoSuchMethodException e) { - throw new AssertionFailure( - "Could not resolve JavaXAnnotatedElement#toAnnotatedElement method in order to access XProperty member signature", - e - ); - } - } }