notify-dpml AT lists.ibiblio.org
Subject: DPML Notify
List archive
r1019 - in trunk/main/transit/core/src: main/net/dpml/transit test/net/dpml/transit/test
- From: mcconnell at BerliOS <mcconnell AT mail.berlios.de>
- To: notify-dpml AT lists.ibiblio.org
- Subject: r1019 - in trunk/main/transit/core/src: main/net/dpml/transit test/net/dpml/transit/test
- Date: Sat, 4 Feb 2006 18:56:43 +0100
Author: mcconnell
Date: 2006-02-04 18:56:40 +0100 (Sat, 04 Feb 2006)
New Revision: 1019
Added:
trunk/main/transit/core/src/test/net/dpml/transit/test/ArrayTestCase.java
Modified:
trunk/main/transit/core/src/main/net/dpml/transit/Construct.java
Log:
add array handling directly within the construct class
Modified: trunk/main/transit/core/src/main/net/dpml/transit/Construct.java
===================================================================
--- trunk/main/transit/core/src/main/net/dpml/transit/Construct.java
2006-02-04 16:34:39 UTC (rev 1018)
+++ trunk/main/transit/core/src/main/net/dpml/transit/Construct.java
2006-02-04 17:56:40 UTC (rev 1019)
@@ -18,6 +18,10 @@
package net.dpml.transit;
+import java.beans.SimpleBeanInfo;
+import java.beans.BeanDescriptor;
+import java.beans.DefaultPersistenceDelegate;
+import java.beans.Encoder;
import java.beans.Expression;
import java.io.Serializable;
import java.lang.reflect.Field;
@@ -444,6 +448,14 @@
}
}
}
+ System.out.println( "TARGET: " + target );
+ System.out.println( "METHOD: " + method );
+ System.out.println( "INSTANCES: " + instances.length );
+ for( int i=0; i<instances.length; i++ )
+ {
+ Object instance = instances[i];
+ System.out.println( "INSTANCE: " + instance.getClass().getName()
);
+ }
Expression expression = new Expression( target, method, instances );
return expression.getValue();
}
@@ -727,4 +739,212 @@
return a.equals( b );
}
}
+
+
//--------------------------------------------------------------------------
+ // Array
+
//--------------------------------------------------------------------------
+
+ public static final class Array implements Value
+ {
+ /**
+ * Serial version identifier.
+ */
+ static final long serialVersionUID = 1L;
+
+
//--------------------------------------------------------------------------
+ // state
+
//--------------------------------------------------------------------------
+
+ private final String m_classname;
+ private final Value[] m_values;
+
+
//--------------------------------------------------------------------------
+ // constructors
+
//--------------------------------------------------------------------------
+
+ /**
+ * Create a new array construct.
+ *
+ * @param classname the array type
+ * @param values the construct values
+ */
+ public Array( String classname, Value[] values )
+ {
+ if( null == classname )
+ {
+ m_classname = Object.class.getName();
+ }
+ else
+ {
+ m_classname = classname;
+ }
+
+ m_values = values;
+ }
+
+
//--------------------------------------------------------------------------
+ // ArrayDirective
+
//--------------------------------------------------------------------------
+
+ public String getClassname()
+ {
+ return m_classname;
+ }
+
+ public Value[] getValues()
+ {
+ return m_values;
+ }
+
+
//--------------------------------------------------------------------------
+ // Value
+
//--------------------------------------------------------------------------
+
+ /**
+ * Resolve an instance from the value using the context classloader.
+ * @return the resolved instance
+ * @exception Exception if error occurs during instance resolution
+ */
+ public Object resolve() throws Exception
+ {
+ return resolve( null, false );
+ }
+
+ /**
+ * Resolve an instance from the value using a supplied context map.
If any
+ * target expressions in immediate or nested values contain a symbolic
+ * expression the value will be resolved using the supplied map.
+ *
+ * @param map the context map
+ * @return the resolved instance
+ * @exception Exception if error occurs during instance resolution
+ */
+ public Object resolve( Map map ) throws Exception
+ {
+ return resolve( map, false );
+ }
+
+ /**
+ * Resolve an instance from the value using a supplied isolvation
policy.
+ *
+ * @param isolate the isolation policy
+ * @return the resolved instance
+ * @exception Exception if error occurs during instance resolution
+ */
+ public Object resolve( boolean isolate ) throws Exception
+ {
+ return resolve( null, isolate );
+ }
+
+ /**
+ * Resolve an instance from the value using a supplied context map.
If any
+ * target expressions in immediate or nested values contain a symbolic
+ * expression the value will be resolved using the supplied map.
+ *
+ * @param map the context map
+ * @param isolate the isolation policy
+ * @return the resolved instance
+ * @exception Exception if error occurs during instance resolution
+ */
+ public Object resolve( Map map, boolean isolate ) throws Exception
+ {
+ ClassLoader loader =
Thread.currentThread().getContextClassLoader();
+ Class c = loader.loadClass( m_classname );
+ Object[] instances =
+ (Object[]) java.lang.reflect.Array.newInstance( c,
m_values.length );
+ for( int i=0; i<m_values.length; i++ )
+ {
+ Value value = m_values[i];
+ instances[i] = value.resolve( map, isolate );
+ }
+ return instances;
+ }
+
+ /**
+ * Test if the supplied object is equal to this object.
+ * @param other the object to compare with this instance
+ * @return TRUE if the supplied object is equal to this object
+ */
+ public boolean equals( Object other )
+ {
+ if( null == other )
+ {
+ return false;
+ }
+ if( !( other instanceof Array ) )
+ {
+ return false;
+ }
+ else
+ {
+ Array directive = (Array) other;
+ if( !m_classname.equals( directive.m_classname ) )
+ {
+ return false;
+ }
+ else
+ {
+ return Arrays.equals( m_values, directive.m_values );
+ }
+ }
+ }
+
+ /**
+ * Return the hashcode for the instance.
+ * @return the instance hashcode
+ */
+ public int hashCode()
+ {
+ int hash = m_classname.hashCode();
+ for( int i=0; i<m_values.length; i++ )
+ {
+ hash ^= m_values[i].hashCode();
+ }
+ return hash;
+ }
+ }
+
+ public static final class ArrayBeanInfo extends SimpleBeanInfo
+ {
+ private static final BeanDescriptor BEAN_DESCRIPTOR =
setupBeanDescriptor();
+
+ /**
+ * Return the bean descriptor.
+ * @return the descriptor
+ */
+ public BeanDescriptor getBeanDescriptor()
+ {
+ return BEAN_DESCRIPTOR;
+ }
+
+ private static BeanDescriptor setupBeanDescriptor()
+ {
+ BeanDescriptor descriptor = new BeanDescriptor( Array.class );
+ descriptor.setValue(
+ "persistenceDelegate",
+ new ArrayPersistenceDelegate() );
+ return descriptor;
+ }
+
+ /**
+ * Persistence delegate implementation.
+ */
+ private static class ArrayPersistenceDelegate extends
DefaultPersistenceDelegate
+ {
+ /**
+ * Return the expression value.
+ * @param old the old instance
+ * @param encoder the encoder
+ * @return the expression
+ */
+ public Expression instantiate( Object old, Encoder encoder )
+ {
+ Array construct = (Array) old;
+ Object[] args = new Object[2];
+ args[0] = construct.getClassname();
+ args[1] = construct.getValues();
+ return new Expression( old, old.getClass(), "new", args );
+ }
+ }
+ }
}
Added:
trunk/main/transit/core/src/test/net/dpml/transit/test/ArrayTestCase.java
===================================================================
--- trunk/main/transit/core/src/test/net/dpml/transit/test/ArrayTestCase.java
2006-02-04 16:34:39 UTC (rev 1018)
+++ trunk/main/transit/core/src/test/net/dpml/transit/test/ArrayTestCase.java
2006-02-04 17:56:40 UTC (rev 1019)
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2004-2006 Stephen J. McConnell.
+ *
+ * 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 net.dpml.transit.test;
+
+import net.dpml.transit.Value;
+import net.dpml.transit.Construct;
+import net.dpml.transit.Construct.Array;
+
+/**
+ * Construct testcase.
+ *
+ * @author <a href="@PUBLISHER-URL@">@PUBLISHER-NAME@</a>
+ * @version @PROJECT-VERSION@
+ */
+public class ArrayTestCase extends AbstractEncodingTestCase
+{
+ /**
+ * Test creation of a simple construct.
+ * @exception Exception if an unexpected error occurs.
+ */
+ public void testSimpleArray() throws Exception
+ {
+ Construct a = new Construct( "a" );
+ Construct b = new Construct( "b" );
+ Value[] args = new Value[]{a, b};
+ Array array = new Array( String.class.getName(), args );
+ Object value = array.resolve();
+ if( null == value )
+ {
+ throw new NullPointerException( "value" );
+ }
+ if( value instanceof String[] )
+ {
+ String[] strings = (String[]) value;
+ assertEquals( "length", 2, strings.length );
+ }
+ else
+ {
+ throw new IllegalStateException( "result is not an array" );
+ }
+ }
+
+ /**
+ * Test creation of a simple construct.
+ * @exception Exception if an unexpected error occurs.
+ */
+ public void testArrayAsCompositeArgument() throws Exception
+ {
+ Construct a = new Construct( "Hello " );
+ Construct b = new Construct( "World!" );
+ Value[] args = new Value[]{a, b};
+ Array array = new Array( String.class.getName(), args );
+
+ Value[] params = new Value[]{array};
+ Construct construct = new Construct( Demo.class.getName(), params );
+ Demo demo = (Demo) construct.resolve();
+ assertNotNull( "demo", demo );
+ }
+
+ public static class Demo
+ {
+ public Demo( String[] args )
+ {
+ for( int i=0; i<args.length; i++ )
+ {
+ System.out.print( args[i] );
+ }
+ System.out.println("");
+ }
+ }
+}
- r1019 - in trunk/main/transit/core/src: main/net/dpml/transit test/net/dpml/transit/test, mcconnell at BerliOS, 02/04/2006
Archive powered by MHonArc 2.6.24.