我正在构建一个视图活页夹,它使用 KotlinPoet 为我的视图生成一些样板代码。但不知何故,我的注释处理器没有生成视图所需的代码,因此在ClassNotFoundException
我尝试运行演示应用程序的任何时候都会抛出一个。
这是我的处理器
@AutoService(Processor::class)
@SupportedSourceVersion(SourceVersion.RELEASE_8)
@SupportedOptions(KAPT_KOTLIN_GENERATED)
class Processor : AbstractProcessor() {
private lateinit var helper: ProcessorHelper
override fun getSupportedSourceVersion(): SourceVersion {
return SourceVersion.latest()
}
override fun process(annotations: MutableSet<out TypeElement>, roundEnv: RoundEnvironment): Boolean {
// if (!roundEnv.processingOver()) {
//Find all the classes that uses annotations
val typeElements: Set<TypeElement> =
ProcessingUtils.getTypeElementsToProcess(roundEnv.rootElements, annotations)
//Create a wrapper for each such class
for (typeElement in typeElements){
this.helper = ProcessorHelper()
val typeName : String = typeElement.simpleName.toString()
val packageName : String = processingEnv.elementUtils.getPackageOf(typeElement).qualifiedName.toString()
//Use the package name and the type name to form a class name
val className = ClassName(packageName, typeName)
//Get the generated class name
val generatedClassName = ClassName(packageName, NameSpaceStore.getGeneratedClassName(typeName))
//Define the wrapper class
val classBuilder = TypeSpec.classBuilder(generatedClassName)
.addModifiers(KModifier.PUBLIC)
.addAnnotation(Keep::class.java)
//Add Constructor
classBuilder.addFunction(FunSpec.constructorBuilder()
.addModifiers(KModifier.PUBLIC)
.addParameter( NameSpaceStore.Variable.ANDROID_ACTIVITY, className)
.addStatement("%N(%N)", NameSpaceStore.Method.BIND_ONCLICKS,
NameSpaceStore.Variable.ANDROID_ACTIVITY)
.addStatement("%N(%N)", NameSpaceStore.Method.BIND_ONCLICKS,
NameSpaceStore.Variable.ANDROID_ACTIVITY)
.build())
//Add method that map the with with the ID
val bindViewMethodBuilder = helper.with(className).
privateFunctionBuilder(NameSpaceStore.Method.BIND_VIEWS,NameSpaceStore.Variable.ANDROID_ACTIVITY)
for (variableElement in ElementFilter.fieldsIn(typeElement.enclosedElements)){
val bindView = variableElement.getAnnotation(BindView::class.java)
if (null != bindView){
//Start finding every views
bindViewMethodBuilder.addStatement("%N.%N = (%T)%N.findViewById(%L)",
NameSpaceStore.Variable.ANDROID_ACTIVITY,
variableElement.simpleName.toString(),
variableElement,
NameSpaceStore.Variable.ANDROID_ACTIVITY,
bindView.value
)
}
}
//Finally build methods
classBuilder.addFunction(bindViewMethodBuilder.build())
//Add method that attaches onClickListener()
val androidClickClassName = ClassName(NameSpaceStore.Package.ANDROID_VIEW,
NameSpaceStore.Class.ANDROID_VIEW,
NameSpaceStore.Class.ANDROID_VIEW_ONCLICK_LISTENER)
//Map to Android view class name
val androidViewClassName = ClassName(NameSpaceStore.Package.ANDROID_VIEW,
NameSpaceStore.Class.ANDROID_VIEW)
val bindOnClickMethodBuilder = helper.with(className).privateFunctionBuilder(NameSpaceStore.Method.BIND_ONCLICKS,
NameSpaceStore.Variable.ANDROID_ACTIVITY/*, Modifier.FINAL*/)
for (executableElement in ElementFilter.methodsIn(typeElement.enclosedElements)){
val onClick = executableElement.getAnnotation(OnClick::class.java)
if (onClick !=null){
val onClickListenerClass = TypeSpec.anonymousClassBuilder()
.addSuperinterface(androidClickClassName)
.addFunction(
helper.with(androidViewClassName).publicFunctionBuilder(NameSpaceStore.Method.ANDROID_VIEW_ONCLICK,
NameSpaceStore.Variable.ANDROID_VIEW)
.addStatement("%N.%N(%N)", NameSpaceStore.Variable.ANDROID_ACTIVITY,
executableElement.simpleName.toString(),
NameSpaceStore.Variable.ANDROID_VIEW)
.returns(Void::class.java)
.build())
bindOnClickMethodBuilder.addStatement("%N.findViewById(%L).setOnClickListener(%L)",
NameSpaceStore.Variable.ANDROID_ACTIVITY, onClick.id, onClickListenerClass)
}
}
classBuilder.addFunction(bindOnClickMethodBuilder.build())
//Write the define file to java file
val file = File(KAPT_KOTLIN_GENERATED)
try{
FileSpec.builder(packageName, generatedClassName::class.java.name)
.addType(classBuilder.build())
.build()
.writeTo(System.out)
}catch (io : IOException){
processingEnv.messager.printMessage(Diagnostic.Kind.ERROR, io.toString(), typeElement)
}
// }
}
return true
}
override fun getSupportedAnnotationTypes(): MutableSet<String?> {
return mutableSetOf(BindView::class.java.name,
Keep::class.java.name, OnClick::class.java.name)
}
}
如果您需要有关此问题的更多信息,请告诉我。提前致谢。