# HG changeset patch # User Jaroslav Tulach # Date 1358004569 -3600 # Node ID 1bdeeb77f5a3f292a0894462f90783f253c27e76 # Parent d859199a15b7b6d156b7e41d0cd5f6f3af442555 Processor for @JavaScriptBody annotation to warn when incorrect number of args is used and to provide code completion to generate the field diff -r d859199a15b7 -r 1bdeeb77f5a3 core/pom.xml --- a/core/pom.xml Sat Jan 12 15:44:09 2013 +0100 +++ b/core/pom.xml Sat Jan 12 16:29:29 2013 +0100 @@ -19,8 +19,8 @@ maven-compiler-plugin 2.3.2 - 1.6 - 1.6 + 1.7 + 1.7 @@ -35,6 +35,10 @@ 3.8.1 test + + org.netbeans.api + org-openide-util-lookup + Contains esential annotations for associating JavaScript code with methods and classes. diff -r d859199a15b7 -r 1bdeeb77f5a3 core/src/main/java/org/apidesign/bck2brwsr/core/impl/JavaScriptProcesor.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/src/main/java/org/apidesign/bck2brwsr/core/impl/JavaScriptProcesor.java Sat Jan 12 16:29:29 2013 +0100 @@ -0,0 +1,104 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.core.impl; + +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.Completion; +import javax.annotation.processing.Completions; +import javax.annotation.processing.Processor; +import javax.annotation.processing.RoundEnvironment; +import javax.lang.model.element.AnnotationMirror; +import javax.lang.model.element.Element; +import javax.lang.model.element.ElementKind; +import javax.lang.model.element.ExecutableElement; +import javax.lang.model.element.Modifier; +import javax.lang.model.element.TypeElement; +import javax.lang.model.element.VariableElement; +import javax.tools.Diagnostic; +import org.apidesign.bck2brwsr.core.JavaScriptBody; +import org.openide.util.lookup.ServiceProvider; + +/** + * + * @author Jaroslav Tulach + */ +@ServiceProvider(service = Processor.class) +public final class JavaScriptProcesor extends AbstractProcessor { + @Override + public Set getSupportedAnnotationTypes() { + Set set = new HashSet<>(); + set.add(JavaScriptBody.class.getName()); + return set; + } + + @Override + public boolean process(Set annotations, RoundEnvironment roundEnv) { + for (Element e : roundEnv.getElementsAnnotatedWith(JavaScriptBody.class)) { + if (e.getKind() != ElementKind.METHOD) { + continue; + } + ExecutableElement ee = (ExecutableElement)e; + List params = ee.getParameters(); + + JavaScriptBody jsb = e.getAnnotation(JavaScriptBody.class); + String[] arr = jsb.args(); + int indx; + if (!ee.getModifiers().contains(Modifier.STATIC)) { + indx = 1; + } else { + indx = 0; + } + if (indx + params.size() != arr.length) { + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Number of args arguments does not match real arguments!", e); + } + } + return true; + } + + @Override + public Iterable getCompletions(Element e, + AnnotationMirror annotation, ExecutableElement member, String userText + ) { + StringBuilder sb = new StringBuilder(); + if (e.getKind() == ElementKind.METHOD && member.getSimpleName().contentEquals("args")) { + ExecutableElement ee = (ExecutableElement) e; + String sep; + if (!ee.getModifiers().contains(Modifier.STATIC)) { + sb.append("{ \"self\""); + sep = ", "; + } else { + sb.append("{ "); + sep = ""; + } + for (VariableElement ve : ee.getParameters()) { + sb.append(sep).append('"').append(ve.getSimpleName()) + .append('"'); + sep = ", "; + } + sb.append(" }"); + return Collections.nCopies(1, Completions.of(sb.toString())); + } + return null; + } + + +}