-
Notifications
You must be signed in to change notification settings - Fork 1
/
ActivityProcessor.java
115 lines (95 loc) · 3.93 KB
/
ActivityProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package me.aflak.processor;
import com.google.auto.service.AutoService;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
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.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import javax.tools.Diagnostic;
import me.aflak.annotation.Activity;
@AutoService(Processor.class)
public class ActivityProcessor extends AbstractProcessor{
private Filer filer;
private Messager messager;
private Elements elements;
private List<ClassName> classList;
private final ClassName classContext = ClassName.get("android.content", "Context");
private final ClassName classIntent = ClassName.get("android.content", "Intent");
private static final String generatedPackage = "me.aflak.annotations";
@Override
public synchronized void init(ProcessingEnvironment processingEnvironment) {
this.filer = processingEnvironment.getFiler();
this.messager = processingEnvironment.getMessager();
this.elements = processingEnvironment.getElementUtils();
this.classList = new ArrayList<>();
}
@Override
public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
/**
* 1) Getting annotated classes
*/
for(Element element : roundEnvironment.getElementsAnnotatedWith(Activity.class)){
if(element.getKind() != ElementKind.CLASS){
messager.printMessage(Diagnostic.Kind.ERROR, "@Activity should be on top of classes");
return false;
}
classList.add(ClassName.get(elements.getPackageOf(element).getQualifiedName().toString(),
element.getSimpleName().toString()));
}
/**
* 2) For each annotated class, generate new static method
*/
List<MethodSpec> methods = new ArrayList<>();
for(ClassName className : classList){
MethodSpec startMethod = MethodSpec
.methodBuilder("start"+className.simpleName())
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(void.class)
.addParameter(classContext, "context")
.addStatement("$T intent = new $T(context, $T.class)", classIntent, classIntent, className)
.addStatement("context.startActivity(intent)")
.build();
methods.add(startMethod);
}
/**
* 3) Generate a class called Navigator that contains the static methods
*/
TypeSpec.Builder generatedClass = TypeSpec
.classBuilder("Navigator")
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addMethods(methods);
/**
* 4) Write Navigator class into file
*/
try {
JavaFile.builder(generatedPackage, generatedClass.build()).build().writeTo(filer);
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
@Override
public Set<String> getSupportedAnnotationTypes() {
return Collections.singleton(Activity.class.getCanonicalName());
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
}