Class DuckType

  • All Implemented Interfaces:
    java.lang.reflect.InvocationHandler
    Direct Known Subclasses:
    RelaxedDuckType

    public class DuckType
    extends java.lang.Object
    implements java.lang.reflect.InvocationHandler
    DuckType. Implements Duck Typing for Java. ("If it walks like a duck, quacks like a duck, it..."). Essentially allows programs to treat objects from separate hierarchies as if they were designed with common interfaces as long as they adhere to common naming conventions.

    This version is the strict DuckType. All methods present in interfaceToImplement must be present on the target object.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  DuckType.Wrapper
      Interface DuckType#Wrapper.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.lang.Object object  
      protected java.lang.Class objectClass  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected DuckType​(java.lang.Object object)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object obj)  
      int hashCode()  
      static java.lang.Object implement​(java.lang.Class interfaceToImplement, java.lang.Object object)
      Causes object to implement the interfaceToImplement and returns an instance of the object implementing interfaceToImplement even if interfaceToImplement was not declared in object.getClass()'s implements declaration.
      static boolean instanceOf​(java.lang.Class intrface, java.lang.Object object)
      Indicates if object is a (DuckType) instace of intrface.
      java.lang.Object invoke​(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • object

        protected java.lang.Object object
      • objectClass

        protected java.lang.Class objectClass
    • Constructor Detail

      • DuckType

        protected DuckType​(java.lang.Object object)
    • Method Detail

      • implement

        public static java.lang.Object implement​(java.lang.Class interfaceToImplement,
                                                 java.lang.Object object)
        Causes object to implement the interfaceToImplement and returns an instance of the object implementing interfaceToImplement even if interfaceToImplement was not declared in object.getClass()'s implements declaration.

        This works as long as all methods declared in interfaceToImplement are present on object.

        Parameters:
        interfaceToImplement - The Java class of the interface to implement
        object - The object to force to implement interfaceToImplement
        Returns:
        object, but now implementing interfaceToImplement
      • instanceOf

        public static boolean instanceOf​(java.lang.Class intrface,
                                         java.lang.Object object)
        Indicates if object is a (DuckType) instace of intrface. That is, is every method in intrface present on object.
        Parameters:
        intrface - The interface to implement
        object - The object to test
        Returns:
        true if every method in intrface is present on object. false otherwise
      • invoke

        public java.lang.Object invoke​(java.lang.Object proxy,
                                       java.lang.reflect.Method method,
                                       java.lang.Object[] args)
                                throws java.lang.Throwable
        Specified by:
        invoke in interface java.lang.reflect.InvocationHandler
        Throws:
        java.lang.Throwable
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object