Hello, These is an utility to create interface implementations which haven't any implemantation.
Usefull to initialise no final dependencies : private Comparator comparator = new UnsupportedImplementation(Comparator.class).getInstance(); public void setComparator(Comparator comparator) { Validate.notNull(comparator); this.comparator = comparator; } public void doSomething() { // ... // no problem, comparator can't be null comparator.comparare(instance1, instance2); // ... } Or to create simple instances for tests, etc ... A first enhancement could be use a specified Exception. Hope it can be usefull. -- Alban Peignier - [EMAIL PROTECTED] http://people.tryphon.org/~alban
Index: src/test/org/apache/commons/lang/UnsupportedImplementationTest.java =================================================================== --- src/test/org/apache/commons/lang/UnsupportedImplementationTest.java (revision 0) +++ src/test/org/apache/commons/lang/UnsupportedImplementationTest.java (revision 0) @@ -0,0 +1,37 @@ +package org.apache.commons.lang; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit tests [EMAIL PROTECTED] org.apache.commons.lang.UnsupportedImplementation}. + * + * @author Alban Peignier + * @author Arnaud Thiefaine + */ +public class UnsupportedImplementationTest extends TestCase { + + public static Test suite() { + return new TestSuite(UnsupportedImplementationTest.class); + } + + public void testEquals() { + Object instance = new UnsupportedImplementation(Comparable.class).getInstance(); + assertEquals(instance, instance); + + Object otherInstance = new UnsupportedImplementation(Comparable.class).getInstance(); + assertNotSame(instance, otherInstance); + assertFalse(instance.equals(otherInstance)); + } + + public void testGetInstance() { + Comparable comparable = (Comparable) new UnsupportedImplementation(Comparable.class).getInstance(); + + try { + comparable.compareTo(comparable); + fail("should throw an UnsupportedOperationException"); + } catch (UnsupportedOperationException e) {} + } + +} Index: src/test/org/apache/commons/lang/LangTestSuite.java =================================================================== --- src/test/org/apache/commons/lang/LangTestSuite.java (revision 151351) +++ src/test/org/apache/commons/lang/LangTestSuite.java (working copy) @@ -26,7 +26,7 @@ * @author Stephen Colebourne * @author <a href="mailto:[EMAIL PROTECTED]">Ringo De Smet</a> * @author Matthew Hawthorne - * @version $Id: LangTestSuite.java,v 1.29 2004/10/02 01:46:30 bayard Exp $ + * @version $Id$ */ public class LangTestSuite extends TestCase { @@ -78,6 +78,7 @@ suite.addTest(StringEscapeUtilsTest.suite()); suite.addTest(SystemUtilsTest.suite()); suite.addTest(UnhandledExceptionTest.suite()); + suite.addTest(UnsupportedImplementationTest.suite()); suite.addTest(ValidateTest.suite()); suite.addTest(WordUtilsTest.suite()); return suite; Index: src/java/org/apache/commons/lang/UnsupportedImplementation.java =================================================================== --- src/java/org/apache/commons/lang/UnsupportedImplementation.java (revision 0) +++ src/java/org/apache/commons/lang/UnsupportedImplementation.java (revision 0) @@ -0,0 +1,103 @@ +/* + * Copyright 2002-2004 The Apache Software Foundation. + * + * 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.apache.commons.lang; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +import org.apache.commons.lang.BooleanUtils; +import org.apache.commons.lang.UnhandledException; + +/** + * Provides instances of interfaces which have an unsupported implementation (throw an exception). + * <p> + * The only implemented method is the [EMAIL PROTECTED] Object#equals} method in order to allow instances to be + * equals to themselves: + * <code><pre> + * Object instance = new UnsupportedImplementation(..).getInstance(); + * instance.equals(instance); // returns true + * </pre></code> + * + * @author Alban Peignier + * @author Arnaud Thiefaine + */ +public class UnsupportedImplementation { + + /** + * All the interfaces implemented by the returned instances. + */ + private final Class[] interfaceClasses; + + /** + * Creates a <code>UnsupportedImplementation</code> object. + * @param interfaceClass the interface that implement the created instances. + */ + public UnsupportedImplementation(Class interfaceClass) { + this(new Class[] { interfaceClass }); + } + + /** + * Creates a <code>UnsupportedImplementation</code> object. + * @param interfaceClasses the interfaces that implement the created instances. + */ + public UnsupportedImplementation(Class[] interfaceClasses) { + this.interfaceClasses = interfaceClasses; + } + + /** + * Creates a instance which implements the defined interfaces + * which an unsupported implementation (throws an exception). + + * @return a instance which implements the defined interfaces + * which an unsupported implementation (throws an exception). + */ + public Object getInstance() { + ClassLoader loader = getClass().getClassLoader(); + return Proxy.newProxyInstance(loader, interfaceClasses, handler); + } + + /** + * <code>InvocationHandler</code> which throws the wanted exception. + * <p> + * Only the [EMAIL PROTECTED] Object#equals} method is implemented. + */ + private static final InvocationHandler handler = new InvocationHandler() { + + public Object invoke(Object proxy, Method method, Object[] args) + throws Throwable { + if (equalsMethod.equals(method)) { + return BooleanUtils.toBooleanObject(proxy == args[0]); + } + + throw new UnsupportedOperationException(); + } + + }; + + static final Method equalsMethod; + + static { + try { + equalsMethod = Object.class.getMethod("equals", new Class[] { Object.class }); + } catch (SecurityException e) { + throw new UnhandledException("Can't access to the Object.equals method", e); + } catch (NoSuchMethodException e) { + throw new UnhandledException("Can't find the Object.equals method ...", e); + } + } + +}
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]