1.1 --- a/geo/src/main/java/org/apidesign/html/geo/impl/GeoProcessor.java Sun Jul 07 07:22:04 2013 +0200
1.2 +++ b/geo/src/main/java/org/apidesign/html/geo/impl/GeoProcessor.java Sun Jul 07 07:45:11 2013 +0200
1.3 @@ -135,6 +135,9 @@
1.4 if (ol.onError().isEmpty()) {
1.5 w.append(" t.printStackTrace();");
1.6 } else {
1.7 + if (!findOnError(me, te, ol.onError(), isStatic)) {
1.8 + return false;
1.9 + }
1.10 if (isStatic) {
1.11 w.append(" ").append(te.getSimpleName()).append(".");
1.12 } else {
1.13 @@ -161,4 +164,36 @@
1.14
1.15 return true;
1.16 }
1.17 +
1.18 + private boolean findOnError(Element errElem, TypeElement te, String name, boolean onlyStatic) {
1.19 + String err = null;
1.20 + for (Element e : te.getEnclosedElements()) {
1.21 + if (e.getKind() != ElementKind.METHOD) {
1.22 + continue;
1.23 + }
1.24 + if (!e.getSimpleName().contentEquals(name)) {
1.25 + continue;
1.26 + }
1.27 + if (onlyStatic && !e.getModifiers().contains(Modifier.STATIC)) {
1.28 + errElem = e;
1.29 + err = "Would have to be static";
1.30 + continue;
1.31 + }
1.32 + ExecutableElement ee = (ExecutableElement) e;
1.33 + TypeMirror excType = processingEnv.getElementUtils().getTypeElement(Exception.class.getName()).asType();
1.34 + if (ee.getParameters().size() != 1 ||
1.35 + !processingEnv.getTypeUtils().isAssignable(excType, ee.getParameters().get(0).asType())
1.36 + ) {
1.37 + errElem = e;
1.38 + err = "Error method needs to take one Exception argument";
1.39 + continue;
1.40 + }
1.41 + return true;
1.42 + }
1.43 + if (err == null) {
1.44 + err = "Cannot find " + name + "(Exception) method in this class";
1.45 + }
1.46 + error(err, errElem);
1.47 + return false;
1.48 + }
1.49 }
2.1 --- a/geo/src/test/java/org/apidesign/html/geo/impl/GeoProcessorTest.java Sun Jul 07 07:22:04 2013 +0200
2.2 +++ b/geo/src/test/java/org/apidesign/html/geo/impl/GeoProcessorTest.java Sun Jul 07 07:45:11 2013 +0200
2.3 @@ -54,4 +54,39 @@
2.4 res.assertError("cannot be private");
2.5 }
2.6
2.7 + @Test public void onErrorHasToExist() throws IOException {
2.8 + Compile res = Compile.create("", "package x;\n"
2.9 + + "class UseOnLocation {\n"
2.10 + + " @net.java.html.geo.OnLocation(onError=\"doesNotExist\")\n"
2.11 + + " static void cantCallMe(net.java.html.geo.Position p) {}\n"
2.12 + + "}\n"
2.13 + );
2.14 + res.assertErrors();
2.15 + res.assertError("not find doesNotExist");
2.16 + }
2.17 +
2.18 + @Test public void onErrorWouldHaveToBeStatic() throws IOException {
2.19 + Compile res = Compile.create("", "package x;\n"
2.20 + + "class UseOnLocation {\n"
2.21 + + " @net.java.html.geo.OnLocation(onError=\"notStatic\")\n"
2.22 + + " static void cantCallMe(net.java.html.geo.Position p) {}\n"
2.23 + + " void notStatic(Exception e) {}\n"
2.24 + + "}\n"
2.25 + );
2.26 + res.assertErrors();
2.27 + res.assertError("have to be static");
2.28 + }
2.29 +
2.30 + @Test public void onErrorMustAcceptExceptionArgument() throws IOException {
2.31 + Compile res = Compile.create("", "package x;\n"
2.32 + + "class UseOnLocation {\n"
2.33 + + " @net.java.html.geo.OnLocation(onError=\"notStatic\")\n"
2.34 + + " static void cantCallMe(net.java.html.geo.Position p) {}\n"
2.35 + + " static void notStatic(java.io.IOException e) {}\n"
2.36 + + "}\n"
2.37 + );
2.38 + res.assertErrors();
2.39 + res.assertError("take one Exception arg");
2.40 + }
2.41 +
2.42 }