bloritsch 2002/10/02 21:34:47
Added: util/src/java/org/apache/excalibur/util Delegate.java
util/src/test/org/apache/excalibur/util/test
DelegateTestCase.java TestDelegate.java
Log:
initial support for a Delegate framework
Revision Changes Path
1.1
jakarta-avalon-excalibur/util/src/java/org/apache/excalibur/util/Delegate.java
Index: Delegate.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
must not be used to endorse or promote products derived from this software
without prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation. For more information on the
Apache Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.excalibur.util;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
/**
* Used to create a proxy for a method. It is supposed to be as close to the
* C# delegate as possible. The Delegate is only allowed to have one method,
* and all instances of it will call that one method.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
*/
public abstract class Delegate
{
protected final Method m_method;
protected final Object m_object;
/**
* Pass in the delegate object that implements the same method that
* the concrete definition does.
*
* @param objDelegate the Object that implements the method.
*/
public Delegate( Object objDelegate )
{
m_object = objDelegate;
Method[] methods = getClass().getMethods();
Method delegateMethod = null;
for ( int i = 0; i < methods.length; i++ )
{
if ( Modifier.isPublic( methods[i].getModifiers() ) )
{
// ignore all Object methods
String name = methods[i].getName();
if ( name.equals( "getClass" ) )
continue;
if ( name.equals( "equals" ) )
continue;
if ( name.equals( "hashCode" ) )
continue;
if ( name.equals( "notify" ) )
continue;
if ( name.equals( "notifyAll" ) )
continue;
if ( name.equals( "toString" ) )
continue;
if ( name.equals( "wait" ) )
continue;
delegateMethod = methods[i];
}
}
if ( null == delegateMethod )
{
throw new VerifyError("A Delegate must have one method that is not an
Object method.");
}
Class[] argTypes = delegateMethod.getParameterTypes();
String name = delegateMethod.getName();
try
{
Class objClass = m_object.getClass();
m_method = objClass.getMethod( name, argTypes );
}
catch( Exception e )
{
throw new IllegalArgumentException( "The class does not implement the
required method." );
}
}
}
1.1
jakarta-avalon-excalibur/util/src/test/org/apache/excalibur/util/test/DelegateTestCase.java
Index: DelegateTestCase.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
must not be used to endorse or promote products derived from this software
without prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation. For more information on the
Apache Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.excalibur.util.test;
import junit.framework.TestCase;
/**
* @author <a href="${EMAIL}">bloritsch</a>
*/
public class DelegateTestCase extends TestCase
{
private final static String MESSAGE = "message";
public DelegateTestCase( String name )
{
super( name );
}
private final class Echo
{
public String echo( String message )
{
return "Echo: " + message;
}
}
private final class OtherEcho
{
public String echo( String message )
{
return "OtherEcho: " + message;
}
}
public void testDelegate()
{
TestDelegate delegate = new TestDelegate( new Echo() );
assertTrue( delegate.echo( MESSAGE ).startsWith( "Echo: " ) );
}
public void testDifferentDelegate()
{
TestDelegate delegate = new TestDelegate( new Echo() );
TestDelegate otherDelegate = new TestDelegate( new OtherEcho() );
assertTrue( delegate.echo( MESSAGE ).startsWith( "Echo: " ) );
assertTrue( otherDelegate.echo( MESSAGE ).startsWith( "OtherEcho: " ) );
}
protected void checkDelegate( TestDelegate delegate )
{
String answer = delegate.echo( MESSAGE );
assertEquals( MESSAGE, answer.substring(answer.indexOf(' ') + 1) );
}
public void testExpectedUse()
{
checkDelegate( new TestDelegate( new Echo() ) );
checkDelegate( new TestDelegate( new OtherEcho() ) );
}
}
1.1
jakarta-avalon-excalibur/util/src/test/org/apache/excalibur/util/test/TestDelegate.java
Index: TestDelegate.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
must not be used to endorse or promote products derived from this software
without prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation. For more information on the
Apache Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.excalibur.util.test;
import org.apache.excalibur.util.Delegate;
/**
* Used in the testcase to create a Delegate. Eventually the method will be created
with BCEL or something.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
*/
public class TestDelegate extends Delegate
{
public TestDelegate( Object objDelegate )
{
super( objDelegate );
}
/**
* This must be a public method, and all the classes that this delegate uses
must implement the exact
* same signature.
*
* @param message The message that will be returned
* @return the String
*/
public String echo( String message )
{
try
{
return (String) m_method.invoke( m_object, new Object[] {message} );
}
catch( Exception e )
{
throw new RuntimeException( "Bad Method" );
}
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>