json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java
author Jaroslav Tulach <jtulach@netbeans.org>
Mon, 22 Feb 2016 06:09:33 +0100
branchNonMutable258088
changeset 1054 4c40ceb185e5
parent 1050 f18b6cc923d8
child 1055 c61d247f087a
permissions -rw-r--r--
#258088: Introducing @Property(mutable=false) and making sure the generated Java code yields exceptions when such properties are modified after their use by the technology
jtulach@0
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@0
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jtulach@0
     5
 *
jaroslav@358
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@358
     7
 * Other names may be trademarks of their respective owners.
jtulach@0
     8
 *
jaroslav@358
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@358
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@358
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@358
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@358
    13
 * License. You can obtain a copy of the License at
jaroslav@358
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@358
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@358
    16
 * specific language governing permissions and limitations under the
jaroslav@358
    17
 * License.  When distributing the software, include this License Header
jaroslav@358
    18
 * Notice in each file and include the License file at
jaroslav@358
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@358
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@358
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@358
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@358
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@358
    24
 * your own identifying information:
jaroslav@358
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@358
    26
 *
jaroslav@358
    27
 * Contributor(s):
jaroslav@358
    28
 *
jaroslav@358
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jaroslav@358
    31
 *
jaroslav@358
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@358
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@358
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@358
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@358
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@358
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@358
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@358
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@358
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@358
    41
 * made subject to such option by the copyright holder.
jtulach@0
    42
 */
jaroslav@362
    43
package org.netbeans.html.json.impl;
jtulach@0
    44
jtulach@0
    45
import java.io.IOException;
jtulach@0
    46
import java.io.OutputStreamWriter;
jtulach@0
    47
import java.io.StringWriter;
jtulach@0
    48
import java.io.Writer;
jtulach@0
    49
import java.lang.annotation.AnnotationTypeMismatchException;
jtulach@0
    50
import java.lang.annotation.IncompleteAnnotationException;
jtulach@0
    51
import java.lang.reflect.Method;
jtulach@0
    52
import java.util.ArrayList;
jaroslav@70
    53
import java.util.Arrays;
jtulach@0
    54
import java.util.Collection;
jtulach@0
    55
import java.util.Collections;
jtulach@0
    56
import java.util.HashMap;
jtulach@0
    57
import java.util.HashSet;
jtulach@0
    58
import java.util.LinkedHashSet;
jtulach@0
    59
import java.util.List;
jtulach@0
    60
import java.util.Map;
jaroslav@70
    61
import java.util.ResourceBundle;
jtulach@0
    62
import java.util.Set;
jtulach@0
    63
import java.util.WeakHashMap;
jaroslav@71
    64
import java.util.logging.Level;
jaroslav@70
    65
import java.util.logging.Logger;
jtulach@0
    66
import javax.annotation.processing.AbstractProcessor;
jaroslav@70
    67
import javax.annotation.processing.Completion;
jaroslav@70
    68
import javax.annotation.processing.Completions;
jtulach@0
    69
import javax.annotation.processing.ProcessingEnvironment;
jtulach@0
    70
import javax.annotation.processing.Processor;
jtulach@0
    71
import javax.annotation.processing.RoundEnvironment;
jtulach@0
    72
import javax.annotation.processing.SupportedAnnotationTypes;
jaroslav@58
    73
import javax.annotation.processing.SupportedSourceVersion;
jaroslav@58
    74
import javax.lang.model.SourceVersion;
jtulach@0
    75
import javax.lang.model.element.AnnotationMirror;
jtulach@0
    76
import javax.lang.model.element.AnnotationValue;
jtulach@0
    77
import javax.lang.model.element.Element;
jtulach@0
    78
import javax.lang.model.element.ElementKind;
jtulach@0
    79
import javax.lang.model.element.ExecutableElement;
jtulach@0
    80
import javax.lang.model.element.Modifier;
jtulach@0
    81
import javax.lang.model.element.PackageElement;
jtulach@0
    82
import javax.lang.model.element.TypeElement;
jtulach@0
    83
import javax.lang.model.element.VariableElement;
jtulach@0
    84
import javax.lang.model.type.ArrayType;
jaroslav@29
    85
import javax.lang.model.type.DeclaredType;
jtulach@0
    86
import javax.lang.model.type.MirroredTypeException;
jtulach@0
    87
import javax.lang.model.type.TypeKind;
jtulach@0
    88
import javax.lang.model.type.TypeMirror;
jtulach@0
    89
import javax.lang.model.util.Elements;
jtulach@0
    90
import javax.lang.model.util.Types;
jtulach@0
    91
import javax.tools.Diagnostic;
jtulach@0
    92
import javax.tools.FileObject;
jtulach@0
    93
import net.java.html.json.ComputedProperty;
jtulach@938
    94
import net.java.html.json.Function;
jtulach@0
    95
import net.java.html.json.Model;
jaroslav@240
    96
import net.java.html.json.ModelOperation;
jtulach@0
    97
import net.java.html.json.OnPropertyChange;
jtulach@0
    98
import net.java.html.json.OnReceive;
jtulach@0
    99
import net.java.html.json.Property;
jtulach@0
   100
import org.openide.util.lookup.ServiceProvider;
jtulach@0
   101
jaroslav@70
   102
/** Annotation processor to process {@link Model @Model} annotations and
jaroslav@70
   103
 * generate appropriate model classes.
jtulach@0
   104
 *
jtulach@790
   105
 * @author Jaroslav Tulach
jtulach@0
   106
 */
jtulach@0
   107
@ServiceProvider(service=Processor.class)
jaroslav@100
   108
@SupportedSourceVersion(SourceVersion.RELEASE_6)
jtulach@0
   109
@SupportedAnnotationTypes({
jtulach@1
   110
    "net.java.html.json.Model",
jaroslav@240
   111
    "net.java.html.json.ModelOperation",
jtulach@1
   112
    "net.java.html.json.Function",
jtulach@1
   113
    "net.java.html.json.OnReceive",
jtulach@1
   114
    "net.java.html.json.OnPropertyChange",
jtulach@1
   115
    "net.java.html.json.ComputedProperty",
jtulach@1
   116
    "net.java.html.json.Property"
jtulach@0
   117
})
jtulach@3
   118
public final class ModelProcessor extends AbstractProcessor {
jaroslav@70
   119
    private static final Logger LOG = Logger.getLogger(ModelProcessor.class.getName());
jaroslav@100
   120
    private final Map<Element,String> models = new WeakHashMap<Element,String>();
jaroslav@100
   121
    private final Map<Element,Prprt[]> verify = new WeakHashMap<Element,Prprt[]>();
jtulach@0
   122
    @Override
jtulach@0
   123
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
jtulach@0
   124
        boolean ok = true;
jtulach@0
   125
        for (Element e : roundEnv.getElementsAnnotatedWith(Model.class)) {
jtulach@0
   126
            if (!processModel(e)) {
jtulach@0
   127
                ok = false;
jtulach@0
   128
            }
jtulach@0
   129
        }
jtulach@0
   130
        if (roundEnv.processingOver()) {
jtulach@0
   131
            models.clear();
jtulach@0
   132
            for (Map.Entry<Element, Prprt[]> entry : verify.entrySet()) {
jtulach@0
   133
                TypeElement te = (TypeElement)entry.getKey();
jtulach@926
   134
                String fqn = te.getQualifiedName().toString();
jtulach@0
   135
                Element finalElem = processingEnv.getElementUtils().getTypeElement(fqn);
jtulach@0
   136
                if (finalElem == null) {
jtulach@0
   137
                    continue;
jtulach@0
   138
                }
jtulach@0
   139
                Prprt[] props;
jtulach@0
   140
                Model m = finalElem.getAnnotation(Model.class);
jtulach@3
   141
                if (m == null) {
jtulach@3
   142
                    continue;
jtulach@3
   143
                }
jtulach@0
   144
                props = Prprt.wrap(processingEnv, finalElem, m.properties());
jtulach@0
   145
                for (Prprt p : props) {
jtulach@0
   146
                    boolean[] isModel = { false };
jtulach@0
   147
                    boolean[] isEnum = { false };
jtulach@0
   148
                    boolean[] isPrimitive = { false };
jtulach@0
   149
                    String t = checkType(p, isModel, isEnum, isPrimitive);
jtulach@0
   150
                    if (isEnum[0]) {
jtulach@0
   151
                        continue;
jtulach@0
   152
                    }
jtulach@0
   153
                    if (isPrimitive[0]) {
jtulach@0
   154
                        continue;
jtulach@0
   155
                    }
jtulach@0
   156
                    if (isModel[0]) {
jtulach@0
   157
                        continue;
jtulach@0
   158
                    }
jtulach@0
   159
                    if ("java.lang.String".equals(t)) {
jtulach@0
   160
                        continue;
jtulach@0
   161
                    }
jtulach@0
   162
                    error("The type " + t + " should be defined by @Model annotation", entry.getKey());
jtulach@0
   163
                }
jtulach@0
   164
            }
jtulach@0
   165
            verify.clear();
jtulach@0
   166
        }
jtulach@0
   167
        return ok;
jtulach@0
   168
    }
jtulach@0
   169
jtulach@0
   170
    private void error(String msg, Element e) {
jtulach@0
   171
        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, msg, e);
jtulach@0
   172
    }
jtulach@938
   173
