core/src/main/java/org/apidesign/bck2brwsr/core/impl/JavaScriptProcesor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 12 Jan 2013 16:29:29 +0100
changeset 432 1bdeeb77f5a3
child 443 9359b006782b
permissions -rw-r--r--
Processor for @JavaScriptBody annotation to warn when incorrect number of args is used and to provide code completion to generate the field
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.core.impl;
    19 
    20 import java.util.Collections;
    21 import java.util.HashSet;
    22 import java.util.List;
    23 import java.util.Set;
    24 import javax.annotation.processing.AbstractProcessor;
    25 import javax.annotation.processing.Completion;
    26 import javax.annotation.processing.Completions;
    27 import javax.annotation.processing.Processor;
    28 import javax.annotation.processing.RoundEnvironment;
    29 import javax.lang.model.element.AnnotationMirror;
    30 import javax.lang.model.element.Element;
    31 import javax.lang.model.element.ElementKind;
    32 import javax.lang.model.element.ExecutableElement;
    33 import javax.lang.model.element.Modifier;
    34 import javax.lang.model.element.TypeElement;
    35 import javax.lang.model.element.VariableElement;
    36 import javax.tools.Diagnostic;
    37 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    38 import org.openide.util.lookup.ServiceProvider;
    39 
    40 /**
    41  *
    42  * @author Jaroslav Tulach <jtulach@netbeans.org>
    43  */
    44 @ServiceProvider(service = Processor.class)
    45 public final class JavaScriptProcesor extends AbstractProcessor {
    46     @Override
    47     public Set<String> getSupportedAnnotationTypes() {
    48         Set<String> set = new HashSet<>();
    49         set.add(JavaScriptBody.class.getName());
    50         return set;
    51     }
    52     
    53     @Override
    54     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    55         for (Element e : roundEnv.getElementsAnnotatedWith(JavaScriptBody.class)) {
    56             if (e.getKind() != ElementKind.METHOD) {
    57                 continue;
    58             }
    59             ExecutableElement ee = (ExecutableElement)e;
    60             List<? extends VariableElement> params = ee.getParameters();
    61             
    62             JavaScriptBody jsb = e.getAnnotation(JavaScriptBody.class);
    63             String[] arr = jsb.args();
    64             int indx;
    65             if (!ee.getModifiers().contains(Modifier.STATIC)) {
    66                 indx = 1;
    67             } else {
    68                 indx = 0;
    69             }
    70             if (indx + params.size() != arr.length) {
    71                 processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Number of args arguments does not match real arguments!", e);
    72             }
    73         }
    74         return true;
    75     }
    76 
    77     @Override
    78     public Iterable<? extends Completion> getCompletions(Element e, 
    79         AnnotationMirror annotation, ExecutableElement member, String userText
    80     ) {
    81         StringBuilder sb = new StringBuilder();
    82         if (e.getKind() == ElementKind.METHOD && member.getSimpleName().contentEquals("args")) {
    83             ExecutableElement ee = (ExecutableElement) e;
    84             String sep;
    85             if (!ee.getModifiers().contains(Modifier.STATIC)) {
    86                 sb.append("{ \"self\"");
    87                 sep = ", ";
    88             } else {
    89                 sb.append("{ ");
    90                 sep = "";
    91             }
    92             for (VariableElement ve : ee.getParameters()) {
    93                 sb.append(sep).append('"').append(ve.getSimpleName())
    94                     .append('"');
    95                 sep = ", ";
    96             }
    97             sb.append(" }");
    98             return Collections.nCopies(1, Completions.of(sb.toString()));
    99         }
   100         return null;
   101     }
   102 
   103     
   104 }