View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2004 Open World Ltd
4   // Copyright (C) 2005 Kevin Rogers
5   //
6   // This file is part of MillScript.
7   //
8   // MillScript is free software; you can redistribute it and/or modify it under
9   // the terms of the GNU General Public License as published by the Free
10  // Software Foundation; either version 2 of the License, or (at your option)
11  // any later version.
12  //
13  // MillScript is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  // more details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA  02111-1307  USA
21  ////////////////////////////////////////////////////////////////////////////////
22  package org.millscript.millscript.functions;
23  
24  import org.millscript.millscript.alert.Alerts;
25  import org.millscript.millscript.vm.Machine;
26  
27  import java.lang.reflect.Constructor;
28  
29  /**
30   * This class implements a function which invokes one of several Java
31   * constructors to create a new instance of a Java class. The exact Java
32   * constructor that is invoked depends on the number of arguments supplied when
33   * this function is called.
34   */
35  public final class JOConstructor extends Function {
36  
37      /**
38       * The array of constructors to choose from, linking the number of
39       * arguments(position in the array) to the Java constructor to be called.
40       */
41      private final JConstructor[] jconstructors;
42  
43      /**
44       * Constructs a new <code>JOConstructor</code> function to invoke one of
45       * the specified Java constructors.
46       *
47       * @param cons  the array of Java constructors to choose from
48       */
49      public JOConstructor( final Constructor[] cons ) {
50          jconstructors = new JConstructor[ cons.length ];
51          for ( int i = 0; i < cons.length; i++ ) {
52              Constructor c = cons[ i ];
53              if ( c != null ) {
54                  jconstructors[ i ] = new JConstructor( c );
55              }
56          }
57      }
58  
59      /**
60       * @see org.millscript.millscript.functions.Function#apply(org.millscript.millscript.vm.Machine, int)
61       */
62      @Override
63      public void apply( final Machine mc, final int nargs ) {
64          try {
65              JConstructor jc = jconstructors[ nargs ];
66              if ( jc == null ) {
67                  throw(
68                      Alerts.fault( "No constructors" ).mishap()
69                  );
70              }
71              jc.apply( mc, nargs );
72          } catch ( IndexOutOfBoundsException ex ) {
73              throw(
74                  Alerts.eval(
75                      "No constructor with this arity",
76                      null
77                  ).culprit( "arity", new Integer( nargs ) ).mishap()
78              );
79          }
80      }
81  
82  }