javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 22 Mar 2013 16:43:51 +0100
branchmodel
changeset 876 4f02c384b13d
parent 875 ac3b09b93f36
child 878 ecbd252fd3a7
permissions -rw-r--r--
Allow boolean properties
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.htmlpage;
    19 
    20 import java.io.IOException;
    21 import java.io.InputStream;
    22 import java.io.OutputStreamWriter;
    23 import java.io.StringWriter;
    24 import java.io.Writer;
    25 import java.util.ArrayList;
    26 import java.util.Collection;
    27 import java.util.Collections;
    28 import java.util.HashMap;
    29 import java.util.LinkedHashSet;
    30 import java.util.List;
    31 import java.util.Locale;
    32 import java.util.Map;
    33 import java.util.Set;
    34 import javax.annotation.processing.AbstractProcessor;
    35 import javax.annotation.processing.Completion;
    36 import javax.annotation.processing.Completions;
    37 import javax.annotation.processing.Processor;
    38 import javax.annotation.processing.RoundEnvironment;
    39 import javax.annotation.processing.SupportedAnnotationTypes;
    40 import javax.lang.model.element.AnnotationMirror;
    41 import javax.lang.model.element.Element;
    42 import javax.lang.model.element.ElementKind;
    43 import javax.lang.model.element.ExecutableElement;
    44 import javax.lang.model.element.Modifier;
    45 import javax.lang.model.element.PackageElement;
    46 import javax.lang.model.element.TypeElement;
    47 import javax.lang.model.element.VariableElement;
    48 import javax.lang.model.type.MirroredTypeException;
    49 import javax.lang.model.type.TypeMirror;
    50 import javax.lang.model.util.Types;
    51 import javax.tools.Diagnostic;
    52 import javax.tools.FileObject;
    53 import javax.tools.StandardLocation;
    54 import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
    55 import org.apidesign.bck2brwsr.htmlpage.api.Model;
    56 import org.apidesign.bck2brwsr.htmlpage.api.On;
    57 import org.apidesign.bck2brwsr.htmlpage.api.Page;
    58 import org.apidesign.bck2brwsr.htmlpage.api.Property;
    59 import org.openide.util.lookup.ServiceProvider;
    60 
    61 /** Annotation processor to process an XHTML page and generate appropriate 
    62  * "id" file.
    63  *
    64  * @author Jaroslav Tulach <jtulach@netbeans.org>
    65  */
    66 @ServiceProvider(service=Processor.class)
    67 @SupportedAnnotationTypes({
    68     "org.apidesign.bck2brwsr.htmlpage.api.Model",
    69     "org.apidesign.bck2brwsr.htmlpage.api.Page",
    70     "org.apidesign.bck2brwsr.htmlpage.api.On"
    71 })
    72 public final class PageProcessor extends AbstractProcessor {
    73     @Override
    74     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    75         boolean ok = true;
    76         for (Element e : roundEnv.getElementsAnnotatedWith(Model.class)) {
    77             if (!processModel(e)) {
    78                 ok = false;
    79             }
    80         }
    81         for (Element e : roundEnv.getElementsAnnotatedWith(Page.class)) {
    82             if (!processPage(e)) {
    83                 ok = false;
    84             }
    85         }
    86         return ok;
    87     }
    88 
    89     private InputStream openStream(String pkg, String name) throws IOException {
    90         try {
    91             FileObject fo = processingEnv.getFiler().getResource(
    92                 StandardLocation.SOURCE_PATH, pkg, name);
    93             return fo.openInputStream();
    94         } catch (IOException ex) {
    95             return processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, pkg, name).openInputStream();
    96         }
    97     }
    98     
    99     private boolean processModel(Element e) {
   100         boolean ok = true;
   101         Model m = e.getAnnotation(Model.class);
   102         if (m == null) {
   103             return true;
   104         }
   105         String pkg = findPkgName(e);
   106         Writer w;
   107         String className = m.className();
   108         try {
   109             StringWriter body = new StringWriter();
   110             List<String> propsGetSet = new ArrayList<>();
   111             Map<String, Collection<String>> propsDeps = new HashMap<>();
   112             if (!generateComputedProperties(body, m.properties(), e.getEnclosedElements(), propsGetSet, propsDeps)) {
   113                 ok = false;
   114             }
   115             if (!generateProperties(e, body, m.properties(), propsGetSet, propsDeps)) {
   116                 ok = false;
   117             }
   118             FileObject java = processingEnv.getFiler().createSourceFile(pkg + '.' + className, e);
   119             w = new OutputStreamWriter(java.openOutputStream());
   120             try {
   121                 w.append("package " + pkg + ";\n");
   122                 w.append("import org.apidesign.bck2brwsr.htmlpage.api.*;\n");
   123                 w.append("import org.apidesign.bck2brwsr.htmlpage.KOList;\n");
   124                 w.append("final class ").append(className).append(" {\n");
   125                 w.append("  private Object json;\n");
   126                 w.append("  private boolean locked;\n");
   127                 w.append("  private org.apidesign.bck2brwsr.htmlpage.Knockout ko;\n");
   128                 w.append(body.toString());
   129                 w.append("}\n");
   130             } finally {
   131                 w.close();
   132             }
   133         } catch (IOException ex) {
   134             processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't create " + className + ".java", e);
   135             return false;
   136         }
   137         return ok;
   138     }
   139     
   140     private boolean processPage(Element e) {
   141         boolean ok = true;
   142         Page p = e.getAnnotation(Page.class);
   143         if (p == null) {
   144             return true;
   145         }
   146         String pkg = findPkgName(e);
   147 
   148         ProcessPage pp;
   149         try (InputStream is = openStream(pkg, p.xhtml())) {
   150             pp = ProcessPage.readPage(is);
   151             is.close();
   152         } catch (IOException iOException) {
   153             processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't read " + p.xhtml(), e);
   154             ok = false;
   155             pp = null;
   156         }
   157         Writer w;
   158         String className = p.className();
   159         if (className.isEmpty()) {
   160             int indx = p.xhtml().indexOf('.');
   161             className = p.xhtml().substring(0, indx);
   162         }
   163         try {
   164             StringWriter body = new StringWriter();
   165             List<String> propsGetSet = new ArrayList<>();
   166             Map<String, Collection<String>> propsDeps = new HashMap<>();
   167             if (!generateComputedProperties(body, p.properties(), e.getEnclosedElements(), propsGetSet, propsDeps)) {
   168                 ok = false;
   169             }
   170             if (!generateProperties(e, body, p.properties(), propsGetSet, propsDeps)) {
   171                 ok = false;
   172             }
   173             
   174             FileObject java = processingEnv.getFiler().createSourceFile(pkg + '.' + className, e);
   175             w = new OutputStreamWriter(java.openOutputStream());
   176             try {
   177                 w.append("package " + pkg + ";\n");
   178                 w.append("import org.apidesign.bck2brwsr.htmlpage.api.*;\n");
   179                 w.append("import org.apidesign.bck2brwsr.htmlpage.KOList;\n");
   180                 w.append("final class ").append(className).append(" {\n");
   181                 w.append("  private boolean locked;\n");
   182                 if (!initializeOnClick(className, (TypeElement) e, w, pp)) {
   183                     ok = false;
   184                 } else {
   185                     for (String id : pp.ids()) {
   186                         String tag = pp.tagNameForId(id);
   187                         String type = type(tag);
   188                         w.append("  ").append("public final ").
   189                             append(type).append(' ').append(cnstnt(id)).append(" = new ").
   190                             append(type).append("(\"").append(id).append("\");\n");
   191                     }
   192                 }
   193                 w.append("  private org.apidesign.bck2brwsr.htmlpage.Knockout ko;\n");
   194                 w.append(body.toString());
   195                 if (!propsGetSet.isEmpty()) {
   196                     w.write("public " + className + " applyBindings() {\n");
   197                     w.write("  ko = org.apidesign.bck2brwsr.htmlpage.Knockout.applyBindings(");
   198                     w.write(className + ".class, this, ");
   199                     w.write("new String[] {\n");
   200                     String sep = "";
   201                     for (String n : propsGetSet) {
   202                         w.write(sep);
   203                         if (n == null) {
   204                             w.write("    null");
   205                         } else {
   206                             w.write("    \"" + n + "\"");
   207                         }
   208                         sep = ",\n";
   209                     }
   210                     w.write("\n  });\n  return this;\n}\n");
   211 
   212                     w.write("public void triggerEvent(Element e, OnEvent ev) {\n");
   213                     w.write("  org.apidesign.bck2brwsr.htmlpage.Knockout.triggerEvent(e.getId(), ev.getElementPropertyName());\n");
   214                     w.write("}\n");
   215                 }
   216                 w.append("}\n");
   217             } finally {
   218                 w.close();
   219             }
   220         } catch (IOException ex) {
   221             processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't create " + className + ".java", e);
   222             return false;
   223         }
   224         return ok;
   225     }
   226 
   227     private static String type(String tag) {
   228         if (tag.equals("title")) {
   229             return "Title";
   230         }
   231         if (tag.equals("button")) {
   232             return "Button";
   233         }
   234         if (tag.equals("input")) {
   235             return "Input";
   236         }
   237         if (tag.equals("canvas")) {
   238             return "Canvas";
   239         }
   240         if (tag.equals("img")) {
   241             return "Image";
   242         }
   243         return "Element";
   244     }
   245 
   246     private static String cnstnt(String id) {
   247         return id.toUpperCase(Locale.ENGLISH).replace('.', '_').replace('-', '_');
   248     }
   249 
   250     private boolean initializeOnClick(
   251         String className, TypeElement type, Writer w, ProcessPage pp
   252     ) throws IOException {
   253         boolean ok = true;
   254         TypeMirror stringType = processingEnv.getElementUtils().getTypeElement("java.lang.String").asType();
   255         { //for (Element clazz : pe.getEnclosedElements()) {
   256           //  if (clazz.getKind() != ElementKind.CLASS) {
   257             //    continue;
   258            // }
   259             w.append("  public ").append(className).append("() {\n");
   260             StringBuilder dispatch = new StringBuilder();
   261             int dispatchCnt = 0;
   262             for (Element method : type.getEnclosedElements()) {
   263                 On oc = method.getAnnotation(On.class);
   264                 if (oc != null) {
   265                     for (String id : oc.id()) {
   266                         if (pp == null) {
   267                             processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "id = " + id + " not found in HTML page.");
   268                             ok = false;
   269                             continue;
   270                         }
   271                         if (pp.tagNameForId(id) == null) {
   272                             processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "id = " + id + " does not exist in the HTML page. Found only " + pp.ids(), method);
   273                             ok = false;
   274                             continue;
   275                         }
   276                         ExecutableElement ee = (ExecutableElement)method;
   277                         StringBuilder params = new StringBuilder();
   278                         {
   279                             boolean first = true;
   280                             for (VariableElement ve : ee.getParameters()) {
   281                                 if (!first) {
   282                                     params.append(", ");
   283                                 }
   284                                 first = false;
   285                                 if (ve.asType() == stringType) {
   286                                     params.append('"').append(id).append('"');
   287                                     continue;
   288                                 }
   289                                 String rn = ve.asType().toString();
   290                                 int last = rn.lastIndexOf('.');
   291                                 if (last >= 0) {
   292                                     rn = rn.substring(last + 1);
   293                                 }
   294                                 if (rn.equals(className)) {
   295                                     params.append(className).append(".this");
   296                                     continue;
   297                                 }
   298                                 processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, 
   299                                     "@On method can only accept String or " + className + " arguments",
   300                                     ee
   301                                 );
   302                                 return false;
   303                             }
   304                         }
   305                         if (!ee.getModifiers().contains(Modifier.STATIC)) {
   306                             processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@On method has to be static", ee);
   307                             ok = false;
   308                             continue;
   309                         }
   310                         if (ee.getModifiers().contains(Modifier.PRIVATE)) {
   311                             processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@On method can't be private", ee);
   312                             ok = false;
   313                             continue;
   314                         }
   315                         w.append("  OnEvent." + oc.event()).append(".of(").append(cnstnt(id)).
   316                             append(").perform(new OnDispatch(" + dispatchCnt + "));\n");
   317 
   318                         dispatch.
   319                             append("      case ").append(dispatchCnt).append(": ").
   320                             append(type.getSimpleName().toString()).
   321                             append('.').append(ee.getSimpleName()).append("(").
   322                             append(params).
   323                             append("); break;\n");
   324                         
   325                         dispatchCnt++;
   326                     }
   327                 }
   328             }
   329             w.append("  }\n");
   330             if (dispatchCnt > 0) {
   331                 w.append("class OnDispatch implements Runnable {\n");
   332                 w.append("  private final int dispatch;\n");
   333                 w.append("  OnDispatch(int d) { dispatch = d; }\n");
   334                 w.append("  public void run() {\n");
   335                 w.append("    switch (dispatch) {\n");
   336                 w.append(dispatch);
   337                 w.append("    }\n");
   338                 w.append("  }\n");
   339                 w.append("}\n");
   340             }
   341             
   342 
   343         }
   344         return ok;
   345     }
   346 
   347     @Override
   348     public Iterable<? extends Completion> getCompletions(
   349         Element element, AnnotationMirror annotation, 
   350         ExecutableElement member, String userText
   351     ) {
   352         if (!userText.startsWith("\"")) {
   353             return Collections.emptyList();
   354         }
   355         
   356         Element cls = findClass(element);
   357         Page p = cls.getAnnotation(Page.class);
   358         String pkg = findPkgName(cls);
   359         ProcessPage pp;
   360         try {
   361             InputStream is = openStream(pkg, p.xhtml());
   362             pp = ProcessPage.readPage(is);
   363             is.close();
   364         } catch (IOException iOException) {
   365             return Collections.emptyList();
   366         }
   367         
   368         List<Completion> cc = new ArrayList<>();
   369         userText = userText.substring(1);
   370         for (String id : pp.ids()) {
   371             if (id.startsWith(userText)) {
   372                 cc.add(Completions.of("\"" + id + "\"", id));
   373             }
   374         }
   375         return cc;
   376     }
   377     
   378     private static Element findClass(Element e) {
   379         if (e == null) {
   380             return null;
   381         }
   382         Page p = e.getAnnotation(Page.class);
   383         if (p != null) {
   384             return e;
   385         }
   386         return e.getEnclosingElement();
   387     }
   388 
   389     private boolean generateProperties(
   390         Element where,
   391         Writer w, Property[] properties,
   392         Collection<String> props, Map<String,Collection<String>> deps
   393     ) throws IOException {
   394         boolean ok = true;
   395         for (Property p : properties) {
   396             final String tn;
   397             tn = typeName(where, p);
   398             String[] gs = toGetSet(p.name(), tn, p.array());
   399 
   400             if (p.array()) {
   401                 w.write("private KOList<" + tn + "> prop_" + p.name() + " = new KOList<" + tn + ">(\""
   402                     + p.name() + "\"");
   403                 final Collection<String> dependants = deps.get(p.name());
   404                 if (dependants != null) {
   405                     for (String depProp : dependants) {
   406                         w.write(", ");
   407                         w.write('\"');
   408                         w.write(depProp);
   409                         w.write('\"');
   410                     }
   411                 }
   412                 w.write(");\n");
   413                 w.write("public java.util.List<" + tn + "> " + gs[0] + "() {\n");
   414                 w.write("  if (locked) throw new IllegalStateException();\n");
   415                 w.write("  prop_" + p.name() + ".assign(ko);\n");
   416                 w.write("  return prop_" + p.name() + ";\n");
   417                 w.write("}\n");
   418                 w.write("private Object[] " + gs[0] + "ToArray() {\n");
   419                 w.write("  return " + gs[0] + "().toArray(new Object[0]);\n");
   420                 w.write("}\n");
   421             } else {
   422                 w.write("private " + tn + " prop_" + p.name() + ";\n");
   423                 w.write("public " + tn + " " + gs[0] + "() {\n");
   424                 w.write("  if (locked) throw new IllegalStateException();\n");
   425                 w.write("  return prop_" + p.name() + ";\n");
   426                 w.write("}\n");
   427                 w.write("public void " + gs[1] + "(" + tn + " v) {\n");
   428                 w.write("  if (locked) throw new IllegalStateException();\n");
   429                 w.write("  prop_" + p.name() + " = v;\n");
   430                 w.write("  if (ko != null) {\n");
   431                 w.write("    ko.valueHasMutated(\"" + p.name() + "\");\n");
   432                 final Collection<String> dependants = deps.get(p.name());
   433                 if (dependants != null) {
   434                     for (String depProp : dependants) {
   435                         w.write("    ko.valueHasMutated(\"" + depProp + "\");\n");
   436                     }
   437                 }
   438                 w.write("  }\n");
   439                 w.write("}\n");
   440             }
   441             
   442             props.add(p.name());
   443             props.add(gs[2]);
   444             props.add(gs[3]);
   445             props.add(gs[0]);
   446         }
   447         return ok;
   448     }
   449 
   450     private boolean generateComputedProperties(
   451         Writer w, Property[] fixedProps,
   452         Collection<? extends Element> arr, Collection<String> props,
   453         Map<String,Collection<String>> deps
   454     ) throws IOException {
   455         boolean ok = true;
   456         for (Element e : arr) {
   457             if (e.getKind() != ElementKind.METHOD) {
   458                 continue;
   459             }
   460             if (e.getAnnotation(ComputedProperty.class) == null) {
   461                 continue;
   462             }
   463             ExecutableElement ee = (ExecutableElement)e;
   464             final TypeMirror rt = ee.getReturnType();
   465             final Types tu = processingEnv.getTypeUtils();
   466             TypeMirror ert = tu.erasure(rt);
   467             String tn = ert.toString();
   468             boolean array = false;
   469             if (tn.equals("java.util.List")) {
   470                 array = true;
   471             }
   472             
   473             final String sn = ee.getSimpleName().toString();
   474             String[] gs = toGetSet(sn, tn, array);
   475             
   476             w.write("public " + tn + " " + gs[0] + "() {\n");
   477             w.write("  if (locked) throw new IllegalStateException();\n");
   478             int arg = 0;
   479             for (VariableElement pe : ee.getParameters()) {
   480                 final String dn = pe.getSimpleName().toString();
   481                 
   482                 if (!verifyPropName(pe, dn, fixedProps)) {
   483                     ok = false;
   484                 }
   485                 
   486                 final String dt = pe.asType().toString();
   487                 String[] call = toGetSet(dn, dt, false);
   488                 w.write("  " + dt + " arg" + (++arg) + " = ");
   489                 w.write(call[0] + "();\n");
   490                 
   491                 Collection<String> depends = deps.get(dn);
   492                 if (depends == null) {
   493                     depends = new LinkedHashSet<>();
   494                     deps.put(dn, depends);
   495                 }
   496                 depends.add(sn);
   497             }
   498             w.write("  try {\n");
   499             w.write("    locked = true;\n");
   500             w.write("    return " + e.getEnclosingElement().getSimpleName() + '.' + e.getSimpleName() + "(");
   501             String sep = "";
   502             for (int i = 1; i <= arg; i++) {
   503                 w.write(sep);
   504                 w.write("arg" + i);
   505                 sep = ", ";
   506             }
   507             w.write(");\n");
   508             w.write("  } finally {\n");
   509             w.write("    locked = false;\n");
   510             w.write("  }\n");
   511             w.write("}\n");
   512 
   513             if (array) {
   514                 w.write("private Object[] " + gs[0] + "ToArray() {\n");
   515                 w.write("  return " + gs[0] + "().toArray(new Object[0]);\n");
   516                 w.write("}\n");
   517             }
   518             props.add(e.getSimpleName().toString());
   519             props.add(gs[2]);
   520             props.add(null);
   521             props.add(gs[0]);
   522         }
   523         
   524         return ok;
   525     }
   526 
   527     private static String[] toGetSet(String name, String type, boolean array) {
   528         String n = Character.toUpperCase(name.charAt(0)) + name.substring(1);
   529         String bck2brwsrType = "L" + type.replace('.', '_') + "_2";
   530         if ("int".equals(type)) {
   531             bck2brwsrType = "I";
   532         }
   533         if ("double".equals(type)) {
   534             bck2brwsrType = "D";
   535         }
   536         String pref = "get";
   537         if ("boolean".equals(type)) {
   538             pref = "is";
   539             bck2brwsrType = "Z";
   540         }
   541         final String nu = n.replace('.', '_');
   542         if (array) {
   543             return new String[] { 
   544                 "get" + n,
   545                 null,
   546                 "get" + nu + "ToArray___3Ljava_lang_Object_2",
   547                 null
   548             };
   549         }
   550         return new String[]{
   551             pref + n, 
   552             "set" + n, 
   553             pref + nu + "__" + bck2brwsrType,
   554             "set" + nu + "__V" + bck2brwsrType
   555         };
   556     }
   557 
   558     private String typeName(Element where, Property p) {
   559         String ret;
   560         boolean isModel = false;
   561         try {
   562             ret = p.type().getName();
   563         } catch (MirroredTypeException ex) {
   564             TypeMirror tm = processingEnv.getTypeUtils().erasure(ex.getTypeMirror());
   565             final Element e = processingEnv.getTypeUtils().asElement(tm);
   566             final Model m = e == null ? null : e.getAnnotation(Model.class);
   567             if (m != null) {
   568                 ret = findPkgName(e) + '.' + m.className();
   569                 isModel = true;
   570             } else {
   571                 ret = tm.toString();
   572             }
   573         }
   574         if (p.array()) {
   575             String bt = findBoxedType(ret);
   576             if (bt != null) {
   577                 return bt;
   578             }
   579         }
   580         if (!isModel && !"java.lang.String".equals(ret)) {
   581             String bt = findBoxedType(ret);
   582             if (bt == null) {
   583                 processingEnv.getMessager().printMessage(
   584                     Diagnostic.Kind.ERROR, 
   585                     "Only primitive types supported in the mapping. Not " + ret,
   586                     where
   587                 );
   588             }
   589         }
   590         return ret;
   591     }
   592     
   593     private static String findBoxedType(String ret) {
   594         if (ret.equals("boolean")) {
   595             return Boolean.class.getName();
   596         }
   597         if (ret.equals("byte")) {
   598             return Byte.class.getName();
   599         }
   600         if (ret.equals("short")) {
   601             return Short.class.getName();
   602         }
   603         if (ret.equals("char")) {
   604             return Character.class.getName();
   605         }
   606         if (ret.equals("int")) {
   607             return Integer.class.getName();
   608         }
   609         if (ret.equals("long")) {
   610             return Long.class.getName();
   611         }
   612         if (ret.equals("float")) {
   613             return Float.class.getName();
   614         }
   615         if (ret.equals("double")) {
   616             return Double.class.getName();
   617         }
   618         return null;
   619     }
   620 
   621     private boolean verifyPropName(Element e, String propName, Property[] existingProps) {
   622         StringBuilder sb = new StringBuilder();
   623         String sep = "";
   624         for (Property property : existingProps) {
   625             if (property.name().equals(propName)) {
   626                 return true;
   627             }
   628             sb.append(sep);
   629             sb.append('"');
   630             sb.append(property.name());
   631             sb.append('"');
   632             sep = ", ";
   633         }
   634         processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
   635             propName + " is not one of known properties: " + sb
   636             , e
   637         );
   638         return false;
   639     }
   640 
   641     private static String findPkgName(Element e) {
   642         for (;;) {
   643             if (e.getKind() == ElementKind.PACKAGE) {
   644                 return ((PackageElement)e).getQualifiedName().toString();
   645             }
   646             e = e.getEnclosingElement();
   647         }
   648     }
   649 }