boot/src/main/java/org/netbeans/html/boot/impl/JavaScriptProcesor.java
branchnetbeans
changeset 362 92fb71afdc0e
parent 358 80702021b851
child 365 5c93ad8c7a15
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/boot/src/main/java/org/netbeans/html/boot/impl/JavaScriptProcesor.java	Mon Dec 16 16:59:43 2013 +0100
     1.3 @@ -0,0 +1,369 @@
     1.4 +/**
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
     1.8 + *
     1.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    1.10 + * Other names may be trademarks of their respective owners.
    1.11 + *
    1.12 + * The contents of this file are subject to the terms of either the GNU
    1.13 + * General Public License Version 2 only ("GPL") or the Common
    1.14 + * Development and Distribution License("CDDL") (collectively, the
    1.15 + * "License"). You may not use this file except in compliance with the
    1.16 + * License. You can obtain a copy of the License at
    1.17 + * http://www.netbeans.org/cddl-gplv2.html
    1.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.19 + * specific language governing permissions and limitations under the
    1.20 + * License.  When distributing the software, include this License Header
    1.21 + * Notice in each file and include the License file at
    1.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    1.23 + * particular file as subject to the "Classpath" exception as provided
    1.24 + * by Oracle in the GPL Version 2 section of the License file that
    1.25 + * accompanied this code. If applicable, add the following below the
    1.26 + * License Header, with the fields enclosed by brackets [] replaced by
    1.27 + * your own identifying information:
    1.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.29 + *
    1.30 + * Contributor(s):
    1.31 + *
    1.32 + * The Original Software is NetBeans. The Initial Developer of the Original
    1.33 + * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
    1.34 + *
    1.35 + * If you wish your version of this file to be governed by only the CDDL
    1.36 + * or only the GPL Version 2, indicate your decision by adding
    1.37 + * "[Contributor] elects to include this software in this distribution
    1.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    1.39 + * single choice of license, a recipient has the option to distribute
    1.40 + * your version of this file under either the CDDL, the GPL Version 2 or
    1.41 + * to extend the choice of license to its licensees as provided above.
    1.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    1.43 + * Version 2 license, then the option applies only if the new code is
    1.44 + * made subject to such option by the copyright holder.
    1.45 + */
    1.46 +package org.netbeans.html.boot.impl;
    1.47 +
    1.48 +import java.io.IOException;
    1.49 +import java.io.Writer;
    1.50 +import java.util.Collections;
    1.51 +import java.util.HashMap;
    1.52 +import java.util.HashSet;
    1.53 +import java.util.List;
    1.54 +import java.util.Map;
    1.55 +import java.util.Set;
    1.56 +import java.util.TreeMap;
    1.57 +import javax.annotation.processing.AbstractProcessor;
    1.58 +import javax.annotation.processing.Completion;
    1.59 +import javax.annotation.processing.Completions;
    1.60 +import javax.annotation.processing.Messager;
    1.61 +import javax.annotation.processing.Processor;
    1.62 +import javax.annotation.processing.RoundEnvironment;
    1.63 +import javax.lang.model.SourceVersion;
    1.64 +import javax.lang.model.element.AnnotationMirror;
    1.65 +import javax.lang.model.element.Element;
    1.66 +import javax.lang.model.element.ElementKind;
    1.67 +import javax.lang.model.element.ExecutableElement;
    1.68 +import javax.lang.model.element.Modifier;
    1.69 +import javax.lang.model.element.PackageElement;
    1.70 +import javax.lang.model.element.TypeElement;
    1.71 +import javax.lang.model.element.VariableElement;
    1.72 +import javax.lang.model.type.ArrayType;
    1.73 +import javax.lang.model.type.ExecutableType;
    1.74 +import javax.lang.model.type.TypeKind;
    1.75 +import javax.lang.model.type.TypeMirror;
    1.76 +import javax.tools.Diagnostic;
    1.77 +import javax.tools.FileObject;
    1.78 +import javax.tools.StandardLocation;
    1.79 +import net.java.html.js.JavaScriptBody;
    1.80 +import net.java.html.js.JavaScriptResource;
    1.81 +import org.openide.util.lookup.ServiceProvider;
    1.82 +
    1.83 +/**
    1.84 + *
    1.85 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.86 + */
    1.87 +@ServiceProvider(service = Processor.class)
    1.88 +public final class JavaScriptProcesor extends AbstractProcessor {
    1.89 +    private final Map<String,Map<String,ExecutableElement>> javacalls = 
    1.90 +        new HashMap<String,Map<String,ExecutableElement>>();
    1.91 +    
    1.92 +    @Override
    1.93 +    public Set<String> getSupportedAnnotationTypes() {
    1.94 +        Set<String> set = new HashSet<String>();
    1.95 +        set.add(JavaScriptBody.class.getName());
    1.96 +        set.add(JavaScriptResource.class.getName());
    1.97 +        return set;
    1.98 +    }
    1.99 +    
   1.100 +    @Override
   1.101 +    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
   1.102 +        final Messager msg = processingEnv.getMessager();
   1.103 +        for (Element e : roundEnv.getElementsAnnotatedWith(JavaScriptBody.class)) {
   1.104 +            if (e.getKind() != ElementKind.METHOD && e.getKind() != ElementKind.CONSTRUCTOR) {
   1.105 +                continue;
   1.106 +            }
   1.107 +            ExecutableElement ee = (ExecutableElement)e;
   1.108 +            List<? extends VariableElement> params = ee.getParameters();
   1.109 +            
   1.110 +            JavaScriptBody jsb = e.getAnnotation(JavaScriptBody.class);
   1.111 +            if (jsb == null) {
   1.112 +                continue;
   1.113 +            }
   1.114 +            String[] arr = jsb.args();
   1.115 +            if (params.size() != arr.length) {
   1.116 +                msg.printMessage(Diagnostic.Kind.ERROR, "Number of args arguments does not match real arguments!", e);
   1.117 +            }
   1.118 +            if (!jsb.javacall() && jsb.body().contains(".@")) {
   1.119 +                msg.printMessage(Diagnostic.Kind.WARNING, "Usage of .@ usually requires javacall=true", e);
   1.120 +            }
   1.121 +            if (jsb.javacall()) {
   1.122 +                JsCallback verify = new VerifyCallback(e);
   1.123 +                try {
   1.124 +                    verify.parse(jsb.body());
   1.125 +                } catch (IllegalStateException ex) {
   1.126 +                    msg.printMessage(Diagnostic.Kind.ERROR, ex.getLocalizedMessage(), e);
   1.127 +                }
   1.128 +            }
   1.129 +        }
   1.130 +        for (Element e : roundEnv.getElementsAnnotatedWith(JavaScriptResource.class)) {
   1.131 +            JavaScriptResource r = e.getAnnotation(JavaScriptResource.class);
   1.132 +            if (r == null) {
   1.133 +                continue;
   1.134 +            }
   1.135 +            final String res;
   1.136 +            if (r.value().startsWith("/")) {
   1.137 +                res = r.value();
   1.138 +            } else {
   1.139 +                res = findPkg(e).replace('.', '/') + "/" + r.value();
   1.140 +            }
   1.141 +            
   1.142 +            try {
   1.143 +                FileObject os = processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, "", res);
   1.144 +                os.openInputStream().close();
   1.145 +            } catch (IOException ex1) {
   1.146 +                try {
   1.147 +                    FileObject os2 = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", res);
   1.148 +                    os2.openInputStream().close();
   1.149 +                } catch (IOException ex2) {
   1.150 +                    msg.printMessage(Diagnostic.Kind.ERROR, "Cannot find " + res + " in " + res + " package", e);
   1.151 +                }
   1.152 +            }
   1.153 +        }
   1.154 +
   1.155 +        if (roundEnv.processingOver()) {
   1.156 +            generateCallbackClass(javacalls);
   1.157 +            javacalls.clear();
   1.158 +        }
   1.159 +        return true;
   1.160 +    }
   1.161 +
   1.162 +    @Override
   1.163 +    public Iterable<? extends Completion> getCompletions(Element e, 
   1.164 +        AnnotationMirror annotation, ExecutableElement member, String userText
   1.165 +    ) {
   1.166 +        StringBuilder sb = new StringBuilder();
   1.167 +        if (e.getKind() == ElementKind.METHOD && member.getSimpleName().contentEquals("args")) {
   1.168 +            ExecutableElement ee = (ExecutableElement) e;
   1.169 +            String sep = "";
   1.170 +            sb.append("{ ");
   1.171 +            for (VariableElement ve : ee.getParameters()) {
   1.172 +                sb.append(sep).append('"').append(ve.getSimpleName())
   1.173 +                    .append('"');
   1.174 +                sep = ", ";
   1.175 +            }
   1.176 +            sb.append(" }");
   1.177 +            return Collections.nCopies(1, Completions.of(sb.toString()));
   1.178 +        }
   1.179 +        return null;
   1.180 +    }
   1.181 +
   1.182 +    private class VerifyCallback extends JsCallback {
   1.183 +        private final Element e;
   1.184 +        public VerifyCallback(Element e) {
   1.185 +            this.e = e;
   1.186 +        }
   1.187 +
   1.188 +        @Override
   1.189 +        protected CharSequence callMethod(String ident, String fqn, String method, String params) {
   1.190 +            final TypeElement type = processingEnv.getElementUtils().getTypeElement(fqn);
   1.191 +            if (type == null) {
   1.192 +                processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, 
   1.193 +                    "Callback to non-existing class " + fqn, e
   1.194 +                );
   1.195 +                return "";
   1.196 +            }
   1.197 +            ExecutableElement found = null;
   1.198 +            StringBuilder foundParams = new StringBuilder();
   1.199 +            for (Element m : type.getEnclosedElements()) {
   1.200 +                if (m.getKind() != ElementKind.METHOD) {
   1.201 +                    continue;
   1.202 +                }
   1.203 +                if (m.getSimpleName().contentEquals(method)) {
   1.204 +                    String paramTypes = findParamTypes((ExecutableElement)m);
   1.205 +                    if (paramTypes.equals(params)) {
   1.206 +                        found = (ExecutableElement) m;
   1.207 +                        break;
   1.208 +                    }
   1.209 +                    foundParams.append(paramTypes).append("\n");
   1.210 +                }
   1.211 +            }
   1.212 +            if (found == null) {
   1.213 +                if (foundParams.length() == 0) {
   1.214 +                    processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, 
   1.215 +                        "Callback to class " + fqn + " with unknown method " + method, e
   1.216 +                    );
   1.217 +                } else {
   1.218 +                    processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, 
   1.219 +                        "Callback to " + fqn + "." + method + " with wrong parameters: " + 
   1.220 +                        params + ". Only known parameters are " + foundParams, e
   1.221 +                    );
   1.222 +                }
   1.223 +            } else {
   1.224 +                Map<String,ExecutableElement> mangledOnes = javacalls.get(findPkg(e));
   1.225 +                if (mangledOnes == null) {
   1.226 +                    mangledOnes = new TreeMap<String, ExecutableElement>();
   1.227 +                    javacalls.put(findPkg(e), mangledOnes);
   1.228 +                }
   1.229 +                String mangled = JsCallback.mangle(fqn, method, findParamTypes(found));
   1.230 +                mangledOnes.put(mangled, found);
   1.231 +            }
   1.232 +            return "";
   1.233 +        }
   1.234 +
   1.235 +        private String findParamTypes(ExecutableElement method) {
   1.236 +            ExecutableType t = (ExecutableType) method.asType();
   1.237 +            StringBuilder sb = new StringBuilder();
   1.238 +            sb.append('(');
   1.239 +            for (TypeMirror tm : t.getParameterTypes()) {
   1.240 +                if (tm.getKind().isPrimitive()) {
   1.241 +                    switch (tm.getKind()) {
   1.242 +                        case INT: sb.append('I'); break;
   1.243 +                        case BOOLEAN: sb.append('Z'); break;
   1.244 +                        case BYTE: sb.append('B'); break;
   1.245 +                        case CHAR: sb.append('C'); break;
   1.246 +                        case SHORT: sb.append('S'); break;
   1.247 +                        case DOUBLE: sb.append('D'); break;
   1.248 +                        case FLOAT: sb.append('F'); break;
   1.249 +                        case LONG: sb.append('J'); break;
   1.250 +                        default:
   1.251 +                            throw new IllegalStateException("Uknown " + tm.getKind());
   1.252 +                    }
   1.253 +                } else {
   1.254 +                    while (tm.getKind() == TypeKind.ARRAY) {
   1.255 +                        sb.append('[');
   1.256 +                        tm = ((ArrayType)tm).getComponentType();
   1.257 +                    }
   1.258 +                    sb.append('L');
   1.259 +                    sb.append(tm.toString().replace('.', '/'));
   1.260 +                    sb.append(';');
   1.261 +                }
   1.262 +            }
   1.263 +            sb.append(')');
   1.264 +            return sb.toString();
   1.265 +        }
   1.266 +    }
   1.267 +    
   1.268 +    private void generateCallbackClass(Map<String,Map<String, ExecutableElement>> process) {
   1.269 +        for (Map.Entry<String, Map<String, ExecutableElement>> pkgEn : process.entrySet()) {
   1.270 +            String pkgName = pkgEn.getKey();
   1.271 +            Map<String, ExecutableElement> map = pkgEn.getValue();
   1.272 +            StringBuilder source = new StringBuilder();
   1.273 +            source.append("package ").append(pkgName).append(";\n");
   1.274 +            source.append("public final class $JsCallbacks$ {\n");
   1.275 +            source.append("  static final $JsCallbacks$ VM = new $JsCallbacks$(null);\n");
   1.276 +            source.append("  private final org.apidesign.html.boot.spi.Fn.Presenter p;\n");
   1.277 +            source.append("  private $JsCallbacks$ last;\n");
   1.278 +            source.append("  private $JsCallbacks$(org.apidesign.html.boot.spi.Fn.Presenter p) {\n");
   1.279 +            source.append("    this.p = p;\n");
   1.280 +            source.append("  }\n");
   1.281 +            source.append("  final $JsCallbacks$ current() {\n");
   1.282 +            source.append("    org.apidesign.html.boot.spi.Fn.Presenter now = org.apidesign.html.boot.spi.Fn.activePresenter();\n");
   1.283 +            source.append("    if (now == p) return this;\n");
   1.284 +            source.append("    if (last != null && now == last.p) return last;\n");
   1.285 +            source.append("    return last = new $JsCallbacks$(now);\n");
   1.286 +            source.append("  }\n");
   1.287 +            for (Map.Entry<String, ExecutableElement> entry : map.entrySet()) {
   1.288 +                final String mangled = entry.getKey();
   1.289 +                final ExecutableElement m = entry.getValue();
   1.290 +                final boolean isStatic = m.getModifiers().contains(Modifier.STATIC);
   1.291 +                
   1.292 +                source.append("\n  public java.lang.Object ")
   1.293 +                    .append(mangled)
   1.294 +                    .append("(");
   1.295 +                
   1.296 +                String sep = "";
   1.297 +                if (!isStatic) {
   1.298 +                    source.append(((TypeElement)m.getEnclosingElement()).getQualifiedName());
   1.299 +                    source.append(" self");
   1.300 +                    sep = ", ";
   1.301 +                }
   1.302 +                
   1.303 +                int cnt = 0;
   1.304 +                for (VariableElement ve : m.getParameters()) {
   1.305 +                    source.append(sep);
   1.306 +                    source.append(ve.asType());
   1.307 +                    source.append(" arg").append(++cnt);
   1.308 +                    sep = ", ";
   1.309 +                }
   1.310 +                source.append(") throws Throwable {\n");
   1.311 +                if (processingEnv.getSourceVersion().compareTo(SourceVersion.RELEASE_7) >= 0) {
   1.312 +                    source.append("    try (java.io.Closeable a = org.apidesign.html.boot.spi.Fn.activate(p)) { \n");
   1.313 +                } else {
   1.314 +                    source.append("    java.io.Closeable a = org.apidesign.html.boot.spi.Fn.activate(p); try {\n");
   1.315 +                }
   1.316 +                source.append("    ");
   1.317 +                if (m.getReturnType().getKind() != TypeKind.VOID) {
   1.318 +                    source.append("return ");
   1.319 +                }
   1.320 +                if (isStatic) {
   1.321 +                    source.append(((TypeElement)m.getEnclosingElement()).getQualifiedName());
   1.322 +                    source.append('.');
   1.323 +                } else {
   1.324 +                    source.append("self.");
   1.325 +                }
   1.326 +                source.append(m.getSimpleName());
   1.327 +                source.append("(");
   1.328 +                cnt = 0;
   1.329 +                sep = "";
   1.330 +                for (VariableElement ve : m.getParameters()) {
   1.331 +                    source.append(sep);
   1.332 +                    source.append("arg").append(++cnt);
   1.333 +                    sep = ", ";
   1.334 +                }
   1.335 +                source.append(");\n");
   1.336 +                if (m.getReturnType().getKind() == TypeKind.VOID) {
   1.337 +                    source.append("    return null;\n");
   1.338 +                }
   1.339 +                if (processingEnv.getSourceVersion().compareTo(SourceVersion.RELEASE_7) >= 0) {
   1.340 +                    source.append("    }\n");
   1.341 +                } else {
   1.342 +                    
   1.343 +                    source.append("    } finally {\n");
   1.344 +                    source.append("      a.close();\n");
   1.345 +                    source.append("    }\n");
   1.346 +                }
   1.347 +                source.append("  }\n");
   1.348 +            }
   1.349 +            source.append("}\n");
   1.350 +            final String srcName = pkgName + ".$JsCallbacks$";
   1.351 +            try {
   1.352 +                Writer w = processingEnv.getFiler().createSourceFile(srcName,
   1.353 +                    map.values().toArray(new Element[map.size()])
   1.354 +                ).openWriter();
   1.355 +                w.write(source.toString());
   1.356 +                w.close();
   1.357 +            } catch (IOException ex) {
   1.358 +                processingEnv.getMessager().printMessage(
   1.359 +                    Diagnostic.Kind.ERROR, "Can't write " + srcName + ": " + ex.getMessage()
   1.360 +                );
   1.361 +            }
   1.362 +        }
   1.363 +    }
   1.364 +    
   1.365 +    private static String findPkg(Element e) {
   1.366 +        while (e.getKind() != ElementKind.PACKAGE) {
   1.367 +            e = e.getEnclosingElement();
   1.368 +        }
   1.369 +        return ((PackageElement)e).getQualifiedName().toString();
   1.370 +    }
   1.371 +    
   1.372 +}