EOS 2  1.1.0
Einfache Objektbasierte Sprache
Method.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.vm.commands;
2 
3 import java.lang.reflect.InvocationTargetException;
4 
5 import de.lathanda.eos.vm.Command;
6 import de.lathanda.eos.vm.MObject;
7 import de.lathanda.eos.vm.MType;
8 import de.lathanda.eos.vm.Machine;
9 import de.lathanda.eos.vm.Variable;
10 import de.lathanda.eos.vm.exceptions.NullAccessException;
11 
19 public class Method extends Command {
20  private final MType[] parameters;
21  private final java.lang.reflect.Method method;
22 
23  public Method(MType[] parameters, java.lang.reflect.Method method) {
24  this.parameters = parameters;
25  this.method = method;
26  }
27 
28  @Override
29  public boolean execute(Machine m) throws Exception {
30  Object target = m.pop();
31  Object[] args = new Object[parameters.length];
32  for (int i = 0; i < args.length; i++) {
33  args[i] = m.pop();
34  args[i] = parameters[i].checkAndCast(args[i]);
35  }
36  if (target == null) {
37  throw new NullAccessException();
38  }
39  try {
40  Object result;
41  if (target instanceof MObject) {
42  result = method.invoke(((MObject) target).getJavaObject(), args);
43  } else if (target instanceof Variable) {
44  result = method.invoke(((Variable) target).get(), args);
45  } else {
46  result = method.invoke(target, args);
47  }
48  if (result != null) {
49  m.push(result);
50  }
51  } catch (InvocationTargetException ite) {
52  throw (Exception) ite.getTargetException();
53  }
54  return true;
55  }
56 
57  @Override
58  public String toString() {
59  return "Method{" + method.getName() + "(" + parameters.length + ") }";
60  }
61 
62 }
boolean execute(Machine m)
Definition: Method.java:29
Method(MType[] parameters, java.lang.reflect.Method method)
Definition: Method.java:23
Object checkAndCast(Object obj)
Impressum