2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
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]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
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.
43 package org.netbeans.html.geo.impl;
45 import java.io.IOException;
46 import java.io.Writer;
47 import java.util.List;
48 import java.util.Locale;
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;
72 /** Annotation processor to generate callbacks from {@link GeoHandle} class.
74 * @author Jaroslav Tulach <jtulach@netbeans.org>
76 @ServiceProvider(service=Processor.class)
77 @SupportedSourceVersion(SourceVersion.RELEASE_6)
78 @SupportedAnnotationTypes({
79 "net.java.html.geo.OnLocation"
81 public final class GeoProcessor extends AbstractProcessor {
82 private static final Logger LOG = Logger.getLogger(GeoProcessor.class.getName());
84 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
86 for (Element e : roundEnv.getElementsAnnotatedWith(OnLocation.class)) {
87 if (!processLocation(e)) {
94 private void error(String msg, Element e) {
95 processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, msg, e);
98 private boolean processLocation(Element e) {
99 if (e.getKind() != ElementKind.METHOD) {
102 ExecutableElement me = (ExecutableElement) e;
103 OnLocation ol = e.getAnnotation(OnLocation.class);
107 if (me.getModifiers().contains(Modifier.PRIVATE)) {
108 error("Method annotated by @OnLocation cannot be private", e);
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);
117 String className = ol.className();
118 if (className.isEmpty()) {
119 String n = e.getSimpleName().toString();
121 error("Empty method name", e);
124 final String firstLetter = n.substring(0, 1).toUpperCase(Locale.ENGLISH);
125 className = firstLetter + n.substring(1) + "Handle";
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);
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");
139 w.append(" private final ").append(te.getSimpleName()).append(" $i;\n");
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");
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());
151 w.append(") {\n super(oneTime);\n");
153 w.append(" this.$i = i;\n");
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");
160 w.append(" static net.java.html.geo.Position.Handle createQuery(");
163 w.append(te.getSimpleName()).append(" instance");
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());
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());
181 w.append(" static net.java.html.geo.Position.Handle createWatch(");
183 w.append(te.getSimpleName()).append(" instance");
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());
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());
198 w.append(" @Override protected void onError(Exception t) throws Throwable {\n");
199 if (ol.onError().isEmpty()) {
200 w.append(" t.printStackTrace();");
202 if (!findOnError(me, te, ol.onError(), isStatic)) {
206 w.append(" ").append(te.getSimpleName()).append(".");
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());
218 w.append(" @Override protected void onLocation(net.java.html.geo.Position p) throws Throwable {\n");
220 w.append(" ").append(te.getSimpleName()).append(".");
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());
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);
242 private boolean findOnError(ExecutableElement errElem, TypeElement te, String name, boolean onlyStatic) {
244 METHODS: for (Element e : te.getEnclosedElements()) {
245 if (e.getKind() != ElementKind.METHOD) {
248 if (!e.getSimpleName().contentEquals(name)) {
251 if (onlyStatic && !e.getModifiers().contains(Modifier.STATIC)) {
252 errElem = (ExecutableElement) e;
253 err = "Would have to be static";
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())
262 errElem = (ExecutableElement) e;
263 err = "Error method first argument needs to be Exception";
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";
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";
284 err = "Cannot find " + name + "(Exception) method in this class";