Mirror of the JODE repository
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
jode/jode/jode/jvm/SimpleRuntimeEnvironment.java

198 lines
6.1 KiB

/* SimpleRuntimeEnvironment Copyright (C) 1999 Jochen Hoenicke.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id$
*/
package jode.jvm;
import jode.AssertError;
import jode.bytecode.Reference;
import jode.type.Type;
import jode.type.IntegerType;
import jode.type.MethodType;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
public class SimpleRuntimeEnvironment implements RuntimeEnvironment {
public Class findClazz(String clName) throws ClassNotFoundException {
if (clName.charAt(0) == 'L')
clName = clName.substring(1, clName.length()-1);
return Class.forName(clName.replace('/','.'));
}
public Object getField(Reference ref, Object obj)
throws InterpreterException {
Field f;
try {
Class clazz = findClazz(ref.getClazz());
try {
f = clazz.getField(ref.getName());
} catch (NoSuchFieldException ex) {
f = clazz.getDeclaredField(ref.getName());
}
} catch (ClassNotFoundException ex) {
throw new InterpreterException
(ref+": Class not found");
} catch (NoSuchFieldException ex) {
throw new InterpreterException
("Constructor "+ref+" not found");
} catch (SecurityException ex) {
throw new InterpreterException
(ref+": Security exception");
}
try {
return f.get(obj);
} catch (IllegalAccessException ex) {
throw new InterpreterException
("Field " + ref + " not accessible");
}
}
public void putField(Reference ref, Object obj, Object value)
throws InterpreterException {
Field f;
try {
Class clazz = findClazz(ref.getClazz());
try {
f = clazz.getField(ref.getName());
} catch (NoSuchFieldException ex) {
f = clazz.getDeclaredField(ref.getName());
}
} catch (ClassNotFoundException ex) {
throw new InterpreterException
(ref+": Class not found");
} catch (NoSuchFieldException ex) {
throw new InterpreterException
("Constructor "+ref+" not found");
} catch (SecurityException ex) {
throw new InterpreterException
(ref+": Security exception");
}
try {
f.set(obj, value);
} catch (IllegalAccessException ex) {
throw new InterpreterException
("Field " + ref + " not accessible");
}
}
public Object invokeConstructor(Reference ref, Object[] params)
throws InterpreterException, InvocationTargetException {
Constructor c;
try {
Class clazz = findClazz(ref.getClazz());
MethodType mt = (MethodType) Type.tType(ref.getType());
Class[] paramTypes = mt.getParameterClasses();
try {
c = clazz.getConstructor(paramTypes);
} catch (NoSuchMethodException ex) {
c = clazz.getDeclaredConstructor(paramTypes);
}
} catch (ClassNotFoundException ex) {
throw new InterpreterException
(ref+": Class not found");
} catch (NoSuchMethodException ex) {
throw new InterpreterException
("Constructor "+ref+" not found");
} catch (SecurityException ex) {
throw new InterpreterException
(ref+": Security exception");
}
try {
return c.newInstance(params);
} catch (IllegalAccessException ex) {
throw new InterpreterException
("Constructor " + ref + " not accessible");
} catch (InstantiationException ex) {
throw new InterpreterException
("InstantiationException in " + ref + ".");
}
}
public Object invokeMethod(Reference ref, boolean isVirtual,
Object cls, Object[] params)
throws InterpreterException, InvocationTargetException {
Method m;
if (!isVirtual && cls != null) /*XXX*/
throw new InterpreterException
("Can't invoke nonvirtual Method " + ref + ".");
MethodType mt = (MethodType) Type.tType(ref.getType());
try {
Class clazz = findClazz(ref.getClazz());
Class[] paramTypes = mt.getParameterClasses();
try {
m = clazz.getMethod(ref.getName(), paramTypes);
} catch (NoSuchMethodException ex) {
m = clazz.getDeclaredMethod(ref.getName(), paramTypes);
}
} catch (ClassNotFoundException ex) {
throw new InterpreterException
(ref+": Class not found");
} catch (NoSuchMethodException ex) {
throw new InterpreterException
("Method "+ref+" not found");
} catch (SecurityException ex) {
throw new InterpreterException
(ref+": Security exception");
}
try {
return m.invoke(cls, params);
} catch (IllegalAccessException ex) {
throw new InterpreterException
("Method " + ref + " not accessible");
}
}
public boolean instanceOf(Object obj, String className)
throws InterpreterException {
Class clazz;
try {
clazz = Class.forName(className);
} catch (ClassNotFoundException ex) {
throw new InterpreterException
("Class "+ex.getMessage()+" not found");
}
return obj != null && !clazz.isInstance(obj);
}
public Object newArray(String type, int[] dimensions)
throws InterpreterException, NegativeArraySizeException {
Class clazz;
try {
/* get the base class (strip leading "[") */
clazz = Type.tType(type.substring(dimensions.length))
.getTypeClass();
} catch (ClassNotFoundException ex) {
throw new InterpreterException
("Class "+ex.getMessage()+" not found");
}
return Array.newInstance(clazz, dimensions);
}
public void enterMonitor(Object obj)
throws InterpreterException {
throw new InterpreterException("monitorenter not implemented");
}
public void exitMonitor(Object obj)
throws InterpreterException {
throw new InterpreterException("monitorenter not implemented");
}
}