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 SimLiveClass implements LiveClass
028 {
029     private final Class<?> cls;
030 
031     public SimLiveClass(Class<?> cls) {
032         this.cls = cls;
033     }
034     public SimLiveClass(String cls) throws ClassNotFoundException {
035         this(Class.forName(cls));
036     }
037 
038     //@Override
039     public final Object call(String met) {
040         try {
041             return cls.getMethod(met, (Class<?>[])null).invoke(null, (Object[])null);
042         } catch (Exception e) {
043             throw new RuntimeException(e);
044         }
045     }
046 
047     //@Override
048     public final Object call(String met, String atype, Object arg) {
049         try {
050             return cls.getMethod(met, new Class<?>[]{Class.forName(atype)}).invoke(null, new Object[]{Js.valueOf(arg)});
051         } catch (Exception e) {
052             throw new RuntimeException(e);
053         }
054     }
055 
056     //@Override
057     public final Object call(String met, Vars<String> atypes, Vars<?> args) {
058         try {
059             return cls.getMethod(met, Simul.aTypes(atypes)).invoke(null, Simul.args(args));
060         } catch (Exception e) {
061             throw new RuntimeException(e);
062         }
063     }
064 
065     //@Override
066     public final LiveObject create() {
067         try {
068             return Js.connect(cls.newInstance());
069         } catch (Exception e) {
070             throw new RuntimeException(e);
071         }
072     }
073 
074     //@Override
075     public final LiveObject create(String atype, Object arg) {
076         try {
077             return Js.connect(cls.getConstructor(new Class<?>[]{Class.forName(atype)}).newInstance(new Object[]{Js.valueOf(arg)}));
078         } catch (Exception e) {
079             throw new RuntimeException(e);
080         }
081     }
082 
083     //@Override
084     public final LiveObject create(Vars<String> atypes, Vars<?> args) {
085         try {
086             return Js.connect(cls.getConstructor(Simul.aTypes(atypes)).newInstance(Simul.args(args)));
087         } catch (Exception e) {
088             throw new RuntimeException(e);
089         }
090     }
091 
092     //@Override
093     public final Object get(String fld) {
094         try {
095             return cls.getField(fld).get(null);
096         } catch (Exception e) {
097             throw new RuntimeException(e);
098         }
099     }
100 
101     //@Override
102     public final void run(String met) {
103         try {
104             cls.getMethod(met, (Class<?>[])null).invoke(null, (Object[])null);
105         } catch (Exception e) {
106             throw new RuntimeException(e);
107         }
108     }
109 
110     //@Override
111     public final void run(String met, String atype, Object arg) {
112         try {
113             cls.getMethod(met, new Class<?>[]{Class.forName(atype)}).invoke(null, new Object[]{Js.valueOf(arg)});
114         } catch (Exception e) {
115             throw new RuntimeException(e);
116         }
117     }
118 
119     //@Override
120     public final void run(String met, Vars<String> atypes, Vars<?> args) {
121         try {
122             cls.getMethod(met, Simul.aTypes(atypes)).invoke(null, Simul.args(args));
123         } catch (Exception e) {
124             throw new RuntimeException(e);
125         }
126     }
127 
128     //@Override
129     public final Object set(String fld, Object val) {
130         try {
131             cls.getField(fld).set(null, val);
132             return val;
133         } catch (Exception e) {
134             throw new RuntimeException(e);
135         }
136     }
137 
138     //@Override
139     public final String toString() {
140         return cls.toString();
141     }
142 
143     //@Override
144     public final Class<?> valueOf() {
145         return cls;
146     }
147 }