labs-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simo...@apache.org
Subject svn commit: r1133087 - in /labs/magma/trunk/foundation-beans/src: main/java/org/apache/magma/beans/PropertyInfo.java test/java/org/apache/magma/beans/BeanDataTest.java
Date Tue, 07 Jun 2011 16:35:51 GMT
Author: simoneg
Date: Tue Jun  7 16:35:51 2011
New Revision: 1133087

URL: http://svn.apache.org/viewvc?rev=1133087&view=rev
Log:
Access to underlying property, not modified by magma specific interpretations like MagreadOnly
or Consider

Modified:
    labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/PropertyInfo.java
    labs/magma/trunk/foundation-beans/src/test/java/org/apache/magma/beans/BeanDataTest.java

Modified: labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/PropertyInfo.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/PropertyInfo.java?rev=1133087&r1=1133086&r2=1133087&view=diff
==============================================================================
--- labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/PropertyInfo.java
(original)
+++ labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/PropertyInfo.java
Tue Jun  7 16:35:51 2011
@@ -25,6 +25,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.commons.beanutils.PropertyUtils;
 import org.apache.magma.basics.MagmaException;
 import org.apache.magma.basics.utils.GenericClass;
 import org.apache.magma.basics.utils.GenericClass.MethodDef;
@@ -78,9 +79,14 @@ public class PropertyInfo implements Clo
 	private boolean readable;
 	
 	/**
-	 * Whether the property is writable 
+	 * Whether the property is to be considered writable 
 	 */
 	private boolean writeable;
+
+	/**
+	 * Whether the property is really writable or not 
+	 */
+	private boolean underlyingWriteable;
 	
 	/**
 	 * Whether the property represents a collection (this includes {@link List}, {@link Set}
and arrays, but not maps or tables).
@@ -118,6 +124,26 @@ public class PropertyInfo implements Clo
 	 * If the type is a basic type, that is a primitive type, a wrapper, one of java.math numbers,
or Date.
 	 */
 	private boolean isBasicType;
+
+	/**
+	 * If the type is an enum.
+	 */
+	private boolean isEnum;
+	
+	/**
+	 * Another PropertyInfo representing the underlying property, without conversion or hiding.
Could be the same instance
+	 * if there is no conversion nor hiding to handle.
+	 */
+	private PropertyInfo underlyingProperty = null;
+	
+	/**
+	 * true is this instance is already an underlying representation.
+	 */
+	private boolean isUnderlying = false;
+
+	
+	private Method readMethod;
+	private Method writeMethod;
 	
 	/**
 	 * @return true if this property is readable from the bean, false otherwise
@@ -133,18 +159,18 @@ public class PropertyInfo implements Clo
 	}
 
 	/**
-	 * @return true if this property is writable to the bean, false otherwise
+	 * @return true if this property is to be considered writable to the bean, false otherwise
 	 */	
 	public boolean isWriteable() {
 		return writeable;
 	}
 	/**
-	 * @param writeable true if this property is writable to the bean, false otherwise
+	 * @param writeable true if this property is to be considered writable to the bean, false
otherwise
 	 */	
 	public void setWriteable(boolean writeable) {
 		this.writeable = writeable;
 	}
-	
+
 	/**
 	 * @return the name of the property.
 	 */		
@@ -166,6 +192,13 @@ public class PropertyInfo implements Clo
 	public boolean isBasicType() {
 		return this.isBasicType;
 	}
+
+	/**
+	 * @return true if the type is an {@link Enum}.
+	 */
+	public boolean isEnum() {
+		return this.isEnum;
+	}
 	
 	/**
 	 * Initializes this class, parsing the {@link PropertyDescriptor} and eventually annotations
found on the getter or setter methods.
@@ -179,14 +212,8 @@ public class PropertyInfo implements Clo
 	public void init(PropertyDescriptor descriptor, Class beanClass) {
 		this.beanClass = beanClass;
 		this.name = descriptor.getName();
-		Method readMethod = descriptor.getReadMethod();
-		Method writeMethod = descriptor.getWriteMethod();
-		
-		if ((readMethod != null && readMethod.isAnnotationPresent(MagReadOnly.class)) ||

-				(writeMethod != null && writeMethod.isAnnotationPresent(MagReadOnly.class)) ||
-				beanClass.isAnnotationPresent(MagReadOnly.class)) {
-			writeMethod = null;
-		}
+		readMethod = descriptor.getReadMethod();
+		writeMethod = descriptor.getWriteMethod();
 		
 		Consider consider = null;
 		if (readMethod != null) consider = readMethod.getAnnotation(Consider.class);
@@ -224,6 +251,16 @@ public class PropertyInfo implements Clo
 		
 		this.readable = readMethod != null;
 		this.writeable = writeMethod != null;
+		this.underlyingWriteable = this.writeable;
+		if ((readMethod != null && readMethod.isAnnotationPresent(MagReadOnly.class)) ||

+				(writeMethod != null && writeMethod.isAnnotationPresent(MagReadOnly.class)) ||
+				beanClass.isAnnotationPresent(MagReadOnly.class)) {
+			this.writeable = false;
+		}
+		initType();
+	}
+	
+	private void initType() {
 		if (this.type != null) {
 			this.isCollection = Collection.class.isAssignableFrom(this.type);
 			this.isMap = Map.class.isAssignableFrom(this.type);
@@ -256,8 +293,19 @@ public class PropertyInfo implements Clo
 					this.type.getName().startsWith("java.lang") ||
 					this.type.getName().startsWith("java.math") ||
 					Date.class.isAssignableFrom(this.type);
+				this.isEnum = Enum.class.isAssignableFrom(this.type);
 			} 
-		}
+		}		
+	}
+	
+	protected void initAsUnderlying(PropertyInfo sup) {
+		this.isUnderlying = true;
+		this.name = sup.name;
+		this.beanClass = sup.beanClass;
+		this.readable = sup.readable;
+		this.type = sup.underlyingType;
+		this.writeable = sup.underlyingWriteable;
+		this.initType();
 	}
 	
 	/**
@@ -362,11 +410,7 @@ public class PropertyInfo implements Clo
 	 * @return the string human friendly representation of value.
 	 */	
 	public String toUser(Object value) {
-		if (underlyingType != null && value != null) {
-			if (underlyingType.isInstance(value)) {
-				value = fromUnderlying(value);
-			}
-		}
+		value = fromUnderlying(value);
 		String ret = toString(value);
 		if (ret == null) return "";
 		ret = ret.trim();
@@ -382,6 +426,7 @@ public class PropertyInfo implements Clo
 	@SuppressWarnings("unchecked")
 	public Object toUnderlying(Object value) {
 		if (underlyingType == null) return value;
+		if (underlyingType.isInstance(value)) return value;		
 		return underlyingConverter.to(value);
 	}
 	
@@ -394,6 +439,7 @@ public class PropertyInfo implements Clo
 	@SuppressWarnings("unchecked")
 	public Object fromUnderlying(Object value) {
 		if (underlyingType == null) return value;
+		if (type.isInstance(value)) return value;		
 		return underlyingConverter.from(value);
 	}
 	
@@ -418,4 +464,54 @@ public class PropertyInfo implements Clo
 		return mapValueClass;
 	}
 	
