improved error messages

This commit is contained in:
hneemann 2018-03-18 10:01:50 +01:00
parent 4fda2edf0d
commit d9efeff324
2 changed files with 26 additions and 5 deletions

View File

@ -35,8 +35,13 @@ public final class JavaClass<T> {
methods = new HashMap<>(); methods = new HashMap<>();
for (Method m : clazz.getDeclaredMethods()) { for (Method m : clazz.getDeclaredMethods()) {
int mod = m.getModifiers(); int mod = m.getModifiers();
if (Modifier.isPublic(mod)) if (Modifier.isPublic(mod)) {
methods.put(m.getName(), new MyMethod<>(m, Modifier.isStatic(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 { private Object call(T instance, Context c, ArrayList<Expression> args) throws HGSEvalException {
if (instance == null && !isStatic) 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()) 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]; Object[] a = new Object[javaArgCount];
int i = 0; int i = 0;
@ -92,24 +98,28 @@ public final class JavaClass<T> {
a[0] = c; a[0] = c;
i++; i++;
} }
if (!isVarArgs) { if (!isVarArgs) {
for (Expression exp : args) { for (Expression exp : args) {
a[i] = exp.value(c); a[i] = exp.value(c);
i++; i++;
} }
} else { } else {
// ellipsis
try { try {
// ellipse // the fixed args
int fixed = javaArgCount - i - 1; int fixed = javaArgCount - i - 1;
for (int n = 0; n < fixed; n++) { for (int n = 0; n < fixed; n++) {
a[i] = args.get(n).value(c); a[i] = args.get(n).value(c);
i++; i++;
} }
// put the var args to an array
final int numVarArgs = args.size() - fixed; final int numVarArgs = args.size() - fixed;
Object varArgs = Array.newInstance(compType, numVarArgs); Object varArgs = Array.newInstance(compType, numVarArgs);
for (int n = fixed; n < args.size(); n++) for (int n = fixed; n < args.size(); n++)
Array.set(varArgs, n - fixed, args.get(n).value(c)); Array.set(varArgs, n - fixed, args.get(n).value(c));
// and pass the array
a[i] = varArgs; a[i] = varArgs;
} catch (RuntimeException e) { } catch (RuntimeException e) {
throw new HGSEvalException("type error assigning value to var array in " throw new HGSEvalException("type error assigning value to var array in "

View File

@ -445,6 +445,13 @@ public class ParserTest extends TestCase {
sum += value[i]; sum += value[i];
return sum / value.length; 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 { 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)); ?>", c = exec("<? print(z.mean(1),\",\",z.mean(3,5),\",\",z.mean(3,4,5)); ?>",
new Context().declareVar("z", jcs.createMap(null))); new Context().declareVar("z", jcs.createMap(null)));
assertEquals("1,4,4", c.toString()); 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());
} }