001: // ============================================================================
002: // $Id: ConstructDefault.java,v 1.9 2006/04/26 03:33:37 davidahall Exp $
003: // Copyright (c) 2004-2005 David A. Hall
004: // ============================================================================
005: // The contents of this file are subject to the Common Development and
006: // Distribution License (CDDL), Version 1.0 (the License); you may not use this
007: // file except in compliance with the License. You should have received a copy
008: // of the the License along with this file: if not, a copy of the License is
009: // available from Sun Microsystems, Inc.
010: //
011: // http://www.sun.com/cddl/cddl.html
012: //
013: // From time to time, the license steward (initially Sun Microsystems, Inc.) may
014: // publish revised and/or new versions of the License. You may not use,
015: // distribute, or otherwise make this file available under subsequent versions
016: // of the License.
017: //
018: // Alternatively, the contents of this file may be used under the terms of the
019: // GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
020: // case the provisions of the LGPL are applicable instead of those above. If you
021: // wish to allow use of your version of this file only under the terms of the
022: // LGPL, and not to allow others to use your version of this file under the
023: // terms of the CDDL, indicate your decision by deleting the provisions above
024: // and replace them with the notice and other provisions required by the LGPL.
025: // If you do not delete the provisions above, a recipient may use your version
026: // of this file under the terms of either the CDDL or the LGPL.
027: //
028: // This library is distributed in the hope that it will be useful,
029: // but WITHOUT ANY WARRANTY; without even the implied warranty of
030: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
031: // ============================================================================
032:
033: package net.sf.jga.fn.property;
034:
035: import java.lang.reflect.Constructor;
036: import java.lang.reflect.InvocationTargetException;
037: import java.text.MessageFormat;
038: import net.sf.jga.fn.EvaluationException;
039: import net.sf.jga.fn.Generator;
040:
041: /**
042: * Generator that constructs an object of the given class using its default
043: * constructor.
044: * <p>
045: * Copyright © 2004-2005 David A. Hall
046: *
047: * @author <a href="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
048: **/
049:
050: public class ConstructDefault<R> extends Generator<R> {
051:
052: static final long serialVersionUID = 9150185598879755334L;
053:
054: private Class<R> _objclass;
055:
056: private transient Constructor<R> _ctor;
057:
058: /**
059: * Builds a generator that will return instances of the class
060: * <code>ctorclass</code>, built using the default constructor.
061: * @throws IllegalArgumentException if the constructor cannot be found
062: * @throws IllegalArgumentException if the class argument is omitted or does not
063: * define an accessible default constructor.
064: */
065:
066: public ConstructDefault(Class<R> ctorclass) {
067: if (ctorclass == null) {
068: String msg = "Class to be constructed must be specified";
069: throw new IllegalArgumentException(msg);
070: }
071:
072: Class[] argclasses = new Class[0];
073:
074: _objclass = ctorclass;
075:
076: try {
077: _ctor = ctorclass.getConstructor(argclasses);
078: } catch (NoSuchMethodException x) {
079: String msg = "No default constructor for class {0}";
080: Object[] args = new Object[] { _objclass.getName() };
081: IllegalArgumentException x1 = new IllegalArgumentException(
082: MessageFormat.format(msg, args));
083: x1.initCause(x);
084: throw x1;
085: }
086: }
087:
088: /**
089: * Lazily loads the constructor
090: */
091: public synchronized Constructor<R> getConstructor()
092: throws NoSuchMethodException {
093: if (_ctor == null)
094: _ctor = _objclass.getConstructor(new Class[0]);
095:
096: return _ctor;
097: }
098:
099: /**
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[0]);
107: return val;
108: } catch (NoSuchMethodException x) {
109: String msg = "No default constructor for class {0}";
110: Object[] args = new Object[] { _objclass.getName() };
111: throw new EvaluationException(MessageFormat.format(msg,
112: args), x);
113: } catch (InstantiationException x) {
114: String msg = "class {0} is abstract: cannot be constructed";
115: Object[] args = new Object[] { _objclass.getName() };
116: throw new EvaluationException(MessageFormat.format(msg,
117: args), x);
118: } catch (IllegalAccessException x) {
119: String msg = "default ctor in class {0} is not accessible";
120: Object[] args = new Object[] { _objclass.getName() };
121: throw new EvaluationException(MessageFormat.format(msg,
122: args), x);
123: } catch (InvocationTargetException x) {
124: String msg = "default ctor in class {0} failed: "
125: + x.getMessage();
126: Object[] args = new Object[] { _objclass.getName() };
127: throw new EvaluationException(MessageFormat.format(msg,
128: args), x);
129: }
130: }
131:
132: /**
133: * Calls the Visitor's <code>visit(ConstructDefault)</code> method, if it
134: * implements the nested Visitor interface.
135: */
136: public void accept(net.sf.jga.fn.Visitor v) {
137: if (v instanceof ConstructDefault.Visitor)
138: ((ConstructDefault.Visitor) v).visit(this );
139: else
140: v.visit(this );
141: }
142:
143: // Object overrides
144:
145: public String toString() {
146: return "new " + _objclass.getName() + "()";
147: }
148:
149: // Acyclic Visitor
150:
151: /**
152: * Interface for classes that may interpret a <b>ConstructDefault</b>
153: * functor.
154: */
155: public interface Visitor extends net.sf.jga.fn.Visitor {
156: public void visit(ConstructDefault host);
157: }
158: }
|