|
|
@ -18,6 +18,7 @@ package com.arialyy.compiler; |
|
|
|
import com.arialyy.annotations.Download; |
|
|
|
import com.arialyy.annotations.Download; |
|
|
|
import com.arialyy.annotations.Upload; |
|
|
|
import com.arialyy.annotations.Upload; |
|
|
|
import com.squareup.javapoet.ClassName; |
|
|
|
import com.squareup.javapoet.ClassName; |
|
|
|
|
|
|
|
import com.squareup.javapoet.CodeBlock; |
|
|
|
import com.squareup.javapoet.FieldSpec; |
|
|
|
import com.squareup.javapoet.FieldSpec; |
|
|
|
import com.squareup.javapoet.JavaFile; |
|
|
|
import com.squareup.javapoet.JavaFile; |
|
|
|
import com.squareup.javapoet.MethodSpec; |
|
|
|
import com.squareup.javapoet.MethodSpec; |
|
|
@ -26,7 +27,9 @@ import com.squareup.javapoet.TypeSpec; |
|
|
|
import com.squareup.javapoet.ParameterizedTypeName; |
|
|
|
import com.squareup.javapoet.ParameterizedTypeName; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.lang.annotation.Annotation; |
|
|
|
import java.lang.annotation.Annotation; |
|
|
|
|
|
|
|
import java.util.Collections; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.HashMap; |
|
|
|
|
|
|
|
import java.util.HashSet; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.Set; |
|
|
@ -63,40 +66,32 @@ class ElementHandle { |
|
|
|
* PackageElement 一般代表Package |
|
|
|
* PackageElement 一般代表Package |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
void handleDownload(RoundEnvironment roundEnv) { |
|
|
|
void handleDownload(RoundEnvironment roundEnv) { |
|
|
|
saveMethod(true, roundEnv, Download.onNoSupportBreakPoint.class); |
|
|
|
saveMethod(true, roundEnv, Download.onNoSupportBreakPoint.class, |
|
|
|
saveMethod(true, roundEnv, Download.onPre.class); |
|
|
|
ProxyConstance.DOWNLOAD_TASK_NO_SUPPORT_BREAKPOINT); |
|
|
|
saveMethod(true, roundEnv, Download.onTaskCancel.class); |
|
|
|
saveMethod(true, roundEnv, Download.onPre.class, ProxyConstance.DOWNLOAD_PRE); |
|
|
|
saveMethod(true, roundEnv, Download.onTaskComplete.class); |
|
|
|
saveMethod(true, roundEnv, Download.onTaskCancel.class, ProxyConstance.DOWNLOAD_TASK_CANCEL); |
|
|
|
saveMethod(true, roundEnv, Download.onTaskFail.class); |
|
|
|
saveMethod(true, roundEnv, Download.onTaskComplete.class, |
|
|
|
saveMethod(true, roundEnv, Download.onTaskPre.class); |
|
|
|
ProxyConstance.DOWNLOAD_TASK_COMPLETE); |
|
|
|
saveMethod(true, roundEnv, Download.onTaskResume.class); |
|
|
|
saveMethod(true, roundEnv, Download.onTaskFail.class, ProxyConstance.DOWNLOAD_TASK_FAIL); |
|
|
|
saveMethod(true, roundEnv, Download.onTaskRunning.class); |
|
|
|
saveMethod(true, roundEnv, Download.onTaskPre.class, ProxyConstance.DOWNLOAD_TASK_PRE); |
|
|
|
saveMethod(true, roundEnv, Download.onTaskStart.class); |
|
|
|
saveMethod(true, roundEnv, Download.onTaskResume.class, ProxyConstance.DOWNLOAD_TASK_RESUME); |
|
|
|
saveMethod(true, roundEnv, Download.onTaskStop.class); |
|
|
|
saveMethod(true, roundEnv, Download.onTaskRunning.class, ProxyConstance.DOWNLOAD_TASK_RUNNING); |
|
|
|
|
|
|
|
saveMethod(true, roundEnv, Download.onTaskStart.class, ProxyConstance.DOWNLOAD_TASK_START); |
|
|
|
|
|
|
|
saveMethod(true, roundEnv, Download.onTaskStop.class, ProxyConstance.DOWNLOAD_TASK_STOP); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void handleUpload(RoundEnvironment roundEnv) { |
|
|
|
void handleUpload(RoundEnvironment roundEnv) { |
|
|
|
saveMethod(false, roundEnv, Upload.onNoSupportBreakPoint.class); |
|
|
|
saveMethod(false, roundEnv, Upload.onNoSupportBreakPoint.class, |
|
|
|
saveMethod(false, roundEnv, Upload.onPre.class); |
|
|
|
ProxyConstance.UPLOAD_TASK_NO_SUPPORT_BREAKPOINT); |
|
|
|
saveMethod(false, roundEnv, Upload.onTaskCancel.class); |
|
|
|
saveMethod(false, roundEnv, Upload.onPre.class, ProxyConstance.UPLOAD_PRE); |
|
|
|
saveMethod(false, roundEnv, Upload.onTaskComplete.class); |
|
|
|
saveMethod(false, roundEnv, Upload.onTaskCancel.class, ProxyConstance.UPLOAD_TASK_CANCEL); |
|
|
|
saveMethod(false, roundEnv, Upload.onTaskFail.class); |
|
|
|
saveMethod(false, roundEnv, Upload.onTaskComplete.class, ProxyConstance.UPLOAD_TASK_COMPLETE); |
|
|
|
saveMethod(false, roundEnv, Upload.onTaskPre.class); |
|
|
|
saveMethod(false, roundEnv, Upload.onTaskFail.class, ProxyConstance.UPLOAD_TASK_FAIL); |
|
|
|
|
|
|
|
saveMethod(false, roundEnv, Upload.onTaskPre.class, ProxyConstance.UPLOAD_TASK_PRE); |
|
|
|
//saveMethod(false, roundEnv, Upload.onTaskResume.class);
|
|
|
|
//saveMethod(false, roundEnv, Upload.onTaskResume.class);
|
|
|
|
saveMethod(false, roundEnv, Upload.onTaskRunning.class); |
|
|
|
saveMethod(false, roundEnv, Upload.onTaskRunning.class, ProxyConstance.UPLOAD_TASK_RUNNING); |
|
|
|
saveMethod(false, roundEnv, Upload.onTaskStart.class); |
|
|
|
saveMethod(false, roundEnv, Upload.onTaskStart.class, ProxyConstance.UPLOAD_TASK_START); |
|
|
|
saveMethod(false, roundEnv, Upload.onTaskStop.class); |
|
|
|
saveMethod(false, roundEnv, Upload.onTaskStop.class, ProxyConstance.UPLOAD_TASK_STOP); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void printMethods() { |
|
|
|
|
|
|
|
//Set<String> keys = mMethods.keySet();
|
|
|
|
|
|
|
|
//for (String key : keys) {
|
|
|
|
|
|
|
|
// ProxyEntity entity = mMethods.get(key);
|
|
|
|
|
|
|
|
// for (String method : entity.methods) {
|
|
|
|
|
|
|
|
// PrintLog.getInstance().info(method);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
PrintLog.getInstance().info("size ==> " + mMethods.size()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -157,16 +152,29 @@ class ElementHandle { |
|
|
|
isDownload ? "DownloadTask" : "UploadTask"); |
|
|
|
isDownload ? "DownloadTask" : "UploadTask"); |
|
|
|
ParameterSpec parameterSpec = |
|
|
|
ParameterSpec parameterSpec = |
|
|
|
ParameterSpec.builder(task, "task").addModifiers(Modifier.FINAL).build(); |
|
|
|
ParameterSpec.builder(task, "task").addModifiers(Modifier.FINAL).build(); |
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
|
|
|
|
sb.append("Set<String> keys = keyMapping.get(\"") |
|
|
|
|
|
|
|
.append(methodName) |
|
|
|
|
|
|
|
.append("\");\n"); |
|
|
|
|
|
|
|
sb.append("if (keys != null) {\n\tif (keys.contains(task.getKey())) {\n") |
|
|
|
|
|
|
|
.append("\t\tobj.") |
|
|
|
|
|
|
|
.append(methodName) |
|
|
|
|
|
|
|
.append("(") |
|
|
|
|
|
|
|
.append(isDownload ? "(DownloadTask)" : "(UploadTask)") |
|
|
|
|
|
|
|
.append("task);\n") |
|
|
|
|
|
|
|
.append("\t}\n} else {\n") |
|
|
|
|
|
|
|
.append("\tobj.") |
|
|
|
|
|
|
|
.append(methodName) |
|
|
|
|
|
|
|
.append("(") |
|
|
|
|
|
|
|
.append(isDownload ? "(DownloadTask)" : "(UploadTask)") |
|
|
|
|
|
|
|
.append("task);\n}\n"); |
|
|
|
|
|
|
|
|
|
|
|
return MethodSpec.methodBuilder(annotation.getSimpleName()) |
|
|
|
return MethodSpec.methodBuilder(annotation.getSimpleName()) |
|
|
|
.addModifiers(Modifier.PUBLIC) |
|
|
|
.addModifiers(Modifier.PUBLIC) |
|
|
|
.returns(void.class) |
|
|
|
.returns(void.class) |
|
|
|
.addParameter(parameterSpec) |
|
|
|
.addParameter(parameterSpec) |
|
|
|
.addAnnotation(Override.class) |
|
|
|
.addAnnotation(Override.class) |
|
|
|
.addCode("obj." |
|
|
|
.addCode(sb.toString()) |
|
|
|
+ methodName |
|
|
|
|
|
|
|
+ "(" |
|
|
|
|
|
|
|
+ (isDownload ? "(DownloadTask)" : "(UploadTask)") |
|
|
|
|
|
|
|
+ "task);\n") |
|
|
|
|
|
|
|
.build(); |
|
|
|
.build(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -184,6 +192,13 @@ class ElementHandle { |
|
|
|
FieldSpec observerField = FieldSpec.builder(obj, "obj").addModifiers(Modifier.PRIVATE).build(); |
|
|
|
FieldSpec observerField = FieldSpec.builder(obj, "obj").addModifiers(Modifier.PRIVATE).build(); |
|
|
|
builder.addField(observerField); |
|
|
|
builder.addField(observerField); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//添加url映射表
|
|
|
|
|
|
|
|
FieldSpec mappingField = FieldSpec.builder( |
|
|
|
|
|
|
|
ParameterizedTypeName.get(ClassName.get(Map.class), ClassName.get(String.class), |
|
|
|
|
|
|
|
ParameterizedTypeName.get(ClassName.get(Set.class), ClassName.get(String.class))), |
|
|
|
|
|
|
|
"keyMapping").addModifiers(Modifier.PRIVATE).initializer("new $T()", HashMap.class).build(); |
|
|
|
|
|
|
|
builder.addField(mappingField); |
|
|
|
|
|
|
|
|
|
|
|
//添加注解方法
|
|
|
|
//添加注解方法
|
|
|
|
for (Class<? extends Annotation> annotation : entity.methods.keySet()) { |
|
|
|
for (Class<? extends Annotation> annotation : entity.methods.keySet()) { |
|
|
|
MethodSpec method = |
|
|
|
MethodSpec method = |
|
|
@ -191,6 +206,26 @@ class ElementHandle { |
|
|
|
builder.addMethod(method); |
|
|
|
builder.addMethod(method); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//增加构造函数
|
|
|
|
|
|
|
|
CodeBlock.Builder cb = CodeBlock.builder(); |
|
|
|
|
|
|
|
cb.add("Set<String> set = null;\n"); |
|
|
|
|
|
|
|
for (String methodName : entity.keyMappings.keySet()) { |
|
|
|
|
|
|
|
Set<String> keys = entity.keyMappings.get(methodName); |
|
|
|
|
|
|
|
if (keys == null || keys.size() == 0) continue; |
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
|
|
|
|
sb.append("set = new $T();\n"); |
|
|
|
|
|
|
|
for (String key : keys) { |
|
|
|
|
|
|
|
if (key.isEmpty()) continue; |
|
|
|
|
|
|
|
sb.append("set.add(\"").append(key).append("\");\n"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sb.append("keyMapping.put(\"").append(methodName).append("\", ").append("set);\n"); |
|
|
|
|
|
|
|
cb.add(sb.toString(), ClassName.get(HashSet.class)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
MethodSpec structure = |
|
|
|
|
|
|
|
MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addCode(cb.build()).build(); |
|
|
|
|
|
|
|
builder.addMethod(structure); |
|
|
|
|
|
|
|
|
|
|
|
//添加设置代理的类
|
|
|
|
//添加设置代理的类
|
|
|
|
ParameterSpec parameterSpec = |
|
|
|
ParameterSpec parameterSpec = |
|
|
|
ParameterSpec.builder(Object.class, "obj").addModifiers(Modifier.FINAL).build(); |
|
|
|
ParameterSpec.builder(Object.class, "obj").addModifiers(Modifier.FINAL).build(); |
|
|
@ -222,7 +257,7 @@ class ElementHandle { |
|
|
|
* 查找并保存扫描到的方法 |
|
|
|
* 查找并保存扫描到的方法 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void saveMethod(boolean isDownload, RoundEnvironment roundEnv, |
|
|
|
private void saveMethod(boolean isDownload, RoundEnvironment roundEnv, |
|
|
|
Class<? extends Annotation> annotationClazz) { |
|
|
|
Class<? extends Annotation> annotationClazz, int annotationType) { |
|
|
|
for (Element element : roundEnv.getElementsAnnotatedWith(annotationClazz)) { |
|
|
|
for (Element element : roundEnv.getElementsAnnotatedWith(annotationClazz)) { |
|
|
|
ElementKind kind = element.getKind(); |
|
|
|
ElementKind kind = element.getKind(); |
|
|
|
if (kind == ElementKind.METHOD) { |
|
|
|
if (kind == ElementKind.METHOD) { |
|
|
@ -236,14 +271,90 @@ class ElementHandle { |
|
|
|
if (proxyEntity == null) { |
|
|
|
if (proxyEntity == null) { |
|
|
|
proxyEntity = new ProxyEntity(); |
|
|
|
proxyEntity = new ProxyEntity(); |
|
|
|
proxyEntity.isDownlaod = isDownload; |
|
|
|
proxyEntity.isDownlaod = isDownload; |
|
|
|
//proxyEntity.packageName = classElement.getQualifiedName().toString();
|
|
|
|
|
|
|
|
proxyEntity.packageName = packageElement.getQualifiedName().toString(); |
|
|
|
proxyEntity.packageName = packageElement.getQualifiedName().toString(); |
|
|
|
proxyEntity.className = classElement.getSimpleName().toString(); |
|
|
|
proxyEntity.className = classElement.getSimpleName().toString(); |
|
|
|
mMethods.put(className, proxyEntity); |
|
|
|
mMethods.put(className, proxyEntity); |
|
|
|
} |
|
|
|
} |
|
|
|
proxyEntity.methods.put(annotationClazz, methodName); |
|
|
|
proxyEntity.methods.put(annotationClazz, methodName); |
|
|
|
|
|
|
|
proxyEntity.keyMappings.put(methodName, getValues(method, isDownload, annotationType)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 获取注解的内容 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private Set<String> getValues(ExecutableElement method, boolean isDownload, int annotationType) { |
|
|
|
|
|
|
|
String[] keys = null; |
|
|
|
|
|
|
|
if (isDownload) { |
|
|
|
|
|
|
|
switch (annotationType) { |
|
|
|
|
|
|
|
case ProxyConstance.DOWNLOAD_PRE: |
|
|
|
|
|
|
|
keys = method.getAnnotation(Download.onPre.class).value(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ProxyConstance.DOWNLOAD_TASK_PRE: |
|
|
|
|
|
|
|
keys = method.getAnnotation(Download.onTaskPre.class).value(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ProxyConstance.DOWNLOAD_TASK_RESUME: |
|
|
|
|
|
|
|
keys = method.getAnnotation(Download.onTaskResume.class).value(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ProxyConstance.DOWNLOAD_TASK_START: |
|
|
|
|
|
|
|
keys = method.getAnnotation(Download.onTaskStart.class).value(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ProxyConstance.DOWNLOAD_TASK_RUNNING: |
|
|
|
|
|
|
|
keys = method.getAnnotation(Download.onTaskRunning.class).value(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ProxyConstance.DOWNLOAD_TASK_STOP: |
|
|
|
|
|
|
|
keys = method.getAnnotation(Download.onTaskStop.class).value(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ProxyConstance.DOWNLOAD_TASK_COMPLETE: |
|
|
|
|
|
|
|
keys = method.getAnnotation(Download.onTaskComplete.class).value(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ProxyConstance.DOWNLOAD_TASK_CANCEL: |
|
|
|
|
|
|
|
keys = method.getAnnotation(Download.onTaskCancel.class).value(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ProxyConstance.DOWNLOAD_TASK_FAIL: |
|
|
|
|
|
|
|
keys = method.getAnnotation(Download.onTaskFail.class).value(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ProxyConstance.DOWNLOAD_TASK_NO_SUPPORT_BREAKPOINT: |
|
|
|
|
|
|
|
keys = method.getAnnotation(Download.onNoSupportBreakPoint.class).value(); |
|
|
|
|
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
switch (annotationType) { |
|
|
|
|
|
|
|
case ProxyConstance.UPLOAD_PRE: |
|
|
|
|
|
|
|
keys = method.getAnnotation(Upload.onPre.class).value(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ProxyConstance.UPLOAD_TASK_PRE: |
|
|
|
|
|
|
|
keys = method.getAnnotation(Upload.onTaskPre.class).value(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ProxyConstance.UPLOAD_TASK_RESUME: |
|
|
|
|
|
|
|
//keys = method.getAnnotation(Upload.onTaskResume.class).value();
|
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ProxyConstance.UPLOAD_TASK_START: |
|
|
|
|
|
|
|
keys = method.getAnnotation(Upload.onTaskStart.class).value(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ProxyConstance.UPLOAD_TASK_RUNNING: |
|
|
|
|
|
|
|
keys = method.getAnnotation(Upload.onTaskRunning.class).value(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ProxyConstance.UPLOAD_TASK_STOP: |
|
|
|
|
|
|
|
keys = method.getAnnotation(Upload.onTaskStop.class).value(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ProxyConstance.UPLOAD_TASK_COMPLETE: |
|
|
|
|
|
|
|
keys = method.getAnnotation(Upload.onTaskComplete.class).value(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ProxyConstance.UPLOAD_TASK_CANCEL: |
|
|
|
|
|
|
|
keys = method.getAnnotation(Upload.onTaskCancel.class).value(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ProxyConstance.UPLOAD_TASK_FAIL: |
|
|
|
|
|
|
|
keys = method.getAnnotation(Upload.onTaskFail.class).value(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ProxyConstance.UPLOAD_TASK_NO_SUPPORT_BREAKPOINT: |
|
|
|
|
|
|
|
keys = method.getAnnotation(Upload.onNoSupportBreakPoint.class).value(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return keys == null ? null : convertSet(keys); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -272,6 +383,21 @@ class ElementHandle { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 字符串数组转set |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param keys 注解中查到的key |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private Set<String> convertSet(final String[] keys) { |
|
|
|
|
|
|
|
if (keys == null || keys.length == 0) { |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (keys[0].isEmpty()) return null; |
|
|
|
|
|
|
|
Set<String> set = new HashSet<>(); |
|
|
|
|
|
|
|
Collections.addAll(set, keys); |
|
|
|
|
|
|
|
return set; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private String getCheckParams(boolean isDownload) { |
|
|
|
private String getCheckParams(boolean isDownload) { |
|
|
|
return isDownload ? "com.arialyy.aria.core.download.DownloadTask" |
|
|
|
return isDownload ? "com.arialyy.aria.core.download.DownloadTask" |
|
|
|
: "com.arialyy.aria.core.upload.UploadTask"; |
|
|
|
: "com.arialyy.aria.core.upload.UploadTask"; |
|
|
|