-
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Retrieve annotations in super and interface classes
- Loading branch information
Showing
5 changed files
with
104 additions
and
27 deletions.
There are no files selected for viewing
72 changes: 49 additions & 23 deletions
72
src/main/kotlin/app/revanced/patcher/extensions/AnnotationExtensions.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 |
---|---|---|
@@ -1,35 +1,61 @@ | ||
@file:Suppress("UNCHECKED_CAST") | ||
|
||
package app.revanced.patcher.extensions | ||
|
||
import kotlin.reflect.KClass | ||
|
||
internal object AnnotationExtensions { | ||
/** | ||
* Search for an annotation recursively. | ||
* | ||
* @param targetAnnotation The annotation to search for. | ||
* @param targetAnnotationClass The annotation class to search for. | ||
* @param searchedClasses A set of annotations that have already been searched. | ||
* @return The annotation if found, otherwise null. | ||
*/ | ||
fun <T : Annotation> Class<*>.findAnnotationRecursively(targetAnnotation: Class<T>): T? { | ||
fun <T : Annotation> Class<*>.findAnnotationRecursively( | ||
targetAnnotation: Class<T>, | ||
searchedAnnotations: HashSet<Annotation>, | ||
): T? { | ||
val found = this.annotations.firstOrNull { it.annotationClass.java.name == targetAnnotation.name } | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
if (found != null) return found as T | ||
|
||
for (annotation in this.annotations) { | ||
if (searchedAnnotations.contains(annotation)) continue | ||
searchedAnnotations.add(annotation) | ||
|
||
return annotation.annotationClass.java.findAnnotationRecursively( | ||
targetAnnotation, | ||
searchedAnnotations | ||
) ?: continue | ||
} | ||
|
||
return null | ||
fun <T : Annotation> Class<*>.findAnnotationRecursively( | ||
targetAnnotationClass: Class<T>, | ||
searchedClasses: HashSet<Annotation> = hashSetOf(), | ||
): T? { | ||
annotations.forEach { annotation -> | ||
// Terminate if the annotation is already searched. | ||
if (annotation in searchedClasses) return@forEach | ||
searchedClasses.add(annotation) | ||
|
||
// Terminate if the annotation is found. | ||
if (targetAnnotationClass == annotation.annotationClass.java) return annotation as T | ||
|
||
return annotation.annotationClass.java.findAnnotationRecursively( | ||
targetAnnotationClass, | ||
searchedClasses, | ||
) ?: return@forEach | ||
} | ||
|
||
// Search the super class. | ||
superclass?.findAnnotationRecursively( | ||
targetAnnotationClass, | ||
searchedClasses, | ||
)?.let { return it } | ||
|
||
// Search the interfaces. | ||
interfaces.forEach { superClass -> | ||
return superClass.findAnnotationRecursively( | ||
targetAnnotationClass, | ||
searchedClasses, | ||
) ?: return@forEach | ||
} | ||
|
||
return this.findAnnotationRecursively(targetAnnotation, hashSetOf()) | ||
return null | ||
} | ||
|
||
/** | ||
* Search for an annotation recursively. | ||
* | ||
* First the annotations, then the annotated classes super class and then it's interfaces | ||
* are searched for the annotation recursively. | ||
* | ||
* @param targetAnnotation The annotation to search for. | ||
* @return The annotation if found, otherwise null. | ||
*/ | ||
fun <T : Annotation> KClass<*>.findAnnotationRecursively(targetAnnotation: KClass<T>) = | ||
java.findAnnotationRecursively(targetAnnotation.java) | ||
} |
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
50 changes: 50 additions & 0 deletions
50
src/test/kotlin/app/revanced/patcher/extensions/AnnotationExtensionsTest.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,50 @@ | ||
package app.revanced.patcher.extensions | ||
|
||
import app.revanced.patcher.extensions.AnnotationExtensions.findAnnotationRecursively | ||
import kotlin.test.Test | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertNull | ||
|
||
private object AnnotationExtensionsTest { | ||
@Test | ||
fun `find annotation in annotated class`() { | ||
assertNotNull(TestClasses.Annotation2::class.findAnnotationRecursively(TestClasses.Annotation::class)) | ||
} | ||
|
||
@Test | ||
fun `find annotation`() { | ||
assertNotNull(TestClasses.AnnotatedClass::class.findAnnotationRecursively(TestClasses.Annotation::class)) | ||
} | ||
|
||
@Test | ||
fun `find annotation recursively in super class`() { | ||
assertNotNull(TestClasses.AnnotatedClass2::class.findAnnotationRecursively(TestClasses.Annotation::class)) | ||
} | ||
|
||
@Test | ||
fun `find annotation recursively in super class with annotation`() { | ||
assertNotNull(TestClasses.AnnotatedTestClass3::class.findAnnotationRecursively(TestClasses.Annotation::class)) | ||
} | ||
|
||
@Test | ||
fun `don't find unknown annotation in annotated class`() { | ||
assertNull(TestClasses.AnnotatedClass::class.findAnnotationRecursively(TestClasses.UnknownAnnotation::class)) | ||
} | ||
|
||
object TestClasses { | ||
annotation class Annotation | ||
|
||
@Annotation | ||
annotation class Annotation2 | ||
|
||
annotation class UnknownAnnotation | ||
|
||
@Annotation | ||
abstract class AnnotatedClass | ||
|
||
@Annotation2 | ||
class AnnotatedTestClass3 | ||
|
||
abstract class AnnotatedClass2 : AnnotatedClass() | ||
} | ||
} |