问题,
public class MyClass extends AbstractProcessor { @Override public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) { System.out.println("process aaaaaaa*****************"); return false; } }
并没有执行这个任务。
解决办法:
implementation 'com.google.auto.service:auto-service:1.0-rc4'
添加上面的依赖
应该是创建META-INF/service/....的问题
项目创建:
-----------------------------------------------------------------------------------------------
源码:
annotation 的module的代码
package gatemachine.jwipc.com.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.CLASS) @Target(ElementType.FIELD) public @interface Annot { int value(); }
-----------------------------------------------------------------------------------------------------------
app 的module的代码 一个 MainActivity类
package gatemachine.jwipc.com.annotationtest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; import gatemachine.jwipc.com.annotation.Annot; public class MainActivity extends AppCompatActivity { @Annot(R.id.textview) TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
app 的module的代码 一个 MyActivity类
package gatemachine.jwipc.com.annotationtest; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import gatemachine.jwipc.com.annotation.Annot; public class MyActivity extends Activity { @Annot(R.id.textview) TextView textView; @Override protected void onCreate( Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
app 的module的代码 一个 BindIng类
import android.app.Activity; import java.lang.reflect.Constructor; public class Binding { public static void bind(Activity activity) { //根据类名判断创建什么类。 String className = activity.getClass().getCanonicalName() + "Binding"; //获取实际的类名 try { Class bindClass = Class.forName(className);//通过反射获取到类 比如gatemachine.jwipc.com.aptipmbutterknife.MainActivityBind Constructor constructor = bindClass.getDeclaredConstructor(activity.getClass()); //获取类的构造器,通过类型来获取构造器方法。跟获取普通方法类似。 constructor.setAccessible(true); constructor.newInstance(activity);//创建新类对象 比如MainActivityBind 类。传参是构造器的入参。 } catch (Exception e) { e.printStackTrace(); } } }
--------------------------------------------------------------------------------------------------------------------------
lib 的module 的 代码的类MyClass一个类。
package gatemachine.jwipc.com.lib; 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.Collections; import java.util.Set; import javax.annotation.processing.AbstractProcessor; import javax.annotation.processing.Filer; 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 gatemachine.jwipc.com.annotation.Annot; @AutoService(Processor.class) //如果没有这个注解的话,这个注解处理器就不会被执行。 public class MyClass extends AbstractProcessor { Filer filer; @Override public synchronized void init(ProcessingEnvironment processingEnvironment) { super.init(processingEnvironment); filer = processingEnvironment.getFiler(); } @Override public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) { System.out.println("process aaaaaaa****---------------------------*************"); for (Element element : roundEnvironment.getRootElements()){ String packageStr = element.getEnclosingElement().toString(); String classStr = element.getSimpleName().toString(); ClassName className = ClassName.get(packageStr, classStr + "Binding"); MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder() .addModifiers(Modifier.PUBLIC) .addParameter(ClassName.get(packageStr, classStr), "activity"); boolean hasBinding = false; for (Element enclosedElement : element.getEnclosedElements()){ if (enclosedElement.getKind()==ElementKind.FIELD){ Annot bindView = enclosedElement.getAnnotation(Annot.class); if (bindView!=null){ hasBinding = true; constructorBuilder.addStatement("activity.$N = activity.findViewById($L)", enclosedElement.getSimpleName(), bindView.value()); } } } TypeSpec builtClass = TypeSpec.classBuilder(className) .addModifiers(Modifier.PUBLIC) .addMethod(constructorBuilder.build()) .build(); if (hasBinding){ try { JavaFile.builder(packageStr,builtClass) .build().writeTo(filer); } catch (IOException e) { e.printStackTrace(); } } } return false; } @Override public Set<String> getSupportedAnnotationTypes() { return Collections.singleton(gatemachine.jwipc.com.annotation.Annot.class.getCanonicalName()); } @Override public SourceVersion getSupportedSourceVersion() { return SourceVersion.RELEASE_8; } }