Author: julien(a)jboss.com
Date: 2007-03-14 10:31:01 -0400 (Wed, 14 Mar 2007)
New Revision: 6666
Added:
trunk/common/src/main/org/jboss/portal/test/common/ModifierTestCase.java
Modified:
trunk/common/build.xml
trunk/faces/src/main/org/jboss/portal/faces/el/DelegatingPropertyResolver.java
Log:
added support for public bean fields in delegating property resolver
Modified: trunk/common/build.xml
===================================================================
--- trunk/common/build.xml 2007-03-14 14:07:51 UTC (rev 6665)
+++ trunk/common/build.xml 2007-03-14 14:31:01 UTC (rev 6666)
@@ -231,6 +231,7 @@
<test todir="${test.reports}"
name="org.jboss.portal.test.common.CopyOnWriteRegistryTestCase"/>
<test todir="${test.reports}"
name="org.jboss.portal.test.common.URLToolsTestCase"/>
<test todir="${test.reports}"
name="org.jboss.portal.test.common.ToolsTestCase"/>
+ <test todir="${test.reports}"
name="org.jboss.portal.test.common.ModifierTestCase"/>
</x-test>
<x-classpath>
<pathelement location="${build.classes}"/>
Added: trunk/common/src/main/org/jboss/portal/test/common/ModifierTestCase.java
===================================================================
--- trunk/common/src/main/org/jboss/portal/test/common/ModifierTestCase.java
(rev 0)
+++ trunk/common/src/main/org/jboss/portal/test/common/ModifierTestCase.java 2007-03-14
14:31:01 UTC (rev 6666)
@@ -0,0 +1,103 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.test.common;
+
+import junit.framework.TestCase;
+import org.jboss.portal.common.reflect.Modifier;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class ModifierTestCase extends TestCase
+{
+
+ public void testIsReadableProperty() throws Exception
+ {
+
assertTrue(Modifier.isReadableProperty(Bean.class.getDeclaredField("publicField")));
+
assertFalse(Modifier.isReadableProperty(Bean.class.getDeclaredField("privateField")));
+
assertFalse(Modifier.isReadableProperty(Bean.class.getDeclaredField("protectedField")));
+
assertFalse(Modifier.isReadableProperty(Bean.class.getDeclaredField("packageProtectedField")));
+
+
assertTrue(Modifier.isReadableProperty(Bean.class.getDeclaredField("finalPublicField")));
+
assertFalse(Modifier.isReadableProperty(Bean.class.getDeclaredField("finalPrivateField")));
+
assertFalse(Modifier.isReadableProperty(Bean.class.getDeclaredField("finalProtectedField")));
+
assertFalse(Modifier.isReadableProperty(Bean.class.getDeclaredField("finalPackageProtectedField")));
+
+
assertFalse(Modifier.isReadableProperty(Bean.class.getDeclaredField("staticPublicField")));
+
assertFalse(Modifier.isReadableProperty(Bean.class.getDeclaredField("staticPrivateField")));
+
assertFalse(Modifier.isReadableProperty(Bean.class.getDeclaredField("staticProtectedField")));
+
assertFalse(Modifier.isReadableProperty(Bean.class.getDeclaredField("staticPackageProtectedField")));
+
+
assertFalse(Modifier.isReadableProperty(Bean.class.getDeclaredField("finalStaticPublicField")));
+
assertFalse(Modifier.isReadableProperty(Bean.class.getDeclaredField("finalStaticPrivateField")));
+
assertFalse(Modifier.isReadableProperty(Bean.class.getDeclaredField("finalStaticProtectedField")));
+
assertFalse(Modifier.isReadableProperty(Bean.class.getDeclaredField("finalStaticPackageProtectedField")));
+ }
+
+ public void testIsWritableProperty() throws Exception
+ {
+
assertTrue(Modifier.isWritableProperty(Bean.class.getDeclaredField("publicField")));
+
assertFalse(Modifier.isWritableProperty(Bean.class.getDeclaredField("privateField")));
+
assertFalse(Modifier.isWritableProperty(Bean.class.getDeclaredField("protectedField")));
+
assertFalse(Modifier.isWritableProperty(Bean.class.getDeclaredField("packageProtectedField")));
+
+
assertFalse(Modifier.isWritableProperty(Bean.class.getDeclaredField("finalPublicField")));
+
assertFalse(Modifier.isWritableProperty(Bean.class.getDeclaredField("finalPrivateField")));
+
assertFalse(Modifier.isWritableProperty(Bean.class.getDeclaredField("finalProtectedField")));
+
assertFalse(Modifier.isWritableProperty(Bean.class.getDeclaredField("finalPackageProtectedField")));
+
+
assertFalse(Modifier.isWritableProperty(Bean.class.getDeclaredField("staticPublicField")));
+
assertFalse(Modifier.isWritableProperty(Bean.class.getDeclaredField("staticPrivateField")));
+
assertFalse(Modifier.isWritableProperty(Bean.class.getDeclaredField("staticProtectedField")));
+
assertFalse(Modifier.isWritableProperty(Bean.class.getDeclaredField("staticPackageProtectedField")));
+
+
assertFalse(Modifier.isWritableProperty(Bean.class.getDeclaredField("finalStaticPublicField")));
+
assertFalse(Modifier.isWritableProperty(Bean.class.getDeclaredField("finalStaticPrivateField")));
+
assertFalse(Modifier.isWritableProperty(Bean.class.getDeclaredField("finalStaticProtectedField")));
+
assertFalse(Modifier.isWritableProperty(Bean.class.getDeclaredField("finalStaticPackageProtectedField")));
+ }
+
+ private static class Bean
+ {
+ public Object publicField;
+ private Object privateField;
+ protected Object protectedField;
+ Object packageProtectedField;
+
+ final public Object finalPublicField = new Object();
+ final private Object finalPrivateField = new Object();
+ final protected Object finalProtectedField = new Object();
+ final Object finalPackageProtectedField = new Object();
+
+ static public Object staticPublicField;
+ static private Object staticPrivateField;
+ static protected Object staticProtectedField;
+ static Object staticPackageProtectedField;
+
+ final static public Object finalStaticPublicField = new Object();
+ final static private Object finalStaticPrivateField = new Object();
+ final static protected Object finalStaticProtectedField = new Object();
+ final static Object finalStaticPackageProtectedField = new Object();
+ }
+}
Modified: trunk/faces/src/main/org/jboss/portal/faces/el/DelegatingPropertyResolver.java
===================================================================
---
trunk/faces/src/main/org/jboss/portal/faces/el/DelegatingPropertyResolver.java 2007-03-14
14:07:51 UTC (rev 6665)
+++
trunk/faces/src/main/org/jboss/portal/faces/el/DelegatingPropertyResolver.java 2007-03-14
14:31:01 UTC (rev 6666)
@@ -25,12 +25,14 @@
import org.apache.log4j.Logger;
import org.jboss.portal.faces.el.dynamic.DynamicBean;
import org.jboss.portal.faces.el.decorator.BeanDecorator;
+import org.jboss.portal.common.reflect.Modifier;
import javax.faces.el.EvaluationException;
import javax.faces.el.PropertyNotFoundException;
import javax.faces.el.PropertyResolver;
import java.util.HashMap;
import java.util.Map;
+import java.lang.reflect.Field;
/**
* A property resolver that provide fine grained configuration of the resolution
mechanism.
@@ -97,6 +99,22 @@
}
//
+ if (property instanceof String)
+ {
+ try
+ {
+ Field f = base.getClass().getField((String)property);
+ if (Modifier.isReadableProperty(f))
+ {
+ return f.getType();
+ }
+ }
+ catch (NoSuchFieldException ignore)
+ {
+ }
+ }
+
+ //
if (delegate != null)
{
return delegate.getType(base, property);
@@ -139,6 +157,26 @@
}
//
+ if (property instanceof String)
+ {
+ try
+ {
+ Field f = base.getClass().getField((String)property);
+ if (Modifier.isReadableProperty(f))
+ {
+ return f.get(base);
+ }
+ }
+ catch (NoSuchFieldException ignore)
+ {
+ }
+ catch (IllegalAccessException e)
+ {
+ log.error("Was not able to read the field " + property + " of
object " + base + " with class " + base.getClass().getName());
+ }
+ }
+
+ //
if (delegate != null)
{
return delegate.getValue(base, property);
@@ -168,6 +206,27 @@
}
//
+ if (property instanceof String)
+ {
+ try
+ {
+ Field f = base.getClass().getField((String)property);
+ if (Modifier.isWritableProperty(f))
+ {
+ f.set(base, value);
+ return;
+ }
+ }
+ catch (NoSuchFieldException ignore)
+ {
+ }
+ catch (IllegalAccessException e)
+ {
+ log.error("Was not able to write the field " + property + " of
object " + base + " with class " + base.getClass().getName());
+ }
+ }
+
+ //
if (delegate != null)
{
delegate.setValue(base, property, value);