1   /*
2    Copyright (c) 2003, Laurent Caillette.
3    All rights reserved.
4   
5    Redistribution and use in source and binary forms, with or without modifica-
6    tion, are permitted provided that the following conditions are met:
7   
8    1. Redistributions of  source code must  retain the above copyright  notice,
9       this list of conditions and the following disclaimer.
10  
11   2. Redistributions in binary form must reproduce the above copyright notice,
12      this list of conditions and the following disclaimer in the documentation
13      and/or other materials provided with the distribution.
14  
15   3. The end-user documentation included with the redistribution, if any, must
16      include  the following  acknowledgment:  "This product includes  software
17      written by Laurent Caillette."
18      Alternately, this  acknowledgment may  appear in the software itself,  if
19      and wherever such third-party acknowledgments normally appear.
20  
21   4. The name "Laurent Caillette" must  not  be used to  endorse or
22       promote  products derived from  this software without
23      prior written permission. For written permission, please contact
24      laurent.caillette@laposte.net
25  
26   5. Products  derived from this software may not  be called
27      "Laurent Caillette", nor may "Laurent Caillette" appear
28      in their name,  without prior written permission  of the
29      author.
30  
31   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
32   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
33   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
34   AUTHOR (LAURENT CAILLETTE) BE LIABLE FOR  ANY DIRECT, INDIRECT, INCIDENTAL,
35   SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED
36   TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
37   OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON ANY
38   THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
39   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
40   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41   */
42  package tweed.system;
43  import java.lang.reflect.Constructor;
44  import java.lang.reflect.InvocationTargetException;
45  
46  /***
47   * Helper for instantiating runtime-defined classes, whose constructor
48   * takes parameters.
49   *
50   * @author Laurent Caillette
51   * @version $Id$
52   */
53  public class Instantiator {
54  
55    private final Class instantiatedBaseClass ;
56    private final Class[] constructorParametersBaseTypes ;
57  
58    public Instantiator(
59        Class instantiatedBaseClass,
60        Class[] constructorParametersBaseTypes
61    ) {
62      if( instantiatedBaseClass == null ) {
63        throw new NullPointerException( "instantiatedBaseClass" ) ;
64      }
65      if( constructorParametersBaseTypes == null ) {
66        throw new NullPointerException( "constructorParametersBaseTypes" ) ;
67      }
68      for( int i = 0 ; i < constructorParametersBaseTypes.length ; i++ ) {
69        if( constructorParametersBaseTypes[ i ] == null ) {
70          throw new NullPointerException(
71              "constructorParametersBaseTypes[ " + i + " ] == null"
72          ) ;
73        }
74      }
75      this.instantiatedBaseClass = instantiatedBaseClass ;
76      this.constructorParametersBaseTypes = ( Class[] )
77          constructorParametersBaseTypes.clone() ;
78    }
79  
80  
81    private Class concreteClass = null ;
82    private Constructor constructor = null ;
83  
84    public void setConcreteClass( Class concreteClass )
85        throws NoSuchMethodException
86    {
87      if( concreteClass == null ) {
88        throw new NullPointerException( "concreteClass" ) ;
89      }
90      if( ! instantiatedBaseClass.isAssignableFrom( concreteClass ) ) {
91        throw new IllegalArgumentException(
92            concreteClass.toString() +
93            " is not a child of " +
94            instantiatedBaseClass.getName()
95        ) ;
96      }
97      resolveConstructor( concreteClass ) ;
98      this.concreteClass = concreteClass ;
99    }
100 
101   /***
102    *
103    * @param clazz
104    * @throws java.lang.Error
105    */
106   public void setConcreteClassQuiet( Class clazz ) {
107     try {
108       setConcreteClass( clazz ) ;
109     } catch( NoSuchMethodException ex ) {
110       throw new RuntimeException( ex ) ;
111     }
112   }
113 
114   public Class getConcreteClass() {
115     return concreteClass ;
116   }
117 
118   private void resolveConstructor( Class clazz ) throws NoSuchMethodException {
119     Constructor[] constructors = clazz.getConstructors() ;
120     for( int i = 0 ; i < constructors.length ; i++ ) {
121       Constructor aConstructor = constructors[ i ] ;
122       Class[] parameterTypes = aConstructor.getParameterTypes() ;
123       boolean ok = true ;
124       if( parameterTypes.length == constructorParametersBaseTypes.length ) {
125         for( int j = 0 ; j < parameterTypes.length ; j++ ) {
126           Class pType = parameterTypes[ j ] ;
127           Class cType = constructorParametersBaseTypes[ j ] ;
128           if( ! cType.isAssignableFrom( pType ) ) {
129             ok = false ;
130           }
131         }
132       } else {
133         ok = false ;
134       }
135       if( ok ) {
136         constructor = aConstructor ;
137         return ;
138       }
139     }
140     throw new NoSuchMethodException( "No constructor found" ) ;
141   }
142 
143   public Object instantiate( Object parameter1 )
144       throws
145       IllegalAccessException,
146       InstantiationException,
147       InvocationTargetException
148   {
149     return instantiate( new Object[] { parameter1 } ) ;
150   }
151 
152   public Object instantiate( Object parameter1, Object parameter2 )
153       throws
154       IllegalAccessException,
155       InstantiationException,
156       InvocationTargetException
157   {
158     return instantiate( new Object[] { parameter1, parameter2 } ) ;
159   }
160 
161   public Object instantiate(
162       Object parameter1,
163       Object parameter2,
164       Object parameter3
165   )
166       throws
167       IllegalAccessException,
168       InstantiationException,
169       InvocationTargetException
170   {
171     return instantiate( new Object[] {
172         parameter1,
173         parameter2,
174         parameter3
175     } ) ;
176   }
177 
178   public Object instantiateQuiet( Object parameter1 ) {
179     return instantiateQuiet( new Object[] { parameter1 } ) ;
180   }
181 
182   public Object instantiateQuiet( Object parameter1, Object parameter2 ) {
183     return instantiateQuiet( new Object[] { parameter1, parameter2 } ) ;
184   }
185 
186   public Object instantiateQuiet(
187       Object parameter1,
188       Object parameter2,
189       Object parameter3
190   ) {
191     return instantiateQuiet( new Object[] {
192         parameter1,
193         parameter2,
194         parameter3
195     } ) ;
196   }
197 
198   public Object instantiate( Object[] constructorParameters )
199       throws
200       IllegalAccessException,
201       InstantiationException,
202       InvocationTargetException
203   {
204     return constructor.newInstance( constructorParameters ) ;
205   }
206 
207   public Object instantiateQuiet( Object[] constructorParameters ) {
208     try {
209       return instantiate( constructorParameters ) ;
210     } catch( IllegalAccessException ex ) {
211       throw new RuntimeException( ex ) ;
212     } catch( InstantiationException ex ) {
213       throw new RuntimeException( ex ) ;
214     } catch( InvocationTargetException ex ) {
215       throw new RuntimeException( ex ) ;
216     }
217 
218   }
219 
220 }
This page was automatically generated by Maven