This commit is contained in:
laoyuyu
2018-11-05 21:13:58 +08:00
parent 0b095b9b11
commit 66b36b355a
43 changed files with 598 additions and 275 deletions

View File

@@ -16,6 +16,7 @@
package com.arialyy.compiler;
import com.arialyy.annotations.Download;
import com.arialyy.annotations.DownloadGroup;
import com.arialyy.annotations.Upload;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
@@ -91,33 +92,54 @@ final class EventProxyFiler {
*
* @param taskEnum 任务类型枚举{@link TaskEnum}
* @param annotation {@link Download}、{@link Upload}
* @param methodName 被代理类注解的方法
* @param methodInfo 被代理类注解的方法信息
*/
private MethodSpec createProxyMethod(TaskEnum taskEnum, Class<? extends Annotation> annotation,
String methodName) {
MethodInfo methodInfo) {
ClassName task = ClassName.get(taskEnum.getPkg(), taskEnum.getClassName());
ParameterSpec taskParam =
ParameterSpec.builder(task, "task").addModifiers(Modifier.FINAL).build();
String callCode;
if (taskEnum == TaskEnum.DOWNLOAD_GROUP_SUB) {
callCode = "task, subEntity";
if (methodInfo.params.get(methodInfo.params.size() - 1)
.asType()
.toString()
.equals(Exception.class.getName())
&& annotation == DownloadGroup.onSubTaskFail.class) {
callCode = "task, subEntity, e";
} else {
callCode = "task, subEntity";
}
} else {
callCode = "task";
if (methodInfo.params.get(methodInfo.params.size() - 1)
.asType()
.toString()
.equals(Exception.class.getName())
&& (annotation == Download.onTaskFail.class
|| annotation == Upload.onTaskFail.class
|| annotation == DownloadGroup.onTaskFail.class)) {
callCode = "task, e";
} else {
callCode = "task";
}
}
StringBuilder sb = new StringBuilder();
sb.append("Set<String> keys = keyMapping.get(\"").append(methodName).append("\");\n");
sb.append("Set<String> keys = keyMapping.get(\"")
.append(methodInfo.methodName)
.append("\");\n");
sb.append("if (keys != null) {\n\tif (keys.contains(task.getKey())) {\n")
.append("\t\tobj.")
.append(methodName)
.append(methodInfo.methodName)
.append("((")
.append(taskEnum.getClassName())
.append(")")
.append(callCode)
.append(");\n\t}\n} else {\n")
.append("\tobj.")
.append(methodName)
.append(methodInfo.methodName)
.append("((")
.append(taskEnum.getClassName())
.append(")")
@@ -140,6 +162,15 @@ final class EventProxyFiler {
builder.addParameter(subTaskParam);
}
if (annotation == Download.onTaskFail.class
|| annotation == Upload.onTaskFail.class
|| annotation == DownloadGroup.onTaskFail.class
|| annotation == DownloadGroup.onSubTaskFail.class) {
ParameterSpec exception = ParameterSpec.builder(Exception.class, "e").build();
builder.addParameter(exception);
}
return builder.build();
}
@@ -165,7 +196,7 @@ final class EventProxyFiler {
//Set<Integer> type = new HashSet<>();
//添加注解方法
for (TaskEnum te : entity.methods.keySet()) {
Map<Class<? extends Annotation>, String> temp = entity.methods.get(te);
Map<Class<? extends Annotation>, MethodInfo> methodInfoMap = entity.methods.get(te);
//if (entity.proxyClassName.contains(TaskEnum.DOWNLOAD.proxySuffix)) {
// type.add(1);
//} else if (entity.proxyClassName.contains(TaskEnum.DOWNLOAD_GROUP.proxySuffix)) {
@@ -175,9 +206,9 @@ final class EventProxyFiler {
//} else if (entity.proxyClassName.contains(TaskEnum.UPLOAD_GROUP.proxySuffix)) {
// type.add(4);
//}
if (temp != null) {
for (Class<? extends Annotation> annotation : temp.keySet()) {
MethodSpec method = createProxyMethod(te, annotation, temp.get(annotation));
if (methodInfoMap != null) {
for (Class<? extends Annotation> annotation : methodInfoMap.keySet()) {
MethodSpec method = createProxyMethod(te, annotation, methodInfoMap.get(annotation));
builder.addMethod(method);
}
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.compiler;
import java.util.List;
import javax.lang.model.element.VariableElement;
/**
* 方法信息
*/
final class MethodInfo {
String methodName;
List<VariableElement> params;
}

View File

@@ -15,6 +15,9 @@
*/
package com.arialyy.compiler;
import com.arialyy.annotations.Download;
import com.arialyy.annotations.DownloadGroup;
import com.arialyy.annotations.Upload;
import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.HashMap;
@@ -70,11 +73,15 @@ class ParamObtainUtil {
ExecutableElement method = (ExecutableElement) element;
TypeElement classElement = (TypeElement) method.getEnclosingElement();
PackageElement packageElement = mElementUtil.getPackageOf(classElement);
checkDownloadMethod(taskEnum, method);
String methodName = method.getSimpleName().toString();
String className = method.getEnclosingElement().toString(); //全类名\
String key = className + taskEnum.proxySuffix;
ProxyClassParam proxyEntity = mMethodParams.get(key);
MethodInfo methodInfo = new MethodInfo();
methodInfo.methodName = methodName;
methodInfo.params = (List<VariableElement>) method.getParameters();
checkMethod(taskEnum, method, annotationClazz, methodInfo.params);
if (proxyEntity == null) {
proxyEntity = new ProxyClassParam();
@@ -90,9 +97,10 @@ class ParamObtainUtil {
}
proxyEntity.taskEnums.add(taskEnum);
if (proxyEntity.methods.get(taskEnum) == null) {
proxyEntity.methods.put(taskEnum, new HashMap<Class<? extends Annotation>, String>());
proxyEntity.methods.put(taskEnum, new HashMap<Class<? extends Annotation>, MethodInfo>());
}
proxyEntity.methods.get(taskEnum).put(annotationClazz, methodName);
proxyEntity.methods.get(taskEnum).put(annotationClazz, methodInfo);
proxyEntity.keyMappings.put(methodName, getValues(taskEnum, method, annotationType));
}
}
@@ -144,52 +152,71 @@ class ParamObtainUtil {
/**
* 检查和下载相关的方法如果被注解的方法为private或参数不合法则抛异常
*/
private void checkDownloadMethod(TaskEnum taskEnum, ExecutableElement method) {
private void checkMethod(TaskEnum taskEnum, ExecutableElement method,
Class<? extends Annotation> annotationClazz, List<VariableElement> params) {
String methodName = method.getSimpleName().toString();
String className = method.getEnclosingElement().toString();
Set<Modifier> modifiers = method.getModifiers();
if (modifiers.contains(Modifier.PRIVATE)) {
throw new IllegalAccessError(className + "." + methodName + "不能为private方法");
throw new IllegalAccessError(String.format("%s.%s, 不能为private方法", className, methodName));
}
List<VariableElement> params = (List<VariableElement>) method.getParameters();
if (taskEnum == TaskEnum.DOWNLOAD_GROUP_SUB) {
if (params.size() != 2) {
throw new IllegalArgumentException(className
+ "."
+ methodName
+ "参数错误, 参数只有两个,且第一个参数必须是"
+ getCheckParams(taskEnum)
+ ",第二个参数必须是"
+ getCheckSubParams(taskEnum));
if (isFailAnnotation(annotationClazz)) {
if (params.size() != 3 && params.size() != 2) {
throw new IllegalArgumentException(
String.format("%s.%s参数错误, 参数只能是两个或三个,第一个参数是:%s第二个参数是%s第三个参数可选%s", className,
methodName,
getCheckParams(taskEnum), getCheckSubParams(taskEnum),
Exception.class.getSimpleName()));
}
} else {
if (params.size() != 2) {
throw new IllegalArgumentException(
String.format("%s.%s参数错误, 参数只能是两个,第一个参数是:%s第二个参数是%s", className, methodName,
getCheckParams(taskEnum), getCheckSubParams(taskEnum)));
}
}
} else {
if (params.size() != 1) {
throw new IllegalArgumentException(
className + "." + methodName + "参数错误, 参数只能有一个,且参数必须是" + getCheckParams(taskEnum));
if (isFailAnnotation(annotationClazz)) {
if (params.size() != 1 && params.size() != 2) {
throw new IllegalArgumentException(
String.format("%s.%s参数错误, 参数只能有一个或两个,第一个参数是:%s第二个参可选数是%s", className, methodName,
getCheckParams(taskEnum), Exception.class.getSimpleName()));
}
} else {
if (params.size() != 1) {
throw new IllegalArgumentException(
String.format("%s.%s参数错误, 参数只能有一个,且参数必须是:%s", className, methodName,
getCheckParams(taskEnum)));
}
}
}
if (!params.get(0).asType().toString().equals(getCheckParams(taskEnum))) {
throw new IllegalArgumentException(className
+ "."
+ methodName
+ "参数【"
+ params.get(0).getSimpleName()
+ "】类型错误,参数必须是"
+ getCheckParams(taskEnum));
throw new IllegalArgumentException(
String.format("%s.%s参数【%s】类型错误参数必须是%s", className, methodName,
params.get(0).getSimpleName(), getCheckParams(taskEnum)));
}
if (taskEnum == TaskEnum.DOWNLOAD_GROUP_SUB) {
if (!params.get(1).asType().toString().equals(getCheckSubParams(taskEnum))) {
throw new IllegalArgumentException(className
+ "."
+ methodName
+ "参数【"
+ params.get(0).getSimpleName()
+ "】类型错误,参数必须是"
+ getCheckSubParams(taskEnum));
throw new IllegalArgumentException(
String.format("%s.%s参数【%s】类型错误参数必须是%s", className, methodName,
params.get(0).getSimpleName(), getCheckSubParams(taskEnum)));
}
}
}
/**
* 判断是否是任务失败的回调注解
*
* @return @code true是任务失败的回调注解
*/
private boolean isFailAnnotation(Class<? extends Annotation> annotationClazz) {
return annotationClazz == Download.onTaskFail.class
|| annotationClazz == DownloadGroup.onTaskFail.class
|| annotationClazz == DownloadGroup.onSubTaskFail.class
|| annotationClazz == Upload.onTaskFail.class;
}
/**
* 字符串数组转set
*

View File

@@ -48,5 +48,5 @@ class ProxyClassParam {
Set<TaskEnum> taskEnums;
Map<String, Set<String>> keyMappings = new HashMap<>();
Map<TaskEnum, Map<Class<? extends Annotation>, String>> methods = new HashMap<>();
Map<TaskEnum, Map<Class<? extends Annotation>, MethodInfo>> methods = new HashMap<>();
}