rt/core/src/main/java/org/apidesign/bck2brwsr/core/impl/JavaScriptProcesor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 757 core/src/main/java/org/apidesign/bck2brwsr/core/impl/JavaScriptProcesor.java@70ff060e1674
child 1502 fab4a72543ab
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
     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.Messager;
    28 import javax.annotation.processing.Processor;
    29 import javax.annotation.processing.RoundEnvironment;
    30 import javax.lang.model.element.AnnotationMirror;
    31 import javax.lang.model.element.Element;
    32 import javax.lang.model.element.ElementKind;
    33 import javax.lang.model.element.ExecutableElement;
    34 import javax.lang.model.element.TypeElement;
    35 import javax.lang.model.element.VariableElement;
    36 import javax.lang.model.type.TypeKind;
    37 import javax.tools.Diagnostic;
    38 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    39 import org.openide.util.lookup.ServiceProvider;
    40 
    41 /**
    42  *
    43  * @author Jaroslav Tulach <jtulach@netbeans.org>
    44  */
    45 @ServiceProvider(service = Processor.class)
    46 public final class JavaScriptProcesor extends AbstractProcessor {
    47     @Override
    48     public Set<String> getSupportedAnnotationTypes() {
    49         Set<String> set = new HashSet<>();
    50         set.add(JavaScriptBody.class.getName());
    51         return set;
    52     }
    53     
    54     @Override
    55     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    56         final Messager msg = processingEnv.getMessager();
    57         for (Element e : roundEnv.getElementsAnnotatedWith(JavaScriptBody.class)) {
    58             if (e.getKind() != ElementKind.METHOD && e.getKind() != ElementKind.CONSTRUCTOR) {
    59                 continue;
    60             }
    61             ExecutableElement ee = (ExecutableElement)e;
    62             List<? extends VariableElement> params = ee.getParameters();
    63             
    64             JavaScriptBody jsb = e.getAnnotation(JavaScriptBody.class);
    65             if (jsb == null) {
    66                 continue;
    67             }
    68             String[] arr = jsb.args();
    69             if (params.size() != arr.length) {
    70                 msg.printMessage(Diagnostic.Kind.ERROR, "Number of args arguments does not match real arguments!", e);
    71             }
    72             if (ee.getReturnType().getKind() == TypeKind.LONG) {
    73                 msg.printMessage(Diagnostic.Kind.WARNING, "Don't return long. Return double and convert it to long in Java code.", e);
    74             }
    75         }
    76         return true;
    77     }
    78 
    79     @Override
    80     public Iterable<? extends Completion> getCompletions(Element e, 
    81         AnnotationMirror annotation, ExecutableElement member, String userText
    82     ) {
    83         StringBuilder sb = new StringBuilder();
    84         if (e.getKind() == ElementKind.METHOD && member.getSimpleName().contentEquals("args")) {
    85             ExecutableElement ee = (ExecutableElement) e;
    86             String sep = "";
    87             sb.append("{ ");
    88             for (VariableElement ve : ee.getParameters()) {
    89                 sb.append(sep).append('"').append(ve.getSimpleName())
    90                     .append('"');
    91                 sep = ", ";
    92             }
    93             sb.append(" }");
    94             return Collections.nCopies(1, Completions.of(sb.toString()));
    95         }
    96         return null;
    97     }
    98 
    99     
   100 }