jtulach@0
   174
    private boolean processModel(Element e) {
jtulach@0
   175
        boolean ok = true;
jtulach@0
   176
        Model m = e.getAnnotation(Model.class);
jtulach@0
   177
        if (m == null) {
jtulach@0
   178
            return true;
jtulach@0
   179
        }
jtulach@0
   180
        String pkg = findPkgName(e);
jtulach@0
   181
        Writer w;
jtulach@0
   182
        String className = m.className();
jtulach@0
   183
        models.put(e, className);
jtulach@0
   184
        try {
jtulach@0
   185
            StringWriter body = new StringWriter();
jaroslav@386
   186
            StringBuilder onReceiveType = new StringBuilder();
jtulach@823
   187
            List<GetSet> propsGetSet = new ArrayList<GetSet>();
jaroslav@597
   188
            List<Object> functions = new ArrayList<Object>();
jaroslav@567
   189
            Map<String, Collection<String[]>> propsDeps = new HashMap<String, Collection<String[]>>();
jaroslav@100
   190
            Map<String, Collection<String>> functionDeps = new HashMap<String, Collection<String>>();
jtulach@0
   191
            Prprt[] props = createProps(e, m.properties());
jtulach@1017
   192
            final String builderPrefix = findBuilderPrefix(e, m);
jtulach@938
   193
jtulach@951
   194
            if (!generateComputedProperties(className, body, props, e.getEnclosedElements(), propsGetSet, propsDeps)) {
jtulach@0
   195
                ok = false;
jtulach@0
   196
            }
jtulach@0
   197
            if (!generateOnChange(e, propsDeps, props, className, functionDeps)) {
jtulach@0
   198
                ok = false;
jtulach@0
   199
            }
jtulach@1017
   200
            if (!generateProperties(e, builderPrefix, body, className, props, propsGetSet, propsDeps, functionDeps)) {
jtulach@0
   201
                ok = false;
jtulach@0
   202
            }
jtulach@0
   203
            if (!generateFunctions(e, body, className, e.getEnclosedElements(), functions)) {
jtulach@0
   204
                ok = false;
jtulach@0
   205
            }
jaroslav@598
   206
            int functionsCount = functions.size() / 2;
jtulach@648
   207
            for (int i = 0; i < functions.size(); i += 2) {
jtulach@648
   208
                for (Prprt p : props) {
jtulach@648
   209
                    if (p.name().equals(functions.get(i))) {
jtulach@648
   210
                        error("Function cannot have the name of an existing property", e);
jtulach@648
   211
                        ok = false;
jtulach@648
   212
                    }
jtulach@648
   213
                }
jtulach@648
   214
            }
jaroslav@386
   215
            if (!generateReceive(e, body, className, e.getEnclosedElements(), onReceiveType)) {
jaroslav@12
   216
                ok = false;
jaroslav@12
   217
            }
jaroslav@598
   218
            if (!generateOperation(e, body, className, e.getEnclosedElements(), functions)) {
jaroslav@240
   219
                ok = false;
jaroslav@240
   220
            }
jtulach@0
   221
            FileObject java = processingEnv.getFiler().createSourceFile(pkg + '.' + className, e);
jtulach@0
   222
            w = new OutputStreamWriter(java.openOutputStream());
jtulach@0
   223
            try {
jtulach@0
   224
                w.append("package " + pkg + ";\n");
jtulach@2
   225
                w.append("import net.java.html.json.*;\n");
jtulach@1023
   226
                final String inPckName = inPckName(e, false);
jtulach@1014
   227
                w.append("/** Generated for {@link ").append(inPckName).append("}*/\n");
jtulach@0
   228
                w.append("public final class ").append(className).append(" implements Cloneable {\n");
jtulach@1014
   229
                w.append("  private static Class<").append(inPckName).append("> modelFor() { return ").append(inPckName).append(".class; }\n");
jaroslav@373
   230
                w.append("  private static final Html4JavaType TYPE = new Html4JavaType();\n");
jtulach@1023
   231
                if (m.instance()) {
jtulach@1023
   232
                    int cCnt = 0;
jtulach@1023
   233
                    for (Element c : e.getEnclosedElements()) {
jtulach@1023
   234
                        if (c.getKind() != ElementKind.CONSTRUCTOR) {
jtulach@1023
   235
                            continue;
jtulach@1023
   236
                        }
jtulach@1023
   237
                        cCnt++;
jtulach@1023
   238
                        ExecutableElement ec = (ExecutableElement) c;
jtulach@1023
   239
                        if (ec.getParameters().size() > 0) {
jtulach@1023
   240
                            continue;
jtulach@1023
   241
                        }
jtulach@1023
   242
                        if (ec.getModifiers().contains(Modifier.PRIVATE)) {
jtulach@1023
   243
                            continue;
jtulach@1023
   244
                        }
jtulach@1023
   245
                        cCnt = 0;
jtulach@1023
   246
                        break;
jtulach@1023
   247
                    }
jtulach@1023
   248
                    if (cCnt > 0) {
jtulach@1023
   249
                        ok = false;
jtulach@1023
   250
                        error("Needs non-private default constructor when instance=true", e);
jtulach@1023
   251
                        w.append("  private final ").append(inPckName).append(" instance = null;\n");
jtulach@1023
   252
                    } else {
jtulach@1023
   253
                        w.append("  private final ").append(inPckName).append(" instance = new ").append(inPckName).append("();\n");
jtulach@1023
   254
                    }
jtulach@1023
   255
                }
jtulach@838
   256
                w.append("  private final org.netbeans.html.json.spi.Proto proto;\n");
jtulach@0
   257
                w.append(body.toString());
jaroslav@111
   258
                w.append("  private ").append(className).append("(net.java.html.BrwsrCtx context) {\n");
jaroslav@410
   259
                w.append("    this.proto = TYPE.createProto(this, context);\n");
jaroslav@374
   260
                for (Prprt p : props) {
jaroslav@374
   261
                    if (p.array()) {
jtulach@949
   262
                        final String tn = typeName(p);
jaroslav@374
   263
                        String[] gs = toGetSet(p.name(), tn, p.array());
jaroslav@383
   264
                        w.write("    this.prop_" + p.name() + " = proto.createList(\""
jaroslav@374
   265
                            + p.name() + "\"");
jtulach@1054
   266
                        if (p.mutable()) {
jtulach@1054
   267
                            if (functionDeps.containsKey(p.name())) {
jtulach@1054
   268
                                int index = Arrays.asList(functionDeps.keySet().toArray()).indexOf(p.name());
jtulach@1054
   269
                                w.write(", " + index);
jtulach@1054
   270
                            } else {
jtulach@1054
   271
                                w.write(", -1");
jtulach@1054
   272
                            }
jaroslav@383
   273
                        } else {
jtulach@1054
   274
                            w.write(", java.lang.Integer.MIN_VALUE");
jaroslav@383
   275
                        }
jaroslav@567
   276
                        Collection<String[]> dependants = propsDeps.get(p.name());
jaroslav@374
   277
                        if (dependants != null) {
jaroslav@567
   278
                            for (String[] depProp : dependants) {
jaroslav@374
   279
                                w.write(", ");
jaroslav@374
   280
                                w.write('\"');
jaroslav@567
   281
                                w.write(depProp[0]);
jaroslav@374
   282
                                w.write('\"');
jaroslav@374
   283
                            }
jaroslav@374
   284
                        }
jaroslav@374
   285
                        w.write(")");
jaroslav@374
   286
                        w.write(";\n");
jaroslav@379
   287
                    }
jaroslav@379
   288
                }
jaroslav@379
   289
                w.append("  };\n");
jaroslav@379
   290
                w.append("  public ").append(className).append("() {\n");
jaroslav@379
   291
                w.append("    this(net.java.html.BrwsrCtx.findDefault(").append(className).append(".class));\n");
jaroslav@379
   292
                for (Prprt p : props) {
jaroslav@379
   293
                    if (!p.array()) {
jaroslav@374
   294
                        boolean[] isModel = {false};
jaroslav@374
   295
                        boolean[] isEnum = {false};
jaroslav@374
   296
                        boolean isPrimitive[] = {false};
jaroslav@374
   297
                        String tn = checkType(p, isModel, isEnum, isPrimitive);
jaroslav@374
   298
                        if (isModel[0]) {
jtulach@1039
   299
                            w.write("    prop_" + p.name() + " = this;\n");
jaroslav@374
   300
                        }
jaroslav@374
   301
                    }
jaroslav@374
   302
                }
jtulach@0
   303
                w.append("  };\n");
jtulach@1017
   304
                if (props.length > 0 && builderPrefix == null) {
jtulach@1011
   305
                    StringBuilder constructorWithArguments = new StringBuilder();
jtulach@1011
   306
                    constructorWithArguments.append("  public ").append(className).append("(");
jaroslav@284
   307
                    Prprt firstArray = null;
jaroslav@284
   308
                    String sep = "";
jtulach@1011
   309
                    int parameterCount = 0;
jaroslav@284
   310
                    for (Prprt p : props) {
jaroslav@284
   311
                        if (p.array()) {
jaroslav@284
   312
                            if (firstArray == null) {
jaroslav@284
   313
                                firstArray = p;
jaroslav@284
   314
                            }
jaroslav@284
   315
                            continue;
jaroslav@232
   316
                        }
jtulach@949
   317
                        String tn = typeName(p);
jtulach@1011
   318
                        constructorWithArguments.append(sep);
jtulach@1011
   319
                        constructorWithArguments.append(tn);
jtulach@862
   320
                        String[] third = toGetSet(p.name(), tn, false);
jtulach@1011
   321
                        constructorWithArguments.append(" ").append(third[2]);
jaroslav@284
   322
                        sep = ", ";
jtulach@1011
   323
                        parameterCount++;
jaroslav@232
   324
                    }
jaroslav@284
   325
                    if (firstArray != null) {
jaroslav@312
   326
                        String tn;
jaroslav@312
   327
                        boolean[] isModel = {false};
jaroslav@312
   328
                        boolean[] isEnum = {false};
jaroslav@312
   329
                        boolean isPrimitive[] = {false};
jaroslav@312
   330
                        tn = checkType(firstArray, isModel, isEnum, isPrimitive);
jtulach@1011
   331
                        constructorWithArguments.append(sep);
jtulach@1011
   332
                        constructorWithArguments.append(tn);
jtulach@862
   333
                        String[] third = toGetSet(firstArray.name(), tn, true);
jtulach@1011
   334
                        constructorWithArguments.append("... ").append(third[2]);
jtulach@1011
   335
                        parameterCount++;
jaroslav@284
   336
                    }
jtulach@1011
   337
                    constructorWithArguments.append(") {\n");
jtulach@1011
   338
                    constructorWithArguments.append("    this(net.java.html.BrwsrCtx.findDefault(").append(className).append(".class));\n");
jaroslav@284
   339
                    for (Prprt p : props) {
jaroslav@284
   340
                        if (p.array()) {
jaroslav@284
   341
                            continue;
jaroslav@284
   342
                        }
jtulach@862
   343
                        String[] third = toGetSet(p.name(), null, false);
jtulach@1011
   344
                        constructorWithArguments.append("    this.prop_" + p.name() + " = " + third[2] + ";\n");
jaroslav@284
   345
                    }
jaroslav@284
   346
                    if (firstArray != null) {
jtulach@862
   347
                        String[] third = toGetSet(firstArray.name(), null, true);
jtulach@1011
   348
                        constructorWithArguments.append("    proto.initTo(this.prop_" + firstArray.name() + ", " + third[2] + ");\n");
jaroslav@284
   349
                    }
jtulach@1011
   350
                    constructorWithArguments.append("  };\n");
jtulach@1011
   351
                    if (parameterCount < 255) {
jtulach@1011
   352
                        w.write(constructorWithArguments.toString());
jtulach@1011
   353
                    }
jaroslav@232
   354
                }
jtulach@838
   355
                w.append("  private static class Html4JavaType extends org.netbeans.html.json.spi.Proto.Type<").append(className).append("> {\n");
jaroslav@373
   356
                w.append("    private Html4JavaType() {\n      super(").append(className).append(".class, ").
jtulach@1014
   357
                    append(inPckName).append(".class, " + propsGetSet.size() + ", "
jaroslav@598
   358
                    + functionsCount + ");\n");
jaroslav@275
   359
                {
jtulach@823
   360
                    for (int i = 0; i < propsGetSet.size(); i++) {
jtulach@823
   361
                        w.append("      registerProperty(\"").append(propsGetSet.get(i).name).append("\", ");
jtulach@824
   362
                        w.append((i) + ", " + propsGetSet.get(i).readOnly + ");\n");
jaroslav@275
   363
                    }
jaroslav@17
   364
                }
jaroslav@275
   365
                {
jaroslav@603
   366
                    for (int i = 0; i < functionsCount; i++) {
jaroslav@603
   367
                        w.append("      registerFunction(\"").append((String)functions.get(i * 2)).append("\", ");
jaroslav@603
   368
                        w.append(i + ");\n");
jaroslav@275
   369
                    }
jaroslav@20
   370
                }
jaroslav@373
   371
                w.append("    }\n");
jaroslav@374
   372
                w.append("    @Override public void setValue(" + className + " data, int type, Object value) {\n");
jaroslav@17
   373
                w.append("      switch (type) {\n");
jtulach@823
   374
                for (int i = 0; i < propsGetSet.size(); i++) {
jtulach@824
   375
                    final GetSet pgs = propsGetSet.get(i);
jtulach@824
   376
                    if (pgs.readOnly) {
jtulach@824
   377
                        continue;
jtulach@824
   378
                    }
jtulach@824
   379
                    final String set = pgs.setter;
jtulach@824
   380
                    String tn = pgs.type;
jaroslav@198
   381
                    String btn = findBoxedType(tn);
jaroslav@198
   382
                    if (btn != null) {
jaroslav@198
   383
                        tn = btn;
jaroslav@68
   384
                    }
jtulach@824
   385
                    w.append("        case " + i + ": ");
jtulach@824
   386
                    if (pgs.setter != null) {
jtulach@824
   387
                        w.append("data.").append(strip(pgs.setter)).append("(TYPE.extractValue(" + tn + ".class, value)); return;\n");
jtulach@824
   388
                    } else {
jtulach@824
   389
                        w.append("TYPE.replaceValue(data.").append(strip(pgs.getter)).append("(), " + tn + ".class, value); return;\n");
jaroslav@17
   390
                    }
jaroslav@17
   391
                }
jaroslav@17
   392
                w.append("      }\n");
jtulach@824
   393
                w.append("      throw new UnsupportedOperationException();\n");
jaroslav@17
   394
                w.append("    }\n");
jaroslav@374
   395
                w.append("    @Override public Object getValue(" + className + " data, int type) {\n");
jaroslav@17
   396
                w.append("      switch (type) {\n");
jtulach@823
   397
                for (int i = 0; i < propsGetSet.size(); i++) {
jtulach@823
   398
                    final String get = propsGetSet.get(i).getter;
jaroslav@17
   399
                    if (get != null) {
jtulach@823
   400
                        w.append("        case " + i + ": return data." + strip(get) + "();\n");
jaroslav@17
   401
                    }
jaroslav@17
   402
                }
jaroslav@17
   403
                w.append("      }\n");
jaroslav@17
   404
                w.append("      throw new UnsupportedOperationException();\n");
jaroslav@17
   405
                w.append("    }\n");
jaroslav@599
   406
                w.append("    @Override public void call(" + className + " model, int type, Object data, Object ev) throws Exception {\n");
jaroslav@20
   407
                w.append("      switch (type) {\n");
jaroslav@20
   408
                for (int i = 0; i < functions.size(); i += 2) {
jaroslav@597
   409
                    final String name = (String)functions.get(i);
jaroslav@598
   410
                    final Object param = functions.get(i + 1);
jaroslav@598
   411
                    if (param instanceof ExecutableElement) {
jaroslav@598
   412
                        ExecutableElement ee = (ExecutableElement)param;
jaroslav@598
   413
                        w.append("        case " + (i / 2) + ":\n");
jtulach@1023
   414
                        w.append("          ");
jtulach@1023
   415
                        if (m.instance()) {
jtulach@1023
   416
                            w.append("model.instance");
jtulach@1023
   417
                        } else {
jtulach@1023
   418
                            w.append(((TypeElement)e).getQualifiedName());
jtulach@1023
   419
                        }
jtulach@1023
   420
                        w.append(".").append(name).append("(");
jaroslav@598
   421
                        w.append(wrapParams(ee, null, className, "model", "ev", "data"));
jaroslav@598
   422
                        w.append(");\n");
jaroslav@598
   423
                        w.append("          return;\n");
jaroslav@598
   424
                    } else {
jaroslav@598
   425
                        String call = (String)param;
jaroslav@598
   426
                        w.append("        case " + (i / 2) + ":\n"); // model." + name + "(data, ev); return;\n");
jaroslav@598
   427
                        w.append("          ").append(call).append("\n");
jaroslav@598
   428
                        w.append("          return;\n");
jtulach@938
   429
jaroslav@598
   430
                    }
jaroslav@20
   431
                }
jaroslav@20
   432
                w.append("      }\n");
jaroslav@20
   433
                w.append("      throw new UnsupportedOperationException();\n");
jaroslav@20
   434
                w.append("    }\n");
jtulach@838
   435
                w.append("    @Override public org.netbeans.html.json.spi.Proto protoFor(Object obj) {\n");
jaroslav@384
   436
                w.append("      return ((" + className + ")obj).proto;");
jaroslav@384
   437
                w.append("    }\n");
jaroslav@383
   438
                w.append("    @Override public void onChange(" + className + " model, int type) {\n");
jaroslav@383
   439
                w.append("      switch (type) {\n");
jaroslav@383
   440
                {
jaroslav@383
   441
                    String[] arr = functionDeps.keySet().toArray(new String[0]);
jaroslav@383
   442
                    for (int i = 0; i < arr.length; i++) {
jaroslav@383
   443
                        Collection<String> onChange = functionDeps.get(arr[i]);
jaroslav@383
   444
                        if (onChange != null) {
jaroslav@383
   445
                            w.append("      case " + i + ":\n");
jaroslav@383
   446
                            for (String call : onChange) {
jaroslav@383
   447
                                w.append("      ").append(call).append("\n");
jaroslav@383
   448
                            }
jaroslav@383
   449
                            w.write("      return;\n");
jaroslav@383
   450
                        }
jaroslav@383
   451
                    }
jaroslav@383
   452
                }
jaroslav@383
   453
                w.append("    }\n");
jaroslav@383
   454
                w.append("      throw new UnsupportedOperationException();\n");
jaroslav@383
   455
                w.append("    }\n");
jaroslav@386
   456
                w.append(onReceiveType);
jaroslav@374
   457
                w.append("    @Override public " + className + " read(net.java.html.BrwsrCtx c, Object json) { return new " + className + "(c, json); }\n");
jaroslav@412
   458
                w.append("    @Override public " + className + " cloneTo(" + className + " o, net.java.html.BrwsrCtx c) { return o.clone(c); }\n");
jaroslav@17
   459
                w.append("  }\n");
jaroslav@110
   460
                w.append("  private ").append(className).append("(net.java.html.BrwsrCtx c, Object json) {\n");
jaroslav@373
   461
                w.append("    this(c);\n");
jtulach@0
   462
                int values = 0;
jtulach@823
   463
                for (int i = 0; i < propsGetSet.size(); i++) {
jtulach@823
   464
                    Prprt p = findPrprt(props, propsGetSet.get(i).name);
jtulach@0
   465
                    if (p == null) {
jtulach@0
   466
                        continue;
jtulach@0
   467
                    }
jtulach@0
   468
                    values++;
jtulach@0
   469
                }
jtulach@0
   470
                w.append("    Object[] ret = new Object[" + values + "];\n");
jaroslav@385
   471
                w.append("    proto.extract(json, new String[] {\n");
jtulach@823
   472
                for (int i = 0; i < propsGetSet.size(); i++) {
jtulach@823
   473
                    Prprt p = findPrprt(props, propsGetSet.get(i).name);
jtulach@0
   474
                    if (p == null) {
jtulach@0
   475
                        continue;
jtulach@0
   476
                    }
jtulach@823
   477
                    w.append("      \"").append(propsGetSet.get(i).name).append("\",\n");
jtulach@0
   478
                }
jtulach@0
   479
                w.append("    }, ret);\n");
jtulach@823
   480
                for (int i = 0, cnt = 0, prop = 0; i < propsGetSet.size(); i++) {
jtulach@823
   481
                    final String pn = propsGetSet.get(i).name;
jtulach@0
   482
                    Prprt p = findPrprt(props, pn);
jtulach@648
   483
                    if (p == null || prop >= props.length) {
jtulach@0
   484
                        continue;
jtulach@0
   485
                    }
jtulach@0
   486
                    boolean[] isModel = { false };
jtulach@0
   487
                    boolean[] isEnum = { false };
jtulach@0
   488
                    boolean isPrimitive[] = { false };
jtulach@0
   489
                    String type = checkType(props[prop++], isModel, isEnum, isPrimitive);
jtulach@0
   490
                    if (p.array()) {
jtulach@943
   491
                        w.append("    for (Object e : useAsArray(ret[" + cnt + "])) {\n");
jtulach@0
   492
                        if (isModel[0]) {
jtulach@943
   493
                            w.append("      this.prop_").append(pn).append(".add(proto.read");
jaroslav@385
   494
                            w.append("(" + type + ".class, e));\n");
jtulach@0
   495
                        } else if (isEnum[0]) {
jaroslav@66
   496
                            w.append("        this.prop_").append(pn);
jtulach@0
   497
                            w.append(".add(e == null ? null : ");
jaroslav@380
   498
                            w.append(type).append(".valueOf(TYPE.stringValue(e)));\n");
jtulach@0
   499
                        } else {
jtulach@0
   500
                            if (isPrimitive(type)) {
jtulach@1017
   501
                                if (type.equals("char")) {
jtulach@1017
   502
                                    w.append("        this.prop_").append(pn).append(".add((char)TYPE.numberValue(e).");
jtulach@1017
   503
                                    w.append("intValue());\n");
jtulach@1017
   504
                                } else {
jtulach@1017
   505
                                    w.append("        this.prop_").append(pn).append(".add(TYPE.numberValue(e).");
jtulach@1017
   506
                                    w.append(type).append("Value());\n");
jtulach@1017
   507
                                }
jtulach@0
   508
                            } else {
jaroslav@66
   509
                                w.append("        this.prop_").append(pn).append(".add((");
jtulach@0
   510
                                w.append(type).append(")e);\n");
jtulach@0
   511
                            }
jtulach@0
   512
                        }
jaroslav@66
   513
                        w.append("    }\n");
jtulach@0
   514
                    } else {
jtulach@0
   515
                        if (isEnum[0]) {
jaroslav@340
   516
                            w.append("    try {\n");
jtulach@0
   517
                            w.append("    this.prop_").append(pn);
jtulach@0
   518
                            w.append(" = ret[" + cnt + "] == null ? null : ");
jaroslav@380
   519
                            w.append(type).append(".valueOf(TYPE.stringValue(ret[" + cnt + "]));\n");
jaroslav@340
   520
                            w.append("    } catch (IllegalArgumentException ex) {\n");
jaroslav@340
   521
                            w.append("      ex.printStackTrace();\n");
jaroslav@340
   522
                            w.append("    }\n");
jtulach@0
   523
                        } else if (isPrimitive(type)) {
jtulach@0
   524
                            w.append("    this.prop_").append(pn);
jaroslav@100
   525
                            w.append(" = ret[" + cnt + "] == null ? ");
jaroslav@100
   526
                            if ("char".equals(type)) {
jaroslav@380
   527
                                w.append("0 : (TYPE.charValue(");
jaroslav@100
   528
                            } else if ("boolean".equals(type)) {
jaroslav@380
   529
                                w.append("false : (TYPE.boolValue(");
jaroslav@100
   530
                            } else {
jaroslav@380
   531
                                w.append("0 : (TYPE.numberValue(");
jaroslav@100
   532
                            }
jaroslav@197
   533
                            w.append("ret[" + cnt + "])).");
jtulach@0
   534
                            w.append(type).append("Value();\n");
jaroslav@136
   535
                        } else if (isModel[0]) {
jaroslav@385
   536
                            w.append("    this.prop_").append(pn).append(" = proto.read");
jaroslav@385
   537
                            w.append("(" + type + ".class, ");
jaroslav@136
   538
                            w.append("ret[" + cnt + "]);\n");
jaroslav@136
   539
                        }else {
jtulach@0
   540
                            w.append("    this.prop_").append(pn);
jtulach@0
   541
                            w.append(" = (").append(type).append(')');
jtulach@0
   542
                            w.append("ret[" + cnt + "];\n");
jtulach@0
   543
                        }
jtulach@0
   544
                    }
jtulach@0
   545
                    cnt++;
jtulach@0
   546
                }
jaroslav@373
   547
                w.append("  }\n");
jtulach@943
   548
                w.append("  private static Object[] useAsArray(Object o) {\n");
jtulach@943
   549
                w.append("    return o instanceof Object[] ? ((Object[])o) : o == null ? new Object[0] : new Object[] { o };\n");
jtulach@943
   550
                w.append("  }\n");
jtulach@0
   551
                writeToString(props, w);
jtulach@0
   552
                writeClone(className, props, w);
jtulach@920
   553
                String targetId = findTargetId(e);
jtulach@920
   554
                if (targetId != null) {
jtulach@920
   555
                    w.write("  /** Activates this model instance in the current {@link \n"
jtulach@920
   556
                        + "net.java.html.json.Models#bind(java.lang.Object, net.java.html.BrwsrCtx) browser context}. \n"
jtulach@920
   557
                        + "In case of using Knockout technology, this means to \n"
jtulach@920
   558
                        + "bind JSON like data in this model instance with Knockout tags in \n"
jtulach@920
   559
                        + "the surrounding HTML page.\n"
jtulach@920
   560
                    );
jtulach@920
   561
                    if (targetId != null) {
jtulach@920
   562
                        w.write("This method binds to element '" + targetId + "' on the page\n");
jtulach@920
   563
                    }
jtulach@920
   564
                    w.write(""
jtulach@920
   565
                        + "@return <code>this</code> object\n"
jtulach@920
   566
                        + "*/\n"
jtulach@920
   567
                    );
jtulach@920
   568
                    w.write("  public " + className + " applyBindings() {\n");
jtulach@920
   569
                    w.write("    proto.applyBindings();\n");
jtulach@920
   570
    //                w.write("    proto.applyBindings(id);\n");
jtulach@920
   571
                    w.write("    return this;\n");
jtulach@920
   572
                    w.write("  }\n");
jtulach@920
   573
                } else {
jtulach@920
   574
                    w.write("  private " + className + " applyBindings() {\n");
jtulach@920
   575
                    w.write("    throw new IllegalStateException(\"Please specify targetId=\\\"\\\" in your @Model annotation\");\n");
jtulach@920
   576
                    w.write("  }\n");
jtulach@920
   577
                }
jaroslav@15
   578
                w.write("  public boolean equals(Object o) {\n");
jaroslav@15
   579
                w.write("    if (o == this) return true;\n");
jaroslav@281
   580
                w.write("    if (!(o instanceof " + className + ")) return false;\n");
jaroslav@281
   581
                w.write("    " + className + " p = (" + className + ")o;\n");
jaroslav@281
   582
                for (Prprt p : props) {
jaroslav@380
   583
                    w.write("    if (!TYPE.isSame(prop_" + p.name() + ", p.prop_" + p.name() + ")) return false;\n");
jaroslav@281
   584
                }
jaroslav@281
   585
                w.write("    return true;\n");
jaroslav@281
   586
                w.write("  }\n");
jaroslav@281
   587
                w.write("  public int hashCode() {\n");
jaroslav@281
   588
                w.write("    int h = " + className + ".class.getName().hashCode();\n");
jaroslav@281
   589
                for (Prprt p : props) {
jaroslav@380
   590
                    w.write("    h = TYPE.hashPlus(prop_" + p.name() + ", h);\n");
jaroslav@281
   591
                }
jaroslav@281
   592
                w.write("    return h;\n");
jaroslav@15
   593
                w.write("  }\n");
jaroslav@14
   594
                w.write("}\n");
jtulach@0
   595
            } finally {
jtulach@0
   596
                w.close();
jtulach@0
   597
            }
jtulach@0
   598
        } catch (IOException ex) {
jtulach@0
   599
            error("Can't create " + className + ".java", e);
jtulach@0
   600
            return false;
jtulach@0
   601
        }
jtulach@0
   602
        return ok;
jtulach@0
   603
    }
jtulach@938
   604
jtulach@1017
   605
    private static String findBuilderPrefix(Element e, Model m) {
jtulach@1017
   606
        if (!m.builder().isEmpty()) {
jtulach@1017
   607
            return m.builder();
jtulach@1017
   608
        }
jtulach@1017
   609
        for (AnnotationMirror am : e.getAnnotationMirrors()) {
jtulach@1017
   610
            for (Map.Entry<? extends Object, ? extends Object> entry : am.getElementValues().entrySet()) {
jtulach@1017
   611
                if ("builder()".equals(entry.getKey().toString())) {
jtulach@1017
   612
                    return "";
jtulach@1017
   613
                }
jtulach@1017
   614
            }
jtulach@1017
   615
        }
jtulach@1017
   616
        return null;
jtulach@1017
   617
    }
jtulach@1017
   618
jtulach@1017
   619
    private static String builderMethod(String builderPrefix, Prprt p) {
jtulach@1017
   620
        if (builderPrefix.isEmpty()) {
jtulach@1017
   621
            return p.name();
jtulach@1017
   622
        }
jtulach@1017
   623
        return builderPrefix + Character.toUpperCase(p.name().charAt(0)) + p.name().substring(1);
jtulach@1017
   624
    }
jtulach@1017
   625
jtulach@0
   626
    private boolean generateProperties(
jtulach@1017
   627
        Element where, String builderPrefix,
jaroslav@383
   628
        Writer w, String className, Prprt[] properties,
jtulach@938
   629
        List<GetSet> props,
jaroslav@567
   630
        Map<String,Collection<String[]>> deps,
jtulach@0
   631
        Map<String,Collection<String>> functionDeps
jtulach@0
   632
    ) throws IOException {
jtulach@0
   633
        boolean ok = true;
jtulach@0
   634
        for (Prprt p : properties) {
jtulach@0
   635
            final String tn;
jtulach@949
   636
            tn = typeName(p);
jtulach@0
   637
            String[] gs = toGetSet(p.name(), tn, p.array());
jaroslav@17
   638
            String castTo;
jtulach@938
   639
jtulach@0
   640
            if (p.array()) {
jaroslav@383
   641
                w.write("  private final java.util.List<" + tn + "> prop_" + p.name() + ";\n");
jtulach@938
   642
jaroslav@17
   643
                castTo = "java.util.List";
jaroslav@66
   644
                w.write("  public java.util.List<" + tn + "> " + gs[0] + "() {\n");
jtulach@786
   645
                w.write("    proto.accessProperty(\"" + p.name() + "\");\n");
jaroslav@66
   646
                w.write("    return prop_" + p.name() + ";\n");
jaroslav@66
   647
                w.write("  }\n");
jtulach@1017
   648
                if (builderPrefix != null) {
jtulach@1017
   649
                    boolean[] isModel = {false};
jtulach@1017
   650
                    boolean[] isEnum = {false};
jtulach@1017
   651
                    boolean isPrimitive[] = {false};
jtulach@1017
   652
                    String ret = checkType(p, isModel, isEnum, isPrimitive);
jtulach@1017
   653
                    w.write("  public " + className + " " + builderMethod(builderPrefix, p) + "(" + ret + "... v) {\n");
jtulach@1017
   654
                    w.write("    proto.accessProperty(\"" + p.name() + "\");\n");
jtulach@1017
   655
                    w.append("   TYPE.replaceValue(prop_").append(p.name()).append(", " + tn + ".class, v);\n");
jtulach@1017
   656
                    w.write("    return this;\n");
jtulach@1017
   657
                    w.write("  }\n");
jtulach@1017
   658
                }
jtulach@0
   659
            } else {
jaroslav@17
   660
                castTo = tn;
jtulach@950
   661
                boolean isModel[] = { false };
jtulach@950
   662
                boolean isEnum[] = { false };
jtulach@950
   663
                boolean isPrimitive[] = { false };
jtulach@950
   664
                checkType(p, isModel, isEnum, isPrimitive);
jtulach@1039
   665
                if (isModel[0]) {
jtulach@1039
   666
                    w.write("  private /*" + tn + "*/Object prop_" + p.name() + ";\n");
jtulach@1039
   667
jtulach@1039
   668
                } else {
jtulach@1039
   669
                    w.write("  private " + tn + " prop_" + p.name() + ";\n");
jtulach@1039
   670
                }
jaroslav@66
   671
                w.write("  public " + tn + " " + gs[0] + "() {\n");
jtulach@786
   672
                w.write("    proto.accessProperty(\"" + p.name() + "\");\n");
jtulach@1039
   673
                if (isModel[0]) {
jtulach@1039
   674
                    w.write("    if (prop_" + p.name() + " == this) prop_" + p.name() + " = new " + tn +"();\n");
jtulach@1039
   675
                }
jtulach@1039
   676
                w.write("    return (" + tn + ")prop_" + p.name() + ";\n");
jaroslav@66
   677
                w.write("  }\n");
jaroslav@66
   678
                w.write("  public void " + gs[1] + "(" + tn + " v) {\n");
jtulach@1054
   679
                if (!p.mutable()) {
jtulach@1054
   680
                    w.write("    proto.initTo(null, null);\n");
jtulach@1054
   681
                }
jaroslav@402
   682
                w.write("    proto.verifyUnlocked();\n");
jaroslav@567
   683
                w.write("    Object o = prop_" + p.name() + ";\n");
jtulach@950
   684
                if (isModel[0]) {
jtulach@990
   685
                    w.write("    if (o == v) return;\n");
jtulach@950
   686
                    w.write("    prop_" + p.name() + " = v;\n");
jtulach@990
   687
                } else {
jtulach@975
   688
                    w.write("    if (TYPE.isSame(o , v)) return;\n");
jtulach@950
   689
                    w.write("    prop_" + p.name() + " = v;\n");
jtulach@950
   690
                }
jaroslav@567
   691
                w.write("    proto.valueHasMutated(\"" + p.name() + "\", o, v);\n");
jaroslav@567
   692
                {
jaroslav@567
   693
                    Collection<String[]> dependants = deps.get(p.name());
jaroslav@567
   694
                    if (dependants != null) {
jaroslav@567
   695
                        for (String[] pair : dependants) {
jtulach@938
   696
                            w.write("    proto.valueHasMutated(\"" + pair[0] + "\", null, " + pair[1] + "());\n");
jaroslav@567
   697
                        }
jaroslav@66
   698
                    }
jaroslav@66
   699
                }
jaroslav@567
   700
                {
jaroslav@567
   701
                    Collection<String> dependants = functionDeps.get(p.name());
jaroslav@567
   702
                    if (dependants != null) {
jtulach@1025
   703
                        w.append("    ");
jaroslav@567
   704
                        w.append(className).append(" model = ").append(className).append(".this;\n");
jaroslav@567
   705
                        for (String call : dependants) {
jaroslav@567
   706
                            w.append("  ").append(call);
jaroslav@567
   707
                        }
jtulach@0
   708
                    }
jtulach@0
   709
                }
jtulach@0
   710
                w.write("  }\n");
jtulach@1017
   711
                if (builderPrefix != null) {
jtulach@1017
   712
                    w.write("  public " + className + " " + builderMethod(builderPrefix, p) + "(" + tn + " v) {\n");
jtulach@1017
   713
                    w.write("    " + gs[1] + "(v);\n");
jtulach@1017
   714
                    w.write("    return this;\n");
jtulach@1017
   715
                    w.write("  }\n");
jtulach@1017
   716
                }
jtulach@0
   717
            }
jtulach@938
   718
jtulach@823
   719
            for (int i = 0; i < props.size(); i++) {
jtulach@823
   720
                if (props.get(i).name.equals(p.name())) {
jtulach@648
   721
                    error("Cannot have the name " + p.name() + " defined twice", where);
jtulach@648
   722
                    ok = false;
jtulach@648
   723
                }
jtulach@648
   724
            }
jtulach@938
   725
jtulach@823
   726
            props.add(new GetSet(
jtulach@823
   727
                p.name(),
jtulach@862
   728
                gs[0],
jtulach@862
   729
                gs[1],
jtulach@824
   730
                tn,
jtulach@824
   731
                gs[3] == null && !p.array()
jtulach@823
   732
            ));
jtulach@0
   733
        }
jtulach@0
   734
        return ok;
jtulach@0
   735
    }
jtulach@0
   736
