mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-17 17:04:42 -04:00
improved error messages
This commit is contained in:
parent
4fda2edf0d
commit
d9efeff324
@ -35,8 +35,13 @@ public final class JavaClass<T> {
|
||||
methods = new HashMap<>();
|
||||
for (Method m : clazz.getDeclaredMethods()) {
|
||||
int mod = m.getModifiers();
|
||||
if (Modifier.isPublic(mod))
|
||||
methods.put(m.getName(), new MyMethod<>(m, Modifier.isStatic(mod)));
|
||||
if (Modifier.isPublic(mod)) {
|
||||
final String name = m.getName();
|
||||
if (methods.containsKey(name))
|
||||
throw new RuntimeException("Method overloading ("
|
||||
+ name + ") is not supported! Try to use the ellipsis (...) instead.");
|
||||
methods.put(name, new MyMethod<>(m, Modifier.isStatic(mod)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -81,10 +86,11 @@ public final class JavaClass<T> {
|
||||
|
||||
private Object call(T instance, Context c, ArrayList<Expression> args) throws HGSEvalException {
|
||||
if (instance == null && !isStatic)
|
||||
throw new HGSEvalException("function " + method.getName() + " is not static!");
|
||||
throw new HGSEvalException("Function " + method.getName() + " is not static!");
|
||||
|
||||
if (argCount >= 0 && argCount != args.size())
|
||||
throw new HGSEvalException("wrong number of arguments! expected: " + argCount + ", but found:" + args.size());
|
||||
throw new HGSEvalException("Wrong number of arguments! expected: "
|
||||
+ argCount + ", but found:" + args.size());
|
||||
|
||||
Object[] a = new Object[javaArgCount];
|
||||
int i = 0;
|
||||
@ -92,24 +98,28 @@ public final class JavaClass<T> {
|
||||
a[0] = c;
|
||||
i++;
|
||||
}
|
||||
|
||||
if (!isVarArgs) {
|
||||
for (Expression exp : args) {
|
||||
a[i] = exp.value(c);
|
||||
i++;
|
||||
}
|
||||
} else {
|
||||
// ellipsis
|
||||
try {
|
||||
// ellipse
|
||||
// the fixed args
|
||||
int fixed = javaArgCount - i - 1;
|
||||
for (int n = 0; n < fixed; n++) {
|
||||
a[i] = args.get(n).value(c);
|
||||
i++;
|
||||
}
|
||||
// put the var args to an array
|
||||
final int numVarArgs = args.size() - fixed;
|
||||
Object varArgs = Array.newInstance(compType, numVarArgs);
|
||||
for (int n = fixed; n < args.size(); n++)
|
||||
Array.set(varArgs, n - fixed, args.get(n).value(c));
|
||||
|
||||
// and pass the array
|
||||
a[i] = varArgs;
|
||||
} catch (RuntimeException e) {
|
||||
throw new HGSEvalException("type error assigning value to var array in "
|
||||
|
@ -445,6 +445,13 @@ public class ParserTest extends TestCase {
|
||||
sum += value[i];
|
||||
return sum / value.length;
|
||||
}
|
||||
|
||||
public static String zzz(Context c, String name, long... value) {
|
||||
long sum = 0;
|
||||
for (int i = 0; i < value.length; i++)
|
||||
sum += value[i];
|
||||
return c.toString() + name + (sum / value.length);
|
||||
}
|
||||
}
|
||||
|
||||
public void testJavaClass() throws ParserException, IOException, HGSEvalException {
|
||||
@ -467,6 +474,10 @@ public class ParserTest extends TestCase {
|
||||
c = exec("<? print(z.mean(1),\",\",z.mean(3,5),\",\",z.mean(3,4,5)); ?>",
|
||||
new Context().declareVar("z", jcs.createMap(null)));
|
||||
assertEquals("1,4,4", c.toString());
|
||||
|
||||
c = exec("Hello <? print(z.zzz(\"World \",5,7,9)); ?>",
|
||||
new Context().declareVar("z", jcs.createMap(null)));
|
||||
assertEquals("Hello Hello World 7", c.toString());
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user