-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from LikeTheSalad/release/2.0.0
Release/2.0.0
- Loading branch information
Showing
80 changed files
with
2,111 additions
and
953 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Change Log | ||
========== | ||
|
||
Version 2.0.0 *(04-02-2023)* | ||
--- | ||
|
||
* Using a combination of annotation processor + the new AGP Instrumentation API |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
aaper-api/src/main/java/com/likethesalad/android/aaper/internal/compiler/AaperMethodDef.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.likethesalad.android.aaper.internal.compiler | ||
|
||
@Retention(AnnotationRetention.BINARY) | ||
annotation class AaperMethodDef(val name: String) |
3 changes: 3 additions & 0 deletions
3
aaper-api/src/main/java/com/likethesalad/android/aaper/internal/compiler/AaperRunnable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.likethesalad.android.aaper.internal.compiler | ||
|
||
interface AaperRunnable : Runnable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
plugins { | ||
id 'java-library' | ||
id 'kotlin-kapt' | ||
id 'org.jetbrains.kotlin.jvm' | ||
} | ||
|
||
dependencies { | ||
implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
implementation project(':aaper-api') | ||
implementation 'com.squareup:javapoet:1.13.0' | ||
compileOnly "com.google.auto.service:auto-service-annotations:$autoService_version" | ||
kapt "com.google.auto.service:auto-service:$autoService_version" | ||
testImplementation "com.google.testing.compile:compile-testing:0.21.0" | ||
} | ||
|
||
compileKotlin { | ||
kotlinOptions { | ||
jvmTarget = "1.8" | ||
} | ||
} | ||
compileTestKotlin { | ||
kotlinOptions { | ||
jvmTarget = "1.8" | ||
} | ||
} | ||
java { | ||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
} |
156 changes: 156 additions & 0 deletions
156
aaper-compiler/src/main/java/com/likethesalad/android/aaper/compiler/AaperProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
package com.likethesalad.android.aaper.compiler | ||
|
||
import com.google.auto.service.AutoService | ||
import com.likethesalad.android.aaper.internal.compiler.AaperMethodDef | ||
import com.likethesalad.android.aaper.internal.compiler.AaperRunnable | ||
import com.squareup.javapoet.AnnotationSpec | ||
import com.squareup.javapoet.JavaFile | ||
import com.squareup.javapoet.MethodSpec | ||
import com.squareup.javapoet.ParameterSpec | ||
import com.squareup.javapoet.TypeName | ||
import com.squareup.javapoet.TypeSpec | ||
import com.squareup.javapoet.TypeVariableName | ||
import javax.annotation.processing.AbstractProcessor | ||
import javax.annotation.processing.Processor | ||
import javax.annotation.processing.RoundEnvironment | ||
import javax.annotation.processing.SupportedAnnotationTypes | ||
import javax.annotation.processing.SupportedSourceVersion | ||
import javax.lang.model.SourceVersion | ||
import javax.lang.model.element.ExecutableElement | ||
import javax.lang.model.element.Modifier | ||
import javax.lang.model.element.Name | ||
import javax.lang.model.element.TypeElement | ||
import javax.lang.model.element.TypeParameterElement | ||
import javax.lang.model.element.VariableElement | ||
import javax.lang.model.type.TypeKind | ||
import javax.tools.Diagnostic | ||
|
||
@AutoService(Processor::class) | ||
@Suppress("UNCHECKED_CAST") | ||
@SupportedAnnotationTypes("com.likethesalad.android.aaper.api.EnsurePermissions") | ||
@SupportedSourceVersion(SourceVersion.RELEASE_8) | ||
class AaperProcessor : AbstractProcessor() { | ||
|
||
override fun process( | ||
annotations: MutableSet<out TypeElement>, | ||
roundEnv: RoundEnvironment | ||
): Boolean { | ||
|
||
annotations.forEach { typeElement -> | ||
val annotatedMethods: Set<ExecutableElement> = | ||
roundEnv.getElementsAnnotatedWith(typeElement) as Set<ExecutableElement> | ||
|
||
annotatedMethods.forEach { method -> | ||
if (TypeKind.VOID != method.returnType.kind) { | ||
processingEnv.messager.printMessage( | ||
Diagnostic.Kind.ERROR, | ||
"EnsurePermissions annotated methods must return void", method | ||
) | ||
} else { | ||
createClassForMethod(method) | ||
} | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
private fun createClassForMethod(method: ExecutableElement) { | ||
val containerClass = method.enclosingElement as TypeElement | ||
val containerTypeName = TypeName.get(containerClass.asType()) | ||
val methodName = method.simpleName | ||
val parameters = method.parameters | ||
|
||
val constructor = createConstructor(containerTypeName, parameters) | ||
val runMethod = createRunMethod() | ||
|
||
val packageName = getPackageName(containerClass) | ||
val generatedSimpleName = "Aaper_${containerClass.simpleName}_$methodName" | ||
|
||
val typeClass = createGeneratedType( | ||
methodName, | ||
generatedSimpleName, | ||
containerTypeName, | ||
parameters, | ||
method.typeParameters, | ||
constructor, | ||
runMethod | ||
) | ||
|
||
val javaFile = JavaFile.builder(packageName, typeClass).build() | ||
val javaWriter = | ||
processingEnv.filer.createSourceFile("${packageName}.$generatedSimpleName", method) | ||
val writer = javaWriter.openWriter() | ||
|
||
javaFile.writeTo(writer) | ||
|
||
writer.close() | ||
} | ||
|
||
private fun createRunMethod(): MethodSpec { | ||
return MethodSpec.methodBuilder("run") | ||
.addModifiers(Modifier.PUBLIC) | ||
.build() | ||
} | ||
|
||
private fun createGeneratedType( | ||
methodName: Name, | ||
generatedSimpleName: String, | ||
containerTypeName: TypeName, | ||
parameters: List<VariableElement>, | ||
typeParameters: List<TypeParameterElement>, | ||
vararg methods: MethodSpec | ||
): TypeSpec { | ||
val typeClass = TypeSpec.classBuilder(generatedSimpleName) | ||
.addSuperinterface(AaperRunnable::class.java) | ||
.addField(containerTypeName, "instance", Modifier.PRIVATE, Modifier.FINAL) | ||
.addMethods(methods.asList()) | ||
|
||
typeParameters.forEach { typeParam -> | ||
typeClass.addTypeVariable(TypeVariableName.get(typeParam)) | ||
} | ||
|
||
parameters.forEach { | ||
typeClass.addField( | ||
TypeName.get(it.asType()), | ||
it.simpleName.toString(), | ||
Modifier.PRIVATE, | ||
Modifier.FINAL | ||
) | ||
} | ||
|
||
typeClass.addAnnotation( | ||
AnnotationSpec.builder(AaperMethodDef::class.java) | ||
.addMember("name", "\$S", methodName.toString()) | ||
.build() | ||
) | ||
|
||
return typeClass.build() | ||
} | ||
|
||
private fun createConstructor( | ||
containerTypeName: TypeName, | ||
parameters: List<VariableElement> | ||
): MethodSpec { | ||
val builder = MethodSpec.constructorBuilder() | ||
.addModifiers(Modifier.PUBLIC) | ||
.addParameter(containerTypeName, "instance") | ||
.addStatement("this.\$N = \$N", "instance", "instance") | ||
|
||
parameters.forEach { | ||
builder.addParameter(ParameterSpec.get(it)) | ||
builder.addStatement("this.\$N = \$N", it.simpleName, it.simpleName) | ||
} | ||
|
||
return builder.build() | ||
} | ||
|
||
private fun getPackageName(containerClass: TypeElement): String { | ||
var packageName = containerClass.qualifiedName.toString() | ||
val lastDot = packageName.indexOfLast { it == '.' } | ||
if (lastDot > 0) { | ||
packageName = packageName.substring(0, lastDot) | ||
} | ||
return packageName | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
aaper-compiler/src/main/resources/META-INF/gradle/incremental.annotation.processors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.likethesalad.android.aaper.compiler.AaperProcessor,isolating |
Oops, something went wrong.