View Javadoc

1   //----------------------------------------------------------------------
2   // 
3   // PerfectJPattern: "Design patterns are good but components are better!" 
4   // DynamicDelegator.java Copyright (c) 2009 Giovanni Azua Garcia
5   // bravegag@hotmail.com
6   //  
7   // This program is free software; you can redistribute it and/or
8   // modify it under the terms of the GNU General Public License
9   // as published by the Free Software Foundation; either version 3
10  // of the License, or (at your option) any later version.
11  //
12  // This program is distributed in the hope that it will be useful,
13  // but WITHOUT ANY WARRANTY; without even the implied warranty of
14  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  // GNU General Public License for more details.
16  //
17  // You should have received a copy of the GNU General Public License
18  // along with this program; if not, see <http://www.gnu.org/licenses/>.
19  //
20  //----------------------------------------------------------------------
21  package org.perfectjpattern.core.extras.delegate;
22  
23  import org.apache.commons.lang.*;
24  import org.perfectjpattern.core.api.extras.delegate.*;
25  
26  
27  /**
28   * Implementation of <code>IDelegator</code> interface for creating
29   * Delegate instances based on the dynamic </code>IDelegate</code> 
30   * type. <code>DynamicDelegator</code> should be used in cases where
31   * there is no known interface definition for the targeted method.
32   * <br/><br/>
33   * <b>Notes</b>: Base source code implemented by Steve Lewis and Wilhelm 
34   * Fitzpatrick and adapted to fit PerfectJPattern componentization 
35   * criteria and code conventions.
36   *
37   * @see IDelegate
38   * 
39   * @author <a href="mailto:smlewis@lordjoe.com">Steve Lewis</a>
40   * @author <a href="mailto:wilhelmf@agileinformatics.com">Wilhelm 
41   * Fitzpatrick</a>
42   * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
43   * @version $Revision: 1.0 $ $Date: Jun 25, 2007 7:07:18 AM $
44   */
45  @SuppressWarnings("unchecked")
46  public final
47  class DynamicDelegator 
48  extends AbstractDelegator<IDelegate> 
49  {
50      //------------------------------------------------------------------------
51      // public
52      //------------------------------------------------------------------------
53      /**
54       * Construct a <code>DynamicDelegator</code> by specifying the target
55       * method signature. The method signature is defined by the types of the
56       * parameters and return.
57       * 
58       * @param aReturnClass Return type for the targeted method.
59       * @param aParameters Array of method parameter types.
60       */
61      public DynamicDelegator(Class aReturnClass, Class... aParameters)
62      {
63          super(aReturnClass, aParameters);
64      }
65  
66      //------------------------------------------------------------------------
67      /** 
68       * {@inheritDoc}
69       */
70      public IDelegate
71      build(Class aTarget, String aMethodName) 
72      throws IllegalArgumentException, UnsupportedOperationException
73      {
74          Validate.notNull(aTarget, "'aTarget' must not be null");
75          Validate.notNull(aMethodName, "'aMethodName' must not be null");
76          
77          DelegateProxy myDelegate = new DelegateProxy(null, aTarget, aMethodName,
78              this);
79  
80          return myDelegate;
81      }
82  
83      //------------------------------------------------------------------------
84      /** 
85       * {@inheritDoc}
86       */
87      public IDelegate 
88      build(Object aTarget, String aMethodName) 
89      throws IllegalArgumentException, UnsupportedOperationException
90      {
91          Validate.notNull(aTarget, "'aTarget' must not be null");
92          Validate.notNull(aMethodName, "'aMethodName' must not be null");
93          
94          DelegateProxy myDelegate = new DelegateProxy(aTarget, aTarget.
95              getClass(), aMethodName, this);
96  
97          return myDelegate;
98      }
99  }