001: /* Copyright 2007 David N. Welton - DedaSys LLC - http://www.dedasys.com
002:
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014: */
015:
016: package org.hecl.java;
017:
018: import java.lang.reflect.InvocationTargetException;
019:
020: import java.util.Enumeration;
021: import java.util.Vector;
022:
023: import org.hecl.ClassCommand;
024: import org.hecl.ClassCommandInfo;
025: import org.hecl.HeclException;
026: import org.hecl.Interp;
027: import org.hecl.IntThing;
028: import org.hecl.ListThing;
029: import org.hecl.ObjectThing;
030: import org.hecl.Properties;
031: import org.hecl.StringThing;
032: import org.hecl.Thing;
033: import java.util.Map;
034:
035: /**
036: * The <code>JavaCmd</code> class is utilized to implement Hecl
037: * commands that can interact with Java classes. The real work is
038: * done in the Reflector class, which maps between Hecl and Java
039: * types.
040: *
041: * @author <a href="mailto:davidw@dedasys.com">David N. Welton</a>
042: * @version 1.0
043: */
044: public class JavaCmd implements ClassCommand, org.hecl.Command {
045: private static Vector commands = null;
046:
047: private String cmdname = null;
048: private Class this class = null;
049: private Reflector classreflector = null;
050:
051: public JavaCmd(String clsname, String cmd) throws HeclException {
052:
053: classreflector = new Reflector(clsname);
054: try {
055: this class = Class.forName(clsname);
056: } catch (Exception e) {
057: //Hecl.logStacktrace(e);
058: throw new HeclException("Error trying to create " + clsname
059: + " : " + e.toString());
060: }
061: }
062:
063: public Thing cmdCode(Interp interp, Thing[] argv)
064: throws HeclException {
065: try {
066: String argv1 = argv[1].toString();
067:
068: /* These are for all the attributes like -text -height, etc... */
069: if (argv1.equals("-new")) {
070: MethodProps mp = new MethodProps();
071: mp.setProps(argv, 1);
072:
073: Thing[] targs;
074: Thing cargs = mp.getProp("-new");
075:
076: /* Instantiate a new one. */
077: String thingclass = cargs.getVal().thingclass();
078: /* Be careful not to turn it into a list over and over again. */
079: if (thingclass.equals("list")
080: || thingclass.equals("string")) {
081: targs = ListThing.getArray(cargs);
082: } else {
083: targs = new Thing[] { cargs };
084: }
085: mp.delProp("-new");
086:
087: /* Create a new instance. */
088: Thing newthing = classreflector.instantiate(targs);
089: mp.evalProps(interp, ObjectThing.get(newthing),
090: classreflector);
091: return newthing;
092: } else if (argv1.equals("-field")) {
093: /* Access a field. */
094: return classreflector.getConstField(argv[2].toString());
095: } else if (argv1.equals("-methods")) {
096: return classreflector.methods();
097: } else {
098: /* Try calling a static method. */
099: return classreflector.evaluate(null, argv1, argv);
100: }
101: /* } catch (InvocationTargetException te) {
102: throw new HeclException("Constructor error: " + te.getTargetException().toString()); */
103: } catch (Exception e) {
104: // Hecl.logStacktrace(e);
105: e.printStackTrace();
106: throw new HeclException(argv[0].toString() + " "
107: + argv[1].toString() + " error " + e.toString());
108: }
109: }
110:
111: public Thing method(Interp interp, ClassCommandInfo context,
112: Thing[] argv) throws HeclException {
113: if (argv.length > 1) {
114: Object target = ObjectThing.get(argv[0]);
115: String subcmd = argv[1].toString().toLowerCase();
116: if (subcmd.equals("-field")) {
117: /* Look for the field. */
118: return classreflector.getField(target, argv[2]
119: .toString());
120: } else {
121: /* Invoke the method. */
122: return classreflector.evaluate(target, subcmd, argv);
123: }
124: }
125: throw HeclException.createWrongNumArgsException(argv, 2,
126: "Object method [arg...]");
127: }
128:
129: public Class getCmdClass() {
130: return this class;
131: }
132:
133: public String getCmdName() {
134: return cmdname;
135: }
136:
137: public static void load(Interp ip, String cname, String cmd)
138: throws HeclException {
139:
140: if (commands == null) {
141: commands = new Vector();
142: }
143: JavaCmd newjavacmd = new JavaCmd(cname, cmd);
144: ip.addCommand(cmd, newjavacmd);
145: ip.addClassCmd(newjavacmd.getCmdClass(), newjavacmd);
146: commands.add(newjavacmd);
147: }
148:
149: public static void unload(Interp ip) {
150: Enumeration e = commands.elements();
151: while (e.hasMoreElements()) {
152: JavaCmd c = (JavaCmd) e.nextElement();
153: ip.removeCommand(c.getCmdName());
154: ip.removeClassCmd(c.getCmdClass());
155: }
156: }
157: }
|