KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jga > fn > property > Construct


1 // ============================================================================
2
// $Id: Construct.java,v 1.11 2006/08/05 21:33:13 davidahall Exp $
3
// Copyright (c) 2003-2005 David A. Hall
4
// ============================================================================
5
// The contents of this file are subject to the Common Development and
6
// Distribution License (CDDL), Version 1.0 (the License); you may not use this
7
// file except in compliance with the License. You should have received a copy
8
// of the the License along with this file: if not, a copy of the License is
9
// available from Sun Microsystems, Inc.
10
//
11
// http://www.sun.com/cddl/cddl.html
12
//
13
// From time to time, the license steward (initially Sun Microsystems, Inc.) may
14
// publish revised and/or new versions of the License. You may not use,
15
// distribute, or otherwise make this file available under subsequent versions
16
// of the License.
17
//
18
// Alternatively, the contents of this file may be used under the terms of the
19
// GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
20
// case the provisions of the LGPL are applicable instead of those above. If you
21
// wish to allow use of your version of this file only under the terms of the
22
// LGPL, and not to allow others to use your version of this file under the
23
// terms of the CDDL, indicate your decision by deleting the provisions above
24
// and replace them with the notice and other provisions required by the LGPL.
25
// If you do not delete the provisions above, a recipient may use your version
26
// of this file under the terms of either the CDDL or the LGPL.
27
//
28
// This library is distributed in the hope that it will be useful,
29
// but WITHOUT ANY WARRANTY; without even the implied warranty of
30
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31
// ============================================================================
32

33 package net.sf.jga.fn.property;
34
35 import java.lang.reflect.Constructor JavaDoc;
36 import java.lang.reflect.InvocationTargetException JavaDoc;
37 import java.text.MessageFormat JavaDoc;
38 import net.sf.jga.fn.EvaluationException;
39 import net.sf.jga.fn.UnaryFunctor;
40 import net.sf.jga.util.ArrayUtils;
41
42 /**
43  * Unary Functor that constructs an object of the given class. The functor
44  * argument is an array containing the arguments to the class' constructor.
45  * <p>
46  * Copyright &copy; 2003-2005 David A. Hall
47  *
48  * @author <a HREF="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
49  **/