jtulach@0
   737
    private boolean generateComputedProperties(
jtulach@951
   738
        String className,
jtulach@0
   739
        Writer w, Prprt[] fixedProps,
jtulach@823
   740
        Collection<? extends Element> arr, Collection<GetSet> props,
jaroslav@567
   741
        Map<String,Collection<String[]>> deps
jtulach@0
   742
    ) throws IOException {
jtulach@0
   743
        boolean ok = true;
jtulach@0
   744
        for (Element e : arr) {
jtulach@0
   745
            if (e.getKind() != ElementKind.METHOD) {
jtulach@0
   746
                continue;
jtulach@0
   747
            }
jtulach@776
   748
            final ComputedProperty cp = e.getAnnotation(ComputedProperty.class);
jtulach@787
   749
            final Transitive tp = e.getAnnotation(Transitive.class);
jtulach@776
   750
            if (cp == null) {
jtulach@0
   751
                continue;
jtulach@0
   752
            }
jaroslav@227
   753
            if (!e.getModifiers().contains(Modifier.STATIC)) {
jaroslav@227
   754
                error("Method " + e.getSimpleName() + " has to be static when annotated by @ComputedProperty", e);
jaroslav@227
   755
                ok = false;
jaroslav@227
   756
                continue;
jaroslav@227
   757
            }
jtulach@0
   758
            ExecutableElement ee = (ExecutableElement)e;
jtulach@951
   759
            ExecutableElement write = null;
jtulach@951
   760
            if (!cp.write().isEmpty()) {
jtulach@951
   761
                write = findWrite(ee, (TypeElement)e.getEnclosingElement(), cp.write(), className);
jtulach@951
   762
                ok = write != null;
jtulach@951
   763
            }
jtulach@0
   764
            final TypeMirror rt = ee.getReturnType();
jtulach@0
   765
            final Types tu = processingEnv.getTypeUtils();
jtulach@0
   766
            TypeMirror ert = tu.erasure(rt);
jtulach@0
   767
            String tn = fqn(ert, ee);
jtulach@0
   768
            boolean array = false;
jaroslav@235
   769
            final TypeMirror toCheck;
jtulach@0
   770
            if (tn.equals("java.util.List")) {
jtulach@0
   771
                array = true;
jaroslav@235
   772
                toCheck = ((DeclaredType)rt).getTypeArguments().get(0);
jaroslav@235
   773
            } else {
jaroslav@235
   774
                toCheck = rt;
jtulach@0
   775
            }
jtulach@938
   776
jtulach@0
   777
            final String sn = ee.getSimpleName().toString();
jtulach@938
   778
jaroslav@235
   779
            if (toCheck.getKind().isPrimitive()) {
jaroslav@235
   780
                // OK
jaroslav@235
   781
            } else {
jaroslav@235
   782
                TypeMirror stringType = processingEnv.getElementUtils().getTypeElement("java.lang.String").asType();
jaroslav@235
   783
                TypeMirror enumType = processingEnv.getElementUtils().getTypeElement("java.lang.Enum").asType();
jaroslav@235
   784
jaroslav@235
   785
                if (tu.isSubtype(toCheck, stringType)) {
jaroslav@235
   786
                    // OK
jaroslav@252
   787
                } else if (tu.isSubtype(tu.erasure(toCheck), tu.erasure(enumType))) {
jaroslav@235
   788
                    // OK
jaroslav@236
   789
                } else if (isModel(toCheck)) {
jaroslav@236
   790
                    // OK
jaroslav@235
   791
                } else {
jtulach@828
   792
                    try {
jtulach@828
   793
                        tu.unboxedType(toCheck);
jtulach@828
   794
                        // boxed types are OK
jtulach@828
   795
                    } catch (IllegalArgumentException ex) {
jtulach@828
   796
                        ok = false;
jtulach@828
   797
                        error(sn + " cannot return " + toCheck, e);
jtulach@828
   798
                    }
jaroslav@235
   799
                }
jaroslav@235
   800
            }
jtulach@938
   801
jtulach@0
   802
            String[] gs = toGetSet(sn, tn, array);
jtulach@938
   803
jaroslav@562
   804
            w.write("  public " + tn);
jaroslav@562
   805
            if (array) {
jaroslav@562
   806
                w.write("<" + toCheck + ">");
jaroslav@562
   807
            }
jaroslav@562
   808
            w.write(" " + gs[0] + "() {\n");
jtulach@0
   809
            int arg = 0;
jtulach@788
   810
            boolean deep = false;
jtulach@0
   811
            for (VariableElement pe : ee.getParameters()) {
jtulach@0
   812
                final String dn = pe.getSimpleName().toString();
jtulach@938
   813
jtulach@0
   814
                if (!verifyPropName(pe, dn, fixedProps)) {
jtulach@0
   815
                    ok = false;
jtulach@0
   816
                }
jtulach@788
   817
                final TypeMirror pt = pe.asType();
jtulach@788
   818
                if (isModel(pt)) {
jtulach@788
   819
                    deep = true;
jtulach@788
   820
                }
jtulach@788
   821
                final String dt = fqn(pt, ee);
jtulach@788
   822
                if (dt.startsWith("java.util.List") && pt instanceof DeclaredType) {
jtulach@788
   823
                    final List<? extends TypeMirror> ptArgs = ((DeclaredType)pt).getTypeArguments();
jtulach@788
   824
                    if (ptArgs.size() == 1 && isModel(ptArgs.get(0))) {
jtulach@788
   825
                        deep = true;
jtulach@788
   826
                    }
jtulach@788
   827
                }
jtulach@0
   828
                String[] call = toGetSet(dn, dt, false);
jaroslav@66
   829
                w.write("    " + dt + " arg" + (++arg) + " = ");
jtulach@0
   830
                w.write(call[0] + "();\n");
jtulach@938
   831
jaroslav@567
   832
                Collection<String[]> depends = deps.get(dn);
jtulach@0
   833
                if (depends == null) {
jaroslav@567
   834
                    depends = new LinkedHashSet<String[]>();
jtulach@0
   835
                    deps.put(dn, depends);
jtulach@0
   836
                }
jaroslav@567
   837
                depends.add(new String[] { sn, gs[0] });
jtulach@0
   838
            }
jaroslav@66
   839
            w.write("    try {\n");
jtulach@788
   840
            if (tp != null) {
jtulach@788
   841
                deep = tp.deep();
jtulach@788
   842
            }
jtulach@788
   843
            if (deep) {
jtulach@776
   844
                w.write("      proto.acquireLock(\"" + sn + "\");\n");
jtulach@776
   845
            } else {
jtulach@776
   846
                w.write("      proto.acquireLock();\n");
jtulach@776
   847
            }
jaroslav@66
   848
            w.write("      return " + fqn(ee.getEnclosingElement().asType(), ee) + '.' + e.getSimpleName() + "(");
jtulach@0
   849
            String sep = "";
jtulach@0
   850
            for (int i = 1; i <= arg; i++) {
jtulach@0
   851
                w.write(sep);
jtulach@0
   852
                w.write("arg" + i);
jtulach@0
   853
                sep = ", ";
jtulach@0
   854
            }
jtulach@0
   855
            w.write(");\n");
jaroslav@66
   856
            w.write("    } finally {\n");
jaroslav@373
   857
            w.write("      proto.releaseLock();\n");
jaroslav@66
   858
            w.write("    }\n");
jtulach@0
   859
            w.write("  }\n");
jtulach@0
   860
jtulach@951
   861
            if (write == null) {
jtulach@951
   862
                props.add(new GetSet(
jtulach@951
   863
                    e.getSimpleName().toString(),
jtulach@951
   864
                    gs[0],
jtulach@951
   865
                    null,
jtulach@951
   866
                    tn,
jtulach@951
   867
                    true
jtulach@951
   868
                ));
jtulach@951
   869
            } else {
jtulach@951
   870
                w.write("  public void " + gs[4] + "(" + write.getParameters().get(1).asType());
jtulach@951
   871
                w.write(" value) {\n");
jtulach@951
   872
                w.write("    " + fqn(ee.getEnclosingElement().asType(), ee) + '.' + write.getSimpleName() + "(this, value);\n");
jtulach@951
   873
                w.write("  }\n");
jtulach@951
   874
jtulach@951
   875
                props.add(new GetSet(
jtulach@951
   876
                    e.getSimpleName().toString(),
jtulach@951
   877
                    gs[0],
jtulach@951
   878
                    gs[4],
jtulach@951
   879
                    tn,
jtulach@951
   880
                    false
jtulach@951
   881
                ));
jtulach@951
   882
            }
jtulach@0
   883
        }
jtulach@938
   884
jtulach@0
   885
        return ok;
jtulach@0
   886
    }
jtulach@0
   887
jtulach@0
   888
    private static String[] toGetSet(String name, String type, boolean array) {
jtulach@0
   889
        String n = Character.toUpperCase(name.charAt(0)) + name.substring(1);
jtulach@862
   890
        boolean clazz = "class".equals(name);
jtulach@862
   891
        String pref = clazz ? "access" : "get";
jtulach@862
   892
        if ("boolean".equals(type) && !array) {
jtulach@862
   893
            pref = "is";
jtulach@0
   894
        }
jtulach@0
   895
        if (array) {
jtulach@938
   896
            return new String[] {
jtulach@862
   897
                pref + n,
jtulach@0
   898
                null,
jtulach@862
   899
                "a" + n,
jtulach@951
   900
                null,
jtulach@951
   901
                "set" + n
jtulach@0
   902
            };
jtulach@0
   903
        }
jtulach@0
   904
        return new String[]{
jtulach@938
   905
            pref + n,
jtulach@938
   906
            "set" + n,
jtulach@862
   907
            "a" + n,
jtulach@951
   908
            "",
jtulach@951
   909
            "set" + n
jtulach@0
   910
        };
jtulach@0
   911
    }
jtulach@0
   912
jtulach@949
   913
    private String typeName(Prprt p) {
jtulach@0
   914
        String ret;
jtulach@0
   915
        boolean[] isModel = { false };
jtulach@0
   916
        boolean[] isEnum = { false };
jtulach@0
   917
        boolean isPrimitive[] = { false };
jtulach@0
   918
        ret = checkType(p, isModel, isEnum, isPrimitive);
jtulach@0
   919
        if (p.array()) {
jtulach@0
   920
            String bt = findBoxedType(ret);
jtulach@0
   921
            if (bt != null) {
jtulach@0
   922
                return bt;
jtulach@0
   923
            }
jtulach@0
   924
        }
jtulach@0
   925
        return ret;
jtulach@0
   926
    }
jtulach@938
   927
jtulach@0
   928
    private static String findBoxedType(String ret) {
jtulach@0
   929
        if (ret.equals("boolean")) {
jtulach@0
   930
            return Boolean.class.getName();
jtulach@0
   931
        }
jtulach@0
   932
        if (ret.equals("byte")) {
jtulach@0
   933
            return Byte.class.getName();
jtulach@0
   934
        }
jtulach@0
   935
        if (ret.equals("short")) {
jtulach@0
   936
            return Short.class.getName();
jtulach@0
   937
        }
jtulach@0
   938
        if (ret.equals("char")) {
jtulach@0
   939
            return Character.class.getName();
jtulach@0
   940
        }
jtulach@0
   941
        if (ret.equals("int")) {
jtulach@0
   942
            return Integer.class.getName();
jtulach@0
   943
        }
jtulach@0
   944
        if (ret.equals("long")) {
jtulach@0
   945
            return Long.class.getName();
jtulach@0
   946
        }
jtulach@0
   947
        if (ret.equals("float")) {
jtulach@0
   948
            return Float.class.getName();
jtulach@0
   949
        }
jtulach@0
   950
        if (ret.equals("double")) {
jtulach@0
   951
            return Double.class.getName();
jtulach@0
   952
        }
jtulach@0
   953
        return null;
jtulach@0
   954
    }
jtulach@0
   955
jtulach@0
   956
    private boolean verifyPropName(Element e, String propName, Prprt[] existingProps) {
jtulach@0
   957
        StringBuilder sb = new StringBuilder();
jtulach@0
   958
        String sep = "";
jtulach@0
   959
        for (Prprt Prprt : existingProps) {
jtulach@0
   960
            if (Prprt.name().equals(propName)) {
jtulach@0
   961
                return true;
jtulach@0
   962
            }
jtulach@0
   963
            sb.append(sep);
jtulach@0
   964
            sb.append('"');
jtulach@0
   965
            sb.append(Prprt.name());
jtulach@0
   966
            sb.append('"');
jtulach@0
   967
            sep = ", ";
jtulach@0
   968
        }
jtulach@0
   969
        error(
jtulach@0
   970
            propName + " is not one of known properties: " + sb
jtulach@0
   971
            , e
jtulach@0
   972
        );
jtulach@0
   973
        return false;
jtulach@0
   974
    }
jtulach@0
   975
jtulach@0
   976
    private static String findPkgName(Element e) {
jtulach@0
   977
        for (;;) {
jtulach@0
   978
            if (e.getKind() == ElementKind.PACKAGE) {
jtulach@0
   979
                return ((PackageElement)e).getQualifiedName().toString();
jtulach@0
   980
            }
jtulach@0
   981
            e = e.getEnclosingElement();
jtulach@0
   982
        }
jtulach@0
   983
    }
jtulach@0
   984
jtulach@0
   985
    private boolean generateFunctions(
jtulach@938
   986
        Element clazz, StringWriter body, String className,
jaroslav@597
   987
        List<? extends Element> enclosedElements, List<Object> functions
jtulach@0
   988
    ) {
jtulach@1023
   989
        boolean instance = clazz.getAnnotation(Model.class).instance();
jtulach@0
   990
        for (Element m : enclosedElements) {
jtulach@0
   991
            if (m.getKind() != ElementKind.METHOD) {
jtulach@0
   992
                continue;
jtulach@0
   993
            }
jtulach@0
   994
            ExecutableElement e = (ExecutableElement)m;
jtulach@0
   995
            Function onF = e.getAnnotation(Function.class);
jtulach@0
   996
            if (onF == null) {
jtulach@0
   997
                continue;
jtulach@0
   998
            }
jtulach@1023
   999
            if (!instance && !e.getModifiers().contains(Modifier.STATIC)) {
jtulach@0
  1000
                error("@OnFunction method needs to be static", e);
jtulach@0
  1001
                return false;
jtulach@0
  1002
            }
jtulach@0
  1003
            if (e.getModifiers().contains(Modifier.PRIVATE)) {
jtulach@0
  1004
                error("@OnFunction method cannot be private", e);
jtulach@0
  1005
                return false;
jtulach@0
  1006
            }
jtulach@0
  1007
            if (e.getReturnType().getKind() != TypeKind.VOID) {
jtulach@0
  1008
                error("@OnFunction method should return void", e);
jtulach@0
  1009
                return false;
jtulach@0
  1010
            }
jtulach@0
  1011
            String n = e.getSimpleName().toString();
jtulach@0
  1012
            functions.add(n);
jaroslav@597
  1013
            functions.add(e);
jtulach@0
  1014
        }
jtulach@0
  1015
        return true;
jtulach@0
  1016
    }
jtulach@0
  1017
jaroslav@567
  1018
    private boolean generateOnChange(Element clazz, Map<String,Collection<String[]>> propDeps,
jtulach@938
  1019
        Prprt[] properties, String className,
jtulach@0
  1020
        Map<String, Collection<String>> functionDeps
jtulach@0
  1021
    ) {
jtulach@1023
  1022
        boolean instance = clazz.getAnnotation(Model.class).instance();
jtulach@0
  1023
        for (Element m : clazz.getEnclosedElements()) {
jtulach@0
  1024
            if (m.getKind() != ElementKind.METHOD) {
jtulach@0
  1025
                continue;
jtulach@0
  1026
            }
jtulach@0
  1027
            ExecutableElement e = (ExecutableElement) m;
jtulach@0
  1028
            OnPropertyChange onPC = e.getAnnotation(OnPropertyChange.class);
jtulach@0
  1029
            if (onPC == null) {
jtulach@0
  1030
                continue;
jtulach@0
  1031
            }
jtulach@0
  1032
            for (String pn : onPC.value()) {
jtulach@0
  1033
                if (findPrprt(properties, pn) == null && findDerivedFrom(propDeps, pn).isEmpty()) {
jtulach@0
  1034
                    error("No Prprt named '" + pn + "' in the model", clazz);
jtulach@0
  1035
                    return false;
jtulach@0
  1036
                }
jtulach@0
  1037
            }
jtulach@1023
  1038
            if (!instance && !e.getModifiers().contains(Modifier.STATIC)) {
jtulach@0
  1039
                error("@OnPrprtChange method needs to be static", e);
jtulach@0
  1040
                return false;
jtulach@0
  1041
            }
jtulach@0
  1042
            if (e.getModifiers().contains(Modifier.PRIVATE)) {
jtulach@0
  1043
                error("@OnPrprtChange method cannot be private", e);
jtulach@0
  1044
                return false;
jtulach@0
  1045
            }
jtulach@0
  1046
            if (e.getReturnType().getKind() != TypeKind.VOID) {
jtulach@0
  1047
                error("@OnPrprtChange method should return void", e);
jtulach@0
  1048
                return false;
jtulach@0
  1049
            }
jtulach@0
  1050
            String n = e.getSimpleName().toString();
jtulach@938
  1051
jtulach@938
  1052
jtulach@0
  1053
            for (String pn : onPC.value()) {
jtulach@0
  1054
                StringBuilder call = new StringBuilder();
jtulach@1023
  1055
                call.append("  ").append(inPckName(clazz, instance)).append(".").append(n).append("(");
jtulach@0
  1056
                call.append(wrapPropName(e, className, "name", pn));
jtulach@0
  1057
                call.append(");\n");
jtulach@938
  1058
jtulach@0
  1059
                Collection<String> change = functionDeps.get(pn);
jtulach@0
  1060
                if (change == null) {
jaroslav@100
  1061
                    change = new ArrayList<String>();
jtulach@0
  1062
                    functionDeps.put(pn, change);
jtulach@0
  1063
                }
jtulach@0
  1064
                change.add(call.toString());
jtulach@0
  1065
                for (String dpn : findDerivedFrom(propDeps, pn)) {
jtulach@0
  1066
                    change = functionDeps.get(dpn);
jtulach@0
  1067
                    if (change == null) {
jaroslav@100
  1068
                        change = new ArrayList<String>();
jtulach@0
  1069
                        functionDeps.put(dpn, change);
jtulach@0
  1070
                    }
jtulach@0
  1071
                    change.add(call.toString());
jtulach@0
  1072
                }
jtulach@0
  1073
            }
jtulach@0
  1074
        }
jtulach@0
  1075
        return true;
jtulach@0
  1076
    }
jaroslav@240
  1077
