From c0e94443b1841a5dd229194c7b8c9fe69789be51 Mon Sep 17 00:00:00 2001 From: Hardy Ferentschik Date: Tue, 15 Jun 2010 14:53:17 +0000 Subject: [PATCH] HHH-5094 applied some changes as on 3.5 branch git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@19740 1b8cb986-b30d-0410-93ca-fae66ebed9b2 --- .../ejb/util/PersistenceUtilHelper.java | 12 +- .../test/util/PersistenceUtilHelperTest.java | 109 ++++++++++++++++++ 2 files changed, 114 insertions(+), 7 deletions(-) create mode 100644 entitymanager/src/test/java/org/hibernate/ejb/test/util/PersistenceUtilHelperTest.java diff --git a/entitymanager/src/main/java/org/hibernate/ejb/util/PersistenceUtilHelper.java b/entitymanager/src/main/java/org/hibernate/ejb/util/PersistenceUtilHelper.java index 51c47fc093..5408816d43 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/util/PersistenceUtilHelper.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/util/PersistenceUtilHelper.java @@ -5,7 +5,6 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Member; -import java.lang.reflect.Modifier; import java.lang.reflect.AccessibleObject; import java.util.ArrayList; import java.util.HashMap; @@ -24,6 +23,7 @@ import org.hibernate.collection.PersistentCollection; /** * @author Emmanuel Bernard + * @author Hardy Ferentschik */ public class PersistenceUtilHelper { public static LoadState isLoadedWithoutReference(Object proxy, String property, MetadataCache cache) { @@ -112,10 +112,8 @@ public class PersistenceUtilHelper { } private static void setAccessibility(Member member) { - if ( !Modifier.isPublic( member.getModifiers() ) ) { - //Sun's ease of use, sigh... - ( ( AccessibleObject ) member ).setAccessible( true ); - } + //Sun's ease of use, sigh... + ( ( AccessibleObject ) member ).setAccessible( true ); } public static LoadState isLoaded(Object o) { @@ -146,10 +144,10 @@ public class PersistenceUtilHelper { string[0] = Character.toUpperCase( string[0] ); methodName = new String( string ); try { - return clazz.getMethod( "get" + methodName ); + return clazz.getDeclaredMethod( "get" + methodName ); } catch ( NoSuchMethodException e ) { - return clazz.getMethod( "is" + methodName ); + return clazz.getDeclaredMethod( "is" + methodName ); } } catch ( NoSuchMethodException e ) { diff --git a/entitymanager/src/test/java/org/hibernate/ejb/test/util/PersistenceUtilHelperTest.java b/entitymanager/src/test/java/org/hibernate/ejb/test/util/PersistenceUtilHelperTest.java new file mode 100644 index 0000000000..faf69e7397 --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/test/util/PersistenceUtilHelperTest.java @@ -0,0 +1,109 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.ejb.test.util; + +import javax.persistence.spi.LoadState; + +import org.hibernate.ejb.util.PersistenceUtilHelper; + +/** + * Tests for HHH-5094 + * + * @author Hardy Ferentschik + */ +public class PersistenceUtilHelperTest extends junit.framework.TestCase { + private final PersistenceUtilHelper.MetadataCache cache = new PersistenceUtilHelper.MetadataCache(); + + public static class FieldAccessBean { + public String publicAccessProperty; + protected String protectedAccessProperty; + private String privateAccessProperty; + } + + public static class MethodAccessBean { + private String publicAccessProperty; + private String protectedAccessProperty; + private String privateAccessProperty; + + public String getPublicAccessPropertyValue() { + return publicAccessProperty; + } + + protected String getProtectedAccessPropertyValue() { + return protectedAccessProperty; + } + + private String getPrivateAccessPropertyValue() { + return privateAccessProperty; + } + } + + public void testIsLoadedWithReferencePublicField() { + assertEquals( + LoadState.UNKNOWN, + PersistenceUtilHelper.isLoadedWithReference( new FieldAccessBean(), "publicAccessProperty", cache ) + ); + } + + public void testIsLoadedWithReferencePublicMethod() { + assertEquals( + LoadState.UNKNOWN, + PersistenceUtilHelper.isLoadedWithReference( + new MethodAccessBean(), "publicAccessPropertyValue", cache + ) + ); + } + + public void testIsLoadedWithReferenceProtectedField() { + assertEquals( + LoadState.UNKNOWN, + PersistenceUtilHelper.isLoadedWithReference( new FieldAccessBean(), "protectedAccessProperty", cache ) + ); + } + + public void testIsLoadedWithReferenceProtectedMethod() { + assertEquals( + LoadState.UNKNOWN, + PersistenceUtilHelper.isLoadedWithReference( + new MethodAccessBean(), "protectedAccessPropertyValue", cache + ) + ); + } + + public void testIsLoadedWithReferencePrivateField() { + assertEquals( + LoadState.UNKNOWN, + PersistenceUtilHelper.isLoadedWithReference( new FieldAccessBean(), "privateAccessProperty", cache ) + ); + } + + public void testIsLoadedWithReferencePrivateMethod() { + assertEquals( + LoadState.UNKNOWN, + PersistenceUtilHelper.isLoadedWithReference( + new MethodAccessBean(), "privateAccessPropertyValue", cache + ) + ); + } +}