我正在使用 Android Studio 中的 Java模块编写自定义@Builder
注释处理库(类似于Project Lombok)。我不想用Java编写 Java模块,而是想用Kotlin编写。
由于 Android Studio 中没有 Kotlin模块选项,因此我启动了一个新的 Java模块并用Java编写。
@AutoService(Processor.class)
public class BuilderProcessor extends AbstractProcessor {
private Filer filer;
@Override
public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
for (Element element : roundEnvironment.getElementsAnnotatedWith(Builder.class)){
TypeSpec binder = createClass(element.getSimpleName().toString());
TypeElement typeElement = (TypeElement) element;
JavaFile javaFile = JavaFile.builder(getPackage(typeElement.getQualifiedName().toString()), binder).build();
try {
javaFile.writeTo(filer);
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
@Override
public Set<String> getSupportedAnnotationTypes() {
Set<String> annotations = new LinkedHashSet<>();
for (Class<? extends Annotation> annotation : Collections.singleton(Builder.class)) {
annotations.add(annotation.getCanonicalName());
}
return annotations;
}
@Override
public synchronized void init(ProcessingEnvironment processingEnvironment) {
super.init(processingEnvironment);
this.filer = processingEnvironment.getFiler();
}
private String getPackage(String s){
return s.substring(0, s.lastIndexOf('.'));
}
private TypeSpec createClass(String className){
return TypeSpec.classBuilder(className + "Builder")
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.build();
}
}
如果在UserBuilder.java
类之上有注释@Builder
,则上面的代码可以很好地工作并创建类User
。
现在我想在Kotlin中进行转换。因此,我将代码转换为Kotlin
@AutoService(Processor::class)
class BuilderProcessor : AbstractProcessor() {
private var filer: Filer? = null
override fun process(set: Set<TypeElement>, roundEnvironment: RoundEnvironment): Boolean {
for (element in roundEnvironment.getElementsAnnotatedWith(Builder::class.java)) {
val binder = createClass(element.simpleName.toString())
val typeElement = element as TypeElement
val javaFile = JavaFile.builder(getPackage(typeElement.qualifiedName.toString()), binder).build()
try {
javaFile.writeTo(filer!!)
} catch (e: IOException) {
e.printStackTrace()
}
}
return false
}
override fun getSupportedAnnotationTypes(): Set<String> {
val annotations = LinkedHashSet<String>()
for (annotation in setOf(Builder::class.java)) {
annotations.add(annotation.canonicalName)
}
return annotations
}
@Synchronized
override fun init(processingEnvironment: ProcessingEnvironment) {
super.init(processingEnvironment)
this.filer = processingEnvironment.filer
}
private fun getPackage(s: String): String {
return s.substring(0, s.lastIndexOf('.'))
}
private fun createClass(className: String): TypeSpec {
return TypeSpec.classBuilder(className + "Builder")
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.build()
}
}
并使用Kotlin配置成绩
plugins {
id 'org.jetbrains.kotlin.jvm'
}
apply plugin: 'java-library'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
api 'com.google.guava:guava:27.0.1-android'
implementation 'com.squareup:javapoet:1.9.0'
implementation 'com.google.auto.service:auto-service:1.0-rc2'
implementation project(':builder')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}
sourceCompatibility = "7"
targetCompatibility = "7"
repositories {
mavenCentral()
}
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
但是,不幸的是,当我构建它时,它不起作用。它会生成,但不会生成UserBuilder.java
类。我做错了还是应该采取任何步骤?
有reference的 Project Lombok 的Kotlin版本。