jtulach@938
  1078
    private boolean generateOperation(Element clazz,
jtulach@938
  1079
        StringWriter body, String className,
jaroslav@598
  1080
        List<? extends Element> enclosedElements,
jaroslav@598
  1081
        List<Object> functions
jaroslav@240
  1082
    ) {
jtulach@1023
  1083
        boolean instance = clazz.getAnnotation(Model.class).instance();
jaroslav@240
  1084
        for (Element m : enclosedElements) {
jaroslav@240
  1085
            if (m.getKind() != ElementKind.METHOD) {
jaroslav@240
  1086
                continue;
jaroslav@240
  1087
            }
jaroslav@240
  1088
            ExecutableElement e = (ExecutableElement)m;
jaroslav@240
  1089
            ModelOperation mO = e.getAnnotation(ModelOperation.class);
jaroslav@240
  1090
            if (mO == null) {
jaroslav@240
  1091
                continue;
jaroslav@240
  1092
            }
jtulach@1023
  1093
            if (!instance && !e.getModifiers().contains(Modifier.STATIC)) {
jaroslav@240
  1094
                error("@ModelOperation method needs to be static", e);
jaroslav@240
  1095
                return false;
jaroslav@240
  1096
            }
jaroslav@240
  1097
            if (e.getModifiers().contains(Modifier.PRIVATE)) {
jaroslav@240
  1098
                error("@ModelOperation method cannot be private", e);
jaroslav@240
  1099
                return false;
jaroslav@240
  1100
            }
jaroslav@240
  1101
            if (e.getReturnType().getKind() != TypeKind.VOID) {
jaroslav@240
  1102
                error("@ModelOperation method should return void", e);
jaroslav@240
  1103
                return false;
jaroslav@240
  1104
            }
jaroslav@240
  1105
            List<String> args = new ArrayList<String>();
jaroslav@240
  1106
            {
jtulach@1050
  1107
                body.append("  /** @see " + clazz.getSimpleName() + "#" + m.getSimpleName() + " */\n");
jaroslav@240
  1108
                body.append("  public void ").append(m.getSimpleName()).append("(");
jaroslav@240
  1109
                String sep = "";
jaroslav@240
  1110
                boolean checkFirst = true;
jaroslav@240
  1111
                for (VariableElement ve : e.getParameters()) {
jaroslav@240
  1112
                    final TypeMirror type = ve.asType();
jaroslav@240
  1113
                    CharSequence simpleName;
jaroslav@240
  1114
                    if (type.getKind() == TypeKind.DECLARED) {
jaroslav@240
  1115
                        simpleName = ((DeclaredType)type).asElement().getSimpleName();
jaroslav@240
  1116
                    } else {
jaroslav@240
  1117
                        simpleName = type.toString();
jaroslav@240
  1118
                    }
jtulach@643
  1119
                    if (checkFirst && simpleName.toString().equals(className)) {
jaroslav@240
  1120
                        checkFirst = false;
jaroslav@240
  1121
                    } else {
jaroslav@240
  1122
                        if (checkFirst) {
jaroslav@240
  1123
                            error("First parameter of @ModelOperation method must be " + className, m);
jaroslav@240
  1124
                            return false;
jaroslav@240
  1125
                        }
jaroslav@240
  1126
                        args.add(ve.getSimpleName().toString());
jaroslav@240
  1127
                        body.append(sep).append("final ");
jaroslav@240
  1128
                        body.append(ve.asType().toString()).append(" ");
jaroslav@240
  1129
                        body.append(ve.toString());
jaroslav@240
  1130
                        sep = ", ";
jaroslav@240
  1131
                    }
jaroslav@240
  1132
                }
jaroslav@240
  1133
                body.append(") {\n");
jaroslav@598
  1134
                int idx = functions.size() / 2;
jaroslav@598
  1135
                functions.add(m.getSimpleName().toString());
jaroslav@598
  1136
                body.append("    proto.runInBrowser(" + idx);
jaroslav@240
  1137
                for (String s : args) {
jaroslav@240
  1138
                    body.append(", ").append(s);
jaroslav@240
  1139
                }
jaroslav@240
  1140
                body.append(");\n");
jaroslav@240
  1141
                body.append("  }\n");
jaroslav@598
  1142
jaroslav@598
  1143
                StringBuilder call = new StringBuilder();
jaroslav@598
  1144
                call.append("{ Object[] arr = (Object[])data; ");
jtulach@1023
  1145
                call.append(inPckName(clazz, true)).append(".").append(m.getSimpleName()).append("(");
jaroslav@598
  1146
                int i = 0;
jaroslav@598
  1147
                for (VariableElement ve : e.getParameters()) {
jaroslav@598
  1148
                    if (i++ == 0) {
jaroslav@598
  1149
                        call.append("model");
jaroslav@598
  1150
                        continue;
jaroslav@598
  1151
                    }
jaroslav@601
  1152
                    String type = ve.asType().toString();
jaroslav@601
  1153
                    String boxedType = findBoxedType(type);
jaroslav@601
  1154
                    if (boxedType != null) {
jaroslav@601
  1155
                        type = boxedType;
jaroslav@601
  1156
                    }
jaroslav@601
  1157
                    call.append(", ").append("(").append(type).append(")arr[").append(i - 2).append("]");
jaroslav@598
  1158
                }
jaroslav@598
  1159
                call.append("); }");
jaroslav@598
  1160
                functions.add(call.toString());
jaroslav@240
  1161
            }
jtulach@938
  1162
jaroslav@240
  1163
        }
jaroslav@240
  1164
        return true;
jaroslav@240
  1165
    }
jtulach@938
  1166
jtulach@938
  1167
jtulach@0
  1168
    private boolean generateReceive(
jtulach@938
  1169
        Element clazz, StringWriter body, String className,
jaroslav@386
  1170
        List<? extends Element> enclosedElements, StringBuilder inType
jtulach@0
  1171
    ) {
jtulach@947
  1172
        boolean ret = generateReceiveImpl(clazz, body, className, enclosedElements, inType);
jtulach@947
  1173
        if (!ret) {
jtulach@947
  1174
            inType.setLength(0);
jtulach@947
  1175
        }
jtulach@947
  1176
        return ret;
jtulach@947
  1177
    }
jtulach@947
  1178
    private boolean generateReceiveImpl(
jtulach@947
  1179
        Element clazz, StringWriter body, String className,
jtulach@947
  1180
        List<? extends Element> enclosedElements, StringBuilder inType
jtulach@947
  1181
    ) {
jtulach@650
  1182
        inType.append("  @Override public void onMessage(").append(className).append(" model, int index, int type, Object data, Object[] params) {\n");
jaroslav@386
  1183
        inType.append("    switch (index) {\n");
jaroslav@386
  1184
        int index = 0;
jtulach@947
  1185
        boolean ok = true;
jtulach@1023
  1186
        boolean instance = clazz.getAnnotation(Model.class).instance();
jtulach@0
  1187
        for (Element m : enclosedElements) {
jtulach@0
  1188
            if (m.getKind() != ElementKind.METHOD) {
jtulach@0
  1189
                continue;
jtulach@0
  1190
            }
jtulach@0
  1191
            ExecutableElement e = (ExecutableElement)m;
jtulach@0
  1192
            OnReceive onR = e.getAnnotation(OnReceive.class);
jtulach@0
  1193
            if (onR == null) {
jtulach@0
  1194
                continue;
jtulach@0
  1195
            }
jtulach@1023
  1196
            if (!instance && !e.getModifiers().contains(Modifier.STATIC)) {
jtulach@0
  1197
                error("@OnReceive method needs to be static", e);
jtulach@0
  1198
                return false;
jtulach@0
  1199
            }
jtulach@0
  1200
            if (e.getModifiers().contains(Modifier.PRIVATE)) {
jtulach@0
  1201
                error("@OnReceive method cannot be private", e);
jtulach@0
  1202
                return false;
jtulach@0
  1203
            }
jtulach@0
  1204
            if (e.getReturnType().getKind() != TypeKind.VOID) {
jtulach@0
  1205
                error("@OnReceive method should return void", e);
jtulach@0
  1206
                return false;
jtulach@0
  1207
            }
jaroslav@73
  1208
            if (!onR.jsonp().isEmpty() && !"GET".equals(onR.method())) {
jaroslav@73
  1209
                error("JSONP works only with GET transport method", e);
jaroslav@73
  1210
            }
jaroslav@73
  1211
            String dataMirror = findDataSpecified(e, onR);
jaroslav@72
  1212
            if ("PUT".equals(onR.method()) && dataMirror == null) {
jaroslav@70
  1213
                error("PUT method needs to specify a data() class", e);
jaroslav@70
  1214
                return false;
jaroslav@70
  1215
            }
jaroslav@72
  1216
            if ("POST".equals(onR.method()) && dataMirror == null) {
jaroslav@70
  1217
                error("POST method needs to specify a data() class", e);
jaroslav@70
  1218
                return false;
jaroslav@70
  1219
            }
jtulach@649
  1220
            if (e.getParameters().size() < 2) {
jtulach@649
  1221
                error("@OnReceive method needs at least two parameters", e);
jtulach@649
  1222
            }
jtulach@947
  1223
            final boolean isWebSocket = "WebSocket".equals(onR.method());
jtulach@948
  1224
            if (isWebSocket && dataMirror == null) {
jtulach@948
  1225
                error("WebSocket method needs to specify a data() class", e);
jtulach@948
  1226
            }
jtulach@647
  1227
            int expectsList = 0;
jaroslav@100
  1228
            List<String> args = new ArrayList<String>();
jtulach@650
  1229
            List<String> params = new ArrayList<String>();
jtulach@649
  1230
            // first argument is model class
jtulach@649
  1231
            {
jtulach@649
  1232
                TypeMirror type = e.getParameters().get(0).asType();
jtulach@649
  1233
                CharSequence simpleName;
jtulach@649
  1234
                if (type.getKind() == TypeKind.DECLARED) {
jtulach@649
  1235
                    simpleName = ((DeclaredType) type).asElement().getSimpleName();
jtulach@649
  1236
                } else {
jtulach@649
  1237
                    simpleName = type.toString();
jtulach@649
  1238
                }
jtulach@649
  1239
                if (simpleName.toString().equals(className)) {
jtulach@649
  1240
                    args.add("model");
jtulach@649
  1241
                } else {
jtulach@649
  1242
                    error("First parameter needs to be " + className, e);
jtulach@649
  1243
                    return false;
jtulach@938
  1244
                }
jtulach@649
  1245
            }
jtulach@938
  1246
jtulach@649
  1247
            String modelClass;
jtulach@0
  1248
            {
jtulach@647
  1249
                final Types tu = processingEnv.getTypeUtils();
jtulach@649
  1250
                TypeMirror type = e.getParameters().get(1).asType();
jtulach@649
  1251
                TypeMirror modelType = null;
jtulach@649
  1252
                TypeMirror ert = tu.erasure(type);
jtulach@649
  1253
jtulach@649
  1254
                if (isModel(type)) {
jtulach@649
  1255
                    modelType = type;
jtulach@649
  1256
                } else if (type.getKind() == TypeKind.ARRAY) {
jtulach@649
  1257
                    modelType = ((ArrayType)type).getComponentType();
jtulach@649
  1258
                    expectsList = 1;
jtulach@649
  1259
                } else if ("java.util.List".equals(fqn(ert, e))) {
jtulach@649
  1260
                    List<? extends TypeMirror> typeArgs = ((DeclaredType)type).getTypeArguments();
jtulach@649
  1261
                    if (typeArgs.size() == 1) {
jtulach@649
  1262
                        modelType = typeArgs.get(0);
jtulach@649
  1263
                        expectsList = 2;
jaroslav@29
  1264
                    }
jtulach@649
  1265
                } else if (type.toString().equals("java.lang.String")) {
jtulach@649
  1266
                    modelType = type;
jtulach@0
  1267
                }
jtulach@649
  1268
                if (modelType == null) {
jtulach@649
  1269
                    error("Second arguments needs to be a model, String or array or List of models", e);
jtulach@649
  1270
                    return false;
jtulach@649
  1271
                }
jtulach@649
  1272
                modelClass = modelType.toString();
jtulach@649
  1273
                if (expectsList == 1) {
jtulach@649
  1274
                    args.add("arr");
jtulach@649
  1275
                } else if (expectsList == 2) {
jtulach@649
  1276
                    args.add("java.util.Arrays.asList(arr)");
jtulach@649
  1277
                } else {
jtulach@649
  1278
                    args.add("arr[0]");
jtulach@649
  1279
                }
jtulach@0
  1280
            }
jtulach@0
  1281
            String n = e.getSimpleName().toString();
jtulach@650
  1282
            if (isWebSocket) {
jaroslav@250
  1283
                body.append("  /** Performs WebSocket communication. Call with <code>null</code> data parameter\n");
jaroslav@250
  1284
                body.append("  * to open the connection (even if not required). Call with non-null data to\n");
jaroslav@250
  1285
                body.append("  * send messages to server. Call again with <code>null</code> data to close the socket.\n");
jaroslav@250
  1286
                body.append("  */\n");
jtulach@940
  1287
                if (onR.headers().length > 0) {
jtulach@940
  1288
                    error("WebSocket spec does not support headers", e);
jtulach@940
  1289
                }
jaroslav@250
  1290
            }
jaroslav@66
  1291
            body.append("  public void ").append(n).append("(");
jaroslav@24
  1292
            StringBuilder urlBefore = new StringBuilder();
jaroslav@24
  1293
            StringBuilder urlAfter = new StringBuilder();
jtulach@940
  1294
            StringBuilder headers = new StringBuilder();
jtulach@0
  1295
            String jsonpVarName = null;
jtulach@0
  1296
            {
jtulach@0
  1297
                String sep = "";
jtulach@0
  1298
                boolean skipJSONP = onR.jsonp().isEmpty();
jtulach@940
  1299
                Set<String> receiveParams = new LinkedHashSet<String>();
jtulach@940
  1300
                findParamNames(receiveParams, e, onR.url(), onR.jsonp(), urlBefore, urlAfter);
jtulach@940
  1301
                for (String headerLine : onR.headers()) {
jtulach@940
  1302
                    if (headerLine.contains("\r") || headerLine.contains("\n")) {
jtulach@940
  1303
                        error("Header line cannot contain line separator", e);
jtulach@940
  1304
                    }
jtulach@940
  1305
                    findParamNames(receiveParams, e, headerLine, null, headers);
jtulach@940
  1306
                    headers.append("+ \"\\r\\n\" +\n");
jtulach@940
  1307
                }
jtulach@940
  1308
                if (headers.length() > 0) {
jtulach@940
  1309
                    headers.append("\"\"");
jtulach@940
  1310
                }
jtulach@940
  1311
                for (String p : receiveParams) {
jtulach@0
  1312
                    if (!skipJSONP && p.equals(onR.jsonp())) {
jtulach@0
  1313
                        skipJSONP = true;
jtulach@0
  1314
                        jsonpVarName = p;
jtulach@0
  1315
                        continue;
jtulach@0
  1316
                    }
jtulach@0
  1317
                    body.append(sep);
jtulach@0
  1318
                    body.append("String ").append(p);
jtulach@0
  1319
                    sep = ", ";
jtulach@0
  1320
                }
jtulach@0
  1321
                if (!skipJSONP) {
jtulach@0
  1322
                    error(
jtulach@938
  1323
                        "Name of jsonp attribute ('" + onR.jsonp() +
jtulach@0
  1324
                        "') is not used in url attribute '" + onR.url() + "'", e
jtulach@0
  1325
                    );
jtulach@0
  1326
                }
jaroslav@73
  1327
                if (dataMirror != null) {
jaroslav@73
  1328
                    body.append(sep).append(dataMirror.toString()).append(" data");
jaroslav@73
  1329
                }
jtulach@650
  1330
                for (int i = 2; i < e.getParameters().size(); i++) {
jtulach@650
  1331
                    if (isWebSocket) {
jtulach@650
  1332
                        error("@OnReceive(method=\"WebSocket\") can only have two arguments", e);
jtulach@948
  1333
                        ok = false;
jtulach@650
  1334
                    }
jtulach@938
  1335
jtulach@650
  1336
                    VariableElement ve = e.getParameters().get(i);
jtulach@650
  1337
                    body.append(sep).append(ve.asType().toString()).append(" ").append(ve.getSimpleName());
jtulach@650
  1338
                    final String tp = ve.asType().toString();
jtulach@650
  1339
                    String btn = findBoxedType(tp);
jtulach@650
  1340
                    if (btn == null) {
jtulach@650
  1341
                        btn = tp;
jtulach@650
  1342
                    }
jtulach@650
  1343
                    args.add("(" + btn + ")params[" + (i - 2) + "]");
jtulach@650
  1344
                    params.add(ve.getSimpleName().toString());
jtulach@650
  1345
                    sep = ", ";
jtulach@650
  1346
                }
jtulach@0
  1347
            }
jtulach@0
  1348
            body.append(") {\n");
jaroslav@258
  1349
            boolean webSocket = onR.method().equals("WebSocket");
jaroslav@258
  1350
            if (webSocket) {
jtulach@940
  1351
                if (generateWSReceiveBody(index++, body, inType, onR, e, clazz, className, expectsList != 0, modelClass, n, args, params, urlBefore, jsonpVarName, urlAfter, dataMirror, headers)) {
jaroslav@387
  1352
                    return false;
jaroslav@387
  1353
                }
jaroslav@258
  1354
                body.append("  }\n");
jaroslav@387
  1355
                body.append("  private Object ws_" + e.getSimpleName() + ";\n");
jaroslav@258
  1356
            } else {
jtulach@940
  1357
                if (generateJSONReceiveBody(index++, body, inType, onR, e, clazz, className, expectsList != 0, modelClass, n, args, params, urlBefore, jsonpVarName, urlAfter, dataMirror, headers)) {
jtulach@947
  1358
                    ok = false;
jaroslav@258
  1359
                }
jaroslav@258
  1360
                body.append("  }\n");
jaroslav@258
  1361
            }
jaroslav@258
  1362
        }
jaroslav@386
  1363
        inType.append("    }\n");
jaroslav@386
  1364
        inType.append("    throw new UnsupportedOperationException(\"index: \" + index + \" type: \" + type);\n");
jaroslav@386
  1365
        inType.append("  }\n");
jtulach@947
  1366
        return ok;
jaroslav@258
  1367
    }
jaroslav@258
  1368
