[hibernate-commits] Hibernate SVN: r17838 - in validator/trunk/hibernate-validator/src: test/java/org/hibernate/validator/engine and 1 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Mon Oct 26 12:11:42 EDT 2009


Author: hardy.ferentschik
Date: 2009-10-26 12:11:42 -0400 (Mon, 26 Oct 2009)
New Revision: 17838

Added:
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/proxy/
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/proxy/A.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/proxy/B.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/proxy/ProxyTest.java
Modified:
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ReflectionHelper.java
Log:
 HV-257

Changed the implementation of ReflectionHelper.setAccessibility() to take into accound the abstract keyword

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ReflectionHelper.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ReflectionHelper.java	2009-10-26 05:26:51 UTC (rev 17837)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ReflectionHelper.java	2009-10-26 16:11:42 UTC (rev 17838)
@@ -181,10 +181,14 @@
 		return value;
 	}
 
-	//run client in privileged block
 	static void setAccessibility(Member member) {
-		if ( !Modifier.isPublic( member.getModifiers() ) ) {
-			//Sun's ease of use, sigh...
+		// HV-257
+		// Also set accessibility in case of public abstract members. If you proxy an interface using java.lang.reflect.Proxy
+		// per default you will get a IllegalAccessException since you are not allowed to access public abstract methods.
+		// Seems odd. One could argue that the proxy 'is' the implementation for the interface method and hence they
+		// should be accessible. Maybe this is a JVM bug !?
+		if ( !Modifier.isPublic( member.getModifiers() )
+				|| ( Modifier.isPublic( member.getModifiers() ) && Modifier.isAbstract( member.getModifiers() ) ) ) {
 			( ( AccessibleObject ) member ).setAccessible( true );
 		}
 	}

Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/proxy/A.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/proxy/A.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/proxy/A.java	2009-10-26 16:11:42 UTC (rev 17838)
@@ -0,0 +1,29 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+* http://www.apache.org/licenses/LICENSE-2.0
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.hibernate.validator.engine.proxy;
+
+import javax.validation.constraints.Min;
+import javax.validation.constraints.Size;
+
+interface A {
+	@Min(5)
+	public Integer getInteger();
+
+	@Size(min = 2)
+	public String getString();
+}
\ No newline at end of file


Property changes on: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/proxy/A.java
___________________________________________________________________
Name: svn:keywords
   + Id

Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/proxy/B.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/proxy/B.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/proxy/B.java	2009-10-26 16:11:42 UTC (rev 17838)
@@ -0,0 +1,24 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+* http://www.apache.org/licenses/LICENSE-2.0
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.hibernate.validator.engine.proxy;
+
+import javax.validation.constraints.Min;
+import javax.validation.constraints.Size;
+
+public interface B extends A {
+}
\ No newline at end of file


Property changes on: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/proxy/B.java
___________________________________________________________________
Name: svn:keywords
   + Id

Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/proxy/ProxyTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/proxy/ProxyTest.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/proxy/ProxyTest.java	2009-10-26 16:11:42 UTC (rev 17838)
@@ -0,0 +1,83 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+* http://www.apache.org/licenses/LICENSE-2.0
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.hibernate.validator.engine.proxy;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.Set;
+import javax.validation.ConstraintViolation;
+import javax.validation.Validator;
+
+import static org.testng.Assert.assertEquals;
+import org.testng.annotations.Test;
+
+import org.hibernate.validator.util.TestUtil;
+import static org.hibernate.validator.util.TestUtil.assertNumberOfViolations;
+
+/**
+ * See HV-257
+ *
+ * @author Hardy Ferentschik
+ */
+public class ProxyTest {
+	@Test
+	public void testValidateA() {
+		InvocationHandler handler = new CustomInvocationHandler( "some object" );
+
+		A a = ( A ) Proxy.newProxyInstance( getClass().getClassLoader(), new Class<?>[] { A.class }, handler );
+		assertEquals( Integer.valueOf( 0 ), a.getInteger() );
+
+		Validator validator = TestUtil.getValidator();
+		Set<ConstraintViolation<A>> violations = validator.validate( a );
+		assertNumberOfViolations( violations, 2 );
+	}
+
+	@Test
+	public void testValidateB() {
+		InvocationHandler handler = new CustomInvocationHandler( "some object" );
+
+		B b = ( B ) Proxy.newProxyInstance( getClass().getClassLoader(), new Class<?>[] { B.class }, handler );
+		assertEquals( Integer.valueOf( 0 ), b.getInteger() );
+
+		Validator validator = TestUtil.getValidator();
+		Set<ConstraintViolation<B>> violations = validator.validate( b );
+		assertNumberOfViolations( violations, 2 );
+	}
+
+	private class CustomInvocationHandler implements InvocationHandler {
+		private Object o;
+
+		public CustomInvocationHandler(Object o) {
+			this.o = o;
+		}
+
+		public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+			if ( method.getName().equals( "getInteger" ) ) {
+				method.setAccessible( true );
+				return 0;
+			}
+			if ( method.getName().equals( "getString" ) ) {
+				return "a";
+			}
+			return method.invoke( o, args );
+		}
+	}
+}
+
+


Property changes on: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/proxy/ProxyTest.java
___________________________________________________________________
Name: svn:keywords
   + Id



More information about the hibernate-commits mailing list