1.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Mon Feb 18 19:52:06 2013 +0100
1.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Tue Feb 19 15:41:37 2013 +0100
1.3 @@ -68,6 +68,7 @@
1.4 public final class PageProcessor extends AbstractProcessor {
1.5 @Override
1.6 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
1.7 + boolean ok = true;
1.8 for (Element e : roundEnv.getElementsAnnotatedWith(Page.class)) {
1.9 Page p = e.getAnnotation(Page.class);
1.10 if (p == null) {
1.11 @@ -83,7 +84,8 @@
1.12 is.close();
1.13 } catch (IOException iOException) {
1.14 processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't read " + p.xhtml(), e);
1.15 - return false;
1.16 + ok = false;
1.17 + pp = null;
1.18 }
1.19 Writer w;
1.20 String className = p.className();
1.21 @@ -101,18 +103,21 @@
1.22 w.append("final class ").append(className).append(" {\n");
1.23 w.append(" private boolean locked;\n");
1.24 if (!initializeOnClick(className, (TypeElement) e, w, pp)) {
1.25 - return false;
1.26 - }
1.27 - for (String id : pp.ids()) {
1.28 - String tag = pp.tagNameForId(id);
1.29 - String type = type(tag);
1.30 - w.append(" ").append("public final ").
1.31 - append(type).append(' ').append(cnstnt(id)).append(" = new ").
1.32 - append(type).append("(\"").append(id).append("\");\n");
1.33 + ok = false;
1.34 + } else {
1.35 + for (String id : pp.ids()) {
1.36 + String tag = pp.tagNameForId(id);
1.37 + String type = type(tag);
1.38 + w.append(" ").append("public final ").
1.39 + append(type).append(' ').append(cnstnt(id)).append(" = new ").
1.40 + append(type).append("(\"").append(id).append("\");\n");
1.41 + }
1.42 }
1.43 List<String> propsGetSet = new ArrayList<String>();
1.44 Map<String,Collection<String>> propsDeps = new HashMap<String, Collection<String>>();
1.45 - generateComputedProperties(w, e.getEnclosedElements(), propsGetSet, propsDeps);
1.46 + if (!generateComputedProperties(w, p.properties(), e.getEnclosedElements(), propsGetSet, propsDeps)) {
1.47 + ok = false;
1.48 + }
1.49 generateProperties(w, p.properties(), propsGetSet, propsDeps);
1.50 w.append(" private org.apidesign.bck2brwsr.htmlpage.Knockout ko;\n");
1.51 if (!propsGetSet.isEmpty()) {
1.52 @@ -145,7 +150,7 @@
1.53 return false;
1.54 }
1.55 }
1.56 - return true;
1.57 + return ok;
1.58 }
1.59
1.60 private InputStream openStream(String pkg, String name) throws IOException {
1.61 @@ -184,6 +189,7 @@
1.62 private boolean initializeOnClick(
1.63 String className, TypeElement type, Writer w, ProcessPage pp
1.64 ) throws IOException {
1.65 + boolean ok = true;
1.66 TypeMirror stringType = processingEnv.getElementUtils().getTypeElement("java.lang.String").asType();
1.67 { //for (Element clazz : pe.getEnclosedElements()) {
1.68 // if (clazz.getKind() != ElementKind.CLASS) {
1.69 @@ -196,9 +202,15 @@
1.70 On oc = method.getAnnotation(On.class);
1.71 if (oc != null) {
1.72 for (String id : oc.id()) {
1.73 + if (pp == null) {
1.74 + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "id = " + id + " not found in HTML page.");
1.75 + ok = false;
1.76 + continue;
1.77 + }
1.78 if (pp.tagNameForId(id) == null) {
1.79 processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "id = " + id + " does not exist in the HTML page. Found only " + pp.ids(), method);
1.80 - return false;
1.81 + ok = false;
1.82 + continue;
1.83 }
1.84 ExecutableElement ee = (ExecutableElement)method;
1.85 StringBuilder params = new StringBuilder();
1.86 @@ -231,11 +243,13 @@
1.87 }
1.88 if (!ee.getModifiers().contains(Modifier.STATIC)) {
1.89 processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@On method has to be static", ee);
1.90 - return false;
1.91 + ok = false;
1.92 + continue;
1.93 }
1.94 if (ee.getModifiers().contains(Modifier.PRIVATE)) {
1.95 processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@On method can't be private", ee);
1.96 - return false;
1.97 + ok = false;
1.98 + continue;
1.99 }
1.100 w.append(" OnEvent." + oc.event()).append(".of(").append(cnstnt(id)).
1.101 append(").perform(new OnDispatch(" + dispatchCnt + "));\n");
1.102 @@ -266,7 +280,7 @@
1.103
1.104
1.105 }
1.106 - return true;
1.107 + return ok;
1.108 }
1.109
1.110 @Override
1.111 @@ -312,7 +326,7 @@
1.112 return e.getEnclosingElement();
1.113 }
1.114
1.115 - private static void generateProperties(
1.116 + private void generateProperties(
1.117 Writer w, Property[] properties, Collection<String> props,
1.118 Map<String,Collection<String>> deps
1.119 ) throws IOException {
1.120 @@ -370,9 +384,11 @@
1.121 }
1.122
1.123 private boolean generateComputedProperties(
1.124 - Writer w, Collection<? extends Element> arr, Collection<String> props,
1.125 + Writer w, Property[] fixedProps,
1.126 + Collection<? extends Element> arr, Collection<String> props,
1.127 Map<String,Collection<String>> deps
1.128 ) throws IOException {
1.129 + boolean ok = true;
1.130 for (Element e : arr) {
1.131 if (e.getKind() != ElementKind.METHOD) {
1.132 continue;
1.133 @@ -390,6 +406,11 @@
1.134 int arg = 0;
1.135 for (VariableElement pe : ee.getParameters()) {
1.136 final String dn = pe.getSimpleName().toString();
1.137 +
1.138 + if (!verifyPropName(pe, dn, fixedProps)) {
1.139 + ok = false;
1.140 + }
1.141 +
1.142 final String dt = pe.asType().toString();
1.143 String[] call = toGetSet(dn, dt, false);
1.144 w.write(" " + dt + " arg" + (++arg) + " = ");
1.145 @@ -423,7 +444,7 @@
1.146 props.add(gs[0]);
1.147 }
1.148
1.149 - return true;
1.150 + return ok;
1.151 }
1.152
1.153 private static String[] toGetSet(String name, String type, boolean array) {
1.154 @@ -457,12 +478,13 @@
1.155 };
1.156 }
1.157
1.158 - private static String typeName(Property p) {
1.159 + private String typeName(Property p) {
1.160 String ret;
1.161 try {
1.162 ret = p.type().getName();
1.163 } catch (MirroredTypeException ex) {
1.164 - ret = ex.getTypeMirror().toString();
1.165 + TypeMirror tm = processingEnv.getTypeUtils().erasure(ex.getTypeMirror());
1.166 + ret = tm.toString();
1.167 }
1.168 if (p.array()) {
1.169 if (ret.equals("byte")) {
1.170 @@ -489,4 +511,24 @@
1.171 }
1.172 return ret;
1.173 }
1.174 +
1.175 + private boolean verifyPropName(Element e, String propName, Property[] existingProps) {
1.176 + StringBuilder sb = new StringBuilder();
1.177 + String sep = "";
1.178 + for (Property property : existingProps) {
1.179 + if (property.name().equals(propName)) {
1.180 + return true;
1.181 + }
1.182 + sb.append(sep);
1.183 + sb.append('"');
1.184 + sb.append(property.name());
1.185 + sb.append('"');
1.186 + sep = ", ";
1.187 + }
1.188 + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
1.189 + propName + " is not one of known properties: " + sb
1.190 + , e
1.191 + );
1.192 + return false;
1.193 + }
1.194 }