Upload、Download注解

pull/330/head
AriaLyy 8 years ago
parent dc13be2264
commit 190c2b3a05
  1. 12
      AriaAnnotations/src/main/java/com/arialyy/annotations/Test.java
  2. 42
      AriaAnnotations/src/main/java/com/arialyy/annotations/Upload.java
  3. 17
      AriaCompiler/src/main/java/com/arialyy/compiler/AriaProcessor.java
  4. 94
      AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java
  5. 20
      AriaCompiler/src/main/java/com/arialyy/compiler/test.java

@ -1,12 +0,0 @@
package com.arialyy.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Created by Aria.Lao on 2017/6/6.
*/
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface Test {
}

@ -0,0 +1,42 @@
package com.arialyy.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Created by Aria.Lao on 2017/6/6.
*/
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface Upload {
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onPre {
}
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskPre {
}
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskResume {
}
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStart {
}
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStop {
}
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskCancel {
}
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskFail {
}
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskComplete {
}
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskRunning {
}
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD)
public @interface onNoSupportBreakPoint {
}
}

@ -1,7 +1,7 @@
package com.arialyy.compiler; package com.arialyy.compiler;
import com.arialyy.annotations.Download; import com.arialyy.annotations.Download;
import com.arialyy.annotations.Test; import com.arialyy.annotations.Upload;
import com.google.auto.service.AutoService; import com.google.auto.service.AutoService;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
@ -37,7 +37,16 @@ import javax.lang.model.element.TypeElement;
annotataions.add(Download.onTaskRunning.class.getCanonicalName()); annotataions.add(Download.onTaskRunning.class.getCanonicalName());
annotataions.add(Download.onTaskStart.class.getCanonicalName()); annotataions.add(Download.onTaskStart.class.getCanonicalName());
annotataions.add(Download.onTaskStop.class.getCanonicalName()); annotataions.add(Download.onTaskStop.class.getCanonicalName());
annotataions.add(Test.class.getCanonicalName()); annotataions.add(Upload.onPre.class.getCanonicalName());
annotataions.add(Upload.onNoSupportBreakPoint.class.getCanonicalName());
annotataions.add(Upload.onTaskCancel.class.getCanonicalName());
annotataions.add(Upload.onTaskComplete.class.getCanonicalName());
annotataions.add(Upload.onTaskFail.class.getCanonicalName());
annotataions.add(Upload.onTaskPre.class.getCanonicalName());
annotataions.add(Upload.onTaskResume.class.getCanonicalName());
annotataions.add(Upload.onTaskRunning.class.getCanonicalName());
annotataions.add(Upload.onTaskStart.class.getCanonicalName());
annotataions.add(Upload.onTaskStop.class.getCanonicalName());
return annotataions; return annotataions;
} }
@ -48,7 +57,9 @@ import javax.lang.model.element.TypeElement;
@Override @Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
PrintLog.getInstance().info("开始扫描"); PrintLog.getInstance().info("开始扫描");
mHandler.handle(roundEnv); mHandler.clean();
mHandler.handleDownload(roundEnv);
mHandler.handleUpload(roundEnv);
return true; return true;
} }
} }

