geo/src/main/java/org/netbeans/html/geo/impl/GeoProcessor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 16 Dec 2013 16:59:43 +0100
branchnetbeans
changeset 362 92fb71afdc0e
parent 358 geo/src/main/java/org/apidesign/html/geo/impl/GeoProcessor.java@80702021b851
child 365 5c93ad8c7a15
permissions -rw-r--r--
Moving implementation classes into org.netbeans.html namespace
     1 /**
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
     5  *
     6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     7  * Other names may be trademarks of their respective owners.
     8  *
     9  * The contents of this file are subject to the terms of either the GNU
    10  * General Public License Version 2 only ("GPL") or the Common
    11  * Development and Distribution License("CDDL") (collectively, the
    12  * "License"). You may not use this file except in compliance with the
    13  * License. You can obtain a copy of the License at
    14  * http://www.netbeans.org/cddl-gplv2.html
    15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    16  * specific language governing permissions and limitations under the
    17  * License.  When distributing the software, include this License Header
    18  * Notice in each file and include the License file at
    19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    20  * particular file as subject to the "Classpath" exception as provided
    21  * by Oracle in the GPL Version 2 section of the License file that
    22  * accompanied this code. If applicable, add the following below the
    23  * License Header, with the fields enclosed by brackets [] replaced by
    24  * your own identifying information:
    25  * "Portions Copyrighted [year] [name of copyright owner]"
    26  *
    27  * Contributor(s):
    28  *
    29  * The Original Software is NetBeans. The Initial Developer of the Original
    30  * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
    31  *
    32  * If you wish your version of this file to be governed by only the CDDL
    33  * or only the GPL Version 2, indicate your decision by adding
    34  * "[Contributor] elects to include this software in this distribution
    35  * under the [CDDL or GPL Version 2] license." If you do not indicate a
    36  * single choice of license, a recipient has the option to distribute
    37  * your version of this file under either the CDDL, the GPL Version 2 or
    38  * to extend the choice of license to its licensees as provided above.
    39  * However, if you add GPL Version 2 code and therefore, elected the GPL
    40  * Version 2 license, then the option applies only if the new code is
    41  * made subject to such option by the copyright holder.
    42  */
    43 package org.netbeans.html.geo.impl;
    44 
    45 import java.io.IOException;
    46 import java.io.Writer;
    47 import java.util.List;
    48 import java.util.Locale;
    49 import java.util.Set;
    50 import java.util.logging.Level;
    51 import java.util.logging.Logger;
    52 import javax.annotation.processing.AbstractProcessor;
    53 import javax.annotation.processing.Processor;
    54 import javax.annotation.processing.RoundEnvironment;
    55 import javax.annotation.processing.SupportedAnnotationTypes;
    56 import javax.annotation.processing.SupportedSourceVersion;
    57 import javax.lang.model.SourceVersion;
    58 import javax.lang.model.element.Element;
    59 import javax.lang.model.element.ElementKind;
    60 import javax.lang.model.element.ExecutableElement;
    61 import javax.lang.model.element.Modifier;
    62 import javax.lang.model.element.PackageElement;
    63 import javax.lang.model.element.TypeElement;
    64 import javax.lang.model.element.VariableElement;
    65 import javax.lang.model.type.TypeMirror;
    66 import javax.tools.Diagnostic;
    67 import javax.tools.JavaFileObject;
    68 import net.java.html.geo.OnLocation;
    69 import net.java.html.geo.Position;
    70 import org.openide.util.lookup.ServiceProvider;
    71 
    72 /** Annotation processor to generate callbacks from {@link GeoHandle} class.
    73  *
    74  * @author Jaroslav Tulach <jtulach@netbeans.org>
    75  */
    76 @ServiceProvider(service=Processor.class)
    77 @SupportedSourceVersion(SourceVersion.RELEASE_6)
    78 @SupportedAnnotationTypes({
    79     "net.java.html.geo.OnLocation"
    80 })
    81 public final class GeoProcessor extends AbstractProcessor {
    82     private static final Logger LOG = Logger.getLogger(GeoProcessor.class.getName());
    83     @Override
    84     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    85         boolean ok = true;
    86         for (Element e : roundEnv.getElementsAnnotatedWith(OnLocation.class)) {
    87             if (!processLocation(e)) {
    88                 ok = false;
    89             }
    90         }
    91         return ok;
    92     }
    93 
    94     private void error(String msg, Element e) {
    95         processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, msg, e);
    96     }
    97     
    98     private boolean processLocation(Element e) {
    99         if (e.getKind() != ElementKind.METHOD) {
   100             return false;
   101         }
   102         ExecutableElement me = (ExecutableElement) e;
   103         OnLocation ol = e.getAnnotation(OnLocation.class);
   104         if (ol == null) {
   105             return true;
   106         }
   107         if (me.getModifiers().contains(Modifier.PRIVATE)) {
   108             error("Method annotated by @OnLocation cannot be private", e);
   109             return false;
   110         }
   111         TypeMirror positionClass = processingEnv.getElementUtils().getTypeElement(Position.class.getName()).asType();
   112         final List<? extends VariableElement> params = me.getParameters();
   113         if (params.size() < 1 || !params.get(0).asType().equals(positionClass)) {
   114             error("Method annotated by @OnLocation first argument must be net.java.html.geo.Position!", e);
   115             return false;
   116         }
   117         String className = ol.className();
   118         if (className.isEmpty()) {
   119             String n = e.getSimpleName().toString();
   120             if (n.isEmpty()) {
   121                 error("Empty method name", e);
   122                 return false;
   123             }
   124             final String firstLetter = n.substring(0, 1).toUpperCase(Locale.ENGLISH);
   125             className = firstLetter + n.substring(1) + "Handle";
   126         }
   127         TypeElement te = (TypeElement)e.getEnclosingElement();
   128         PackageElement pe = (PackageElement) te.getEnclosingElement();
   129         final String pkg = pe.getQualifiedName().toString();
   130         final String fqn = pkg + "." + className;
   131         final boolean isStatic = me.getModifiers().contains(Modifier.STATIC);
   132         String sep;
   133         try {
   134             JavaFileObject fo = processingEnv.getFiler().createSourceFile(fqn, e);
   135             Writer w = fo.openWriter();
   136             w.append("package ").append(pkg).append(";\n");
   137             w.append("class ").append(className).append(" extends net.java.html.geo.Position.Handle {\n");
   138             if (!isStatic) {
   139                 w.append("  private final ").append(te.getSimpleName()).append(" $i;\n");
   140             }
   141             for (int i = 1; i < params.size(); i++) {
   142                 final VariableElement p = params.get(i);
   143                 w.append("  private final ").append(p.asType().toString()).append(" ").append(p.getSimpleName()).append(";\n");
   144             }
   145             w.append("  private ").append(className).append("(boolean oneTime");
   146             w.append(", ").append(te.getSimpleName()).append(" i");
   147             for (int i = 1; i < params.size(); i++) {
   148                 final VariableElement p = params.get(i);
   149                 w.append(", ").append(p.asType().toString()).append(" ").append(p.getSimpleName());
   150             }
   151             w.append(") {\n    super(oneTime);\n");
   152             if (!isStatic) {
   153                 w.append("    this.$i = i;\n");
   154             }
   155             for (int i = 1; i < params.size(); i++) {
   156                 final VariableElement p = params.get(i);
   157                 w.append("  this.").append(p.getSimpleName()).append(" = ").append(p.getSimpleName()).append(";\n");
   158             }
   159             w.append("}\n");
   160             w.append("  static net.java.html.geo.Position.Handle createQuery(");
   161             String inst;
   162             if (!isStatic) {
   163                 w.append(te.getSimpleName()).append(" instance");
   164                 inst = "instance";
   165                 sep = ", ";
   166             } else {
   167                 inst = "null";
   168                 sep = "";
   169             }
   170             for (int i = 1; i < params.size(); i++) {
   171                 final VariableElement p = params.get(i);
   172                 w.append(sep).append(p.asType().toString()).append(" ").append(p.getSimpleName());
   173                 sep = ", ";
   174             }
   175             w.append(") { return new ").append(className).append("(true, ").append(inst);
   176             for (int i = 1; i < params.size(); i++) {
   177                 final VariableElement p = params.get(i);
   178                 w.append(", ").append(p.getSimpleName());
   179             }
   180             w.append("); }\n");
   181             w.append("  static net.java.html.geo.Position.Handle createWatch(");
   182             if (!isStatic) {
   183                 w.append(te.getSimpleName()).append(" instance");
   184                 sep = ", ";
   185             } else {
   186                 sep = "";
   187             }
   188             for (int i = 1; i < params.size(); i++) {
   189                 final VariableElement p = params.get(i);
   190                 w.append(sep).append(p.asType().toString()).append(" ").append(p.getSimpleName());
   191             }
   192             w.append(") { return new ").append(className).append("(false, ").append(inst);
   193             for (int i = 1; i < params.size(); i++) {
   194                 final VariableElement p = params.get(i);
   195                 w.append(", ").append(p.getSimpleName());
   196             }
   197             w.append("); }\n");
   198             w.append("  @Override protected void onError(Exception t) throws Throwable {\n");
   199             if (ol.onError().isEmpty()) {
   200                 w.append("    t.printStackTrace();");
   201             } else {
   202                 if (!findOnError(me, te, ol.onError(), isStatic)) {
   203                     return false;
   204                 }
   205                 if (isStatic) {
   206                     w.append("    ").append(te.getSimpleName()).append(".");
   207                 } else {
   208                     w.append("    $i.");
   209                 }
   210                 w.append(ol.onError()).append("(t");
   211                 for (int i = 1; i < params.size(); i++) {
   212                     final VariableElement p = params.get(i);
   213                     w.append(", ").append(p.getSimpleName());
   214                 }
   215                 w.append(");\n");
   216             }
   217             w.append("  }\n");
   218             w.append("  @Override protected void onLocation(net.java.html.geo.Position p) throws Throwable {\n");
   219             if (isStatic) {
   220                 w.append("    ").append(te.getSimpleName()).append(".");
   221             } else {
   222                 w.append("    $i.");
   223             }
   224             w.append(me.getSimpleName()).append("(p");
   225             for (int i = 1; i < params.size(); i++) {
   226                 final VariableElement p = params.get(i);
   227                 w.append(", ").append(p.getSimpleName());
   228             }
   229             w.append(");\n");
   230             w.append("  }\n");
   231             w.append("}\n");
   232             w.close();
   233         } catch (IOException ex) {
   234             Logger.getLogger(GeoProcessor.class.getName()).log(Level.SEVERE, null, ex);
   235             error("Can't write handler class: " + ex.getMessage(), e);
   236             return false;
   237         }
   238         
   239         return true;
   240     }
   241 
   242     private boolean findOnError(ExecutableElement errElem, TypeElement te, String name, boolean onlyStatic) {
   243         String err = null;
   244         METHODS: for (Element e : te.getEnclosedElements()) {
   245             if (e.getKind() != ElementKind.METHOD) {
   246                 continue;
   247             }
   248             if (!e.getSimpleName().contentEquals(name)) {
   249                 continue;
   250             }
   251             if (onlyStatic && !e.getModifiers().contains(Modifier.STATIC)) {
   252                 errElem = (ExecutableElement) e;
   253                 err = "Would have to be static";
   254                 continue;
   255             }
   256             ExecutableElement ee = (ExecutableElement) e;
   257             TypeMirror excType = processingEnv.getElementUtils().getTypeElement(Exception.class.getName()).asType();
   258             final List<? extends VariableElement> params = ee.getParameters(); 
   259             if (params.size() < 1 || 
   260                 !processingEnv.getTypeUtils().isAssignable(excType, ee.getParameters().get(0).asType())
   261             ) {
   262                 errElem = (ExecutableElement) e;
   263                 err = "Error method first argument needs to be Exception";
   264                 continue;
   265             }
   266             final List<? extends Element> origParams = errElem.getParameters();
   267             if (params.size() != origParams.size()) {
   268                 errElem = (ExecutableElement) e;
   269                 err = "Error method must have the same parameters as @OnLocation one";
   270                 continue;
   271             }
   272             for (int i = 1; i < origParams.size(); i++) {
   273                 final TypeMirror t1 = params.get(i).asType();
   274                 final TypeMirror t2 = origParams.get(i).asType();
   275                 if (!processingEnv.getTypeUtils().isSameType(t1, t2)) {
   276                     errElem = (ExecutableElement) e;
   277                     err = "Error method must have the same parameters as @OnLocation one";
   278                     continue METHODS;
   279                 }
   280             }
   281             return true;
   282         }
   283         if (err == null) {
   284             err = "Cannot find " + name + "(Exception) method in this class";
   285         }
   286         error(err, errElem);
   287         return false;
   288     }
   289 }