KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > jvm > SimpleRuntimeEnvironment


1 /* SimpleRuntimeEnvironment Copyright (C) 1999-2002 Jochen Hoenicke.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU Lesser General Public License as published by
5  * the Free Software Foundation; either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; see the file COPYING.LESSER. If not, write to
15  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  * $Id: SimpleRuntimeEnvironment.java,v 1.8.2.1 2002/05/28 17:34:12 hoenicke Exp $
18  */

19
20 package jode.jvm;
21 import jode.AssertError;
22 import jode.bytecode.Reference;
23 import jode.bytecode.TypeSignature;
24
25 import java.lang.reflect.Array JavaDoc;
26 import java.lang.reflect.Constructor JavaDoc;
27 import java.lang.reflect.Field JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.lang.reflect.InvocationTargetException JavaDoc;
30
31 public class SimpleRuntimeEnvironment implements RuntimeEnvironment {
32
33     public static Object JavaDoc fromReflectType(String JavaDoc typeSig, Object JavaDoc value) {
34     switch(typeSig.charAt(0)) {
35     case 'Z':
36         return new Integer JavaDoc(((Boolean JavaDoc) value).booleanValue() ? 1 : 0);
37     case 'B':
38     case 'S':
39         return new Integer JavaDoc(((Number JavaDoc) value).intValue());
40     case 'C':
41         return new Integer JavaDoc(((Character JavaDoc) value).charValue());
42     default:
43         return value;
44     }
45     }
46
47     public static Object JavaDoc toReflectType(String JavaDoc typeSig, Object JavaDoc value) {
48     switch(typeSig.charAt(0)) {
49     case 'Z':
50         return new Boolean JavaDoc(((Integer JavaDoc)value).intValue() != 0);
51     case 'B':
52         return new Byte JavaDoc(((Integer JavaDoc)value).byteValue());
53     case 'S':
54         return new Short JavaDoc(((Integer JavaDoc)value).shortValue());
55     case 'C':
56         return new Character JavaDoc((char) ((Integer JavaDoc)value).intValue());
57     default:
58         return value;
59     }
60     }
61
62     public Object JavaDoc getField(Reference ref, Object JavaDoc obj)
63     throws InterpreterException {
64     Field JavaDoc f;
65     try {
66         Class JavaDoc clazz = TypeSignature.getClass(ref.getClazz());
67         try {
68         f = clazz.getField(ref.getName());
69         } catch (NoSuchFieldException JavaDoc ex) {
70         f = clazz.getDeclaredField(ref.getName());
71         }
72     } catch (ClassNotFoundException JavaDoc ex) {
73         throw new InterpreterException
74         (ref+": Class not found");
75     } catch (NoSuchFieldException JavaDoc ex) {
76         throw new InterpreterException
77         ("Constructor "+ref+" not found");
78     } catch (SecurityException JavaDoc ex) {
79         throw new InterpreterException
80         (ref+": Security exception");
81     }
82     try {
83         return fromReflectType(ref.getType(), f.get(obj));
84     } catch (IllegalAccessException JavaDoc ex) {
85         throw new InterpreterException
86         ("Field " + ref + " not accessible");
87     }
88     }
89
90     public void putField(Reference ref, Object JavaDoc obj, Object JavaDoc value)
91     throws InterpreterException {
92     Field JavaDoc f;
93     try {
94         Class JavaDoc clazz = TypeSignature.getClass(ref.getClazz());
95         try {
96         f = clazz.getField(ref.getName());
97         } catch (NoSuchFieldException JavaDoc ex) {
98         f = clazz.getDeclaredField(ref.getName());
99         }
100     } catch (ClassNotFoundException JavaDoc ex) {
101         throw new InterpreterException
102         (ref+": Class not found");
103     } catch (NoSuchFieldException JavaDoc ex) {
104         throw new InterpreterException
105         ("Constructor "+ref+" not found");
106     } catch (SecurityException JavaDoc ex) {
107         throw new InterpreterException
108         (ref+": Security exception");
109     }
110     try {
111         f.set(obj, toReflectType(ref.getType(), value));
112     } catch (IllegalAccessException JavaDoc ex) {
113         throw new InterpreterException
114         ("Field " + ref + " not accessible");
115     }
116     }
117     
118     public Object JavaDoc invokeConstructor(Reference ref, Object JavaDoc[] params)
119     throws InterpreterException, InvocationTargetException JavaDoc {
120     Constructor JavaDoc c;
121     try {
122         String JavaDoc[] paramTypeSigs
123         = TypeSignature.getParameterTypes(ref.getType());
124         Class JavaDoc clazz = TypeSignature.getClass(ref.getClazz());
125         Class JavaDoc[] paramTypes = new Class JavaDoc[paramTypeSigs.length];
126         for (int i=0; i< paramTypeSigs.length; i++) {
127         params[i] = toReflectType(paramTypeSigs[i], params[i]);
128         paramTypes[i] = TypeSignature.getClass(paramTypeSigs[i]);
129         }
130         try {
131         c = clazz.getConstructor(paramTypes);
132         } catch (NoSuchMethodException JavaDoc ex) {
133         c = clazz.getDeclaredConstructor(paramTypes);
134         }
135     } catch (ClassNotFoundException JavaDoc ex) {
136         throw new InterpreterException
137         (ref+": Class not found");
138     } catch (NoSuchMethodException JavaDoc ex) {
139         throw new InterpreterException
140         ("Constructor "+ref+" not found");
141     } catch (SecurityException JavaDoc ex) {
142         throw new InterpreterException
143         (ref+": Security exception");
144     }
145     
146     try {
147         return c.newInstance(params);
148     } catch (IllegalAccessException JavaDoc ex) {
149         throw new InterpreterException
150         ("Constructor " + ref + " not accessible");
151     } catch (InstantiationException JavaDoc ex) {
152         throw new InterpreterException
153         ("InstantiationException in " + ref + ".");
154     }
155     }
156     
157     public Object JavaDoc invokeMethod(Reference ref, boolean isVirtual,
158                    Object JavaDoc cls, Object JavaDoc[] params)
159     throws InterpreterException, InvocationTargetException JavaDoc {
160     if (!isVirtual && cls != null) /*XXX*/
161         throw new InterpreterException
162         ("Can't invoke nonvirtual Method " + ref + ".");
163
164     Method JavaDoc m;
165     try {
166         String JavaDoc[] paramTypeSigs
167         = TypeSignature.getParameterTypes(ref.getType());
168         Class JavaDoc clazz = TypeSignature.getClass(ref.getClazz());
169         Class JavaDoc[] paramTypes = new Class JavaDoc[paramTypeSigs.length];
170         for (int i=0; i< paramTypeSigs.length; i++) {
171         params[i] = toReflectType(paramTypeSigs[i], params[i]);
172         paramTypes[i] = TypeSignature.getClass(paramTypeSigs[i]);
173         }
174         try {
175         m = clazz.getMethod(ref.getName(), paramTypes);
176         } catch (NoSuchMethodException JavaDoc ex) {
177         m = clazz.getDeclaredMethod(ref.getName(), paramTypes);
178         }
179     } catch (ClassNotFoundException JavaDoc ex) {
180         throw new InterpreterException
181         (ref+": Class not found");
182     } catch (NoSuchMethodException JavaDoc ex) {
183         throw new InterpreterException
184         ("Method "+ref+" not found");
185     } catch (SecurityException JavaDoc ex) {
186         throw new InterpreterException
187         (ref+": Security exception");
188     }
189     String JavaDoc retType = TypeSignature.getReturnType(ref.getType());
190     try {
191         return fromReflectType(retType, m.invoke(cls, params));
192     } catch (IllegalAccessException JavaDoc ex) {
193         throw new InterpreterException
194         ("Method " + ref + " not accessible");
195     }
196     }
197     
198     public boolean instanceOf(Object JavaDoc obj, String JavaDoc className)
199     throws InterpreterException {
200     Class JavaDoc clazz;
201     try {
202         clazz = Class.forName(className);
203     } catch (ClassNotFoundException JavaDoc ex) {
204         throw new InterpreterException
205         ("Class "+ex.getMessage()+" not found");
206     }
207     return obj != null && !clazz.isInstance(obj);
208     }
209
210     public Object JavaDoc newArray(String JavaDoc type, int[] dimensions)
211     throws InterpreterException, NegativeArraySizeException JavaDoc {
212     Class JavaDoc clazz;
213     try {
214         /* get the base class (strip leading "[") */
215         clazz = TypeSignature.getClass(type.substring(dimensions.length));
216     } catch (ClassNotFoundException JavaDoc ex) {
217         throw new InterpreterException
218         ("Class "+ex.getMessage()+" not found");
219     }
220     return Array.newInstance(clazz, dimensions);
221     }
222
223     public void enterMonitor(Object JavaDoc obj)
224     throws InterpreterException {
225     throw new InterpreterException("monitor not implemented");
226     }
227     public void exitMonitor(Object JavaDoc obj)
228     throws InterpreterException {
229     throw new InterpreterException("monitor not implemented");
230     }
231 }
232
Popular Tags