@ -1,6 +1,12 @@
package com.arialyy.compiler; package com.arialyy.compiler;
import com.arialyy.annotations.Download; import com.arialyy.annotations.Download;
import com.arialyy.annotations.Upload;
import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.annotation.processing.Filer; import javax.annotation.processing.Filer;
import javax.annotation.processing.RoundEnvironment; import javax.annotation.processing.RoundEnvironment;
@ -8,10 +14,7 @@ import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind; import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier; import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.TypeParameterElement;
import javax.lang.model.element.VariableElement; import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
/** /**
* Created by lyy on 2017/6/6. * Created by lyy on 2017/6/6.
@ -20,6 +23,7 @@ import javax.lang.model.type.TypeMirror;
class ElementHandle { class ElementHandle {
private Filer mFiler; private Filer mFiler;
private Map<String, Set<String>> mMethods = new HashMap<>();
ElementHandle(Filer filer) { ElementHandle(Filer filer) {
mFiler = filer; mFiler = filer;
@ -31,40 +35,86 @@ class ElementHandle {
* TypeElement 一般代表代表类 * TypeElement 一般代表代表类
* PackageElement 一般代表Package * PackageElement 一般代表Package
*/ */
void handle(RoundEnvironment roundEnv) { void handleDownload(RoundEnvironment roundEnv) {
handlePre(roundEnv); saveMethod(true, roundEnv, Download.onNoSupportBreakPoint.class);
saveMethod(true, roundEnv, Download.onPre.class);
saveMethod(true, roundEnv, Download.onTaskCancel.class);
saveMethod(true, roundEnv, Download.onTaskComplete.class);
saveMethod(true, roundEnv, Download.onTaskFail.class);
saveMethod(true, roundEnv, Download.onTaskPre.class);
saveMethod(true, roundEnv, Download.onTaskResume.class);
saveMethod(true, roundEnv, Download.onTaskRunning.class);
saveMethod(true, roundEnv, Download.onTaskStart.class);
saveMethod(true, roundEnv, Download.onTaskStop.class);
}
void handleUpload(RoundEnvironment roundEnv) {
saveMethod(false, roundEnv, Upload.onNoSupportBreakPoint.class);
saveMethod(false, roundEnv, Upload.onPre.class);
saveMethod(false, roundEnv, Upload.onTaskCancel.class);
saveMethod(false, roundEnv, Upload.onTaskComplete.class);
saveMethod(false, roundEnv, Upload.onTaskFail.class);
saveMethod(false, roundEnv, Upload.onTaskPre.class);
saveMethod(false, roundEnv, Upload.onTaskResume.class);
saveMethod(false, roundEnv, Upload.onTaskRunning.class);
saveMethod(false, roundEnv, Upload.onTaskStart.class);
saveMethod(false, roundEnv, Upload.onTaskStop.class);
}
void clean() {
mMethods.clear();
} }
/** /**
* 处理{@link Download.onTaskPre}注解 * 查找并保存扫描到的方法
*/ */
private void handlePre(RoundEnvironment roundEnv) { private void saveMethod(boolean isDownload, RoundEnvironment roundEnv,
for (Element element : roundEnv.getElementsAnnotatedWith(Download.onPre.class)) { Class<? extends Annotation> annotationClazz) {
for (Element element : roundEnv.getElementsAnnotatedWith(annotationClazz)) {
ElementKind kind = element.getKind(); ElementKind kind = element.getKind();
if (kind == ElementKind.METHOD) { if (kind == ElementKind.METHOD) {
ExecutableElement method = (ExecutableElement) element; ExecutableElement method = (ExecutableElement) element;
checkDownloadMethod(isDownload, method);
String methodName = method.getSimpleName().toString(); String methodName = method.getSimpleName().toString();
String className = method.getEnclosingElement().toString(); String className = method.getEnclosingElement().toString();
Set<Modifier> modifiers = method.getModifiers(); Set<String> methods = mMethods.get(className);
if (modifiers.contains(Modifier.PRIVATE)){ if (methods == null) {
PrintLog.getInstance().info("私有方法"); methods = new HashSet<>();
} mMethods.put(className, methods);
PrintLog.getInstance().info("注解的方法:" + methodName);
PrintLog.getInstance().info("所在类:" + className);
for (VariableElement te : method.getParameters()) {
TypeMirror paramType = te.asType();
PrintLog.getInstance()
.info("参数名:" + te.getSimpleName().toString() + ",参数类型:" + paramType.toString());
} }
methods.add(methodName);
} }
} }
} }
private void handleNoSupportBreakPoint(RoundEnvironment roundEnv) { /**
* 检查和下载相关的方法如果被注解的方法为private或参数不合法则抛异常
*/
private void checkDownloadMethod(boolean isDownload, ExecutableElement method) {
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方法");
}
List<VariableElement> params = (List<VariableElement>) method.getParameters();
if (params.size() > 1) {
throw new IllegalArgumentException(
className + "." + methodName + "参数错误, 参数只有一个,且参数必须是" + getCheckParams(isDownload));
}
if (!params.get(0).asType().toString().equals(getCheckParams(isDownload))) {
throw new IllegalArgumentException(className
+ "."
+ methodName
+ "参数【"
+ params.get(0).getSimpleName()
+ "】类型错误,参数必须是"
+ getCheckParams(isDownload));
}
} }
private void handleTaskCancel(RoundEnvironment roundEnv) { private String getCheckParams(boolean isDownload) {
return isDownload ? "com.arialyy.aria.core.download.DownloadTask"
: "com.arialyy.aria.core.upload.UploadTask";
} }
} }

@ -0,0 +1,20 @@
//package com.arialyy.compiler;
//
///**
// * Created by AriaL on 2017/6/6.
// */
//
//public class test {
// Object obj;
//
// public void setObj(Object obj){
// this.obj = obj;
// }
//
// public void onPre(DownloadTask task){
// obj.method(task);
// }
//
//
//
//}
Loading…
Cancel
Save