用注解实现回调

pull/330/head
AriaLyy 8 years ago
parent 0d59c7b421
commit 3b7a9eccb8
  1. 3
      Aria/build.gradle
  2. 2
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java
  3. 1
      AriaAnnotations/.gitignore
  4. 12
      AriaAnnotations/build.gradle
  5. 42
      AriaAnnotations/src/main/java/com/arialyy/annotations/Download.java
  6. 12
      AriaAnnotations/src/main/java/com/arialyy/annotations/Test.java
  7. 1
      AriaCompiler/.gitignore
  8. 18
      AriaCompiler/build.gradle
  9. 54
      AriaCompiler/src/main/java/com/arialyy/compiler/AriaProcessor.java
  10. 70
      AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java
  11. 55
      AriaCompiler/src/main/java/com/arialyy/compiler/PrintLog.java
  12. 11
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  13. 1
      build.gradle
  14. 2
      settings.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'

@ -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";
/**
* 文件上传表单
*/

@ -0,0 +1 @@
/build

@ -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"
}

@ -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 {
}
}

@ -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 {
}

@ -0,0 +1 @@
/build

@ -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"
}

@ -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<String> getSupportedAnnotationTypes() {
Set<String> 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<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
PrintLog.getInstance().info("开始扫描");
mHandler.handle(roundEnv);
return true;
}
}

@ -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<Modifier> 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) {
}
}

@ -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);
}
}

@ -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<ActivitySingleBinding> {
}
};
@Download.onPre private void hehe(String str, DownloadTask task) {
}
@Test
public void gg(){
}
/**
* 设置start stop 按钮状态
*/

@ -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
}

@ -1 +1 @@
include ':app', ':Aria'
include ':app', ':Aria', ':AriaAnnotations', ':AriaCompiler'

Loading…
Cancel
Save