HHH-11289 : Lazy-initializing a static Method and making accessible not thread-safe

This commit is contained in:
Gail Badner 2016-12-01 15:35:09 -08:00
parent c3d1573fdf
commit 3a1cbf3280
2 changed files with 25 additions and 39 deletions

View File

@ -18,10 +18,10 @@ import org.hibernate.AnnotationException;
import org.hibernate.HibernateException;
import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.annotations.common.reflection.java.JavaXMember;
import org.hibernate.boot.spi.AttributeConverterDescriptor;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.cfg.AttributeConverterDefinition;
import org.hibernate.cfg.annotations.HCANNHelper;
import com.fasterxml.classmate.ResolvedType;
import com.fasterxml.classmate.ResolvedTypeWithMembers;
@ -148,25 +148,16 @@ public class AttributeConverterDescriptorImpl implements AttributeConverterDescr
);
}
private static Method memberMethod;
private static Member toMember(XProperty xProperty) {
if ( memberMethod == null ) {
Class<JavaXMember> javaXMemberClass = JavaXMember.class;
try {
memberMethod = javaXMemberClass.getDeclaredMethod( "getMember" );
memberMethod.setAccessible( true );
}
catch (NoSuchMethodException e) {
throw new HibernateException( "Could not access org.hibernate.annotations.common.reflection.java.JavaXMember#getMember method", e );
}
}
try {
return (Member) memberMethod.invoke( xProperty );
return HCANNHelper.getUnderlyingMember( xProperty );
}
catch (Exception e) {
throw new HibernateException( "Could not access org.hibernate.annotations.common.reflection.java.JavaXMember#getMember method", e );
throw new HibernateException(
"Could not resolve member signature from XProperty reference",
e
);
}
}

View File

@ -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
);
}
}
}