2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
32 * If you wish your version of this file to be governed by only the CDDL
33 * or only the GPL Version 2, indicate your decision by adding
34 * "[Contributor] elects to include this software in this distribution
35 * under the [CDDL or GPL Version 2] license." If you do not indicate a
36 * single choice of license, a recipient has the option to distribute
37 * your version of this file under either the CDDL, the GPL Version 2 or
38 * to extend the choice of license to its licensees as provided above.
39 * However, if you add GPL Version 2 code and therefore, elected the GPL
40 * Version 2 license, then the option applies only if the new code is
41 * made subject to such option by the copyright holder.
43 package org.netbeans.html.boot.impl;
45 import java.io.IOException;
46 import java.io.OutputStream;
47 import java.io.OutputStreamWriter;
48 import java.io.PrintWriter;
49 import java.io.Writer;
50 import java.util.Arrays;
51 import java.util.Collections;
52 import java.util.HashMap;
53 import java.util.HashSet;
54 import java.util.List;
57 import java.util.TreeMap;
58 import javax.annotation.processing.AbstractProcessor;
59 import javax.annotation.processing.Completion;
60 import javax.annotation.processing.Completions;
61 import javax.annotation.processing.Messager;
62 import javax.annotation.processing.Processor;
63 import javax.annotation.processing.RoundEnvironment;
64 import javax.lang.model.SourceVersion;
65 import javax.lang.model.element.AnnotationMirror;
66 import javax.lang.model.element.Element;
67 import javax.lang.model.element.ElementKind;
68 import javax.lang.model.element.ExecutableElement;
69 import javax.lang.model.element.Modifier;
70 import javax.lang.model.element.Name;
71 import javax.lang.model.element.PackageElement;
72 import javax.lang.model.element.TypeElement;
73 import javax.lang.model.element.VariableElement;
74 import javax.lang.model.type.ArrayType;
75 import javax.lang.model.type.ExecutableType;
76 import javax.lang.model.type.TypeKind;
77 import javax.lang.model.type.TypeMirror;
78 import javax.lang.model.util.Types;
79 import javax.tools.Diagnostic;
80 import javax.tools.FileObject;
81 import javax.tools.StandardLocation;
82 import net.java.html.js.JavaScriptBody;
83 import net.java.html.js.JavaScriptResource;
84 import org.openide.util.lookup.ServiceProvider;
88 * @author Jaroslav Tulach
90 @ServiceProvider(service = Processor.class)
91 public final class JavaScriptProcesor extends AbstractProcessor {
92 private final Map<String,Map<String,ExecutableElement>> javacalls =
93 new HashMap<String,Map<String,ExecutableElement>>();
94 private final Map<String,Set<TypeElement>> bodies =
95 new HashMap<String, Set<TypeElement>>();
98 public Set<String> getSupportedAnnotationTypes() {
99 Set<String> set = new HashSet<String>();
100 set.add(JavaScriptBody.class.getName());
101 set.add(JavaScriptResource.class.getName());
106 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
107 final Messager msg = processingEnv.getMessager();
108 for (Element e : roundEnv.getElementsAnnotatedWith(JavaScriptBody.class)) {
109 if (e.getKind() != ElementKind.METHOD && e.getKind() != ElementKind.CONSTRUCTOR) {
112 ExecutableElement ee = (ExecutableElement)e;
113 List<? extends VariableElement> params = ee.getParameters();
115 JavaScriptBody jsb = e.getAnnotation(JavaScriptBody.class);
119 Set<TypeElement> classes = this.bodies.get(findPkg(e));
120 if (classes == null) {
121 classes = new HashSet<TypeElement>();
122 bodies.put(findPkg(e), classes);
124 Element t = e.getEnclosingElement();
125 while (!t.getKind().isClass() && !t.getKind().isInterface()) {
126 t = t.getEnclosingElement();
128 classes.add((TypeElement)t);
130 String[] arr = jsb.args();
131 if (params.size() != arr.length) {
132 msg.printMessage(Diagnostic.Kind.ERROR, "Number of args arguments does not match real arguments!", e);
134 for (int i = 0; i < arr.length; i++) {
135 if (!params.get(i).getSimpleName().toString().equals(arr[i])) {
136 msg.printMessage(Diagnostic.Kind.WARNING, "Actual method parameter names and args ones " + Arrays.toString(arr) + " differ", e);
139 if (!jsb.wait4js() && ee.getReturnType().getKind() != TypeKind.VOID) {
140 msg.printMessage(Diagnostic.Kind.ERROR, "Methods that don't wait for JavaScript to finish must return void!", e);
142 if (!jsb.javacall() && jsb.body().contains(".@")) {
143 msg.printMessage(Diagnostic.Kind.WARNING, "Usage of .@ usually requires javacall=true", e);
145 if (jsb.javacall()) {
146 JsCallback verify = new VerifyCallback(e);
148 verify.parse(jsb.body());
149 } catch (IllegalStateException ex) {
150 msg.printMessage(Diagnostic.Kind.ERROR, ex.getLocalizedMessage(), e);
154 for (Element e : roundEnv.getElementsAnnotatedWith(JavaScriptResource.class)) {
155 JavaScriptResource r = e.getAnnotation(JavaScriptResource.class);
160 if (r.value().startsWith("/")) {
161 res = r.value().substring(1);
163 res = findPkg(e).replace('.', '/') + "/" + r.value();
167 FileObject os = processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, "", res);
168 os.openInputStream().close();
169 } catch (IOException ex1) {
171 FileObject os2 = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", res);
172 os2.openInputStream().close();
173 } catch (IOException ex2) {
175 FileObject os3 = processingEnv.getFiler().getResource(StandardLocation.CLASS_PATH, "", res);
176 os3.openInputStream().close();
177 } catch (IOException ex3) {
178 msg.printMessage(Diagnostic.Kind.ERROR, "Cannot find resource " + res, e);
183 boolean found = false;
184 for (Element mthod : e.getEnclosedElements()) {
185 if (mthod.getKind() != ElementKind.METHOD) {
188 if (mthod.getAnnotation(JavaScriptBody.class) != null) {
194 msg.printMessage(Diagnostic.Kind.ERROR, "At least one method needs @JavaScriptBody annotation. "
195 + "Otherwise it is not guaranteed the resource will ever be loaded,", e
200 if (roundEnv.processingOver()) {
201 generateCallbackClass(javacalls);
202 generateJavaScriptBodyList(bodies);
209 public Iterable<? extends Completion> getCompletions(Element e,
210 AnnotationMirror annotation, ExecutableElement member, String userText
212 StringBuilder sb = new StringBuilder();
213 if (e.getKind() == ElementKind.METHOD && member.getSimpleName().contentEquals("args")) {
214 ExecutableElement ee = (ExecutableElement) e;
217 for (VariableElement ve : ee.getParameters()) {
218 sb.append(sep).append('"').append(ve.getSimpleName())
223 return Collections.nCopies(1, Completions.of(sb.toString()));
228 private class VerifyCallback extends JsCallback {
229 private final Element e;
230 public VerifyCallback(Element e) {
235 protected CharSequence callMethod(String ident, String fqn, String method, String params) {
236 final TypeElement type = processingEnv.getElementUtils().getTypeElement(fqn);
238 processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
239 "Callback to non-existing class " + fqn, e
243 ExecutableElement found = null;
244 StringBuilder foundParams = new StringBuilder();
245 for (Element m : type.getEnclosedElements()) {
246 if (m.getKind() != ElementKind.METHOD) {
249 if (m.getSimpleName().contentEquals(method)) {
250 String paramTypes = findParamTypes((ExecutableElement)m);
251 if (paramTypes.equals(params)) {
252 found = (ExecutableElement) m;
255 foundParams.append(paramTypes).append("\n");
259 if (foundParams.length() == 0) {
260 processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
261 "Callback to class " + fqn + " with unknown method " + method, e
264 processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
265 "Callback to " + fqn + "." + method + " with wrong parameters: " +
266 params + ". Only known parameters are " + foundParams, e
270 Map<String,ExecutableElement> mangledOnes = javacalls.get(findPkg(e));
271 if (mangledOnes == null) {
272 mangledOnes = new TreeMap<String, ExecutableElement>();
273 javacalls.put(findPkg(e), mangledOnes);
275 String mangled = JsCallback.mangle(fqn, method, findParamTypes(found));
276 mangledOnes.put(mangled, found);
281 private String findParamTypes(ExecutableElement method) {
282 ExecutableType t = (ExecutableType) method.asType();
283 StringBuilder sb = new StringBuilder();
285 for (TypeMirror tm : t.getParameterTypes()) {
286 if (tm.getKind().isPrimitive()) {
287 switch (tm.getKind()) {
288 case INT: sb.append('I'); break;
289 case BOOLEAN: sb.append('Z'); break;
290 case BYTE: sb.append('B'); break;
291 case CHAR: sb.append('C'); break;
292 case SHORT: sb.append('S'); break;
293 case DOUBLE: sb.append('D'); break;
294 case FLOAT: sb.append('F'); break;
295 case LONG: sb.append('J'); break;
297 throw new IllegalStateException("Uknown " + tm.getKind());
300 while (tm.getKind() == TypeKind.ARRAY) {
302 tm = ((ArrayType)tm).getComponentType();
305 Types tu = processingEnv.getTypeUtils();
306 Element elm = tu.asElement(tu.erasure(tm));
307 dumpElems(sb, elm, ';');
311 return sb.toString();
315 private static void dumpElems(StringBuilder sb, Element e, char after) {
319 if (e.getKind() == ElementKind.PACKAGE) {
320 PackageElement pe = (PackageElement) e;
321 sb.append(pe.getQualifiedName().toString().replace('.', '/')).append('/');
324 Element p = e.getEnclosingElement();
325 dumpElems(sb, p, '$');
326 sb.append(e.getSimpleName());
330 private void generateJavaScriptBodyList(Map<String,Set<TypeElement>> bodies) {
331 if (bodies.isEmpty()) {
335 FileObject all = processingEnv.getFiler().createResource(
336 StandardLocation.CLASS_OUTPUT, "", "META-INF/net.java.html.js.classes"
338 PrintWriter wAll = new PrintWriter(new OutputStreamWriter(
339 all.openOutputStream(), "UTF-8"
341 for (Map.Entry<String, Set<TypeElement>> entry : bodies.entrySet()) {
342 String pkg = entry.getKey();
343 Set<TypeElement> classes = entry.getValue();
345 FileObject out = processingEnv.getFiler().createResource(
346 StandardLocation.CLASS_OUTPUT, pkg, "net.java.html.js.classes",
347 classes.iterator().next()
349 OutputStream os = out.openOutputStream();
351 PrintWriter w = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));
352 for (TypeElement type : classes) {
353 final Name bn = processingEnv.getElementUtils().getBinaryName(type);
359 } catch (IOException x) {
360 processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Failed to write to " + entry.getKey() + ": " + x.toString());
366 } catch (IOException x) {
367 processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Failed to write to " + "META-INF/net.java.html.js.classes: " + x.toString());
371 private void generateCallbackClass(Map<String,Map<String, ExecutableElement>> process) {
372 for (Map.Entry<String, Map<String, ExecutableElement>> pkgEn : process.entrySet()) {
373 String pkgName = pkgEn.getKey();
374 Map<String, ExecutableElement> map = pkgEn.getValue();
375 StringBuilder source = new StringBuilder();
376 source.append("package ").append(pkgName).append(";\n");
377 source.append("public final class $JsCallbacks$ {\n");
378 source.append(" static final $JsCallbacks$ VM = new $JsCallbacks$(null);\n");
379 source.append(" private final org.netbeans.html.boot.spi.Fn.Presenter p;\n");
380 source.append(" private $JsCallbacks$ last;\n");
381 source.append(" private $JsCallbacks$(org.netbeans.html.boot.spi.Fn.Presenter p) {\n");
382 source.append(" this.p = p;\n");
383 source.append(" }\n");
384 source.append(" final $JsCallbacks$ current() {\n");
385 source.append(" org.netbeans.html.boot.spi.Fn.Presenter now = org.netbeans.html.boot.spi.Fn.activePresenter();\n");
386 source.append(" if (now == p) return this;\n");
387 source.append(" if (last != null && now == last.p) return last;\n");
388 source.append(" return last = new $JsCallbacks$(now);\n");
389 source.append(" }\n");
390 for (Map.Entry<String, ExecutableElement> entry : map.entrySet()) {
391 final String mangled = entry.getKey();
392 final ExecutableElement m = entry.getValue();
393 generateMethod(false, m, source, mangled);
394 generateMethod(true, m, source, "raw$" + mangled);
396 source.append("}\n");
397 final String srcName = pkgName + ".$JsCallbacks$";
399 Writer w = processingEnv.getFiler().createSourceFile(srcName,
400 map.values().toArray(new Element[map.size()])
402 w.write(source.toString());
404 } catch (IOException ex) {
405 processingEnv.getMessager().printMessage(
406 Diagnostic.Kind.ERROR, "Can't write " + srcName + ": " + ex.getMessage()
412 private void generateMethod(boolean selfObj, final ExecutableElement m, StringBuilder source, final String mangled) {
413 final boolean isStatic = m.getModifiers().contains(Modifier.STATIC);
414 if (isStatic && selfObj) {
417 final TypeElement selfType = (TypeElement)m.getEnclosingElement();
418 Types tu = processingEnv.getTypeUtils();
420 source.append("\n public java.lang.Object ")
425 StringBuilder convert = new StringBuilder();
428 source.append("java.lang.Object self");
429 convert.append(" if (p instanceof org.netbeans.html.boot.spi.Fn.FromJavaScript) {\n");
430 convert.append(" self").
431 append(" = ((org.netbeans.html.boot.spi.Fn.FromJavaScript)p).toJava(self").
433 convert.append(" }\n");
435 source.append(selfType.getQualifiedName());
436 source.append(" self");
442 for (VariableElement ve : m.getParameters()) {
445 final TypeMirror t = ve.asType();
446 if (!t.getKind().isPrimitive() && !"java.lang.String".equals(t.toString())) { // NOI18N
447 source.append("java.lang.Object");
448 convert.append(" if (p instanceof org.netbeans.html.boot.spi.Fn.FromJavaScript) {\n");
449 convert.append(" arg").append(cnt).
450 append(" = ((org.netbeans.html.boot.spi.Fn.FromJavaScript)p).toJava(arg").append(cnt).
452 convert.append(" }\n");
456 source.append(" arg").append(cnt);
459 source.append(") throws Throwable {\n");
460 source.append(convert);
461 if (useTryResources()) {
462 source.append(" try (java.io.Closeable a = org.netbeans.html.boot.spi.Fn.activate(p)) { \n");
464 source.append(" java.io.Closeable a = org.netbeans.html.boot.spi.Fn.activate(p); try {\n");
467 if (m.getReturnType().getKind() != TypeKind.VOID) {
468 source.append("java.lang.Object $ret = ");
471 source.append(((TypeElement)m.getEnclosingElement()).getQualifiedName());
476 source.append(selfType.getQualifiedName());
477 source.append(")self).");
479 source.append("self.");
482 source.append(m.getSimpleName());
486 for (VariableElement ve : m.getParameters()) {
488 source.append("(").append(tu.erasure(ve.asType()));
489 source.append(")arg").append(++cnt);
492 source.append(");\n");
493 if (m.getReturnType().getKind() == TypeKind.VOID) {
494 source.append(" return null;\n");
496 source.append(" if (p instanceof org.netbeans.html.boot.spi.Fn.ToJavaScript) {\n");
497 source.append(" $ret = ((org.netbeans.html.boot.spi.Fn.ToJavaScript)p).toJavaScript($ret);\n");
498 source.append(" }\n");
499 source.append(" return $ret;\n");
501 if (useTryResources()) {
502 source.append(" }\n");
505 source.append(" } finally {\n");
506 source.append(" a.close();\n");
507 source.append(" }\n");
509 source.append(" }\n");
512 private boolean useTryResources() {
514 return processingEnv.getSourceVersion().compareTo(SourceVersion.RELEASE_7) >= 0;
515 } catch (LinkageError err) {
516 // can happen when running on JDK6
521 private static String findPkg(Element e) {
522 while (e.getKind() != ElementKind.PACKAGE) {
523 e = e.getEnclosingElement();
525 return ((PackageElement)e).getQualifiedName().toString();