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