Providing JavaScript processor to verify correct number of args classloader
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 26 Jun 2013 15:11:58 +0200
branchclassloader
changeset 165823e9058cb4d
parent 164 7235d50dd452
child 166 a3c7866bfb61
Providing JavaScript processor to verify correct number of args
boot/pom.xml
boot/src/main/java/org/apidesign/html/boot/impl/JavaScriptProcesor.java
     1.1 --- a/boot/pom.xml	Wed Jun 26 15:11:17 2013 +0200
     1.2 +++ b/boot/pom.xml	Wed Jun 26 15:11:58 2013 +0200
     1.3 @@ -28,5 +28,10 @@
     1.4        <scope>test</scope>
     1.5        <type>jar</type>
     1.6      </dependency>
     1.7 +    <dependency>
     1.8 +      <groupId>org.netbeans.api</groupId>
     1.9 +      <artifactId>org-openide-util-lookup</artifactId>
    1.10 +      <scope>compile</scope>
    1.11 +    </dependency>
    1.12    </dependencies>
    1.13  </project>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/boot/src/main/java/org/apidesign/html/boot/impl/JavaScriptProcesor.java	Wed Jun 26 15:11:58 2013 +0200
     2.3 @@ -0,0 +1,104 @@
     2.4 +/**
     2.5 + * HTML via Java(tm) Language Bindings
     2.6 + * Copyright (C) 2013 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. apidesign.org
    2.16 + * designates this particular file as subject to the
    2.17 + * "Classpath" exception as provided by apidesign.org
    2.18 + * in the License file that accompanied this code.
    2.19 + *
    2.20 + * You should have received a copy of the GNU General Public License
    2.21 + * along with this program. Look for COPYING file in the top folder.
    2.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    2.23 + */
    2.24 +package org.apidesign.html.boot.impl;
    2.25 +
    2.26 +import java.util.Collections;
    2.27 +import java.util.HashSet;
    2.28 +import java.util.List;
    2.29 +import java.util.Set;
    2.30 +import javax.annotation.processing.AbstractProcessor;
    2.31 +import javax.annotation.processing.Completion;
    2.32 +import javax.annotation.processing.Completions;
    2.33 +import javax.annotation.processing.Messager;
    2.34 +import javax.annotation.processing.Processor;
    2.35 +import javax.annotation.processing.RoundEnvironment;
    2.36 +import javax.lang.model.element.AnnotationMirror;
    2.37 +import javax.lang.model.element.Element;
    2.38 +import javax.lang.model.element.ElementKind;
    2.39 +import javax.lang.model.element.ExecutableElement;
    2.40 +import javax.lang.model.element.TypeElement;
    2.41 +import javax.lang.model.element.VariableElement;
    2.42 +import javax.tools.Diagnostic;
    2.43 +import net.java.html.js.JavaScriptBody;
    2.44 +import net.java.html.js.JavaScriptResource;
    2.45 +import org.openide.util.lookup.ServiceProvider;
    2.46 +
    2.47 +/**
    2.48 + *
    2.49 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.50 + */
    2.51 +@ServiceProvider(service = Processor.class)
    2.52 +public final class JavaScriptProcesor extends AbstractProcessor {
    2.53 +    @Override
    2.54 +    public Set<String> getSupportedAnnotationTypes() {
    2.55 +        Set<String> set = new HashSet<String>();
    2.56 +        set.add(JavaScriptBody.class.getName());
    2.57 +        set.add(JavaScriptResource.class.getName());
    2.58 +        return set;
    2.59 +    }
    2.60 +    
    2.61 +    @Override
    2.62 +    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    2.63 +        final Messager msg = processingEnv.getMessager();
    2.64 +        for (Element e : roundEnv.getElementsAnnotatedWith(JavaScriptBody.class)) {
    2.65 +            if (e.getKind() != ElementKind.METHOD && e.getKind() != ElementKind.CONSTRUCTOR) {
    2.66 +                continue;
    2.67 +            }
    2.68 +            ExecutableElement ee = (ExecutableElement)e;
    2.69 +            List<? extends VariableElement> params = ee.getParameters();
    2.70 +            
    2.71 +            JavaScriptBody jsb = e.getAnnotation(JavaScriptBody.class);
    2.72 +            if (jsb == null) {
    2.73 +                continue;
    2.74 +            }
    2.75 +            String[] arr = jsb.args();
    2.76 +            if (params.size() != arr.length) {
    2.77 +                msg.printMessage(Diagnostic.Kind.ERROR, "Number of args arguments does not match real arguments!", e);
    2.78 +            }
    2.79 +            if (!jsb.javacall() && jsb.body().contains(".@")) {
    2.80 +                msg.printMessage(Diagnostic.Kind.WARNING, "Usage of .@ usually requires javacall=true", e);
    2.81 +            }
    2.82 +        }
    2.83 +        return true;
    2.84 +    }
    2.85 +
    2.86 +    @Override
    2.87 +    public Iterable<? extends Completion> getCompletions(Element e, 
    2.88 +        AnnotationMirror annotation, ExecutableElement member, String userText
    2.89 +    ) {
    2.90 +        StringBuilder sb = new StringBuilder();
    2.91 +        if (e.getKind() == ElementKind.METHOD && member.getSimpleName().contentEquals("args")) {
    2.92 +            ExecutableElement ee = (ExecutableElement) e;
    2.93 +            String sep = "";
    2.94 +            sb.append("{ ");
    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 +}