50
51 public class Construct<R> extends UnaryFunctor<Object JavaDoc[],R> {
52
53     static final long serialVersionUID = -4682718030847292345L;
54
55     // the type of the object built by the constructor
56
private Class JavaDoc<R> _objclass;
57
58     // the argument types for the constructor
59
private Class JavaDoc[] _argclasses;
60
61     // the constructor being invoked
62
private transient Constructor JavaDoc<R> _ctor;
63
64     
65     /**
66      * Builds a functor that will build an object of class
67      * <code>ctorclass</code>, given arguments of classes in
68      * <code>argclasses</code>.
69      * @throws IllegalArgumentException if either argument is omitted, or if the
70      * constructor cannot be found.
71      */

72     public Construct(Class JavaDoc<R> ctorclass, Class JavaDoc... argclasses) {
73         this(argclasses, ctorclass);
74     }
75     
76     /**
77      * Builds a functor that will build an object of class
78      * <code>ctorclass</code>, given arguments of classes in
79      * <code>argclasses</code>.
80      * @throws IllegalArgumentException if either argument is omitted, or if the
81      * constructor cannot be found.
82      */

83     public Construct(Class JavaDoc[] argclasses, Class JavaDoc<R> ctorclass) {
84         if (argclasses == null) {
85             String JavaDoc msg = "Argument Classes must be specified";
86             throw new IllegalArgumentException JavaDoc(msg);
87         }
88
89         if (ctorclass == null) {
90             String JavaDoc msg = "Class to be constructed must be specified";
91             throw new IllegalArgumentException JavaDoc(msg);
92         }
93
94         _objclass = ctorclass;
95         _argclasses = argclasses;
96         
97         try {
98             _ctor = ctorclass.getConstructor(argclasses);
99         }
100         catch (NoSuchMethodException JavaDoc x) {
101             String JavaDoc msg = "No constructor for class {0} takes argument(s) of type(s) {1}";
102             Object JavaDoc[] msgargs = new Object JavaDoc[]{_objclass.getName(), ArrayUtils.toString(_argclasses)};
103             IllegalArgumentException JavaDoc x1 =
104                 new IllegalArgumentException JavaDoc(MessageFormat.format(msg,msgargs));
105             x1.initCause(x);
106             throw x1;
107         }
108     }
109
110     /**
111      * Builds a functor that will build an object using the given constructor.
112      * @throws IllegalArgumentException if the constructor is omitted.
113      */

114     public Construct(Constructor JavaDoc<R> ctor) {
115         if (ctor == null) {
116             String JavaDoc msg = "constructor must be specified";
117             throw new IllegalArgumentException JavaDoc(msg);
118         }
119
120         _ctor = ctor;
121         _objclass = ctor.getDeclaringClass();
122         _argclasses = ctor.getParameterTypes();
123     }
124     
125     /**
126      * Lazy loads the constructor (used if the functor is called after
127      * deserialization)
128      */

129     public synchronized Constructor JavaDoc<R> getConstructor() throws NoSuchMethodException JavaDoc {
130         if (_ctor == null)
131             _ctor = _objclass.getConstructor(_argclasses);
132
133         return _ctor;
134     }
135
136     // UnaryFunctor interface
137

138     /**
139      * Builds an object via the designated constructor, passing the given
140      * array of argument values.
141      * <p>
142      * @return the object built by the constructor
143      */

144     public R fn(Object JavaDoc[] args) {
145         try {
146             return (R) getConstructor().newInstance(args);
147         }
148         catch (NoSuchMethodException JavaDoc x) {
149             String JavaDoc msg = "No constructor for class {0} takes argument(s) of type(s) {1}";
150             Object JavaDoc[] msgargs = new Object JavaDoc[]{_objclass.getName(), ArrayUtils.toString(_argclasses)};
151             throw new EvaluationException(MessageFormat.format(msg,msgargs), x);
152         }
153         catch (InstantiationException JavaDoc x) {
154             String JavaDoc msg = "class {0} is abstract: cannot be constructed";
155             Object JavaDoc[] msgargs = new Object JavaDoc[]{_objclass.getName()};
156             throw new EvaluationException(MessageFormat.format(msg,msgargs), x);
157         }
158         catch (IllegalAccessException JavaDoc x) {
159             String JavaDoc msg = "class {0} ctor({1}) is not accessible";
160             Object JavaDoc[] msgargs = new Object JavaDoc[]{_objclass.getName(), ArrayUtils.toString(_argclasses)};
161             throw new EvaluationException(MessageFormat.format(msg,msgargs), x);
162         }
163         catch (InvocationTargetException JavaDoc x) {
164             String JavaDoc msg = "class {0} ctor({1}) failed: "+x.getMessage();
165             Object JavaDoc[] msgargs = new Object JavaDoc[]{_objclass.getName(), ArrayUtils.toString(_argclasses)};
166             throw new EvaluationException(MessageFormat.format(msg,msgargs), x);
167         }
168         catch (ClassCastException JavaDoc x) {
169             String JavaDoc msg = "ClassCastException: " +_objclass +"("+ArrayUtils.toString(args)+")";
170             throw new EvaluationException(msg, x);
171         }
172         catch (IllegalArgumentException JavaDoc x) {
173             String JavaDoc msg = "class {0} ctor({1}) cannot be called with {2}";
174             Object JavaDoc[] msgargs = new Object JavaDoc[]{_objclass.getName(), ArrayUtils.toString(_argclasses),
175                                             ArrayUtils.toString(args)};
176             throw new EvaluationException(MessageFormat.format(msg,msgargs), x);
177         }
178     }
179
180     /**
181      * Calls the Visitor's <code>visit(Construct)</code> method, if it
182      * implements the nested Visitor interface.
183      */

184     public void accept(net.sf.jga.fn.Visitor v) {
185         if (v instanceof Construct.Visitor)
186             ((Construct.Visitor)v).visit(this);
187         else
188             v.visit(this);
189     }
190
191     // Object overrides
192

193     public String JavaDoc toString() {
194         return "Construct("+_objclass.getName()+".getConstructor("+ArrayUtils.toString(_argclasses)+"))";
195     }
196     
197     // Acyclic Visitor
198

199     /**
200      * Interface for classes that may interpret a <b>Construct</b>
201      * predicate.
202      */

203     public interface Visitor extends net.sf.jga.fn.Visitor {
204         public void visit(Construct host);
205     }
206 }
207
Popular Tags