首先看下系统演变
版本1
版本2
分析版本3,新增一个功能支付,但是不能再APP中添加功能和代码,这时候我们可以这样,我们单独做一个支付功能,然后APP去调用支付的主Activity即可,但是需要在APP的mainifest中声明activity
我们将分为三个module:compiler和annotation,pay
首先compiler中添加注解:包括包名和类名
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface WXPayEntry {
/**
* 包名
*/
String packageName();
/**
* 类的class
*/
Class entryClass();
}
compiler需要添加依赖
compile 'com.google.auto.service:auto-service:1.0-rc3'
compile 'com.squareup:javapoet:1.8.0'
//添加支持中文
tasks.withType(JavaCompile){
options.encoding='UTF-8'
}
整个项目的builder.gradle添加依赖
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven { url "https://maven.google.com"}
mavenCentral()
//maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
}
}
JokeProcessor生成器
@AutoService(Processor.class)
public class JokeProcessor extends AbstractProcessor {
private Filer mFiler;
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
mFiler = processingEnv.getFiler();
}
@Override
public Set getSupportedAnnotationTypes() {
Set types = new LinkedHashSet<>();
for (Classextends Annotation> annotation : getSupportedAnnotations()) {
types.add(annotation.getCanonicalName());
}
return types;
}
private Set<Classextends Annotation>> getSupportedAnnotations() {
Set<Classextends Annotation>> annotations = new LinkedHashSet<>();
annotations.add(WXPayEntry.class);
return annotations;
}
//设置为最大版本
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
@Override
public boolean process(Set extends TypeElement> annotations, RoundEnvironment roundEnv) {
// 生成 一个 Class xxx.wxapi.WXPayEntryActivity extends BaseWXPayActivity
generateWXPayCode(roundEnv);
return false;
}
private void generateWXPayCode(RoundEnvironment env) {
WXPayEntryVisitor visitor = new WXPayEntryVisitor();
visitor.setFiler(mFiler);
scanElement(env, WXPayEntry.class, visitor);
}
private void scanElement(RoundEnvironment roundEnvironment, Classextends Annotation> annotation, AnnotationValueVisitor visitor) {
Set extends Element> elements = roundEnvironment.getElementsAnnotatedWith(annotation);
for (Element element : elements) {
//获取到该声明上所添加的注解的实际值
List extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors();
for (AnnotationMirror annotationMirror : annotationMirrors) {
Map extends ExecutableElement, ? extends AnnotationValue> elementValues = annotationMirror.getElementValues();
for (Map.Entry extends ExecutableElement, ? extends AnnotationValue> entry : elementValues.entrySet()) {
//将获得的注解上的实际的值给visitor
entry.getValue().accept(visitor, null);
}
}
}
}
}
WXPayEntryVisitor代码
public class WXPayEntryVisitor extends SimpleAnnotationValueVisitor7<Void,Void> {
private String mPackageName;
private TypeMirror mTypeMirror;
private Filer mFiler;
@Override
public Void visitString(String s, Void aVoid) {
mPackageName=s;
return aVoid;
}
public void setFiler(Filer mFiler) {
this.mFiler = mFiler;
}
@Override
public Void visitType(TypeMirror t, Void aVoid) {
mTypeMirror=t;
generateWXPayCode();
return aVoid;
}
/**
* 生成类
*/
private void generateWXPayCode() {
// 生成 一个 Class xxx.wxapi.WXPayEntryActivity extends BaseWXPayActivity
TypeSpec.Builder builder = TypeSpec.classBuilder("WXPayEntryActivity")
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.superclass(TypeName.get(mTypeMirror));
try {
JavaFile.builder(mPackageName+".wxapi",builder.build())
.addFileComment("微信支付自动生成")
.build().writeTo(mFiler);
} catch (IOException e) {
e.printStackTrace();
System.out.println("失败了");
}
}
}
joke-play中WXPayActivity
public class WXPayActivity extends AppCompatActivity {
}
BaseWXPayActivity
public class BaseWXPayActivity extends WXPayActivity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
app中使用
@WXPayEntry(packageName = "com.example.WXPay",
entryClass = BaseWXPayActivity.class)
public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
}
}
最终会生成这个类
// 微信支付自动生成
package com.example.WXPay.wxapi;
import com.example.joke_pay.BaseWXPayActivity;
public final class WXPayEntryActivity extends BaseWXPayActivity {
}