jtulach@940
  1369
    private boolean generateJSONReceiveBody(int index, StringWriter method, StringBuilder body, OnReceive onR, ExecutableElement e, Element clazz, String className, boolean expectsList, String modelClass, String n, List<String> args, List<String> params, StringBuilder urlBefore, String jsonpVarName, StringBuilder urlAfter, String dataMirror, StringBuilder headers) {
jtulach@947
  1370
        boolean error = false;
jaroslav@258
  1371
        body.append(
jaroslav@386
  1372
            "    case " + index + ": {\n" +
jaroslav@386
  1373
            "      if (type == 2) { /* on error */\n" +
jaroslav@386
  1374
            "        Exception ex = (Exception)data;\n"
jaroslav@258
  1375
            );
jaroslav@258
  1376
        if (onR.onError().isEmpty()) {
jtulach@0
  1377
            body.append(
jaroslav@386
  1378
                "        ex.printStackTrace();\n"
jaroslav@245
  1379
                );
jaroslav@258
  1380
        } else {
jtulach@947
  1381
            error = !findOnError(e, ((TypeElement)clazz), onR.onError(), className);
jaroslav@258
  1382
            body.append("        ").append(clazz.getSimpleName()).append(".").append(onR.onError()).append("(");
jaroslav@386
  1383
            body.append("model, ex);\n");
jaroslav@258
  1384
        }
jaroslav@258
  1385
        body.append(
jaroslav@386
  1386
            "        return;\n" +
jaroslav@386
  1387
            "      } else if (type == 1) {\n" +
jaroslav@386
  1388
            "        Object[] ev = (Object[])data;\n"
jaroslav@258
  1389
            );
jaroslav@258
  1390
        if (expectsList) {
jaroslav@245
  1391
            body.append(
jaroslav@386
  1392
                "        " + modelClass + "[] arr = new " + modelClass + "[ev.length];\n"
jaroslav@258
  1393
                );
jaroslav@258
  1394
        } else {
jaroslav@258
  1395
            body.append(
jaroslav@258
  1396
                "        " + modelClass + "[] arr = { null };\n"
jaroslav@258
  1397
                );
jaroslav@258
  1398
        }
jaroslav@258
  1399
        body.append(
jaroslav@386
  1400
            "        TYPE.copyJSON(model.proto.getContext(), ev, " + modelClass + ".class, arr);\n"
jaroslav@386
  1401
        );
jaroslav@258
  1402
        {
jaroslav@258
  1403
            body.append("        ").append(clazz.getSimpleName()).append(".").append(n).append("(");
jaroslav@258
  1404
            String sep = "";
jaroslav@258
  1405
            for (String arg : args) {
jaroslav@258
  1406
                body.append(sep);
jaroslav@258
  1407
                body.append(arg);
jaroslav@258
  1408
                sep = ", ";
jaroslav@74
  1409
            }
jaroslav@24
  1410
            body.append(");\n");
jtulach@0
  1411
        }
jaroslav@258
  1412
        body.append(
jaroslav@386
  1413
            "        return;\n" +
jaroslav@258
  1414
            "      }\n" +
jaroslav@258
  1415
            "    }\n"
jaroslav@258
  1416
            );
jtulach@940
  1417
        method.append("    proto.loadJSONWithHeaders(" + index + ",\n        ");
jtulach@940
  1418
        method.append(headers.length() == 0 ? "null" : headers).append(",\n        ");
jaroslav@386
  1419
        method.append(urlBefore).append(", ");
jaroslav@258
  1420
        if (jsonpVarName != null) {
jaroslav@386
  1421
            method.append(urlAfter);
jaroslav@258
  1422
        } else {
jaroslav@386
  1423
            method.append("null");
jaroslav@258
  1424
        }
jaroslav@258
  1425
        if (!"GET".equals(onR.method()) || dataMirror != null) {
jaroslav@386
  1426
            method.append(", \"").append(onR.method()).append('"');
jaroslav@258
  1427
            if (dataMirror != null) {
jaroslav@386
  1428
                method.append(", data");
jaroslav@258
  1429
            } else {
jaroslav@386
  1430
                method.append(", null");
jaroslav@258
  1431
            }
jaroslav@258
  1432
        } else {
jaroslav@386
  1433
            method.append(", null, null");
jaroslav@258
  1434
        }
jtulach@650
  1435
        for (String a : params) {
jtulach@650
  1436
            method.append(", ").append(a);
jtulach@650
  1437
        }
jaroslav@386
  1438
        method.append(");\n");
jtulach@947
  1439
        return error;
jaroslav@258
  1440
    }
jtulach@938
  1441
jtulach@940
  1442
    private boolean generateWSReceiveBody(int index, StringWriter method, StringBuilder body, OnReceive onR, ExecutableElement e, Element clazz, String className, boolean expectsList, String modelClass, String n, List<String> args, List<String> params, StringBuilder urlBefore, String jsonpVarName, StringBuilder urlAfter, String dataMirror, StringBuilder headers) {
jaroslav@258
  1443
        body.append(
jaroslav@387
  1444
            "    case " + index + ": {\n" +
jaroslav@387
  1445
            "      if (type == 0) { /* on open */\n" +
jtulach@1023
  1446
            "        ").append(inPckName(clazz, true)).append(".").append(n).append("(");
jaroslav@258
  1447
        {
jaroslav@258
  1448
            String sep = "";
jaroslav@258
  1449
            for (String arg : args) {
jaroslav@258
  1450
                body.append(sep);
jtulach@650
  1451
                if (arg.startsWith("arr") || arg.startsWith("java.util.Array")) {
jaroslav@258
  1452
                    body.append("null");
jaroslav@258
  1453
                } else {
jaroslav@258
  1454
                    body.append(arg);
jaroslav@258
  1455
                }
jaroslav@258
  1456
                sep = ", ";
jaroslav@258
  1457
            }
jaroslav@258
  1458
        }
jaroslav@258
  1459
        body.append(");\n");
jaroslav@258
  1460
        body.append(
jaroslav@387
  1461
            "        return;\n" +
jaroslav@387
  1462
            "      } else if (type == 2) { /* on error */\n" +
jaroslav@387
  1463
            "        Exception value = (Exception)data;\n"
jaroslav@258
  1464
            );
jaroslav@258
  1465
        if (onR.onError().isEmpty()) {
jaroslav@258
  1466
            body.append(
jaroslav@258
  1467
                "        value.printStackTrace();\n"
jaroslav@258
  1468
                );
jaroslav@258
  1469
        } else {
jaroslav@258
  1470
            if (!findOnError(e, ((TypeElement)clazz), onR.onError(), className)) {
jaroslav@258
  1471
                return true;
jaroslav@258
  1472
            }
jtulach@1023
  1473
            body.append("        ").append(inPckName(clazz, true)).append(".").append(onR.onError()).append("(");
jaroslav@387
  1474
            body.append("model, value);\n");
jaroslav@258
  1475
        }
jaroslav@258
  1476
        body.append(
jaroslav@387
  1477
            "        return;\n" +
jaroslav@387
  1478
            "      } else if (type == 1) {\n" +
jaroslav@387
  1479
            "        Object[] ev = (Object[])data;\n"
jaroslav@258
  1480
        );
jaroslav@258
  1481
        if (expectsList) {
jaroslav@258
  1482
            body.append(
jaroslav@387
  1483
                "        " + modelClass + "[] arr = new " + modelClass + "[ev.length];\n"
jaroslav@258
  1484
                );
jaroslav@258
  1485
        } else {
jaroslav@258
  1486
            body.append(
jaroslav@258
  1487
                "        " + modelClass + "[] arr = { null };\n"
jaroslav@258
  1488
                );
jaroslav@258
  1489
        }
jaroslav@258
  1490
        body.append(
jaroslav@387
  1491
            "        TYPE.copyJSON(model.proto.getContext(), ev, " + modelClass + ".class, arr);\n"
jaroslav@387
  1492
        );
jaroslav@258
  1493
        {
jtulach@1023
  1494
            body.append("        ").append(inPckName(clazz, true)).append(".").append(n).append("(");
jaroslav@258
  1495
            String sep = "";
jaroslav@258
  1496
            for (String arg : args) {
jaroslav@258
  1497
                body.append(sep);
jaroslav@258
  1498
                body.append(arg);
jaroslav@258
  1499
                sep = ", ";
jaroslav@258
  1500
            }
jaroslav@258
  1501
            body.append(");\n");
jaroslav@258
  1502
        }
jaroslav@258
  1503
        body.append(
jaroslav@387
  1504
            "        return;\n" +
jaroslav@387
  1505
            "      }"
jaroslav@258
  1506
        );
jaroslav@258
  1507
        if (!onR.onError().isEmpty()) {
jaroslav@387
  1508
            body.append(" else if (type == 3) { /* on close */\n");
jtulach@1023
  1509
            body.append("        ").append(inPckName(clazz, true)).append(".").append(onR.onError()).append("(");
jaroslav@387
  1510
            body.append("model, null);\n");
jaroslav@258
  1511
            body.append(
jaroslav@397
  1512
                "        return;" +
jaroslav@387
  1513
                "      }"
jaroslav@258
  1514
            );
jaroslav@258
  1515
        }
jaroslav@387
  1516
        body.append("\n");
jaroslav@258
  1517
        body.append("    }\n");
jaroslav@387
  1518
        method.append("    if (this.ws_").append(e.getSimpleName()).append(" == null) {\n");
jaroslav@387
  1519
        method.append("      this.ws_").append(e.getSimpleName());
jaroslav@387
  1520
        method.append("= proto.wsOpen(" + index + ", ");
jaroslav@387
  1521
        method.append(urlBefore).append(", data);\n");
jaroslav@387
  1522
        method.append("    } else {\n");
jtulach@650
  1523
        method.append("      proto.wsSend(this.ws_").append(e.getSimpleName()).append(", ").append(urlBefore).append(", data");
jtulach@650
  1524
        for (String a : params) {
jtulach@650
  1525
            method.append(", ").append(a);
jtulach@650
  1526
        }
jtulach@650
  1527
        method.append(");\n");
jaroslav@387
  1528
        method.append("    }\n");
jaroslav@258
  1529
        return false;
jtulach@0
  1530
    }
jtulach@0
  1531
jtulach@0
  1532
    private CharSequence wrapParams(
jaroslav@597
  1533
        ExecutableElement ee, String id, String className, String classRef, String evName, String dataName
jtulach@0
  1534
    ) {
jtulach@0
  1535
        TypeMirror stringType = processingEnv.getElementUtils().getTypeElement("java.lang.String").asType();
jtulach@0
  1536
        StringBuilder params = new StringBuilder();
jtulach@0
  1537
        boolean first = true;
jtulach@0
  1538
        for (VariableElement ve : ee.getParameters()) {
jtulach@0
  1539
            if (!first) {
jtulach@0
  1540
                params.append(", ");
jtulach@0
  1541
            }
jtulach@0
  1542
            first = false;
jtulach@0
  1543
            String toCall = null;
jaroslav@22
  1544
            String toFinish = null;
jaroslav@408
  1545
            boolean addNull = true;
jtulach@0
  1546
            if (ve.asType() == stringType) {
jtulach@0
  1547
                if (ve.getSimpleName().contentEquals("id")) {
jtulach@0
  1548
                    params.append('"').append(id).append('"');
jtulach@0
  1549
                    continue;
jtulach@0
  1550
                }
jaroslav@597
  1551
                toCall = classRef + ".proto.toString(";
jtulach@0
  1552
            }
jtulach@0
  1553
            if (ve.asType().getKind() == TypeKind.DOUBLE) {
jaroslav@597
  1554
                toCall = classRef + ".proto.toNumber(";
jaroslav@22
  1555
                toFinish = ".doubleValue()";
jtulach@0
  1556
            }
jtulach@930
  1557
            if (ve.asType().getKind() == TypeKind.FLOAT) {
jtulach@930
  1558
                toCall = classRef + ".proto.toNumber(";
jtulach@930
  1559
                toFinish = ".floatValue()";
jtulach@930
  1560
            }
jtulach@0
  1561
            if (ve.asType().getKind() == TypeKind.INT) {
jaroslav@597
  1562
                toCall = classRef + ".proto.toNumber(";
jaroslav@22
  1563
                toFinish = ".intValue()";
jtulach@0
  1564
            }
jtulach@930
  1565
            if (ve.asType().getKind() == TypeKind.BYTE) {
jtulach@930
  1566
                toCall = classRef + ".proto.toNumber(";
jtulach@930
  1567
                toFinish = ".byteValue()";
jtulach@930
  1568
            }
jtulach@930
  1569
            if (ve.asType().getKind() == TypeKind.SHORT) {
jtulach@930
  1570
                toCall = classRef + ".proto.toNumber(";
jtulach@930
  1571
                toFinish = ".shortValue()";
jtulach@930
  1572
            }
jtulach@930
  1573
            if (ve.asType().getKind() == TypeKind.LONG) {
jtulach@930
  1574
                toCall = classRef + ".proto.toNumber(";
jtulach@930
  1575
                toFinish = ".longValue()";
jtulach@930
  1576
            }
jtulach@930
  1577
            if (ve.asType().getKind() == TypeKind.BOOLEAN) {
jtulach@930
  1578
                toCall = "\"true\".equals(" + classRef + ".proto.toString(";
jtulach@930
  1579
                toFinish = ")";
jtulach@930
  1580
            }
jtulach@930
  1581
            if (ve.asType().getKind() == TypeKind.CHAR) {
jtulach@930
  1582
                toCall = "(char)" + classRef + ".proto.toNumber(";
jtulach@930
  1583
                toFinish = ".intValue()";
jtulach@930
  1584
            }
jtulach@0
  1585
            if (dataName != null && ve.getSimpleName().contentEquals(dataName) && isModel(ve.asType())) {
jaroslav@597
  1586
                toCall = classRef + ".proto.toModel(" + ve.asType() + ".class, ";
jaroslav@408
  1587
                addNull = false;
jtulach@0
  1588
            }
jtulach@0
  1589
jtulach@0
  1590
            if (toCall != null) {
jtulach@0
  1591
                params.append(toCall);
jtulach@0
  1592
                if (dataName != null && ve.getSimpleName().contentEquals(dataName)) {
jtulach@0
  1593
                    params.append(dataName);
jaroslav@408
  1594
                    if (addNull) {
jaroslav@408
  1595
                        params.append(", null");
jaroslav@408
  1596
                    }
jtulach@0
  1597
                } else {
jtulach@0
  1598
                    if (evName == null) {
jtulach@0
  1599
                        final StringBuilder sb = new StringBuilder();
jtulach@0
  1600
                        sb.append("Unexpected string parameter name.");
jtulach@0
  1601
                        if (dataName != null) {
jtulach@0
  1602
                            sb.append(" Try \"").append(dataName).append("\"");
jtulach@0
  1603
                        }
jtulach@0
  1604
                        error(sb.toString(), ee);
jtulach@0
  1605
                    }
jtulach@0
  1606
                    params.append(evName);
jtulach@0
  1607
                    params.append(", \"");
jtulach@0
  1608
                    params.append(ve.getSimpleName().toString());
jtulach@0
  1609
                    params.append("\"");
jtulach@0
  1610
                }
jtulach@0
  1611
                params.append(")");
jaroslav@22
  1612
                if (toFinish != null) {
jaroslav@22
  1613
                    params.append(toFinish);
jaroslav@22
  1614
                }
jtulach@0
  1615
                continue;
jtulach@0
  1616
            }
jtulach@0
  1617
            String rn = fqn(ve.asType(), ee);
jtulach@0
  1618
            int last = rn.lastIndexOf('.');
jtulach@0
  1619
            if (last >= 0) {
jtulach@0
  1620
                rn = rn.substring(last + 1);
jtulach@0
  1621
            }
jtulach@0
  1622
            if (rn.equals(className)) {
jaroslav@597
  1623
                params.append(classRef);
jtulach@0
  1624
                continue;
jtulach@0
  1625
            }
jtulach@930
  1626
            StringBuilder err = new StringBuilder();
jtulach@930
  1627
            err.append("Argument ").
jtulach@930
  1628
                append(ve.getSimpleName()).
jtulach@930
  1629
                append(" is not valid. The annotated method can only accept ").
jtulach@930
  1630
                append(className).
jtulach@930
  1631
                append(" argument");
jtulach@930
  1632
            if (dataName != null) {
jtulach@930
  1633
                err.append(" or argument named '").append(dataName).append("'");
jtulach@930
  1634
            }
jtulach@930
  1635
            err.append(".");
jtulach@930
  1636
            error(err.toString(), ee);
jtulach@0
  1637
        }
jtulach@0
  1638
        return params;
jtulach@0
  1639
    }
jtulach@938
  1640
jtulach@938
  1641
