001: package sisc.env;
002:
003: import java.lang.reflect.*;
004: import java.beans.*;
005:
006: import sisc.data.*;
007: import sisc.interpreter.*;
008: import java.io.IOException;
009:
010: import sisc.io.ValueWriter;
011: import sisc.ser.Serializer;
012: import sisc.ser.Deserializer;
013:
014: public class NativeParameter extends Parameter {
015:
016: private Method readMethod;
017: private Method writeMethod;
018:
019: private String fieldName;
020:
021: public NativeParameter() {
022: }
023:
024: public NativeParameter(String fieldName) {
025: this .fieldName = fieldName;
026: init();
027: }
028:
029: private void init() {
030: try {
031: PropertyDescriptor[] descriptors = Introspector
032: .getBeanInfo(DynamicEnvironment.class)
033: .getPropertyDescriptors();
034: for (int i = 0; i < descriptors.length; i++) {
035: PropertyDescriptor d = descriptors[i];
036: if (d.getName().equals(fieldName)) {
037: readMethod = d.getReadMethod();
038: writeMethod = d.getWriteMethod();
039: return;
040: }
041: }
042: } catch (IntrospectionException e) {
043: }
044:
045: throw new RuntimeException(liMessage(SISCB,
046: "nativeparamnotfound", fieldName));
047: }
048:
049: public Value getValue(Interpreter r) throws ContinuationException {
050: try {
051: return (Value) readMethod.invoke(r.dynenv, new Object[] {});
052: } catch (InvocationTargetException e) {
053: error(r, liMessage(SISCB, "nativeparamaccess", e
054: .getMessage()));
055: } catch (IllegalAccessException e) {
056: error(r, liMessage(SISCB, "nativeparamaccess", e
057: .getMessage()));
058: }
059: return null;
060: }
061:
062: public void setValue(Interpreter r, Value v)
063: throws ContinuationException {
064: try {
065: writeMethod.invoke(r.dynenv, new Object[] { v });
066: } catch (InvocationTargetException e) {
067: error(r, liMessage(SISCB, "nativeparamaccess", e
068: .getMessage()));
069: throw new RuntimeException(e.getMessage());
070: } catch (IllegalAccessException e) {
071: error(r, liMessage(SISCB, "nativeparamaccess", e
072: .getMessage()));
073: }
074: }
075:
076: public void display(ValueWriter w) throws IOException {
077: w.append("#<").append(liMessage(SISCB, "nativeparameter"));
078: w.append(" ").append(fieldName).append('>');
079: }
080:
081: public void serialize(Serializer s) throws IOException {
082: s.writeUTF(fieldName);
083: }
084:
085: public void deserialize(Deserializer s) throws IOException {
086: fieldName = s.readUTF();
087: init();
088: }
089:
090: }
091:
092: /*
093: * The contents of this file are subject to the Mozilla Public
094: * License Version 1.1 (the "License"); you may not use this file
095: * except in compliance with the License. You may obtain a copy of
096: * the License at http://www.mozilla.org/MPL/
097: *
098: * Software distributed under the License is distributed on an "AS
099: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
100: * implied. See the License for the specific language governing
101: * rights and limitations under the License.
102: *
103: * The Original Code is the Second Interpreter of Scheme Code (SISC).
104: *
105: * The Initial Developer of the Original Code is Scott G. Miller.
106: * Portions created by Scott G. Miller are Copyright (C) 2000-2007
107: * Scott G. Miller. All Rights Reserved.
108: *
109: * Contributor(s):
110: * Matthias Radestock
111: *
112: * Alternatively, the contents of this file may be used under the
113: * terms of the GNU General Public License Version 2 or later (the
114: * "GPL"), in which case the provisions of the GPL are applicable
115: * instead of those above. If you wish to allow use of your
116: * version of this file only under the terms of the GPL and not to
117: * allow others to use your version of this file under the MPL,
118: * indicate your decision by deleting the provisions above and
119: * replace them with the notice and other provisions required by
120: * the GPL. If you do not delete the provisions above, a recipient
121: * may use your version of this file under either the MPL or the
122: * GPL.
123: */
|