Processor for @JavaScriptBody annotation to warn when incorrect number of args is used and to provide code completion to generate the field
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 12 Jan 2013 16:29:29 +0100
changeset 4321bdeeb77f5a3
parent 431 d859199a15b7
child 433 742bb3f91603
Processor for @JavaScriptBody annotation to warn when incorrect number of args is used and to provide code completion to generate the field
core/pom.xml
core/src/main/java/org/apidesign/bck2brwsr/core/impl/JavaScriptProcesor.java
     1.1 --- a/core/pom.xml	Sat Jan 12 15:44:09 2013 +0100
     1.2 +++ b/core/pom.xml	Sat Jan 12 16:29:29 2013 +0100
     1.3 @@ -19,8 +19,8 @@
     1.4                  <artifactId>maven-compiler-plugin</artifactId>
     1.5                  <version>2.3.2</version>
     1.6                  <configuration>
     1.7 -                    <source>1.6</source>
     1.8 -                    <target>1.6</target>
     1.9 +                    <source>1.7</source>
    1.10 +                    <target>1.7</target>
    1.11                  </configuration>
    1.12              </plugin>
    1.13          </plugins>
    1.14 @@ -35,6 +35,10 @@
    1.15        <version>3.8.1</version>
    1.16        <scope>test</scope>
    1.17      </dependency>
    1.18 +    <dependency>
    1.19 +      <groupId>org.netbeans.api</groupId>
    1.20 +      <artifactId>org-openide-util-lookup</artifactId>
    1.21 +    </dependency>
    1.22    </dependencies>
    1.23      <description>Contains esential annotations for associating JavaScript code with
    1.24  methods and classes.</description>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/core/src/main/java/org/apidesign/bck2brwsr/core/impl/JavaScriptProcesor.java	Sat Jan 12 16:29:29 2013 +0100
     2.3 @@ -0,0 +1,104 @@
     2.4 +/**
     2.5 + * Back 2 Browser Bytecode Translator
     2.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, version 2 of the License.
    2.11 + *
    2.12 + * This program is distributed in the hope that it will be useful,
    2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.15 + * GNU General Public License for more details.
    2.16 + *
    2.17 + * You should have received a copy of the GNU General Public License
    2.18 + * along with this program. Look for COPYING file in the top folder.
    2.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    2.20 + */
    2.21 +package org.apidesign.bck2brwsr.core.impl;
    2.22 +
    2.23 +import java.util.Collections;
    2.24 +import java.util.HashSet;
    2.25 +import java.util.List;
    2.26 +import java.util.Set;
    2.27 +import javax.annotation.processing.AbstractProcessor;
    2.28 +import javax.annotation.processing.Completion;
    2.29 +import javax.annotation.processing.Completions;
    2.30 +import javax.annotation.processing.Processor;
    2.31 +import javax.annotation.processing.RoundEnvironment;
    2.32 +import javax.lang.model.element.AnnotationMirror;
    2.33 +import javax.lang.model.element.Element;
    2.34 +import javax.lang.model.element.ElementKind;
    2.35 +import javax.lang.model.element.ExecutableElement;
    2.36 +import javax.lang.model.element.Modifier;
    2.37 +import javax.lang.model.element.TypeElement;
    2.38 +import javax.lang.model.element.VariableElement;
    2.39 +import javax.tools.Diagnostic;
    2.40 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
    2.41 +import org.openide.util.lookup.ServiceProvider;
    2.42 +
    2.43 +/**
    2.44 + *
    2.45 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.46 + */
    2.47 +@ServiceProvider(service = Processor.class)
    2.48 +public final class JavaScriptProcesor extends AbstractProcessor {
    2.49 +    @Override
    2.50 +    public Set<String> getSupportedAnnotationTypes() {
    2.51 +        Set<String> set = new HashSet<>();
    2.52 +        set.add(JavaScriptBody.class.getName());
    2.53 +        return set;
    2.54 +    }
    2.55 +    
    2.56 +    @Override
    2.57 +    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    2.58 +        for (Element e : roundEnv.getElementsAnnotatedWith(JavaScriptBody.class)) {
    2.59 +            if (e.getKind() != ElementKind.METHOD) {
    2.60 +                continue;
    2.61 +            }
    2.62 +            ExecutableElement ee = (ExecutableElement)e;
    2.63 +            List<? extends VariableElement> params = ee.getParameters();
    2.64 +            
    2.65 +            JavaScriptBody jsb = e.getAnnotation(JavaScriptBody.class);
    2.66 +            String[] arr = jsb.args();
    2.67 +            int indx;
    2.68 +            if (!ee.getModifiers().contains(Modifier.STATIC)) {
    2.69 +                indx = 1;
    2.70 +            } else {
    2.71 +                indx = 0;
    2.72 +            }
    2.73 +            if (indx + params.size() != arr.length) {
    2.74 +                processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Number of args arguments does not match real arguments!", e);
    2.75 +            }
    2.76 +        }
    2.77 +        return true;
    2.78 +    }
    2.79 +
    2.80 +    @Override
    2.81 +    public Iterable<? extends Completion> getCompletions(Element e, 
    2.82 +        AnnotationMirror annotation, ExecutableElement member, String userText
    2.83 +    ) {
    2.84 +        StringBuilder sb = new StringBuilder();
    2.85 +        if (e.getKind() == ElementKind.METHOD && member.getSimpleName().contentEquals("args")) {
    2.86 +            ExecutableElement ee = (ExecutableElement) e;
    2.87 +            String sep;
    2.88 +            if (!ee.getModifiers().contains(Modifier.STATIC)) {
    2.89 +                sb.append("{ \"self\"");
    2.90 +                sep = ", ";
    2.91 +            } else {
    2.92 +                sb.append("{ ");
    2.93 +                sep = "";
    2.94 +            }
    2.95 +            for (VariableElement ve : ee.getParameters()) {
    2.96 +                sb.append(sep).append('"').append(ve.getSimpleName())
    2.97 +                    .append('"');
    2.98 +                sep = ", ";
    2.99 +            }
   2.100 +            sb.append(" }");
   2.101 +            return Collections.nCopies(1, Completions.of(sb.toString()));
   2.102 +        }
   2.103 +        return null;
   2.104 +    }
   2.105 +
   2.106 +    
   2.107 +}