📄 httpurlannotationprocessor.java
字号:
pw.println(" <run class=\"" + pa.className + "\" method=\"" + pa.methodName + pa.paramsFormatted + "\"/>");
pw.println("</catch>");
pw.flush();
}
}
private ProcessedHttpUrlAnnotation processHttpUrlAnnotation(Declaration declaration) {
HttpUrl httpUrl = declaration.getAnnotation(HttpUrl.class);
return new ProcessedHttpUrlAnnotation(HttpUrl.class.getName(), declaration, httpUrl.value(), httpUrl.weight());
}
private ProcessedHttpExceptionAnnotation processHttpExceptionHandlerAnnotation(Declaration declaration) {
SourcePosition position = declaration.getPosition();
if (!(declaration instanceof MethodDeclaration)) {
messager.printWarning(declaration.getPosition(), "@HttpExceptionHandler declared on a non-method " + position);
return null;
}
MethodDeclaration methodDeclaration = (MethodDeclaration) declaration;
HttpExceptionHandler httpExceptionHandler = declaration.getAnnotation(HttpExceptionHandler.class);
String className = methodDeclaration.getDeclaringType().getQualifiedName();
ProcessedHttpExceptionAnnotation ea = new ProcessedHttpExceptionAnnotation();
ea.exceptionName = httpExceptionHandler.value(); //.getName();
ea.methodName = declaration.getSimpleName();
ea.docComment = declaration.getDocComment();
ea.className = className;
ea.setParams(methodDeclaration.getParameters());
// out exceptionName might not be set
if ("[ unassigned ]".equals(ea.exceptionName) && methodDeclaration.getParameters().size() > 0) {
// use first param
ea.exceptionName = methodDeclaration.getParameters().iterator().next().getType().toString();
}
if (showPositionsOfAnnotations) {
messager.printNotice(position, "@HttpExceptionHandlerUrl value " + ea.value + " weight " + ea.weight);
}
return ea;
}
class ProcessedHttpUrlAnnotation implements Comparable<ProcessedHttpUrlAnnotation> {
public int weight = 0;
public String value;
public boolean chainUsed;
public String paramsFormatted;
public String methodName;
public String className;
public String docComment;
public String sourceRef;
private static final String FILTER_CHAIN_CLASS_NAME = "javax.servlet.FilterChain";
public ProcessedHttpUrlAnnotation() {
// empty
}
public ProcessedHttpUrlAnnotation(String typeName, Declaration declaration, String value, int weight) {
MethodDeclaration methodDeclaration = (MethodDeclaration) declaration;
String className = methodDeclaration.getDeclaringType().getQualifiedName();
this.methodName = declaration.getSimpleName();
this.docComment = declaration.getDocComment();
this.className = className;
this.value = value;
this.weight = weight;
this.setParams(methodDeclaration.getParameters());
String typeNameShort = typeName.substring(typeName.lastIndexOf("."));
SourcePosition positionInCode = declaration.getPosition();
sourceRef = positionInCode.file().getName() + ":" + positionInCode.line();
if (!(declaration instanceof MethodDeclaration)) {
messager.printWarning(positionInCode, "@" + typeNameShort + " declared on a non-method " + positionInCode);
}
if (showPositionsOfAnnotations) {
messager.printNotice(positionInCode, "@" + typeNameShort + " value " + value + " weight " + weight);
}
}
public int compareTo(ProcessedHttpUrlAnnotation other) {
if (this.weight < other.weight) return 1;
if (this.weight > other.weight) return -1;
if (this.className != null && other.className != null) {
int comp = this.className.compareTo(other.className);
if (comp != 0) return comp;
}
if (this.methodName != null && other.methodName != null) {
return this.methodName.compareTo(other.methodName);
}
return 0;
}
void setParams(Collection<ParameterDeclaration> params) {
paramsFormatted = "(";
chainUsed = false;
if (params.size() > 0) {
int i = 1;
for (ParameterDeclaration paramDeclaration : params) {
String paramType = paramDeclaration.getType().toString();
if (FILTER_CHAIN_CLASS_NAME.equals(paramType)) {
chainUsed = true;
}
paramsFormatted += (i == 1 ? "" : ", ") + paramType;
HttpParam httpParam = paramDeclaration.getAnnotation(HttpParam.class);
if (httpParam != null) {
paramsFormatted += " ";
if (!"[ unassigned ]".equals(httpParam.value())) {
paramsFormatted += httpParam.value();
} else {
paramsFormatted += paramDeclaration.getSimpleName();
}
}
i++;
}
}
paramsFormatted += ")";
}
}
class ProcessedHttpExceptionAnnotation extends ProcessedHttpUrlAnnotation {
public String exceptionName;
public int compareTo(ProcessedHttpExceptionAnnotation other) {
int comp = super.compareTo(other);
if (comp == 0) comp = exceptionName.compareTo(other.exceptionName);
return comp;
}
}
/**
* a very very basic xml escaper.
*
* @param s string to escape
* @return the escaped string
*/
private static String escapeXML(String s) {
if (s == null) {
return null;
}
final int length = s.length();
StringBuffer b = new StringBuffer();
for (int i = 0; i < length; i++) {
char c = s.charAt(i);
switch (c) {
case '&':
b.append("&");
break;
case '<':
b.append("<");
break;
case '>':
b.append(">");
break;
default:
b.append(c);
break;
}
}
return b.toString();
}
private static String padEachLine(String padWith, String str) {
StringBuffer out = new StringBuffer();
String[] lines = str.split("\n");
int i = 0;
while (i < lines.length) {
String line = lines[i];
out.append(padWith);
out.append(line);
i++;
if (i < lines.length) out.append('\n');
}
return out.toString();
}
private static boolean isBlank(final String str) {
return str == null || "".equals(str) || "".equals(str.trim());
}
private static void checkDirsExistMkdir(File dir) {
if (!dir.getParentFile().exists()) {
checkDirsExistMkdir(dir.getParentFile());
}
if (!dir.exists()) {
dir.mkdir();
}
}
private void debugMsg(String msg) {
if (!debug) return;
messager.printNotice("Debug: " + msg);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -