core/src/main/java/org/apidesign/bck2brwsr/core/impl/JavaScriptProcesor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 06 Feb 2013 15:47:06 +0100
branchemul
changeset 687 a9e506a27b55
parent 443 9359b006782b
child 757 70ff060e1674
permissions -rw-r--r--
Workaround of some problems with bytecode conversion demonstrated by ZipCompatibilityTest. Splitting the code into two new methods.
     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.TypeElement;
    34 import javax.lang.model.element.VariableElement;
    35 import javax.tools.Diagnostic;
    36 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    37 import org.openide.util.lookup.ServiceProvider;
    38 
    39 /**
    40  *
    41  * @author Jaroslav Tulach <jtulach@netbeans.org>
    42  */
    43 @ServiceProvider(service = Processor.class)
    44 public final class JavaScriptProcesor extends AbstractProcessor {
    45     @Override
    46     public Set<String> getSupportedAnnotationTypes() {
    47         Set<String> set = new HashSet<>();
    48         set.add(JavaScriptBody.class.getName());
    49         return set;
    50     }
    51     
    52     @Override
    53     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    54         for (Element e : roundEnv.getElementsAnnotatedWith(JavaScriptBody.class)) {
    55             if (e.getKind() != ElementKind.METHOD && e.getKind() != ElementKind.CONSTRUCTOR) {
    56                 continue;
    57             }
    58             ExecutableElement ee = (ExecutableElement)e;
    59             List<? extends VariableElement> params = ee.getParameters();
    60             
    61             JavaScriptBody jsb = e.getAnnotation(JavaScriptBody.class);
    62             if (jsb == null) {
    63                 continue;
    64             }
    65             String[] arr = jsb.args();
    66             if (params.size() != arr.length) {
    67                 processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Number of args arguments does not match real arguments!", e);
    68             }
    69         }
    70         return true;
    71     }
    72 
    73     @Override
    74     public Iterable<? extends Completion> getCompletions(Element e, 
    75         AnnotationMirror annotation, ExecutableElement member, String userText
    76     ) {
    77         StringBuilder sb = new StringBuilder();
    78         if (e.getKind() == ElementKind.METHOD && member.getSimpleName().contentEquals("args")) {
    79             ExecutableElement ee = (ExecutableElement) e;
    80             String sep = "";
    81             sb.append("{ ");
    82             for (VariableElement ve : ee.getParameters()) {
    83                 sb.append(sep).append('"').append(ve.getSimpleName())
    84                     .append('"');
    85                 sep = ", ";
    86             }
    87             sb.append(" }");
    88             return Collections.nCopies(1, Completions.of(sb.toString()));
    89         }
    90         return null;
    91     }
    92 
    93     
    94 }