📄 httpurlannotationprocessor.java
字号:
/**
* Copyright (c) 2005-2007, Paul Tuckey
* All rights reserved.
* ====================================================================
* Licensed under the BSD License. Text as follows.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* - Neither the name tuckey.org nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*/
package org.tuckey.web.filters.urlrewrite.annotation;
import com.sun.mirror.apt.AnnotationProcessor;
import com.sun.mirror.apt.AnnotationProcessorEnvironment;
import com.sun.mirror.apt.Filer;
import com.sun.mirror.apt.Messager;
import com.sun.mirror.declaration.AnnotationTypeDeclaration;
import com.sun.mirror.declaration.Declaration;
import com.sun.mirror.declaration.MethodDeclaration;
import com.sun.mirror.declaration.ParameterDeclaration;
import com.sun.mirror.util.SourcePosition;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Annotation processor for UrlRewrite. Will search compiled classes for annotations and generate XML.
* <p/>
* Processor for JDK 1.5.
*
* @deprecated use UrlRewriteAnnotationProcessor.
*/
public class HttpUrlAnnotationProcessor implements AnnotationProcessor {
private AnnotationProcessorEnvironment environment;
private AnnotationTypeDeclaration httpUrlDeclaration;
private AnnotationTypeDeclaration httpExceptionHandlerDeclaration;
private List<ProcessedHttpUrlAnnotation> processedAnnotations = new ArrayList<ProcessedHttpUrlAnnotation>();
private List<ProcessedHttpExceptionAnnotation> httpExceptionHandlers = new ArrayList<ProcessedHttpExceptionAnnotation>();
private Messager messager;
private boolean showPositionsOfAnnotations = false;
private boolean debug = false;
public HttpUrlAnnotationProcessor(AnnotationProcessorEnvironment env) {
environment = env;
messager = env.getMessager();
// get the type declaration for the annotations we are processing for
httpUrlDeclaration = (AnnotationTypeDeclaration) environment.getTypeDeclaration(HttpUrl.class.getName());
httpExceptionHandlerDeclaration = (AnnotationTypeDeclaration) environment.getTypeDeclaration(HttpExceptionHandler.class.getName());
}
public void process() {
Map<String, String> options = environment.getOptions();
Set<String> keys = options.keySet();
String saveRulesTo = null;
for (String key : keys) {
if (key.startsWith("-AsaveRulesTo=")) {
saveRulesTo = key.substring("-AsaveRulesTo=".length());
}
if (key.startsWith("-AshowPositions=")) {
showPositionsOfAnnotations = "true".equalsIgnoreCase(key.substring("-AshowPositions=".length()));
}
if (key.startsWith("-Adebug=")) {
debug = "true".equalsIgnoreCase(key.substring("-Adebug=".length()));
}
}
debugMsg("Processing");
if (saveRulesTo == null) {
messager.printError("ERROR: conf option must be specified");
return;
}
File confFile = new File(saveRulesTo);
PrintWriter pw;
boolean delFile = false;
try {
if (!confFile.exists()) {
checkDirsExistMkdir(confFile.getParentFile());
confFile.createNewFile();
}
if (!confFile.canWrite()) throw new IOException("cannot write to " + confFile.getName());
pw = environment.getFiler().createTextFile(Filer.Location.CLASS_TREE, "", confFile, null);
} catch (IOException e) {
e.printStackTrace();
return;
}
try {
// Get all declarations that use the HttpUrl annotation.
debugMsg("Looking for @HttpUrl");
Collection<Declaration> urlDeclarations = environment.getDeclarationsAnnotatedWith(httpUrlDeclaration);
for (Declaration declaration : urlDeclarations) {
ProcessedHttpUrlAnnotation pa = processHttpUrlAnnotation(declaration);
if (pa == null) delFile = true;
else processedAnnotations.add(pa);
}
// Get all declarations that use the HttpExceptionHandler annotation.
debugMsg("Looking for @HttpExceptionHandler");
Collection<Declaration> exceptionDeclarations = environment.getDeclarationsAnnotatedWith(httpExceptionHandlerDeclaration);
for (Declaration declaration : exceptionDeclarations) {
ProcessedHttpExceptionAnnotation phea = processHttpExceptionHandlerAnnotation(declaration);
if (phea == null) delFile = true;
else httpExceptionHandlers.add(phea);
}
if (processedAnnotations.size() > 0) {
messager.printNotice("Got " + processedAnnotations.size() + " @HttpUrl annotations");
Collections.sort(processedAnnotations);
}
if (httpExceptionHandlers.size() > 0) {
messager.printNotice("Got " + httpExceptionHandlers.size() + " @HttpExceptionHandler annotations");
Collections.sort(httpExceptionHandlers);
}
if (!delFile) {
environment.getMessager().printNotice("Writing to " + confFile);
outputRules(pw);
outputExceptionHandlers(pw);
} else {
confFile.delete();
}
} catch (Throwable t) {
delFile = true;
t.printStackTrace();
}
if (delFile) {
messager.printError("Error occured during processing deleting generated file.");
confFile.delete();
}
pw.close();
}
private void outputRules(PrintWriter pw) {
for (ProcessedHttpUrlAnnotation pa : processedAnnotations) {
pw.println("<rule>");
pw.println(" <name>" + pa.sourceRef + "</name>");
if (!isBlank(pa.docComment)) {
pw.println(" <note>");
pw.println(padEachLine(" ", escapeXML(pa.docComment)));
pw.println(" </note>");
}
pw.println(" <from>" + pa.value + "</from>");
pw.println(" <run class=\"" + pa.className + "\" method=\"" + pa.methodName + pa.paramsFormatted +
"\" />");
if (!pa.chainUsed) {
pw.println(" <to>null</to>");
}
pw.println("</rule>");
pw.flush();
}
}
private void outputExceptionHandlers(PrintWriter pw) {
for (ProcessedHttpExceptionAnnotation pa : httpExceptionHandlers) {
pw.println("<catch class=\"" + pa.exceptionName + "\">");
if (!isBlank(pa.docComment)) {
pw.println(" <note>");
pw.println(padEachLine(" ", escapeXML(pa.docComment)));
pw.println(" </note>");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -