2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.core.impl;
20 import java.util.Collections;
21 import java.util.HashSet;
22 import java.util.List;
24 import javax.annotation.processing.AbstractProcessor;
25 import javax.annotation.processing.Completion;
26 import javax.annotation.processing.Completions;
27 import javax.annotation.processing.Messager;
28 import javax.annotation.processing.Processor;
29 import javax.annotation.processing.RoundEnvironment;
30 import javax.lang.model.element.AnnotationMirror;
31 import javax.lang.model.element.Element;
32 import javax.lang.model.element.ElementKind;
33 import javax.lang.model.element.ExecutableElement;
34 import javax.lang.model.element.TypeElement;
35 import javax.lang.model.element.VariableElement;
36 import javax.lang.model.type.TypeKind;
37 import javax.tools.Diagnostic;
38 import org.apidesign.bck2brwsr.core.ExtraJavaScript;
39 import org.apidesign.bck2brwsr.core.JavaScriptBody;
40 import org.openide.util.lookup.ServiceProvider;
44 * @author Jaroslav Tulach <jtulach@netbeans.org>
46 @ExtraJavaScript(processByteCode = false, resource="")
47 @ServiceProvider(service = Processor.class)
48 public final class JavaScriptProcesor extends AbstractProcessor {
50 public Set<String> getSupportedAnnotationTypes() {
51 Set<String> set = new HashSet<>();
52 set.add(JavaScriptBody.class.getName());
57 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
58 final Messager msg = processingEnv.getMessager();
59 for (Element e : roundEnv.getElementsAnnotatedWith(JavaScriptBody.class)) {
60 if (e.getKind() != ElementKind.METHOD && e.getKind() != ElementKind.CONSTRUCTOR) {
63 ExecutableElement ee = (ExecutableElement)e;
64 List<? extends VariableElement> params = ee.getParameters();
66 JavaScriptBody jsb = e.getAnnotation(JavaScriptBody.class);
70 String[] arr = jsb.args();
71 if (params.size() != arr.length) {
72 msg.printMessage(Diagnostic.Kind.ERROR, "Number of args arguments does not match real arguments!", e);
74 if (ee.getReturnType().getKind() == TypeKind.LONG) {
75 msg.printMessage(Diagnostic.Kind.WARNING, "Don't return long. Return double and convert it to long in Java code.", e);
82 public Iterable<? extends Completion> getCompletions(Element e,
83 AnnotationMirror annotation, ExecutableElement member, String userText
85 StringBuilder sb = new StringBuilder();
86 if (e.getKind() == ElementKind.METHOD && member.getSimpleName().contentEquals("args")) {
87 ExecutableElement ee = (ExecutableElement) e;
90 for (VariableElement ve : ee.getParameters()) {
91 sb.append(sep).append('"').append(ve.getSimpleName())
96 return Collections.nCopies(1, Completions.of(sb.toString()));