From 3b7a9eccb80b94619a5e63c4ea3d1ba016978c8a Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Tue, 6 Jun 2017 18:16:44 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=B3=A8=E8=A7=A3=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E5=9B=9E=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Aria/build.gradle | 3 +- .../aria/core/upload/UploadTaskEntity.java | 2 +- AriaAnnotations/.gitignore | 1 + AriaAnnotations/build.gradle | 12 ++++ .../com/arialyy/annotations/Download.java | 42 +++++++++++ .../java/com/arialyy/annotations/Test.java | 12 ++++ AriaCompiler/.gitignore | 1 + AriaCompiler/build.gradle | 18 +++++ .../com/arialyy/compiler/AriaProcessor.java | 54 ++++++++++++++ .../com/arialyy/compiler/ElementHandle.java | 70 +++++++++++++++++++ .../java/com/arialyy/compiler/PrintLog.java | 55 +++++++++++++++ .../simple/download/SingleTaskActivity.java | 11 +++ build.gradle | 1 + settings.gradle | 2 +- 14 files changed, 281 insertions(+), 3 deletions(-) create mode 100644 AriaAnnotations/.gitignore create mode 100644 AriaAnnotations/build.gradle create mode 100644 AriaAnnotations/src/main/java/com/arialyy/annotations/Download.java create mode 100644 AriaAnnotations/src/main/java/com/arialyy/annotations/Test.java create mode 100644 AriaCompiler/.gitignore create mode 100644 AriaCompiler/build.gradle create mode 100644 AriaCompiler/src/main/java/com/arialyy/compiler/AriaProcessor.java create mode 100644 AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java create mode 100644 AriaCompiler/src/main/java/com/arialyy/compiler/PrintLog.java diff --git a/Aria/build.gradle b/Aria/build.gradle index dc7a6655..09e7cfa8 100644 --- a/Aria/build.gradle +++ b/Aria/build.gradle @@ -19,8 +19,9 @@ android { } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) + compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' + compile project(':AriaCompiler') } //apply from: 'jcenter.gradle' diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java index 5da1923e..84ea56f1 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java @@ -30,7 +30,7 @@ public class UploadTaskEntity extends AbsTaskEntity { public String contentType = "multipart/form-data"; //上传的文件类型 public String userAgent = "User-Agent"; public String charset = "utf-8"; - + /** * 文件上传表单 */ diff --git a/AriaAnnotations/.gitignore b/AriaAnnotations/.gitignore new file mode 100644 index 00000000..796b96d1 --- /dev/null +++ b/AriaAnnotations/.gitignore @@ -0,0 +1 @@ +/build diff --git a/AriaAnnotations/build.gradle b/AriaAnnotations/build.gradle new file mode 100644 index 00000000..65ee1cf4 --- /dev/null +++ b/AriaAnnotations/build.gradle @@ -0,0 +1,12 @@ +apply plugin: 'java' + +tasks.withType(JavaCompile) { + options.encoding = "UTF-8" +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + + sourceCompatibility = "1.7" + targetCompatibility = "1.7" +} \ No newline at end of file diff --git a/AriaAnnotations/src/main/java/com/arialyy/annotations/Download.java b/AriaAnnotations/src/main/java/com/arialyy/annotations/Download.java new file mode 100644 index 00000000..bbd0385a --- /dev/null +++ b/AriaAnnotations/src/main/java/com/arialyy/annotations/Download.java @@ -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 Download { + @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 { + } +} diff --git a/AriaAnnotations/src/main/java/com/arialyy/annotations/Test.java b/AriaAnnotations/src/main/java/com/arialyy/annotations/Test.java new file mode 100644 index 00000000..7b79963e --- /dev/null +++ b/AriaAnnotations/src/main/java/com/arialyy/annotations/Test.java @@ -0,0 +1,12 @@ +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 { +} diff --git a/AriaCompiler/.gitignore b/AriaCompiler/.gitignore new file mode 100644 index 00000000..796b96d1 --- /dev/null +++ b/AriaCompiler/.gitignore @@ -0,0 +1 @@ +/build diff --git a/AriaCompiler/build.gradle b/AriaCompiler/build.gradle new file mode 100644 index 00000000..665aa139 --- /dev/null +++ b/AriaCompiler/build.gradle @@ -0,0 +1,18 @@ +apply plugin: 'java' + +tasks.withType(JavaCompile) { + options.encoding = "UTF-8" +} + +dependencies { + compile fileTree(include: ['*.jar'], dir: 'libs') + compile 'com.google.auto:auto-common:0.6' + compile 'com.google.auto.service:auto-service:1.0-rc2' + compile 'com.squareup:javapoet:1.7.0' + compile project(':AriaAnnotations') + + sourceCompatibility = "1.7" + targetCompatibility = "1.7" +} + + diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/AriaProcessor.java b/AriaCompiler/src/main/java/com/arialyy/compiler/AriaProcessor.java new file mode 100644 index 00000000..aedb6a51 --- /dev/null +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/AriaProcessor.java @@ -0,0 +1,54 @@ +package com.arialyy.compiler; + +import com.arialyy.annotations.Download; +import com.arialyy.annotations.Test; +import com.google.auto.service.AutoService; +import java.util.LinkedHashSet; +import java.util.Set; +import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.ProcessingEnvironment; +import javax.annotation.processing.Processor; +import javax.annotation.processing.RoundEnvironment; +import javax.lang.model.SourceVersion; +import javax.lang.model.element.TypeElement; + +/** + * Created by lyy on 2017/6/6. + * 事件注解扫描器 + */ +@AutoService(Processor.class) public class AriaProcessor extends AbstractProcessor { + ElementHandle mHandler; + + @Override public synchronized void init(ProcessingEnvironment processingEnv) { + super.init(processingEnv); + PrintLog.init(processingEnv.getMessager()); + mHandler = new ElementHandle(processingEnv.getFiler()); + } + + @Override public Set getSupportedAnnotationTypes() { + Set annotataions = new LinkedHashSet<>(); + annotataions.add(Download.onPre.class.getCanonicalName()); + annotataions.add(Download.onNoSupportBreakPoint.class.getCanonicalName()); + annotataions.add(Download.onTaskCancel.class.getCanonicalName()); + annotataions.add(Download.onTaskComplete.class.getCanonicalName()); + annotataions.add(Download.onTaskFail.class.getCanonicalName()); + annotataions.add(Download.onTaskPre.class.getCanonicalName()); + annotataions.add(Download.onTaskResume.class.getCanonicalName()); + annotataions.add(Download.onTaskRunning.class.getCanonicalName()); + annotataions.add(Download.onTaskStart.class.getCanonicalName()); + annotataions.add(Download.onTaskStop.class.getCanonicalName()); + annotataions.add(Test.class.getCanonicalName()); + return annotataions; + } + + @Override public SourceVersion getSupportedSourceVersion() { + return SourceVersion.latestSupported(); + } + + @Override + public boolean process(Set annotations, RoundEnvironment roundEnv) { + PrintLog.getInstance().info("开始扫描"); + mHandler.handle(roundEnv); + return true; + } +} diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java b/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java new file mode 100644 index 00000000..f332e057 --- /dev/null +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java @@ -0,0 +1,70 @@ +package com.arialyy.compiler; + +import com.arialyy.annotations.Download; +import java.util.Set; +import javax.annotation.processing.Filer; +import javax.annotation.processing.RoundEnvironment; +import javax.lang.model.element.Element; +import javax.lang.model.element.ElementKind; +import javax.lang.model.element.ExecutableElement; +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.type.TypeMirror; + +/** + * Created by lyy on 2017/6/6. + * 元素处理 + */ +class ElementHandle { + + private Filer mFiler; + + ElementHandle(Filer filer) { + mFiler = filer; + } + + /** + * VariableElement 一般代表成员变量 + * ExecutableElement 一般代表类中的方法 + * TypeElement 一般代表代表类 + * PackageElement 一般代表Package + */ + void handle(RoundEnvironment roundEnv) { + handlePre(roundEnv); + } + + /** + * 处理{@link Download.onTaskPre}注解 + */ + private void handlePre(RoundEnvironment roundEnv) { + for (Element element : roundEnv.getElementsAnnotatedWith(Download.onPre.class)) { + ElementKind kind = element.getKind(); + if (kind == ElementKind.METHOD) { + ExecutableElement method = (ExecutableElement) element; + String methodName = method.getSimpleName().toString(); + String className = method.getEnclosingElement().toString(); + Set modifiers = method.getModifiers(); + if (modifiers.contains(Modifier.PRIVATE)){ + PrintLog.getInstance().info("私有方法"); + } + 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()); + } + } + } + } + + private void handleNoSupportBreakPoint(RoundEnvironment roundEnv) { + + } + + private void handleTaskCancel(RoundEnvironment roundEnv) { + + } +} diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/PrintLog.java b/AriaCompiler/src/main/java/com/arialyy/compiler/PrintLog.java new file mode 100644 index 00000000..62383c8b --- /dev/null +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/PrintLog.java @@ -0,0 +1,55 @@ +package com.arialyy.compiler; + +import javax.annotation.processing.Messager; +import javax.lang.model.element.Element; +import javax.tools.Diagnostic; + +/** + * Created by Aria.Lao on 2017/6/6. + */ + +public class PrintLog { + + private volatile static PrintLog INSTANCE = null; + private Messager mMessager; + + public static PrintLog init(Messager msg) { + if (INSTANCE == null) { + synchronized (PrintLog.class) { + INSTANCE = new PrintLog(msg); + } + } + return INSTANCE; + } + + public static PrintLog getInstance() { + return INSTANCE; + } + + private PrintLog() { + } + + private PrintLog(Messager msg) { + mMessager = msg; + } + + public void error(Element e, String msg, Object... args) { + mMessager.printMessage(Diagnostic.Kind.ERROR, String.format(msg, args), e); + } + + public void error(String msg, Object... args) { + mMessager.printMessage(Diagnostic.Kind.ERROR, String.format(msg, args)); + } + + private void warning(String msg) { + mMessager.printMessage(Diagnostic.Kind.WARNING, msg); + } + + public void error(String msg) { + mMessager.printMessage(Diagnostic.Kind.ERROR, msg); + } + + public void info(String str) { + mMessager.printMessage(Diagnostic.Kind.NOTE, str); + } +} diff --git a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java index dfe71692..03bb115c 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -33,6 +33,8 @@ import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; import butterknife.Bind; +import com.arialyy.annotations.Download; +import com.arialyy.annotations.Test; import com.arialyy.aria.core.download.DownloadTarget; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.download.DownloadEntity; @@ -132,6 +134,15 @@ public class SingleTaskActivity extends BaseActivity { } }; + @Download.onPre private void hehe(String str, DownloadTask task) { + + } + + @Test + public void gg(){ + + } + /** * 设置start 和 stop 按钮状态 */ diff --git a/build.gradle b/build.gradle index 29ec0227..03fa24bb 100644 --- a/build.gradle +++ b/build.gradle @@ -9,6 +9,7 @@ buildscript { classpath 'com.android.tools.build:gradle:2.2.2' classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' + classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } diff --git a/settings.gradle b/settings.gradle index 7fa85466..e4d72994 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1 @@ -include ':app', ':Aria' +include ':app', ':Aria', ':AriaAnnotations', ':AriaCompiler'