+	/**
+	 * Set the given value for this property on the given bean. Value will be converted to underlying
if applicable.
+	 * @param bean The bean to set the property to
+	 * @param val The value to set, it will be converted to the underlying type.
+	 */
+	public void set(Object bean, Object val) {
+		try {
+			val = toUnderlying(val);
+			PropertyUtils.setProperty(bean, name, val);
+		} catch (Exception e) {
+			throw new MagmaException("Error setting value {0} on property {1}.{2}", val, this.beanClass,
this.name);
+		}
+	}
+	
+	/**
+	 * Get the value of this property on the given bean. The returned value will be converted
from underlying type if applicable.
+	 * @param bean The bean to get the property from.
+	 * @return The value, eventually converted from the unserlying type.
+	 */
+	public Object get(Object bean) {
+		try {
+			Object val = PropertyUtils.getProperty(bean, name);
+			val = fromUnderlying(val);
+			return val;
+		} catch (Exception e) {
+			throw new MagmaException("Error getting property {0}.{1}", this.beanClass, this.name);
+		}
+	}
+	
+	/**
+	 * Gives access to the underlying property. The underlying property is the raw property,
without
+	 * underlying type conversion given by {@link Consider} nor hiding by {@link MagReadOnly}
or other
+	 * view specific contrains.  
+	 * @return A {@link PropertyInfo} giving access to the raw underlying property, which could
be this same
+	 * 		instance if it has no special view annotations on it.
+	 */
+	public PropertyInfo getUnderlying() {
+		if (this.isUnderlying) return this;
+		if (this.underlyingProperty == null) {
+			if (this.underlyingType == null && this.underlyingWriteable == this.writeable)
{
+				this.underlyingProperty = this;
+			} else {
+				PropertyInfo np = new PropertyInfo();
+				np.initAsUnderlying(this);
+				this.underlyingProperty = np;
+			}
+		}
+		return this.underlyingProperty;
+	}
+	
 }

Modified: labs/magma/trunk/foundation-beans/src/test/java/org/apache/magma/beans/BeanDataTest.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-beans/src/test/java/org/apache/magma/beans/BeanDataTest.java?rev=1133087&r1=1133086&r2=1133087&view=diff
==============================================================================
--- labs/magma/trunk/foundation-beans/src/test/java/org/apache/magma/beans/BeanDataTest.java
(original)
+++ labs/magma/trunk/foundation-beans/src/test/java/org/apache/magma/beans/BeanDataTest.java
Tue Jun  7 16:35:51 2011
@@ -17,6 +17,7 @@
 package org.apache.magma.beans;
 
 import static org.junit.Assert.*;
+import static org.hamcrest.CoreMatchers.*;
 
 import java.util.Date;
 import java.util.Set;
@@ -86,6 +87,12 @@ public class BeanDataTest {
 	public void consider() throws Exception {
 		PropertyInfo property = bd.getProperty("maskedDate");
 		assertEquals(Date.class, property.getType());
+		
+		PropertyInfo und = property.getUnderlying();
+		assertThat(und, notNullValue());
+		assertThat(und, not(sameInstance(property)));
+		assertThat(und.getType(), equalTo(String.class));
+		
 	}
 	
 	@Test
@@ -93,6 +100,11 @@ public class BeanDataTest {
 		PropertyInfo property = bd.getProperty("roProp");
 		assertTrue(property.isReadable());
 		assertFalse(property.isWriteable());
+		
+		PropertyInfo und = property.getUnderlying();
+		assertThat(und, notNullValue());
+		assertThat(und, not(sameInstance(property)));
+		assertThat(und.isWriteable(), equalTo(true));
 	}
 	
 	@Test



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org


Mime
View raw message