rt/core/src/main/java/org/apidesign/bck2brwsr/core/impl/JavaScriptProcesor.java
changeset 772 d382dacfd73f
parent 757 70ff060e1674
child 1502 fab4a72543ab
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/core/src/main/java/org/apidesign/bck2brwsr/core/impl/JavaScriptProcesor.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,100 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.bck2brwsr.core.impl;
    1.22 +
    1.23 +import java.util.Collections;
    1.24 +import java.util.HashSet;
    1.25 +import java.util.List;
    1.26 +import java.util.Set;
    1.27 +import javax.annotation.processing.AbstractProcessor;
    1.28 +import javax.annotation.processing.Completion;
    1.29 +import javax.annotation.processing.Completions;
    1.30 +import javax.annotation.processing.Messager;
    1.31 +import javax.annotation.processing.Processor;
    1.32 +import javax.annotation.processing.RoundEnvironment;
    1.33 +import javax.lang.model.element.AnnotationMirror;
    1.34 +import javax.lang.model.element.Element;
    1.35 +import javax.lang.model.element.ElementKind;
    1.36 +import javax.lang.model.element.ExecutableElement;
    1.37 +import javax.lang.model.element.TypeElement;
    1.38 +import javax.lang.model.element.VariableElement;
    1.39 +import javax.lang.model.type.TypeKind;
    1.40 +import javax.tools.Diagnostic;
    1.41 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
    1.42 +import org.openide.util.lookup.ServiceProvider;
    1.43 +
    1.44 +/**
    1.45 + *
    1.46 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.47 + */
    1.48 +@ServiceProvider(service = Processor.class)
    1.49 +public final class JavaScriptProcesor extends AbstractProcessor {
    1.50 +    @Override
    1.51 +    public Set<String> getSupportedAnnotationTypes() {
    1.52 +        Set<String> set = new HashSet<>();
    1.53 +        set.add(JavaScriptBody.class.getName());
    1.54 +        return set;
    1.55 +    }
    1.56 +    
    1.57 +    @Override
    1.58 +    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    1.59 +        final Messager msg = processingEnv.getMessager();
    1.60 +        for (Element e : roundEnv.getElementsAnnotatedWith(JavaScriptBody.class)) {
    1.61 +            if (e.getKind() != ElementKind.METHOD && e.getKind() != ElementKind.CONSTRUCTOR) {
    1.62 +                continue;
    1.63 +            }
    1.64 +            ExecutableElement ee = (ExecutableElement)e;
    1.65 +            List<? extends VariableElement> params = ee.getParameters();
    1.66 +            
    1.67 +            JavaScriptBody jsb = e.getAnnotation(JavaScriptBody.class);
    1.68 +            if (jsb == null) {
    1.69 +                continue;
    1.70 +            }
    1.71 +            String[] arr = jsb.args();
    1.72 +            if (params.size() != arr.length) {
    1.73 +                msg.printMessage(Diagnostic.Kind.ERROR, "Number of args arguments does not match real arguments!", e);
    1.74 +            }
    1.75 +            if (ee.getReturnType().getKind() == TypeKind.LONG) {
    1.76 +                msg.printMessage(Diagnostic.Kind.WARNING, "Don't return long. Return double and convert it to long in Java code.", e);
    1.77 +            }
    1.78 +        }
    1.79 +        return true;
    1.80 +    }
    1.81 +
    1.82 +    @Override
    1.83 +    public Iterable<? extends Completion> getCompletions(Element e, 
    1.84 +        AnnotationMirror annotation, ExecutableElement member, String userText
    1.85 +    ) {
    1.86 +        StringBuilder sb = new StringBuilder();
    1.87 +        if (e.getKind() == ElementKind.METHOD && member.getSimpleName().contentEquals("args")) {
    1.88 +            ExecutableElement ee = (ExecutableElement) e;
    1.89 +            String sep = "";
    1.90 +            sb.append("{ ");
    1.91 +            for (VariableElement ve : ee.getParameters()) {
    1.92 +                sb.append(sep).append('"').append(ve.getSimpleName())
    1.93 +                    .append('"');
    1.94 +                sep = ", ";
    1.95 +            }
    1.96 +            sb.append(" }");
    1.97 +            return Collections.nCopies(1, Completions.of(sb.toString()));
    1.98 +        }
    1.99 +        return null;
   1.100 +    }
   1.101 +
   1.102 +    
   1.103 +}