1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/src/main/java/org/apidesign/bck2brwsr/core/impl/JavaScriptProcesor.java Sat Jan 12 16:29:29 2013 +0100
1.3 @@ -0,0 +1,104 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.bck2brwsr.core.impl;
1.22 +
1.23 +import java.util.Collections;
1.24 +import java.util.HashSet;
1.25 +import java.util.List;
1.26 +import java.util.Set;
1.27 +import javax.annotation.processing.AbstractProcessor;
1.28 +import javax.annotation.processing.Completion;
1.29 +import javax.annotation.processing.Completions;
1.30 +import javax.annotation.processing.Processor;
1.31 +import javax.annotation.processing.RoundEnvironment;
1.32 +import javax.lang.model.element.AnnotationMirror;
1.33 +import javax.lang.model.element.Element;
1.34 +import javax.lang.model.element.ElementKind;
1.35 +import javax.lang.model.element.ExecutableElement;
1.36 +import javax.lang.model.element.Modifier;
1.37 +import javax.lang.model.element.TypeElement;
1.38 +import javax.lang.model.element.VariableElement;
1.39 +import javax.tools.Diagnostic;
1.40 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.41 +import org.openide.util.lookup.ServiceProvider;
1.42 +
1.43 +/**
1.44 + *
1.45 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.46 + */
1.47 +@ServiceProvider(service = Processor.class)
1.48 +public final class JavaScriptProcesor extends AbstractProcessor {
1.49 + @Override
1.50 + public Set<String> getSupportedAnnotationTypes() {
1.51 + Set<String> set = new HashSet<>();
1.52 + set.add(JavaScriptBody.class.getName());
1.53 + return set;
1.54 + }
1.55 +
1.56 + @Override
1.57 + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
1.58 + for (Element e : roundEnv.getElementsAnnotatedWith(JavaScriptBody.class)) {
1.59 + if (e.getKind() != ElementKind.METHOD) {
1.60 + continue;
1.61 + }
1.62 + ExecutableElement ee = (ExecutableElement)e;
1.63 + List<? extends VariableElement> params = ee.getParameters();
1.64 +
1.65 + JavaScriptBody jsb = e.getAnnotation(JavaScriptBody.class);
1.66 + String[] arr = jsb.args();
1.67 + int indx;
1.68 + if (!ee.getModifiers().contains(Modifier.STATIC)) {
1.69 + indx = 1;
1.70 + } else {
1.71 + indx = 0;
1.72 + }
1.73 + if (indx + params.size() != arr.length) {
1.74 + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Number of args arguments does not match real arguments!", e);
1.75 + }
1.76 + }
1.77 + return true;
1.78 + }
1.79 +
1.80 + @Override
1.81 + public Iterable<? extends Completion> getCompletions(Element e,
1.82 + AnnotationMirror annotation, ExecutableElement member, String userText
1.83 + ) {
1.84 + StringBuilder sb = new StringBuilder();
1.85 + if (e.getKind() == ElementKind.METHOD && member.getSimpleName().contentEquals("args")) {
1.86 + ExecutableElement ee = (ExecutableElement) e;
1.87 + String sep;
1.88 + if (!ee.getModifiers().contains(Modifier.STATIC)) {
1.89 + sb.append("{ \"self\"");
1.90 + sep = ", ";
1.91 + } else {
1.92 + sb.append("{ ");
1.93 + sep = "";
1.94 + }
1.95 + for (VariableElement ve : ee.getParameters()) {
1.96 + sb.append(sep).append('"').append(ve.getSimpleName())
1.97 + .append('"');
1.98 + sep = ", ";
1.99 + }
1.100 + sb.append(" }");
1.101 + return Collections.nCopies(1, Completions.of(sb.toString()));
1.102 + }
1.103 + return null;
1.104 + }
1.105 +
1.106 +
1.107 +}