jtulach@0
  1642
    private CharSequence wrapPropName(
jtulach@0
  1643
        ExecutableElement ee, String className, String propName, String propValue
jtulach@0
  1644
    ) {
jtulach@0
  1645
        TypeMirror stringType = processingEnv.getElementUtils().getTypeElement("java.lang.String").asType();
jtulach@0
  1646
        StringBuilder params = new StringBuilder();
jtulach@0
  1647
        boolean first = true;
jtulach@0
  1648
        for (VariableElement ve : ee.getParameters()) {
jtulach@0
  1649
            if (!first) {
jtulach@0
  1650
                params.append(", ");
jtulach@0
  1651
            }
jtulach@0
  1652
            first = false;
jtulach@0
  1653
            if (ve.asType() == stringType) {
jtulach@0
  1654
                if (propName != null && ve.getSimpleName().contentEquals(propName)) {
jtulach@0
  1655
                    params.append('"').append(propValue).append('"');
jtulach@0
  1656
                } else {
jtulach@0
  1657
                    error("Unexpected string parameter name. Try \"" + propName + "\".", ee);
jtulach@0
  1658
                }
jtulach@0
  1659
                continue;
jtulach@0
  1660
            }
jtulach@0
  1661
            String rn = fqn(ve.asType(), ee);
jtulach@0
  1662
            int last = rn.lastIndexOf('.');
jtulach@0
  1663
            if (last >= 0) {
jtulach@0
  1664
                rn = rn.substring(last + 1);
jtulach@0
  1665
            }
jtulach@0
  1666
            if (rn.equals(className)) {
jaroslav@383
  1667
                params.append("model");
jtulach@0
  1668
                continue;
jtulach@0
  1669
            }
jtulach@0
  1670
            error(
jtulach@0
  1671
                "@OnPrprtChange method can only accept String or " + className + " arguments",
jtulach@0
  1672
                ee);
jtulach@0
  1673
        }
jtulach@0
  1674
        return params;
jtulach@0
  1675
    }
jtulach@938
  1676
jtulach@0
  1677
    private boolean isModel(TypeMirror tm) {
jaroslav@13
  1678
        if (tm.getKind() == TypeKind.ERROR) {
jaroslav@13
  1679
            return true;
jaroslav@13
  1680
        }
jtulach@0
  1681
        final Element e = processingEnv.getTypeUtils().asElement(tm);
jtulach@0
  1682
        if (e == null) {
jtulach@0
  1683
            return false;
jtulach@0
  1684
        }
jtulach@0
  1685
        for (Element ch : e.getEnclosedElements()) {
jtulach@0
  1686
            if (ch.getKind() == ElementKind.METHOD) {
jtulach@0
  1687
                ExecutableElement ee = (ExecutableElement)ch;
jtulach@0
  1688
                if (ee.getParameters().isEmpty() && ee.getSimpleName().contentEquals("modelFor")) {
jtulach@0
  1689
                    return true;
jtulach@0
  1690
                }
jtulach@0
  1691
            }
jtulach@0
  1692
        }
jtulach@0
  1693
        return models.values().contains(e.getSimpleName().toString());
jtulach@0
  1694
    }
jtulach@938
  1695
jtulach@0
  1696
    private void writeToString(Prprt[] props, Writer w) throws IOException {
jtulach@0
  1697
        w.write("  public String toString() {\n");
jtulach@0
  1698
        w.write("    StringBuilder sb = new StringBuilder();\n");
jtulach@0
  1699
        w.write("    sb.append('{');\n");
jtulach@0
  1700
        String sep = "";
jtulach@0
  1701
        for (Prprt p : props) {
jtulach@0
  1702
            w.write(sep);
jaroslav@18
  1703
            w.append("    sb.append('\"').append(\"" + p.name() + "\")");
jaroslav@18
  1704
                w.append(".append('\"').append(\":\");\n");
jaroslav@380
  1705
            w.append("    sb.append(TYPE.toJSON(prop_");
jtulach@0
  1706
            w.append(p.name()).append("));\n");
jtulach@0
  1707
            sep =    "    sb.append(',');\n";
jtulach@0
  1708
        }
jtulach@0
  1709
        w.write("    sb.append('}');\n");
jtulach@0
  1710
        w.write("    return sb.toString();\n");
jtulach@0
  1711
        w.write("  }\n");
jtulach@0
  1712
    }
jtulach@0
  1713
    private void writeClone(String className, Prprt[] props, Writer w) throws IOException {
jtulach@0
  1714
        w.write("  public " + className + " clone() {\n");
jaroslav@373
  1715
        w.write("    return clone(proto.getContext());\n");
jaroslav@111
  1716
        w.write("  }\n");
jaroslav@111
  1717
        w.write("  private " + className + " clone(net.java.html.BrwsrCtx ctx) {\n");
jaroslav@111
  1718
        w.write("    " + className + " ret = new " + className + "(ctx);\n");
jtulach@0
  1719
        for (Prprt p : props) {
jtulach@949
  1720
            String tn = typeName(p);
jtulach@949
  1721
            String[] gs = toGetSet(p.name(), tn, p.array());
jtulach@0
  1722
            if (!p.array()) {
jtulach@0
  1723
                boolean isModel[] = { false };
jtulach@0
  1724
                boolean isEnum[] = { false };
jtulach@0
  1725
                boolean isPrimitive[] = { false };
jtulach@0
  1726
                checkType(p, isModel, isEnum, isPrimitive);
jtulach@0
  1727
                if (!isModel[0]) {
jtulach@949
  1728
                    w.write("    ret.prop_" + p.name() + " = " + gs[0] + "();\n");
jtulach@0
  1729
                    continue;
jtulach@0
  1730
                }
jtulach@1039
  1731
                w.write("    ret.prop_" + p.name() + " =  " + gs[0] + "()  == null ? null : " + gs[0] + "().clone();\n");
jtulach@0
  1732
            } else {
jtulach@949
  1733
                w.write("    proto.cloneList(ret." + gs[0] + "(), ctx, prop_" + p.name() + ");\n");
jtulach@0
  1734
            }
jtulach@0
  1735
        }
jtulach@938
  1736
jtulach@0
  1737
        w.write("    return ret;\n");
jtulach@0
  1738
        w.write("  }\n");
jtulach@0
  1739
    }
jtulach@0
  1740
jtulach@1023
  1741
    private String inPckName(Element e, boolean preferInstance) {
jtulach@1023
  1742
        if (preferInstance && e.getAnnotation(Model.class).instance()) {
jtulach@1023
  1743
            return "model.instance";
jtulach@1023
  1744
        }
jtulach@0
  1745
        StringBuilder sb = new StringBuilder();
jtulach@0
  1746
        while (e.getKind() != ElementKind.PACKAGE) {
jtulach@0
  1747
            if (sb.length() == 0) {
jtulach@0
  1748
                sb.append(e.getSimpleName());
jtulach@0
  1749
            } else {
jtulach@0
  1750
                sb.insert(0, '.');
jtulach@0
  1751
                sb.insert(0, e.getSimpleName());
jtulach@0
  1752
            }
jtulach@0
  1753
            e = e.getEnclosingElement();
jtulach@0
  1754
        }
jtulach@0
  1755
        return sb.toString();
jtulach@0
  1756
    }
jtulach@0
  1757
jtulach@0
  1758
    private String fqn(TypeMirror pt, Element relative) {
jtulach@0
  1759
        if (pt.getKind() == TypeKind.ERROR) {
jtulach@0
  1760
            final Elements eu = processingEnv.getElementUtils();
jtulach@0
  1761
            PackageElement pckg = eu.getPackageOf(relative);
jtulach@0
  1762
            return pckg.getQualifiedName() + "." + pt.toString();
jtulach@0
  1763
        }
jtulach@0
  1764
        return pt.toString();
jtulach@0
  1765
    }
jtulach@0
  1766
jtulach@0
  1767
    private String checkType(Prprt p, boolean[] isModel, boolean[] isEnum, boolean[] isPrimitive) {
jtulach@0
  1768
        TypeMirror tm;
jtulach@0
  1769
        try {
jtulach@0
  1770
            String ret = p.typeName(processingEnv);
jtulach@0
  1771
            TypeElement e = processingEnv.getElementUtils().getTypeElement(ret);
jtulach@0
  1772
            if (e == null) {
jtulach@0
  1773
                isModel[0] = true;
jtulach@0
  1774
                isEnum[0] = false;
jtulach@0
  1775
                isPrimitive[0] = false;
jtulach@0
  1776
                return ret;
jtulach@0
  1777
            }
jtulach@0
  1778
            tm = e.asType();
jtulach@0
  1779
        } catch (MirroredTypeException ex) {
jtulach@0
  1780
            tm = ex.getTypeMirror();
jtulach@0
  1781
        }
jtulach@0
  1782
        tm = processingEnv.getTypeUtils().erasure(tm);
jaroslav@241
  1783
        if (isPrimitive[0] = tm.getKind().isPrimitive()) {
jaroslav@241
  1784
            isEnum[0] = false;
jaroslav@241
  1785
            isModel[0] = false;
jaroslav@241
  1786
            return tm.toString();
jaroslav@241
  1787
        }
jtulach@0
  1788
        final Element e = processingEnv.getTypeUtils().asElement(tm);
jaroslav@241
  1789
        if (e.getKind() == ElementKind.CLASS && tm.getKind() == TypeKind.ERROR) {
jaroslav@241
  1790
            isModel[0] = true;
jaroslav@241
  1791
            isEnum[0] = false;
jaroslav@242
  1792
            return e.getSimpleName().toString();
jaroslav@241
  1793
        }
jtulach@938
  1794
jtulach@0
  1795
        final Model m = e == null ? null : e.getAnnotation(Model.class);
jtulach@0
  1796
        String ret;
jtulach@0
  1797
        if (m != null) {
jtulach@0
  1798
            ret = findPkgName(e) + '.' + m.className();
jtulach@0
  1799
            isModel[0] = true;
jtulach@0
  1800
            models.put(e, m.className());
jtulach@0
  1801
        } else if (findModelForMthd(e)) {
jtulach@0
  1802
            ret = ((TypeElement)e).getQualifiedName().toString();
jtulach@0
  1803
            isModel[0] = true;
jtulach@0
  1804
        } else {
jtulach@0
  1805
            ret = tm.toString();
jtulach@0
  1806
        }
jtulach@0
  1807
        TypeMirror enm = processingEnv.getElementUtils().getTypeElement("java.lang.Enum").asType();
jtulach@0
  1808
        enm = processingEnv.getTypeUtils().erasure(enm);
jtulach@0
  1809
        isEnum[0] = processingEnv.getTypeUtils().isSubtype(tm, enm);
jtulach@0
  1810
        return ret;
jtulach@0
  1811
    }
jtulach@938
  1812
jtulach@0
  1813
    private static boolean findModelForMthd(Element clazz) {
jtulach@0
  1814
        if (clazz == null) {
jtulach@0
  1815
            return false;
jtulach@0
  1816
        }
jtulach@0
  1817
        for (Element e : clazz.getEnclosedElements()) {
jtulach@0
  1818
            if (e.getKind() == ElementKind.METHOD) {
jtulach@0
  1819
                ExecutableElement ee = (ExecutableElement)e;
jtulach@0
  1820
                if (
jtulach@0
  1821
                    ee.getSimpleName().contentEquals("modelFor") &&
jtulach@0
  1822
                    ee.getParameters().isEmpty()
jtulach@0
  1823
                ) {
jtulach@0
  1824
                    return true;
jtulach@0
  1825
                }
jtulach@0
  1826
            }
jtulach@0
  1827
        }
jtulach@0
  1828
        return false;
jtulach@0
  1829
    }
jtulach@0
  1830
jtulach@940
  1831
    private void findParamNames(
jtulach@940
  1832
        Set<String> params, Element e, String url, String jsonParam, StringBuilder... both
jaroslav@24
  1833
    ) {
jaroslav@24
  1834
        int wasJSON = 0;
jtulach@0
  1835
jtulach@0
  1836
        for (int pos = 0; ;) {
jtulach@0
  1837
            int next = url.indexOf('{', pos);
jtulach@0
  1838
            if (next == -1) {
jaroslav@24
  1839
                both[wasJSON].append('"')
jtulach@940
  1840
                    .append(url.substring(pos).replace("\"", "\\\""))
jtulach@0
  1841
                    .append('"');
jtulach@940
  1842
                return;
jtulach@0
  1843
            }
jtulach@0
  1844
            int close = url.indexOf('}', next);
jtulach@0
  1845
            if (close == -1) {
jtulach@0
  1846
                error("Unbalanced '{' and '}' in " + url, e);
jtulach@940
  1847
                return;
jtulach@0
  1848
            }
jtulach@0
  1849
            final String paramName = url.substring(next + 1, close);
jtulach@940
  1850
            params.add(paramName);
jtulach@940
  1851
            if (paramName.equals(jsonParam) && !jsonParam.isEmpty()) {
jtulach@940
  1852
                both[wasJSON].append('"')
jtulach@940
  1853
                    .append(url.substring(pos, next).replace("\"", "\\\""))
jtulach@940
  1854
                    .append('"');
jtulach@940
  1855
                wasJSON = 1;
jtulach@940
  1856
            } else {
jtulach@940
  1857
                both[wasJSON].append('"')
jtulach@940
  1858
                    .append(url.substring(pos, next).replace("\"", "\\\""))
jtulach@940
  1859
                    .append("\" + ").append(paramName).append(" + ");
jaroslav@24
  1860
            }
jtulach@0
  1861
            pos = close + 1;
jtulach@0
  1862
        }
jtulach@0
  1863
    }
jtulach@0
  1864
jtulach@0
  1865
    private static Prprt findPrprt(Prprt[] properties, String propName) {
jtulach@0
  1866
        for (Prprt p : properties) {
jtulach@0
  1867
            if (propName.equals(p.name())) {
jtulach@0
  1868
                return p;
jtulach@0
  1869
            }
jtulach@0
  1870
        }
jtulach@0
  1871
        return null;
jtulach@0
  1872
    }
jtulach@0
  1873
jtulach@0
  1874
    private boolean isPrimitive(String type) {
jtulach@938
  1875
        return
jtulach@0
  1876
            "int".equals(type) ||
jtulach@0
  1877
            "double".equals(type) ||
jtulach@0
  1878
            "long".equals(type) ||
jtulach@0
  1879
            "short".equals(type) ||
jtulach@0
  1880
            "byte".equals(type) ||
jaroslav@100
  1881
            "char".equals(type) ||
jaroslav@100
  1882
            "boolean".equals(type) ||
jtulach@0
  1883
            "float".equals(type);
jtulach@0
  1884
    }
jtulach@0
  1885
jaroslav@567
  1886
    private static Collection<String> findDerivedFrom(Map<String, Collection<String[]>> propsDeps, String derivedProp) {
jaroslav@100
  1887
        Set<String> names = new HashSet<String>();
jaroslav@567
  1888
        for (Map.Entry<String, Collection<String[]>> e : propsDeps.entrySet()) {
jaroslav@567
  1889
            for (String[] pair : e.getValue()) {
jaroslav@567
  1890
                if (pair[0].equals(derivedProp)) {
jaroslav@567
  1891
                    names.add(e.getKey());
jaroslav@567
  1892
                    break;
jaroslav@567
  1893
                }
jtulach@0
  1894
            }
jtulach@0
  1895
        }
jtulach@0
  1896
        return names;
jtulach@0
  1897
    }
jtulach@938
  1898
jtulach@0
  1899
    private Prprt[] createProps(Element e, Property[] arr) {
jtulach@0
  1900
        Prprt[] ret = Prprt.wrap(processingEnv, e, arr);
jtulach@0
  1901
        Prprt[] prev = verify.put(e, ret);
jtulach@0
  1902
        if (prev != null) {
jtulach@0
  1903
            error("Two sets of properties for ", e);
jtulach@0
  1904
        }
jtulach@0
  1905
        return ret;
jtulach@0
  1906
    }
jtulach@938
  1907
jaroslav@17
  1908
    private static String strip(String s) {
jaroslav@17
  1909
        int indx = s.indexOf("__");
jaroslav@17
  1910
        if (indx >= 0) {
jaroslav@17
  1911
            return s.substring(0, indx);
jaroslav@17
  1912
        } else {
jaroslav@17
  1913
            return s;
jaroslav@17
  1914
        }
jaroslav@17
  1915
    }
jaroslav@70
  1916
