KickJava   Java API By Example, From Geeks To Geeks.

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


1 // ============================================================================
2
// $Id: ConstructDefault.java,v 1.9 2006/04/26 03:33:37 davidahall Exp $
3
// Copyright (c) 2004-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.Generator;
40
41 /**
42  * Generator that constructs an object of the given class using its default
43  * constructor.
44  * <p>
45  * Copyright &copy; 2004-2005 David A. Hall
46  *
47  * @author <a HREF="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
48  **/

49
50 public class ConstructDefault<R> extends Generator<R> {
51
52    static final long serialVersionUID = 9150185598879755334L;
53
54     private Class JavaDoc<R> _objclass;
55     
56     private transient Constructor JavaDoc<R> _ctor;
57
58     /**
59      * Builds a generator that will return instances of the class
60      * <code>ctorclass</code>, built using the default constructor.
61      * @throws IllegalArgumentException if the constructor cannot be found
62      * @throws IllegalArgumentException if the class argument is omitted or does not
63      * define an accessible default constructor.
64      */

65     
66     public ConstructDefault(Class JavaDoc<R> ctorclass) {
67         if (ctorclass == null) {
68             String JavaDoc msg = "Class to be constructed must be specified";
69             throw new IllegalArgumentException JavaDoc(msg);
70         }
71         
72         Class JavaDoc[] argclasses = new Class JavaDoc[0];
73
74         _objclass = ctorclass;
75         
76         try {
77             _ctor = ctorclass.getConstructor(argclasses);
78         }
79         catch (NoSuchMethodException JavaDoc x) {
80             String JavaDoc msg = "No default constructor for class {0}";
81             Object JavaDoc[] args = new Object JavaDoc[]{_objclass.getName()};
82             IllegalArgumentException JavaDoc x1 =
83                 new IllegalArgumentException JavaDoc(MessageFormat.format(msg,args));
84             x1.initCause(x);
85             throw x1;
86         }
87     }
88
89     /**
90      * Lazily loads the constructor
91      */

92     public synchronized Constructor JavaDoc<R> getConstructor() throws NoSuchMethodException JavaDoc{
93         if (_ctor == null)
94             _ctor = _objclass.getConstructor(new Class JavaDoc[0]);
95
96         return _ctor;
97     }
98
99     /**
100      * Builds an object via the default constructor
101      * <p>
102      * @return the object built by the constructor
103      */

104     public R gen() {
105         try {
106             R val = (R) getConstructor().newInstance(new Object JavaDoc[0]);
107             return val;
108         }
109         catch (NoSuchMethodException JavaDoc x) {
110             String JavaDoc msg = "No default constructor for class {0}";
111             Object JavaDoc[] args = new Object JavaDoc[]{_objclass.getName()};
112             throw new EvaluationException(MessageFormat.format(msg,args), x);
113         }
114         catch (InstantiationException JavaDoc x) {
115             String JavaDoc msg = "class {0} is abstract: cannot be constructed";
116             Object JavaDoc[] args = new Object JavaDoc[]{_objclass.getName()};
117             throw new EvaluationException(MessageFormat.format(msg,args), x);
118         }
119         catch (IllegalAccessException JavaDoc x) {
120             String JavaDoc msg = "default ctor in class {0} is not accessible";
121             Object JavaDoc[] args = new Object JavaDoc[]{_objclass.getName()};
122             throw new EvaluationException(MessageFormat.format(msg,args), x);
123         }
124         catch (InvocationTargetException JavaDoc x) {
125             String JavaDoc msg = "default ctor in class {0} failed: "+x.getMessage();
126             Object JavaDoc[] args = new Object JavaDoc[]{_objclass.getName()};
127             throw new EvaluationException(MessageFormat.format(msg,args), x);
128         }
129     }
130     
131     /**
132      * Calls the Visitor's <code>visit(ConstructDefault)</code> method, if it
133      * implements the nested Visitor interface.
134      */

135     public void accept(net.sf.jga.fn.Visitor v) {
136         if (v instanceof ConstructDefault.Visitor)
137             ((ConstructDefault.Visitor)v).visit(this);
138         else
139             v.visit(this);
140     }
141
142     // Object overrides
143

144     public String JavaDoc toString() {
145         return "new "+_objclass.getName()+"()";
146     }
147     
148     // Acyclic Visitor
149

150     /**
151      * Interface for classes that may interpret a <b>ConstructDefault</b>
152      * functor.
153      */

154     public interface Visitor extends net.sf.jga.fn.Visitor {
155         public void visit(ConstructDefault host);
156     }
157 }
158
Popular Tags