📄 urlrewriteannotationprocessor.java
字号:
if (!ElementKind.METHOD.equals(declaration.getKind())) {
errorMsg("declared on a non-method (type is " + declaration.getKind() + ")", declaration);
return null;
}
ExecutableElement methodDeclaration = (ExecutableElement) declaration;
className = methodDeclaration.getEnclosingElement().getSimpleName().toString();
methodName = declaration.getSimpleName().toString();
docComment = elementUtils.getDocComment(declaration);
return methodDeclaration;
}
public int compareTo(ProcessedUrlRewriteFilterAnnotation other) {
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<? extends VariableElement> params) {
paramsFormatted = "(";
if (params.size() > 0) {
int i = 1;
for (VariableElement paramDeclaration : params) {
String paramType = paramDeclaration.asType().toString();
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 ProcessedHttpJsonAnnotation extends ProcessedHttpUrlAnnotation {
public ProcessedHttpJsonAnnotation(Element declaration) {
ExecutableElement methodDeclaration = init(declaration);
if (methodDeclaration == null) return;
HttpJson httpJson = declaration.getAnnotation(HttpJson.class);
if ("[ unassigned ]".equals(httpJson.value()) ) {
this.value = rpcBase + this.className + "/" + this.methodName;
} else {
this.value = httpJson.value();
}
this.weight = httpJson.weight();
setParams(methodDeclaration.getParameters());
if (showPositions) {
messager.printMessage(Diagnostic.Kind.NOTE, "@HttpJson value " + value + " weight " + weight, methodDeclaration);
}
}
}
class ProcessedHttpUrlAnnotation extends ProcessedUrlRewriteFilterAnnotation {
public int weight = 0;
public String value;
public boolean chainUsed;
public String sourceRef;
private static final String FILTER_CHAIN_CLASS_NAME = "javax.servlet.FilterChain";
public ProcessedHttpUrlAnnotation() {
// empty
}
public ProcessedHttpUrlAnnotation(Element declaration) {
ExecutableElement methodDeclaration = init(declaration);
if (methodDeclaration == null) return;
HttpUrl httpUrl = declaration.getAnnotation(HttpUrl.class);
this.value = httpUrl.value();
this.weight = httpUrl.weight();
setParams(methodDeclaration.getParameters());
if (showPositions) {
messager.printMessage(Diagnostic.Kind.NOTE, "@HttpUrl value " + value + " weight " + weight, methodDeclaration);
}
}
public int compareTo(ProcessedHttpUrlAnnotation other) {
if (this.weight < other.weight) return 1;
if (this.weight > other.weight) return -1;
return super.compareTo(other);
}
protected void setParams(Collection<? extends VariableElement> params) {
chainUsed = false;
if (params.size() > 0) {
for (VariableElement paramDeclaration : params) {
String paramType = paramDeclaration.asType().toString();
if (FILTER_CHAIN_CLASS_NAME.equals(paramType)) {
chainUsed = true;
}
}
}
super.setParams(params);
}
}
class ProcessedHttpExceptionAnnotation extends ProcessedUrlRewriteFilterAnnotation {
public String exceptionName;
public ProcessedHttpExceptionAnnotation(Element declaration) {
ExecutableElement methodDeclaration = init(declaration);
if (methodDeclaration == null) return;
HttpExceptionHandler httpExceptionHandler = declaration.getAnnotation(HttpExceptionHandler.class);
exceptionName = httpExceptionHandler.value();
// out exceptionName might not be set
if ("[ unassigned ]".equals(exceptionName) ) {
// use first param
exceptionName = methodDeclaration.getParameters().get(0).asType().toString();
}
setParams(methodDeclaration.getParameters());
if (showPositions) {
messager.printMessage(Diagnostic.Kind.NOTE, "@HttpExceptionHandlerUrl exceptionName " + exceptionName, methodDeclaration);
}
}
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.printMessage(Diagnostic.Kind.OTHER, getClass().getSimpleName() + " " + msg);
}
private void infoMsg(String msg) {
messager.printMessage(Diagnostic.Kind.NOTE, msg);
}
private void errorMsg(String msg) {
errorDuringProcessing = true;
messager.printMessage(Diagnostic.Kind.ERROR, msg);
}
private void errorMsg(Exception e) {
errorDuringProcessing = true;
messager.printMessage(Diagnostic.Kind.ERROR, e.getMessage());
}
private void errorMsg(String msg, Element element) {
errorDuringProcessing = true;
messager.printMessage(Diagnostic.Kind.ERROR, msg, element);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -