All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ool.core.interpreter.2.3.6.source-code.TestCase Maven / Gradle / Ivy

There is a newer version: 3.0.2
Show newest version
import org.overture.interpreter.runtime.ClassInterpreter;
import org.overture.interpreter.runtime.Context;
import org.overture.interpreter.runtime.ExitException;
import org.overture.interpreter.runtime.Interpreter;
import org.overture.interpreter.runtime.StateContext;
import org.overture.interpreter.values.NameValuePair;
import org.overture.interpreter.values.ObjectValue;
import org.overture.interpreter.values.OperationValue;
import org.overture.interpreter.values.Value;
import org.overture.interpreter.values.ValueList;
import org.overture.interpreter.values.VoidValue;

public class TestCase
{
	public static Value reflectionRunTest(Value obj, Value name)
			throws Exception
	{
		String methodName = name.toString().replaceAll("\"", "").trim();

		ObjectValue instance = (ObjectValue) obj;
		for (NameValuePair p : instance.members.asList())
		{
			if (p.name.getName().equals(methodName))
			{
				if (p.value instanceof OperationValue)
				{
					OperationValue opVal = (OperationValue) p.value;
					Context mainContext = new StateContext(Interpreter.getInstance().getAssistantFactory(), p.name.getLocation(), "reflection scope");

					mainContext.putAll(ClassInterpreter.getInstance().initialContext);
					// mainContext.putAll(ClassInterpreter.getInstance().);
					mainContext.setThreadState(ClassInterpreter.getInstance().initialContext.threadState.dbgp, ClassInterpreter.getInstance().initialContext.threadState.CPU);
					try
					{
						opVal.eval(p.name.getLocation(), new ValueList(), mainContext);
					} catch (Exception e)
					{
						if (e instanceof ExitException)
						{
							throw e;
						}
						return ClassInterpreter.getInstance().evaluate("Error`throw(\""
								+ e.getMessage().replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"").replaceAll("\'", "\\\'")
								+ "\")", mainContext);
					}
				}
			}
		}
		return new VoidValue();

	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy