001 
002 /*
003  *  JScripter Simulation 1.0 - For Java To Script
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package org.jscripter.sim;
021 
022 import js.LiveClass;
023 import js.LiveObject;
024 import js.Js;
025 import js.Vars;
026 
027 final class SimLiveObject implements LiveObject
028 {
029     private final Object obj;
030     private final LiveClass cls;
031 
032     public SimLiveObject(Object obj) {
033         this.obj = Js.valueOf(obj);
034         cls = new SimLiveClass(this.obj.getClass());
035     }
036 
037     //@Override
038     public final Object call(String met) {
039         try {
040             return obj.getClass().getMethod(met, (Class<?>[])null).invoke(obj, (Object[])null);
041         } catch (Exception e) {
042             throw new RuntimeException(e);
043         }
044     }
045 
046     //@Override
047     public final Object call(String met, String atype, Object arg) {
048         try {
049             return obj.getClass().getMethod(met, new Class<?>[]{Class.forName(atype)}).invoke(obj, new Object[]{Js.valueOf(arg)});
050         } catch (Exception e) {
051             throw new RuntimeException(e);
052         }
053     }
054 
055     //@Override
056     public final Object call(String met, Vars<String> atypes, Vars<?> args) {
057         try {
058             return obj.getClass().getMethod(met, Simul.aTypes(atypes)).invoke(obj, Simul.args(args));
059         } catch (Exception e) {
060             throw new RuntimeException(e);
061         }
062     }
063 
064     //@Override
065     public final Object get(String fld) {
066         try {
067             return obj.getClass().getField(fld).get(obj);
068         } catch (Exception e) {
069             throw new RuntimeException(e);
070         }
071     }
072 
073     //@Override
074     public final LiveClass getJavaClass() {
075         return cls;
076     }
077 
078     //@Override
079     public final void run(String met) {
080         try {
081             obj.getClass().getMethod(met, (Class<?>[])null).invoke(obj, (Object[])null);
082         } catch (Exception e) {
083             throw new RuntimeException(e);
084         }
085     }
086 
087     //@Override
088     public final void run(String met, String atype, Object arg) {
089         try {
090             obj.getClass().getMethod(met, new Class<?>[]{Class.forName(atype)}).invoke(obj, new Object[]{Js.valueOf(arg)});
091         } catch (Exception e) {
092             throw new RuntimeException(e);
093         }
094     }
095 
096     //@Override
097     public final void run(String met, Vars<String> atypes, Vars<?> args) {
098         try {
099             obj.getClass().getMethod(met, Simul.aTypes(atypes)).invoke(obj, Simul.args(args));
100         } catch (Exception e) {
101             throw new RuntimeException(e);
102         }
103     }
104 
105     //@Override
106     public final Object set(String fld, Object val) {
107         try {
108             obj.getClass().getField(fld).set(obj, val);
109         } catch (Exception e) {
110             throw new RuntimeException(e);
111         }
112         return val;
113     }
114 
115     //@Override
116     public final String toString() {
117         return obj.toString();
118     }
119 
120     //@Override
121     public final Object valueOf() {
122         return obj;
123     }
124 }