From d9efeff324ba5d2449a48735a2fac4325eb12892 Mon Sep 17 00:00:00 2001 From: hneemann Date: Sun, 18 Mar 2018 10:01:50 +0100 Subject: [PATCH] improved error messages --- .../digital/hdl/hgs/function/JavaClass.java | 20 ++++++++++++++----- .../neemann/digital/hdl/hgs/ParserTest.java | 11 ++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/neemann/digital/hdl/hgs/function/JavaClass.java b/src/main/java/de/neemann/digital/hdl/hgs/function/JavaClass.java index dd066f6cd..58a2e234d 100644 --- a/src/main/java/de/neemann/digital/hdl/hgs/function/JavaClass.java +++ b/src/main/java/de/neemann/digital/hdl/hgs/function/JavaClass.java @@ -35,8 +35,13 @@ public final class JavaClass { 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 { private Object call(T instance, Context c, ArrayList 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 { 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 " diff --git a/src/test/java/de/neemann/digital/hdl/hgs/ParserTest.java b/src/test/java/de/neemann/digital/hdl/hgs/ParserTest.java index 15e1d6b44..d1c963191 100644 --- a/src/test/java/de/neemann/digital/hdl/hgs/ParserTest.java +++ b/src/test/java/de/neemann/digital/hdl/hgs/ParserTest.java @@ -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("", new Context().declareVar("z", jcs.createMap(null))); assertEquals("1,4,4", c.toString()); + + c = exec("Hello ", + new Context().declareVar("z", jcs.createMap(null))); + assertEquals("Hello Hello World 7", c.toString()); }