jaroslav@73
  1917
    private String findDataSpecified(ExecutableElement e, OnReceive onR) {
jaroslav@73
  1918
        try {
jaroslav@73
  1919
            return onR.data().getName();
jaroslav@73
  1920
        } catch (MirroredTypeException ex) {
jaroslav@242
  1921
            final TypeMirror tm = ex.getTypeMirror();
jaroslav@242
  1922
            String name;
jaroslav@242
  1923
            final Element te = processingEnv.getTypeUtils().asElement(tm);
jaroslav@242
  1924
            if (te.getKind() == ElementKind.CLASS && tm.getKind() == TypeKind.ERROR) {
jaroslav@242
  1925
                name = te.getSimpleName().toString();
jaroslav@242
  1926
            } else {
jaroslav@242
  1927
                name = tm.toString();
jaroslav@242
  1928
            }
jaroslav@73
  1929
            return "java.lang.Object".equals(name) ? null : name;
jaroslav@73
  1930
        } catch (Exception ex) {
jaroslav@73
  1931
            // fallback
jaroslav@73
  1932
        }
jtulach@938
  1933
jaroslav@72
  1934
        AnnotationMirror found = null;
jaroslav@72
  1935
        for (AnnotationMirror am : e.getAnnotationMirrors()) {
jaroslav@72
  1936
            if (am.getAnnotationType().toString().equals(OnReceive.class.getName())) {
jaroslav@72
  1937
                found = am;
jaroslav@72
  1938
            }
jaroslav@72
  1939
        }
jaroslav@72
  1940
        if (found == null) {
jaroslav@72
  1941
            return null;
jaroslav@72
  1942
        }
jtulach@938
  1943
jaroslav@72
  1944
        for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : found.getElementValues().entrySet()) {
jaroslav@72
  1945
            ExecutableElement ee = entry.getKey();
jaroslav@72
  1946
            AnnotationValue av = entry.getValue();
jaroslav@72
  1947
            if (ee.getSimpleName().contentEquals("data")) {
jaroslav@73
  1948
                List<? extends Object> values = getAnnoValues(processingEnv, e, found);
jaroslav@73
  1949
                for (Object v : values) {
jaroslav@73
  1950
                    String sv = v.toString();
jaroslav@73
  1951
                    if (sv.startsWith("data = ") && sv.endsWith(".class")) {
jaroslav@73
  1952
                        return sv.substring(7, sv.length() - 6);
jaroslav@73
  1953
                    }
jaroslav@73
  1954
                }
jaroslav@73
  1955
                return "error";
jaroslav@72
  1956
            }
jaroslav@72
  1957
        }
jaroslav@72
  1958
        return null;
jaroslav@72
  1959
    }
jaroslav@72
  1960
jaroslav@72
  1961
    static List<? extends Object> getAnnoValues(ProcessingEnvironment pe, Element e, AnnotationMirror am) {
jaroslav@70
  1962
        try {
jaroslav@72
  1963
            Class<?> trees = Class.forName("com.sun.tools.javac.api.JavacTrees");
jaroslav@72
  1964
            Method m = trees.getMethod("instance", ProcessingEnvironment.class);
jaroslav@72
  1965
            Object instance = m.invoke(null, pe);
jaroslav@72
  1966
            m = instance.getClass().getMethod("getPath", Element.class, AnnotationMirror.class);
jaroslav@72
  1967
            Object path = m.invoke(instance, e, am);
jaroslav@72
  1968
            m = path.getClass().getMethod("getLeaf");
jaroslav@72
  1969
            Object leaf = m.invoke(path);
jaroslav@72
  1970
            m = leaf.getClass().getMethod("getArguments");
jaroslav@72
  1971
            return (List) m.invoke(leaf);
jaroslav@72
  1972
        } catch (Exception ex) {
jaroslav@72
  1973
            return Collections.emptyList();
jaroslav@70
  1974
        }
jaroslav@70
  1975
    }
jaroslav@240
  1976
jtulach@920
  1977
    private static String findTargetId(Element e) {
jtulach@920
  1978
        for (AnnotationMirror m : e.getAnnotationMirrors()) {
jtulach@920
  1979
            if (m.getAnnotationType().toString().equals(Model.class.getName())) {
jtulach@920
  1980
                for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entrySet : m.getElementValues().entrySet()) {
jtulach@920
  1981
                    ExecutableElement key = entrySet.getKey();
jtulach@920
  1982
                    AnnotationValue value = entrySet.getValue();
jtulach@920
  1983
                    if ("targetId()".equals(key.toString())) {
jtulach@920
  1984
                        return value.toString();
jtulach@920
  1985
                    }
jtulach@920
  1986
                }
jtulach@920
  1987
            }
jtulach@920
  1988
        }
jtulach@920
  1989
        return null;
jtulach@920
  1990
    }
jtulach@920
  1991
jtulach@0
  1992
    private static class Prprt {
jtulach@0
  1993
        private final Element e;
jtulach@0
  1994
        private final AnnotationMirror tm;
jtulach@0
  1995
        private final Property p;
jtulach@0
  1996
jtulach@0
  1997
        public Prprt(Element e, AnnotationMirror tm, Property p) {
jtulach@0
  1998
            this.e = e;
jtulach@0
  1999
            this.tm = tm;
jtulach@0
  2000
            this.p = p;
jtulach@0
  2001
        }
jtulach@938
  2002
jtulach@0
  2003
        String name() {
jtulach@0
  2004
            return p.name();
jtulach@0
  2005
        }
jtulach@938
  2006
jtulach@0
  2007
        boolean array() {
jtulach@0
  2008
            return p.array();
jtulach@0
  2009
        }
jtulach@0
  2010
jtulach@1054
  2011
        boolean mutable() {
jtulach@1054
  2012
            return p.mutable();
jtulach@1054
  2013
        }
jtulach@1054
  2014
jtulach@0
  2015
        String typeName(ProcessingEnvironment env) {
jaroslav@100
  2016
            RuntimeException ex;
jtulach@0
  2017
            try {
jtulach@0
  2018
                return p.type().getName();
jaroslav@100
  2019
            } catch (IncompleteAnnotationException e) {
jaroslav@100
  2020
                ex = e;
jaroslav@100
  2021
            } catch (AnnotationTypeMismatchException e) {
jaroslav@100
  2022
                ex = e;
jaroslav@100
  2023
            }
jaroslav@100
  2024
            for (Object v : getAnnoValues(env, e, tm)) {
jaroslav@100
  2025
                String s = v.toString().replace(" ", "");
jaroslav@100
  2026
                if (s.startsWith("type=") && s.endsWith(".class")) {
jaroslav@100
  2027
                    return s.substring(5, s.length() - 6);
jtulach@0
  2028
                }
jtulach@0
  2029
            }
jaroslav@100
  2030
            throw ex;
jtulach@0
  2031
        }
jtulach@938
  2032
jtulach@938
  2033
jtulach@0
  2034
        static Prprt[] wrap(ProcessingEnvironment pe, Element e, Property[] arr) {
jtulach@0
  2035
            if (arr.length == 0) {
jtulach@0
  2036
                return new Prprt[0];
jtulach@0
  2037
            }
jtulach@938
  2038
jtulach@0
  2039
            if (e.getKind() != ElementKind.CLASS) {
jtulach@0
  2040
                throw new IllegalStateException("" + e.getKind());
jtulach@0
  2041
            }
jtulach@0
  2042
            TypeElement te = (TypeElement)e;
jtulach@0
  2043
            List<? extends AnnotationValue> val = null;
jtulach@0
  2044
            for (AnnotationMirror an : te.getAnnotationMirrors()) {
jtulach@0
  2045
                for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : an.getElementValues().entrySet()) {
jtulach@0
  2046
                    if (entry.getKey().getSimpleName().contentEquals("properties")) {
jtulach@0
  2047
                        val = (List)entry.getValue().getValue();
jtulach@0
  2048
                        break;
jtulach@0
  2049
                    }
jtulach@0
  2050
                }
jtulach@0
  2051
            }
jtulach@0
  2052
            if (val == null || val.size() != arr.length) {
jtulach@0
  2053
                pe.getMessager().printMessage(Diagnostic.Kind.ERROR, "" + val, e);
jtulach@0
  2054
                return new Prprt[0];
jtulach@0
  2055
            }
jtulach@0
  2056
            Prprt[] ret = new Prprt[arr.length];
jtulach@0
  2057
            BIG: for (int i = 0; i < ret.length; i++) {
jtulach@0
  2058
                AnnotationMirror am = (AnnotationMirror)val.get(i).getValue();
jtulach@0
  2059
                ret[i] = new Prprt(e, am, arr[i]);
jtulach@938
  2060
jtulach@0
  2061
            }
jtulach@0
  2062
            return ret;
jtulach@0
  2063
        }
jtulach@823
  2064
    } // end of Prprt
jtulach@938
  2065
jtulach@823
  2066
    private static final class GetSet {
jtulach@823
  2067
        final String name;
jtulach@823
  2068
        final String getter;
jtulach@823
  2069
        final String setter;
jtulach@823
  2070
        final String type;
jtulach@824
  2071
        final boolean readOnly;
jtulach@824
  2072
        GetSet(String name, String getter, String setter, String type, boolean readOnly) {
jtulach@823
  2073
            this.name = name;
jtulach@823
  2074
            this.getter = getter;
jtulach@823
  2075
            this.setter = setter;
jtulach@823
  2076
            this.type = type;
jtulach@824
  2077
            this.readOnly = readOnly;
jtulach@823
  2078
        }
jtulach@0
  2079
    }
jaroslav@70
  2080
jaroslav@70
  2081
    @Override
jaroslav@70
  2082
    public Iterable<? extends Completion> getCompletions(Element element, AnnotationMirror annotation, ExecutableElement member, String userText) {
jaroslav@71
  2083
        final Level l = Level.FINE;
jaroslav@71
  2084
        LOG.log(l, " element: {0}", element);
jaroslav@71
  2085
        LOG.log(l, " annotation: {0}", annotation);
jaroslav@71
  2086
        LOG.log(l, " member: {0}", member);
jaroslav@71
  2087
        LOG.log(l, " userText: {0}", userText);
jaroslav@71
  2088
        LOG.log(l, "str: {0}", annotation.getAnnotationType().toString());
jaroslav@70
  2089
        if (annotation.getAnnotationType().toString().equals(OnReceive.class.getName())) {
jaroslav@70
  2090
            if (member.getSimpleName().contentEquals("method")) {
jaroslav@70
  2091
                return Arrays.asList(
jaroslav@70
  2092
                    methodOf("GET"),
jaroslav@70
  2093
                    methodOf("POST"),
jaroslav@70
  2094
                    methodOf("PUT"),
jaroslav@70
  2095
                    methodOf("DELETE"),
jaroslav@247
  2096
                    methodOf("HEAD"),
jaroslav@247
  2097
                    methodOf("WebSocket")
jaroslav@70
  2098
                );
jaroslav@70
  2099
            }
jaroslav@70
  2100
        }
jtulach@938
  2101
jaroslav@70
  2102
        return super.getCompletions(element, annotation, member, userText);
jaroslav@70
  2103
    }
jtulach@938
  2104
jaroslav@70
  2105
    private static final Completion methodOf(String method) {
jaroslav@362
  2106
        ResourceBundle rb = ResourceBundle.getBundle("org.netbeans.html.json.impl.Bundle");
jaroslav@70
  2107
        return Completions.of('"' + method + '"', rb.getString("MSG_Completion_" + method));
jaroslav@70
  2108
    }
jtulach@938
  2109
jaroslav@245
  2110
    private boolean findOnError(ExecutableElement errElem, TypeElement te, String name, String className) {
jaroslav@245
  2111
        String err = null;
jaroslav@245
  2112
        METHODS:
jaroslav@245
  2113
        for (Element e : te.getEnclosedElements()) {
jaroslav@245
  2114
            if (e.getKind() != ElementKind.METHOD) {
jaroslav@245
  2115
                continue;
jaroslav@245
  2116
            }
jaroslav@245
  2117
            if (!e.getSimpleName().contentEquals(name)) {
jaroslav@245
  2118
                continue;
jaroslav@245
  2119
            }
jaroslav@245
  2120
            if (!e.getModifiers().contains(Modifier.STATIC)) {
jaroslav@245
  2121
                errElem = (ExecutableElement) e;
jaroslav@245
  2122
                err = "Would have to be static";
jaroslav@245
  2123
                continue;
jaroslav@245
  2124
            }
jaroslav@245
  2125
            ExecutableElement ee = (ExecutableElement) e;
jaroslav@245
  2126
            TypeMirror excType = processingEnv.getElementUtils().getTypeElement(Exception.class.getName()).asType();
jaroslav@245
  2127
            final List<? extends VariableElement> params = ee.getParameters();
jaroslav@245
  2128
            boolean error = false;
jaroslav@245
  2129
            if (params.size() != 2) {
jaroslav@245
  2130
                error = true;
jaroslav@245
  2131
            } else {
jaroslav@261
  2132
                String firstType = params.get(0).asType().toString();
jaroslav@261
  2133
                int lastDot = firstType.lastIndexOf('.');
jaroslav@261
  2134
                if (lastDot != -1) {
jaroslav@261
  2135
                    firstType = firstType.substring(lastDot + 1);
jaroslav@261
  2136
                }
jaroslav@261
  2137
                if (!firstType.equals(className)) {
jaroslav@245
  2138
                    error = true;
jaroslav@245
  2139
                }
jaroslav@245
  2140
                if (!processingEnv.getTypeUtils().isAssignable(excType, params.get(1).asType())) {
jaroslav@245
  2141
                    error = true;
jaroslav@245
  2142
                }
jaroslav@245
  2143
            }
jaroslav@245
  2144
            if (error) {
jaroslav@245
  2145
                errElem = (ExecutableElement) e;
jaroslav@245
  2146
                err = "Error method first argument needs to be " + className + " and second Exception";
jaroslav@245
  2147
                continue;
jaroslav@245
  2148
            }
jaroslav@245
  2149
            return true;
jaroslav@245
  2150
        }
jaroslav@245
  2151
        if (err == null) {
jaroslav@245
  2152
            err = "Cannot find " + name + "(" + className + ", Exception) method in this class";
jaroslav@245
  2153
        }
jaroslav@245
  2154
        error(err, errElem);
jaroslav@245
  2155
        return false;
jaroslav@245
  2156
    }
jtulach@938
  2157
jtulach@951
  2158
    private ExecutableElement findWrite(ExecutableElement computedPropElem, TypeElement te, String name, String className) {
jtulach@951
  2159
        String err = null;
jtulach@951
  2160
        METHODS:
jtulach@951
  2161
        for (Element e : te.getEnclosedElements()) {
jtulach@951
  2162
            if (e.getKind() != ElementKind.METHOD) {
jtulach@951
  2163
                continue;
jtulach@951
  2164
            }
jtulach@951
  2165
            if (!e.getSimpleName().contentEquals(name)) {
jtulach@951
  2166
                continue;
jtulach@951
  2167
            }
jtulach@951
  2168
            if (e.equals(computedPropElem)) {
jtulach@951
  2169
                continue;
jtulach@951
  2170
            }
jtulach@951
  2171
            if (!e.getModifiers().contains(Modifier.STATIC)) {
jtulach@951
  2172
                computedPropElem = (ExecutableElement) e;
jtulach@951
  2173
                err = "Would have to be static";
jtulach@951
  2174
                continue;
jtulach@951
  2175
            }
jtulach@951
  2176
            ExecutableElement ee = (ExecutableElement) e;
jtulach@955
  2177
            if (ee.getReturnType().getKind() != TypeKind.VOID) {
jtulach@955
  2178
                computedPropElem = (ExecutableElement) e;
jtulach@955
  2179
                err = "Write method has to return void";
jtulach@955
  2180
                continue;
jtulach@955
  2181
            }
jtulach@951
  2182
            TypeMirror retType = computedPropElem.getReturnType();
jtulach@951
  2183
            final List<? extends VariableElement> params = ee.getParameters();
jtulach@951
  2184
            boolean error = false;
jtulach@951
  2185
            if (params.size() != 2) {
jtulach@951
  2186
                error = true;
jtulach@951
  2187
            } else {
jtulach@951
  2188
                String firstType = params.get(0).asType().toString();
jtulach@951
  2189
                int lastDot = firstType.lastIndexOf('.');
jtulach@951
  2190
                if (lastDot != -1) {
jtulach@951
  2191
                    firstType = firstType.substring(lastDot + 1);
jtulach@951
  2192
                }
jtulach@951
  2193
                if (!firstType.equals(className)) {
jtulach@951
  2194
                    error = true;
jtulach@951
  2195
                }
jtulach@951
  2196
                if (!processingEnv.getTypeUtils().isAssignable(retType, params.get(1).asType())) {
jtulach@951
  2197
                    error = true;
jtulach@951
  2198
                }
jtulach@951
  2199
            }
jtulach@951
  2200
            if (error) {
jtulach@951
  2201
                computedPropElem = (ExecutableElement) e;
jtulach@951
  2202
                err = "Write method first argument needs to be " + className + " and second " + retType + " or Object";
jtulach@951
  2203
                continue;
jtulach@951
  2204
            }
jtulach@951
  2205
            return ee;
jtulach@951
  2206
        }
jtulach@951
  2207
        if (err == null) {
jtulach@951
  2208
            err = "Cannot find " + name + "(" + className + ", value) method in this class";
jtulach@951
  2209
        }
jtulach@951
  2210
        error(err, computedPropElem);
jtulach@951
  2211
        return null;
jtulach@951
  2212
    }
jtulach@951
  2213
jtulach@0
  2214
}